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" |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 44 | |
Luis R. Rodriguez | 5166ccd | 2008-10-30 13:33:56 -0700 | [diff] [blame] | 45 | /** |
| 46 | * struct regulatory_request - receipt of last regulatory request |
| 47 | * |
| 48 | * @wiphy: this is set if this request's initiator is |
| 49 | * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This |
| 50 | * can be used by the wireless core to deal with conflicts |
| 51 | * and potentially inform users of which devices specifically |
| 52 | * cased the conflicts. |
| 53 | * @initiator: indicates who sent this request, could be any of |
| 54 | * of those set in reg_set_by, %REGDOM_SET_BY_* |
| 55 | * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested |
| 56 | * regulatory domain. We have a few special codes: |
| 57 | * 00 - World regulatory domain |
| 58 | * 99 - built by driver but a specific alpha2 cannot be determined |
| 59 | * 98 - result of an intersection between two regulatory domains |
| 60 | * @intersect: indicates whether the wireless core should intersect |
| 61 | * the requested regulatory domain with the presently set regulatory |
| 62 | * domain. |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 63 | * @country_ie_checksum: checksum of the last processed and accepted |
| 64 | * country IE |
| 65 | * @country_ie_env: lets us know if the AP is telling us we are outdoor, |
| 66 | * indoor, or if it doesn't matter |
Johannes Berg | be3d481 | 2008-10-24 20:32:21 +0200 | [diff] [blame] | 67 | */ |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 68 | struct regulatory_request { |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 69 | struct wiphy *wiphy; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 70 | enum reg_set_by initiator; |
| 71 | char alpha2[2]; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 72 | bool intersect; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 73 | u32 country_ie_checksum; |
| 74 | enum environment_cap country_ie_env; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
Luis R. Rodriguez | 5166ccd | 2008-10-30 13:33:56 -0700 | [diff] [blame] | 77 | /* Receipt of information from last regulatory request */ |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 78 | static struct regulatory_request *last_request; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 79 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 80 | /* To trigger userspace events */ |
| 81 | static struct platform_device *reg_pdev; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 82 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 83 | /* Keep the ordering from large to small */ |
| 84 | static u32 supported_bandwidths[] = { |
| 85 | MHZ_TO_KHZ(40), |
| 86 | MHZ_TO_KHZ(20), |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 89 | /* Central wireless core regulatory domains, we only need two, |
| 90 | * the current one and a world regulatory domain in case we have no |
| 91 | * information to give us an alpha2 */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 92 | static const struct ieee80211_regdomain *cfg80211_regdomain; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 93 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 94 | /* We use this as a place for the rd structure built from the |
| 95 | * last parsed country IE to rest until CRDA gets back to us with |
| 96 | * what it thinks should apply for the same country */ |
| 97 | static const struct ieee80211_regdomain *country_ie_regdomain; |
| 98 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 99 | /* We keep a static world regulatory domain in case of the absence of CRDA */ |
| 100 | static const struct ieee80211_regdomain world_regdom = { |
| 101 | .n_reg_rules = 1, |
| 102 | .alpha2 = "00", |
| 103 | .reg_rules = { |
| 104 | REG_RULE(2412-10, 2462+10, 40, 6, 20, |
| 105 | NL80211_RRF_PASSIVE_SCAN | |
| 106 | NL80211_RRF_NO_IBSS), |
| 107 | } |
| 108 | }; |
| 109 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 110 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
| 111 | &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 112 | |
| 113 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY |
| 114 | static char *ieee80211_regdom = "US"; |
| 115 | module_param(ieee80211_regdom, charp, 0444); |
| 116 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); |
| 117 | |
| 118 | /* We assume 40 MHz bandwidth for the old regulatory work. |
| 119 | * We make emphasis we are using the exact same frequencies |
| 120 | * as before */ |
| 121 | |
| 122 | static const struct ieee80211_regdomain us_regdom = { |
| 123 | .n_reg_rules = 6, |
| 124 | .alpha2 = "US", |
| 125 | .reg_rules = { |
| 126 | /* IEEE 802.11b/g, channels 1..11 */ |
| 127 | REG_RULE(2412-10, 2462+10, 40, 6, 27, 0), |
| 128 | /* IEEE 802.11a, channel 36 */ |
| 129 | REG_RULE(5180-10, 5180+10, 40, 6, 23, 0), |
| 130 | /* IEEE 802.11a, channel 40 */ |
| 131 | REG_RULE(5200-10, 5200+10, 40, 6, 23, 0), |
| 132 | /* IEEE 802.11a, channel 44 */ |
| 133 | REG_RULE(5220-10, 5220+10, 40, 6, 23, 0), |
| 134 | /* IEEE 802.11a, channels 48..64 */ |
| 135 | REG_RULE(5240-10, 5320+10, 40, 6, 23, 0), |
| 136 | /* IEEE 802.11a, channels 149..165, outdoor */ |
| 137 | REG_RULE(5745-10, 5825+10, 40, 6, 30, 0), |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | static const struct ieee80211_regdomain jp_regdom = { |
| 142 | .n_reg_rules = 3, |
| 143 | .alpha2 = "JP", |
| 144 | .reg_rules = { |
| 145 | /* IEEE 802.11b/g, channels 1..14 */ |
| 146 | REG_RULE(2412-10, 2484+10, 40, 6, 20, 0), |
| 147 | /* IEEE 802.11a, channels 34..48 */ |
| 148 | REG_RULE(5170-10, 5240+10, 40, 6, 20, |
| 149 | NL80211_RRF_PASSIVE_SCAN), |
| 150 | /* IEEE 802.11a, channels 52..64 */ |
| 151 | REG_RULE(5260-10, 5320+10, 40, 6, 20, |
| 152 | NL80211_RRF_NO_IBSS | |
| 153 | NL80211_RRF_DFS), |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | static const struct ieee80211_regdomain eu_regdom = { |
| 158 | .n_reg_rules = 6, |
| 159 | /* This alpha2 is bogus, we leave it here just for stupid |
| 160 | * backward compatibility */ |
| 161 | .alpha2 = "EU", |
| 162 | .reg_rules = { |
| 163 | /* IEEE 802.11b/g, channels 1..13 */ |
| 164 | REG_RULE(2412-10, 2472+10, 40, 6, 20, 0), |
| 165 | /* IEEE 802.11a, channel 36 */ |
| 166 | REG_RULE(5180-10, 5180+10, 40, 6, 23, |
| 167 | NL80211_RRF_PASSIVE_SCAN), |
| 168 | /* IEEE 802.11a, channel 40 */ |
| 169 | REG_RULE(5200-10, 5200+10, 40, 6, 23, |
| 170 | NL80211_RRF_PASSIVE_SCAN), |
| 171 | /* IEEE 802.11a, channel 44 */ |
| 172 | REG_RULE(5220-10, 5220+10, 40, 6, 23, |
| 173 | NL80211_RRF_PASSIVE_SCAN), |
| 174 | /* IEEE 802.11a, channels 48..64 */ |
| 175 | REG_RULE(5240-10, 5320+10, 40, 6, 20, |
| 176 | NL80211_RRF_NO_IBSS | |
| 177 | NL80211_RRF_DFS), |
| 178 | /* IEEE 802.11a, channels 100..140 */ |
| 179 | REG_RULE(5500-10, 5700+10, 40, 6, 30, |
| 180 | NL80211_RRF_NO_IBSS | |
| 181 | NL80211_RRF_DFS), |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | static const struct ieee80211_regdomain *static_regdom(char *alpha2) |
| 186 | { |
| 187 | if (alpha2[0] == 'U' && alpha2[1] == 'S') |
| 188 | return &us_regdom; |
| 189 | if (alpha2[0] == 'J' && alpha2[1] == 'P') |
| 190 | return &jp_regdom; |
| 191 | if (alpha2[0] == 'E' && alpha2[1] == 'U') |
| 192 | return &eu_regdom; |
| 193 | /* Default, as per the old rules */ |
| 194 | return &us_regdom; |
| 195 | } |
| 196 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 197 | static bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 198 | { |
| 199 | if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom) |
| 200 | return true; |
| 201 | return false; |
| 202 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 203 | #else |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 204 | static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
| 205 | { |
| 206 | return false; |
| 207 | } |
| 208 | #endif |
| 209 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 210 | static void reset_regdomains(void) |
| 211 | { |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 212 | /* avoid freeing static information or freeing something twice */ |
| 213 | if (cfg80211_regdomain == cfg80211_world_regdom) |
| 214 | cfg80211_regdomain = NULL; |
| 215 | if (cfg80211_world_regdom == &world_regdom) |
| 216 | cfg80211_world_regdom = NULL; |
| 217 | if (cfg80211_regdomain == &world_regdom) |
| 218 | cfg80211_regdomain = NULL; |
| 219 | if (is_old_static_regdom(cfg80211_regdomain)) |
| 220 | cfg80211_regdomain = NULL; |
| 221 | |
| 222 | kfree(cfg80211_regdomain); |
| 223 | kfree(cfg80211_world_regdom); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 224 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 225 | cfg80211_world_regdom = &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 226 | cfg80211_regdomain = NULL; |
| 227 | } |
| 228 | |
| 229 | /* Dynamic world regulatory domain requested by the wireless |
| 230 | * core upon initialization */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 231 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 232 | { |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 233 | BUG_ON(!last_request); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 234 | |
| 235 | reset_regdomains(); |
| 236 | |
| 237 | cfg80211_world_regdom = rd; |
| 238 | cfg80211_regdomain = rd; |
| 239 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 240 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 241 | bool is_world_regdom(const char *alpha2) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 242 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 243 | if (!alpha2) |
| 244 | return false; |
| 245 | if (alpha2[0] == '0' && alpha2[1] == '0') |
| 246 | return true; |
| 247 | return false; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 250 | static bool is_alpha2_set(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 251 | { |
| 252 | if (!alpha2) |
| 253 | return false; |
| 254 | if (alpha2[0] != 0 && alpha2[1] != 0) |
| 255 | return true; |
| 256 | return false; |
| 257 | } |
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 | static bool is_alpha_upper(char letter) |
| 260 | { |
| 261 | /* ASCII A - Z */ |
| 262 | if (letter >= 65 && letter <= 90) |
| 263 | return true; |
| 264 | return false; |
| 265 | } |
| 266 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 267 | static bool is_unknown_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 268 | { |
| 269 | if (!alpha2) |
| 270 | return false; |
| 271 | /* Special case where regulatory domain was built by driver |
| 272 | * but a specific alpha2 cannot be determined */ |
| 273 | if (alpha2[0] == '9' && alpha2[1] == '9') |
| 274 | return true; |
| 275 | return false; |
| 276 | } |
| 277 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 278 | static bool is_intersected_alpha2(const char *alpha2) |
| 279 | { |
| 280 | if (!alpha2) |
| 281 | return false; |
| 282 | /* Special case where regulatory domain is the |
| 283 | * result of an intersection between two regulatory domain |
| 284 | * structures */ |
| 285 | if (alpha2[0] == '9' && alpha2[1] == '8') |
| 286 | return true; |
| 287 | return false; |
| 288 | } |
| 289 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 290 | static bool is_an_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 291 | { |
| 292 | if (!alpha2) |
| 293 | return false; |
| 294 | if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1])) |
| 295 | return true; |
| 296 | return false; |
| 297 | } |
| 298 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 299 | 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] | 300 | { |
| 301 | if (!alpha2_x || !alpha2_y) |
| 302 | return false; |
| 303 | if (alpha2_x[0] == alpha2_y[0] && |
| 304 | alpha2_x[1] == alpha2_y[1]) |
| 305 | return true; |
| 306 | return false; |
| 307 | } |
| 308 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 309 | static bool regdom_changed(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 310 | { |
| 311 | if (!cfg80211_regdomain) |
| 312 | return true; |
| 313 | if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) |
| 314 | return false; |
| 315 | return true; |
| 316 | } |
| 317 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 318 | /** |
| 319 | * country_ie_integrity_changes - tells us if the country IE has changed |
| 320 | * @checksum: checksum of country IE of fields we are interested in |
| 321 | * |
| 322 | * If the country IE has not changed you can ignore it safely. This is |
| 323 | * useful to determine if two devices are seeing two different country IEs |
| 324 | * even on the same alpha2. Note that this will return false if no IE has |
| 325 | * been set on the wireless core yet. |
| 326 | */ |
| 327 | static bool country_ie_integrity_changes(u32 checksum) |
| 328 | { |
| 329 | /* If no IE has been set then the checksum doesn't change */ |
| 330 | if (unlikely(!last_request->country_ie_checksum)) |
| 331 | return false; |
| 332 | if (unlikely(last_request->country_ie_checksum != checksum)) |
| 333 | return true; |
| 334 | return false; |
| 335 | } |
| 336 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 337 | /* This lets us keep regulatory code which is updated on a regulatory |
| 338 | * basis in userspace. */ |
| 339 | static int call_crda(const char *alpha2) |
| 340 | { |
| 341 | char country_env[9 + 2] = "COUNTRY="; |
| 342 | char *envp[] = { |
| 343 | country_env, |
| 344 | NULL |
| 345 | }; |
| 346 | |
| 347 | if (!is_world_regdom((char *) alpha2)) |
| 348 | printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n", |
| 349 | alpha2[0], alpha2[1]); |
| 350 | else |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 351 | printk(KERN_INFO "cfg80211: Calling CRDA to update world " |
| 352 | "regulatory domain\n"); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 353 | |
| 354 | country_env[8] = alpha2[0]; |
| 355 | country_env[9] = alpha2[1]; |
| 356 | |
| 357 | return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp); |
| 358 | } |
| 359 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 360 | /* Used by nl80211 before kmalloc'ing our regulatory domain */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 361 | bool reg_is_valid_request(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 362 | { |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 363 | if (!last_request) |
| 364 | return false; |
| 365 | |
| 366 | return alpha2_equal(last_request->alpha2, alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* Sanity check on a regulatory rule */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 370 | 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] | 371 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 372 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 373 | u32 freq_diff; |
| 374 | |
Luis R. Rodriguez | 91e9900 | 2008-11-12 14:21:55 -0800 | [diff] [blame] | 375 | 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] | 376 | return false; |
| 377 | |
| 378 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) |
| 379 | return false; |
| 380 | |
| 381 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 382 | |
Luis R. Rodriguez | d71aaf6 | 2008-10-30 13:33:52 -0700 | [diff] [blame] | 383 | if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 384 | return false; |
| 385 | |
| 386 | return true; |
| 387 | } |
| 388 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 389 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 390 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 391 | const struct ieee80211_reg_rule *reg_rule = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 392 | unsigned int i; |
| 393 | |
| 394 | if (!rd->n_reg_rules) |
| 395 | return false; |
| 396 | |
Luis R. Rodriguez | 88dc1c3 | 2008-11-12 14:22:01 -0800 | [diff] [blame] | 397 | if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) |
| 398 | return false; |
| 399 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 400 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 401 | reg_rule = &rd->reg_rules[i]; |
| 402 | if (!is_valid_reg_rule(reg_rule)) |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | /* Returns value in KHz */ |
| 410 | static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range, |
| 411 | u32 freq) |
| 412 | { |
| 413 | unsigned int i; |
| 414 | for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) { |
| 415 | u32 start_freq_khz = freq - supported_bandwidths[i]/2; |
| 416 | u32 end_freq_khz = freq + supported_bandwidths[i]/2; |
| 417 | if (start_freq_khz >= freq_range->start_freq_khz && |
| 418 | end_freq_khz <= freq_range->end_freq_khz) |
| 419 | return supported_bandwidths[i]; |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 424 | /** |
| 425 | * freq_in_rule_band - tells us if a frequency is in a frequency band |
| 426 | * @freq_range: frequency rule we want to query |
| 427 | * @freq_khz: frequency we are inquiring about |
| 428 | * |
| 429 | * This lets us know if a specific frequency rule is or is not relevant to |
| 430 | * a specific frequency's band. Bands are device specific and artificial |
| 431 | * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is |
| 432 | * safe for now to assume that a frequency rule should not be part of a |
| 433 | * frequency's band if the start freq or end freq are off by more than 2 GHz. |
| 434 | * This resolution can be lowered and should be considered as we add |
| 435 | * regulatory rule support for other "bands". |
| 436 | **/ |
| 437 | static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, |
| 438 | u32 freq_khz) |
| 439 | { |
| 440 | #define ONE_GHZ_IN_KHZ 1000000 |
| 441 | if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) |
| 442 | return true; |
| 443 | if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) |
| 444 | return true; |
| 445 | return false; |
| 446 | #undef ONE_GHZ_IN_KHZ |
| 447 | } |
| 448 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 449 | /* Converts a country IE to a regulatory domain. A regulatory domain |
| 450 | * structure has a lot of information which the IE doesn't yet have, |
| 451 | * so for the other values we use upper max values as we will intersect |
| 452 | * with our userspace regulatory agent to get lower bounds. */ |
| 453 | static struct ieee80211_regdomain *country_ie_2_rd( |
| 454 | u8 *country_ie, |
| 455 | u8 country_ie_len, |
| 456 | u32 *checksum) |
| 457 | { |
| 458 | struct ieee80211_regdomain *rd = NULL; |
| 459 | unsigned int i = 0; |
| 460 | char alpha2[2]; |
| 461 | u32 flags = 0; |
| 462 | u32 num_rules = 0, size_of_regd = 0; |
| 463 | u8 *triplets_start = NULL; |
| 464 | u8 len_at_triplet = 0; |
| 465 | /* the last channel we have registered in a subband (triplet) */ |
| 466 | int last_sub_max_channel = 0; |
| 467 | |
| 468 | *checksum = 0xDEADBEEF; |
| 469 | |
| 470 | /* Country IE requirements */ |
| 471 | BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN || |
| 472 | country_ie_len & 0x01); |
| 473 | |
| 474 | alpha2[0] = country_ie[0]; |
| 475 | alpha2[1] = country_ie[1]; |
| 476 | |
| 477 | /* |
| 478 | * Third octet can be: |
| 479 | * 'I' - Indoor |
| 480 | * 'O' - Outdoor |
| 481 | * |
| 482 | * anything else we assume is no restrictions |
| 483 | */ |
| 484 | if (country_ie[2] == 'I') |
| 485 | flags = NL80211_RRF_NO_OUTDOOR; |
| 486 | else if (country_ie[2] == 'O') |
| 487 | flags = NL80211_RRF_NO_INDOOR; |
| 488 | |
| 489 | country_ie += 3; |
| 490 | country_ie_len -= 3; |
| 491 | |
| 492 | triplets_start = country_ie; |
| 493 | len_at_triplet = country_ie_len; |
| 494 | |
| 495 | *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8); |
| 496 | |
| 497 | /* We need to build a reg rule for each triplet, but first we must |
| 498 | * calculate the number of reg rules we will need. We will need one |
| 499 | * for each channel subband */ |
| 500 | while (country_ie_len >= 3) { |
| 501 | struct ieee80211_country_ie_triplet *triplet = |
| 502 | (struct ieee80211_country_ie_triplet *) country_ie; |
| 503 | int cur_sub_max_channel = 0, cur_channel = 0; |
| 504 | |
| 505 | if (triplet->ext.reg_extension_id >= |
| 506 | IEEE80211_COUNTRY_EXTENSION_ID) { |
| 507 | country_ie += 3; |
| 508 | country_ie_len -= 3; |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | cur_channel = triplet->chans.first_channel; |
| 513 | cur_sub_max_channel = ieee80211_channel_to_frequency( |
| 514 | cur_channel + triplet->chans.num_channels); |
| 515 | |
| 516 | /* Basic sanity check */ |
| 517 | if (cur_sub_max_channel < cur_channel) |
| 518 | return NULL; |
| 519 | |
| 520 | /* Do not allow overlapping channels. Also channels |
| 521 | * passed in each subband must be monotonically |
| 522 | * increasing */ |
| 523 | if (last_sub_max_channel) { |
| 524 | if (cur_channel <= last_sub_max_channel) |
| 525 | return NULL; |
| 526 | if (cur_sub_max_channel <= last_sub_max_channel) |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | /* When dot11RegulatoryClassesRequired is supported |
| 531 | * we can throw ext triplets as part of this soup, |
| 532 | * for now we don't care when those change as we |
| 533 | * don't support them */ |
| 534 | *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) | |
| 535 | ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) | |
| 536 | ((triplet->chans.max_power ^ cur_sub_max_channel) << 24); |
| 537 | |
| 538 | last_sub_max_channel = cur_sub_max_channel; |
| 539 | |
| 540 | country_ie += 3; |
| 541 | country_ie_len -= 3; |
| 542 | num_rules++; |
| 543 | |
| 544 | /* Note: this is not a IEEE requirement but |
| 545 | * simply a memory requirement */ |
| 546 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | country_ie = triplets_start; |
| 551 | country_ie_len = len_at_triplet; |
| 552 | |
| 553 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 554 | (num_rules * sizeof(struct ieee80211_reg_rule)); |
| 555 | |
| 556 | rd = kzalloc(size_of_regd, GFP_KERNEL); |
| 557 | if (!rd) |
| 558 | return NULL; |
| 559 | |
| 560 | rd->n_reg_rules = num_rules; |
| 561 | rd->alpha2[0] = alpha2[0]; |
| 562 | rd->alpha2[1] = alpha2[1]; |
| 563 | |
| 564 | /* This time around we fill in the rd */ |
| 565 | while (country_ie_len >= 3) { |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 566 | int end_channel = 0; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 567 | struct ieee80211_country_ie_triplet *triplet = |
| 568 | (struct ieee80211_country_ie_triplet *) country_ie; |
| 569 | struct ieee80211_reg_rule *reg_rule = NULL; |
| 570 | struct ieee80211_freq_range *freq_range = NULL; |
| 571 | struct ieee80211_power_rule *power_rule = NULL; |
| 572 | |
| 573 | /* Must parse if dot11RegulatoryClassesRequired is true, |
| 574 | * we don't support this yet */ |
| 575 | if (triplet->ext.reg_extension_id >= |
| 576 | IEEE80211_COUNTRY_EXTENSION_ID) { |
| 577 | country_ie += 3; |
| 578 | country_ie_len -= 3; |
| 579 | continue; |
| 580 | } |
| 581 | |
| 582 | reg_rule = &rd->reg_rules[i]; |
| 583 | freq_range = ®_rule->freq_range; |
| 584 | power_rule = ®_rule->power_rule; |
| 585 | |
| 586 | reg_rule->flags = flags; |
| 587 | |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 588 | /* 2 GHz */ |
| 589 | if (triplet->chans.first_channel <= 14) |
| 590 | end_channel = triplet->chans.first_channel + |
| 591 | triplet->chans.num_channels; |
| 592 | else |
| 593 | /* |
| 594 | * 5 GHz -- For example in country IEs if the first |
| 595 | * channel given is 36 and the number of channels is 4 |
| 596 | * then the individual channel numbers defined for the |
| 597 | * 5 GHz PHY by these parameters are: 36, 40, 44, and 48 |
| 598 | * and not 36, 37, 38, 39. |
| 599 | * |
| 600 | * See: http://tinyurl.com/11d-clarification |
| 601 | */ |
| 602 | end_channel = triplet->chans.first_channel + |
| 603 | (4 * (triplet->chans.num_channels - 1)); |
| 604 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 605 | /* The +10 is since the regulatory domain expects |
| 606 | * the actual band edge, not the center of freq for |
| 607 | * its start and end freqs, assuming 20 MHz bandwidth on |
| 608 | * the channels passed */ |
| 609 | freq_range->start_freq_khz = |
| 610 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( |
| 611 | triplet->chans.first_channel) - 10); |
| 612 | freq_range->end_freq_khz = |
| 613 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 614 | end_channel) + 10); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 615 | |
| 616 | /* Large arbitrary values, we intersect later */ |
| 617 | /* Increment this if we ever support >= 40 MHz channels |
| 618 | * in IEEE 802.11 */ |
| 619 | freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40); |
| 620 | power_rule->max_antenna_gain = DBI_TO_MBI(100); |
| 621 | power_rule->max_eirp = DBM_TO_MBM(100); |
| 622 | |
| 623 | country_ie += 3; |
| 624 | country_ie_len -= 3; |
| 625 | i++; |
| 626 | |
| 627 | BUG_ON(i > NL80211_MAX_SUPP_REG_RULES); |
| 628 | } |
| 629 | |
| 630 | return rd; |
| 631 | } |
| 632 | |
| 633 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 634 | /* Helper for regdom_intersect(), this does the real |
| 635 | * mathematical intersection fun */ |
| 636 | static int reg_rules_intersect( |
| 637 | const struct ieee80211_reg_rule *rule1, |
| 638 | const struct ieee80211_reg_rule *rule2, |
| 639 | struct ieee80211_reg_rule *intersected_rule) |
| 640 | { |
| 641 | const struct ieee80211_freq_range *freq_range1, *freq_range2; |
| 642 | struct ieee80211_freq_range *freq_range; |
| 643 | const struct ieee80211_power_rule *power_rule1, *power_rule2; |
| 644 | struct ieee80211_power_rule *power_rule; |
| 645 | u32 freq_diff; |
| 646 | |
| 647 | freq_range1 = &rule1->freq_range; |
| 648 | freq_range2 = &rule2->freq_range; |
| 649 | freq_range = &intersected_rule->freq_range; |
| 650 | |
| 651 | power_rule1 = &rule1->power_rule; |
| 652 | power_rule2 = &rule2->power_rule; |
| 653 | power_rule = &intersected_rule->power_rule; |
| 654 | |
| 655 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, |
| 656 | freq_range2->start_freq_khz); |
| 657 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, |
| 658 | freq_range2->end_freq_khz); |
| 659 | freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, |
| 660 | freq_range2->max_bandwidth_khz); |
| 661 | |
| 662 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 663 | if (freq_range->max_bandwidth_khz > freq_diff) |
| 664 | freq_range->max_bandwidth_khz = freq_diff; |
| 665 | |
| 666 | power_rule->max_eirp = min(power_rule1->max_eirp, |
| 667 | power_rule2->max_eirp); |
| 668 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, |
| 669 | power_rule2->max_antenna_gain); |
| 670 | |
| 671 | intersected_rule->flags = (rule1->flags | rule2->flags); |
| 672 | |
| 673 | if (!is_valid_reg_rule(intersected_rule)) |
| 674 | return -EINVAL; |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * regdom_intersect - do the intersection between two regulatory domains |
| 681 | * @rd1: first regulatory domain |
| 682 | * @rd2: second regulatory domain |
| 683 | * |
| 684 | * Use this function to get the intersection between two regulatory domains. |
| 685 | * Once completed we will mark the alpha2 for the rd as intersected, "98", |
| 686 | * as no one single alpha2 can represent this regulatory domain. |
| 687 | * |
| 688 | * Returns a pointer to the regulatory domain structure which will hold the |
| 689 | * resulting intersection of rules between rd1 and rd2. We will |
| 690 | * kzalloc() this structure for you. |
| 691 | */ |
| 692 | static struct ieee80211_regdomain *regdom_intersect( |
| 693 | const struct ieee80211_regdomain *rd1, |
| 694 | const struct ieee80211_regdomain *rd2) |
| 695 | { |
| 696 | int r, size_of_regd; |
| 697 | unsigned int x, y; |
| 698 | unsigned int num_rules = 0, rule_idx = 0; |
| 699 | const struct ieee80211_reg_rule *rule1, *rule2; |
| 700 | struct ieee80211_reg_rule *intersected_rule; |
| 701 | struct ieee80211_regdomain *rd; |
| 702 | /* This is just a dummy holder to help us count */ |
| 703 | struct ieee80211_reg_rule irule; |
| 704 | |
| 705 | /* Uses the stack temporarily for counter arithmetic */ |
| 706 | intersected_rule = &irule; |
| 707 | |
| 708 | memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule)); |
| 709 | |
| 710 | if (!rd1 || !rd2) |
| 711 | return NULL; |
| 712 | |
| 713 | /* First we get a count of the rules we'll need, then we actually |
| 714 | * build them. This is to so we can malloc() and free() a |
| 715 | * regdomain once. The reason we use reg_rules_intersect() here |
| 716 | * is it will return -EINVAL if the rule computed makes no sense. |
| 717 | * All rules that do check out OK are valid. */ |
| 718 | |
| 719 | for (x = 0; x < rd1->n_reg_rules; x++) { |
| 720 | rule1 = &rd1->reg_rules[x]; |
| 721 | for (y = 0; y < rd2->n_reg_rules; y++) { |
| 722 | rule2 = &rd2->reg_rules[y]; |
| 723 | if (!reg_rules_intersect(rule1, rule2, |
| 724 | intersected_rule)) |
| 725 | num_rules++; |
| 726 | memset(intersected_rule, 0, |
| 727 | sizeof(struct ieee80211_reg_rule)); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (!num_rules) |
| 732 | return NULL; |
| 733 | |
| 734 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 735 | ((num_rules + 1) * sizeof(struct ieee80211_reg_rule)); |
| 736 | |
| 737 | rd = kzalloc(size_of_regd, GFP_KERNEL); |
| 738 | if (!rd) |
| 739 | return NULL; |
| 740 | |
| 741 | for (x = 0; x < rd1->n_reg_rules; x++) { |
| 742 | rule1 = &rd1->reg_rules[x]; |
| 743 | for (y = 0; y < rd2->n_reg_rules; y++) { |
| 744 | rule2 = &rd2->reg_rules[y]; |
| 745 | /* This time around instead of using the stack lets |
| 746 | * write to the target rule directly saving ourselves |
| 747 | * a memcpy() */ |
| 748 | intersected_rule = &rd->reg_rules[rule_idx]; |
| 749 | r = reg_rules_intersect(rule1, rule2, |
| 750 | intersected_rule); |
| 751 | /* No need to memset here the intersected rule here as |
| 752 | * we're not using the stack anymore */ |
| 753 | if (r) |
| 754 | continue; |
| 755 | rule_idx++; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (rule_idx != num_rules) { |
| 760 | kfree(rd); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | rd->n_reg_rules = num_rules; |
| 765 | rd->alpha2[0] = '9'; |
| 766 | rd->alpha2[1] = '8'; |
| 767 | |
| 768 | return rd; |
| 769 | } |
| 770 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 771 | /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may |
| 772 | * want to just have the channel structure use these */ |
| 773 | static u32 map_regdom_flags(u32 rd_flags) |
| 774 | { |
| 775 | u32 channel_flags = 0; |
| 776 | if (rd_flags & NL80211_RRF_PASSIVE_SCAN) |
| 777 | channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN; |
| 778 | if (rd_flags & NL80211_RRF_NO_IBSS) |
| 779 | channel_flags |= IEEE80211_CHAN_NO_IBSS; |
| 780 | if (rd_flags & NL80211_RRF_DFS) |
| 781 | channel_flags |= IEEE80211_CHAN_RADAR; |
| 782 | return channel_flags; |
| 783 | } |
| 784 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 785 | static int freq_reg_info_regd(struct wiphy *wiphy, |
| 786 | u32 center_freq, |
| 787 | u32 *bandwidth, |
| 788 | const struct ieee80211_reg_rule **reg_rule, |
| 789 | const struct ieee80211_regdomain *custom_regd) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 790 | { |
| 791 | int i; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 792 | bool band_rule_found = false; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 793 | const struct ieee80211_regdomain *regd; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 794 | u32 max_bandwidth = 0; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 795 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 796 | regd = custom_regd ? custom_regd : cfg80211_regdomain; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 797 | |
| 798 | /* Follow the driver's regulatory domain, if present, unless a country |
| 799 | * IE has been processed */ |
| 800 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE && |
| 801 | wiphy->regd) |
| 802 | regd = wiphy->regd; |
| 803 | |
| 804 | if (!regd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 807 | for (i = 0; i < regd->n_reg_rules; i++) { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 808 | const struct ieee80211_reg_rule *rr; |
| 809 | const struct ieee80211_freq_range *fr = NULL; |
| 810 | const struct ieee80211_power_rule *pr = NULL; |
| 811 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 812 | rr = ®d->reg_rules[i]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 813 | fr = &rr->freq_range; |
| 814 | pr = &rr->power_rule; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 815 | |
| 816 | /* We only need to know if one frequency rule was |
| 817 | * was in center_freq's band, that's enough, so lets |
| 818 | * not overwrite it once found */ |
| 819 | if (!band_rule_found) |
| 820 | band_rule_found = freq_in_rule_band(fr, center_freq); |
| 821 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 822 | max_bandwidth = freq_max_bandwidth(fr, center_freq); |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 823 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 824 | if (max_bandwidth && *bandwidth <= max_bandwidth) { |
| 825 | *reg_rule = rr; |
| 826 | *bandwidth = max_bandwidth; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | } |
| 830 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 831 | if (!band_rule_found) |
| 832 | return -ERANGE; |
| 833 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 834 | return !max_bandwidth; |
| 835 | } |
Luis R. Rodriguez | 34f5734 | 2009-01-22 15:05:45 -0800 | [diff] [blame] | 836 | EXPORT_SYMBOL(freq_reg_info); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 837 | |
Luis R. Rodriguez | 34f5734 | 2009-01-22 15:05:45 -0800 | [diff] [blame] | 838 | 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] | 839 | const struct ieee80211_reg_rule **reg_rule) |
| 840 | { |
| 841 | return freq_reg_info_regd(wiphy, center_freq, |
| 842 | bandwidth, reg_rule, NULL); |
| 843 | } |
| 844 | |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 845 | static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band, |
| 846 | unsigned int chan_idx) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 847 | { |
| 848 | int r; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 849 | u32 flags; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 850 | u32 max_bandwidth = 0; |
| 851 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 852 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 853 | struct ieee80211_supported_band *sband; |
| 854 | struct ieee80211_channel *chan; |
| 855 | |
| 856 | sband = wiphy->bands[band]; |
| 857 | BUG_ON(chan_idx >= sband->n_channels); |
| 858 | chan = &sband->channels[chan_idx]; |
| 859 | |
| 860 | flags = chan->orig_flags; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 861 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 862 | r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq), |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 863 | &max_bandwidth, ®_rule); |
| 864 | |
| 865 | if (r) { |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 866 | /* This means no regulatory rule was found in the country IE |
| 867 | * with a frequency range on the center_freq's band, since |
| 868 | * IEEE-802.11 allows for a country IE to have a subset of the |
| 869 | * regulatory information provided in a country we ignore |
| 870 | * disabling the channel unless at least one reg rule was |
| 871 | * found on the center_freq's band. For details see this |
| 872 | * clarification: |
| 873 | * |
| 874 | * http://tinyurl.com/11d-clarification |
| 875 | */ |
| 876 | if (r == -ERANGE && |
| 877 | last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { |
| 878 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 879 | printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz " |
| 880 | "intact on %s - no rule found in band on " |
| 881 | "Country IE\n", |
| 882 | chan->center_freq, wiphy_name(wiphy)); |
| 883 | #endif |
| 884 | } else { |
| 885 | /* In this case we know the country IE has at least one reg rule |
| 886 | * for the band so we respect its band definitions */ |
| 887 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 888 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) |
| 889 | printk(KERN_DEBUG "cfg80211: Disabling " |
| 890 | "channel %d MHz on %s due to " |
| 891 | "Country IE\n", |
| 892 | chan->center_freq, wiphy_name(wiphy)); |
| 893 | #endif |
| 894 | flags |= IEEE80211_CHAN_DISABLED; |
| 895 | chan->flags = flags; |
| 896 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 900 | power_rule = ®_rule->power_rule; |
| 901 | |
| 902 | chan->flags = flags | map_regdom_flags(reg_rule->flags); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 903 | chan->max_antenna_gain = min(chan->orig_mag, |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 904 | (int) MBI_TO_DBI(power_rule->max_antenna_gain)); |
| 905 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); |
John W. Linville | 253898c | 2008-04-03 15:32:54 -0400 | [diff] [blame] | 906 | if (chan->orig_mpwr) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 907 | chan->max_power = min(chan->orig_mpwr, |
| 908 | (int) MBM_TO_DBM(power_rule->max_eirp)); |
John W. Linville | 253898c | 2008-04-03 15:32:54 -0400 | [diff] [blame] | 909 | else |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 910 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 913 | static void handle_band(struct wiphy *wiphy, enum ieee80211_band band) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 914 | { |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 915 | unsigned int i; |
| 916 | struct ieee80211_supported_band *sband; |
| 917 | |
| 918 | BUG_ON(!wiphy->bands[band]); |
| 919 | sband = wiphy->bands[band]; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 920 | |
| 921 | for (i = 0; i < sband->n_channels; i++) |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 922 | handle_channel(wiphy, band, i); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 925 | static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby) |
| 926 | { |
| 927 | if (!last_request) |
| 928 | return true; |
| 929 | if (setby == REGDOM_SET_BY_CORE && |
| 930 | wiphy->fw_handles_regulatory) |
| 931 | return true; |
| 932 | return false; |
| 933 | } |
| 934 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 935 | static void update_all_wiphy_regulatory(enum reg_set_by setby) |
| 936 | { |
| 937 | struct cfg80211_registered_device *drv; |
| 938 | |
| 939 | list_for_each_entry(drv, &cfg80211_drv_list, list) |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 940 | if (!ignore_reg_update(&drv->wiphy, setby)) |
| 941 | wiphy_update_regulatory(&drv->wiphy, setby); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 945 | { |
| 946 | enum ieee80211_band band; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 947 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 948 | if (wiphy->bands[band]) |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 949 | handle_band(wiphy, band); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 950 | } |
Luis R. Rodriguez | 560e28e | 2009-01-07 17:43:32 -0800 | [diff] [blame] | 951 | if (wiphy->reg_notifier) |
| 952 | wiphy->reg_notifier(wiphy, setby); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 955 | static void handle_channel_custom(struct wiphy *wiphy, |
| 956 | enum ieee80211_band band, |
| 957 | unsigned int chan_idx, |
| 958 | const struct ieee80211_regdomain *regd) |
| 959 | { |
| 960 | int r; |
| 961 | u32 max_bandwidth = 0; |
| 962 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 963 | const struct ieee80211_power_rule *power_rule = NULL; |
| 964 | struct ieee80211_supported_band *sband; |
| 965 | struct ieee80211_channel *chan; |
| 966 | |
| 967 | sband = wiphy->bands[band]; |
| 968 | BUG_ON(chan_idx >= sband->n_channels); |
| 969 | chan = &sband->channels[chan_idx]; |
| 970 | |
| 971 | r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq), |
| 972 | &max_bandwidth, ®_rule, regd); |
| 973 | |
| 974 | if (r) { |
| 975 | chan->flags = IEEE80211_CHAN_DISABLED; |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | power_rule = ®_rule->power_rule; |
| 980 | |
| 981 | chan->flags |= map_regdom_flags(reg_rule->flags); |
| 982 | chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
| 983 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); |
| 984 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
| 985 | } |
| 986 | |
| 987 | static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band, |
| 988 | const struct ieee80211_regdomain *regd) |
| 989 | { |
| 990 | unsigned int i; |
| 991 | struct ieee80211_supported_band *sband; |
| 992 | |
| 993 | BUG_ON(!wiphy->bands[band]); |
| 994 | sband = wiphy->bands[band]; |
| 995 | |
| 996 | for (i = 0; i < sband->n_channels; i++) |
| 997 | handle_channel_custom(wiphy, band, i, regd); |
| 998 | } |
| 999 | |
| 1000 | /* Used by drivers prior to wiphy registration */ |
| 1001 | void wiphy_apply_custom_regulatory(struct wiphy *wiphy, |
| 1002 | const struct ieee80211_regdomain *regd) |
| 1003 | { |
| 1004 | enum ieee80211_band band; |
| 1005 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 1006 | if (wiphy->bands[band]) |
| 1007 | handle_band_custom(wiphy, band, regd); |
| 1008 | } |
| 1009 | } |
| 1010 | EXPORT_SYMBOL(wiphy_apply_custom_regulatory); |
| 1011 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1012 | static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd, |
| 1013 | const struct ieee80211_regdomain *src_regd) |
| 1014 | { |
| 1015 | struct ieee80211_regdomain *regd; |
| 1016 | int size_of_regd = 0; |
| 1017 | unsigned int i; |
| 1018 | |
| 1019 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 1020 | ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule)); |
| 1021 | |
| 1022 | regd = kzalloc(size_of_regd, GFP_KERNEL); |
| 1023 | if (!regd) |
| 1024 | return -ENOMEM; |
| 1025 | |
| 1026 | memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); |
| 1027 | |
| 1028 | for (i = 0; i < src_regd->n_reg_rules; i++) |
| 1029 | memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], |
| 1030 | sizeof(struct ieee80211_reg_rule)); |
| 1031 | |
| 1032 | *dst_regd = regd; |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1036 | /* Return value which can be used by ignore_request() to indicate |
| 1037 | * it has been determined we should intersect two regulatory domains */ |
| 1038 | #define REG_INTERSECT 1 |
| 1039 | |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1040 | /* This has the logic which determines when a new request |
| 1041 | * should be ignored. */ |
| 1042 | static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by, |
| 1043 | const char *alpha2) |
| 1044 | { |
| 1045 | /* All initial requests are respected */ |
| 1046 | if (!last_request) |
| 1047 | return 0; |
| 1048 | |
| 1049 | switch (set_by) { |
| 1050 | case REGDOM_SET_BY_INIT: |
| 1051 | return -EINVAL; |
| 1052 | case REGDOM_SET_BY_CORE: |
| 1053 | /* |
| 1054 | * Always respect new wireless core hints, should only happen |
| 1055 | * when updating the world regulatory domain at init. |
| 1056 | */ |
| 1057 | return 0; |
| 1058 | case REGDOM_SET_BY_COUNTRY_IE: |
| 1059 | if (unlikely(!is_an_alpha2(alpha2))) |
| 1060 | return -EINVAL; |
| 1061 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { |
| 1062 | if (last_request->wiphy != wiphy) { |
| 1063 | /* |
| 1064 | * Two cards with two APs claiming different |
| 1065 | * different Country IE alpha2s. We could |
| 1066 | * intersect them, but that seems unlikely |
| 1067 | * to be correct. Reject second one for now. |
| 1068 | */ |
| 1069 | if (!alpha2_equal(alpha2, |
| 1070 | cfg80211_regdomain->alpha2)) |
| 1071 | return -EOPNOTSUPP; |
| 1072 | return -EALREADY; |
| 1073 | } |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1074 | /* Two consecutive Country IE hints on the same wiphy. |
| 1075 | * This should be picked up early by the driver/stack */ |
| 1076 | if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2, |
| 1077 | alpha2))) |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1078 | return 0; |
| 1079 | return -EALREADY; |
| 1080 | } |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1081 | return REG_INTERSECT; |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1082 | case REGDOM_SET_BY_DRIVER: |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1083 | if (last_request->initiator == REGDOM_SET_BY_CORE) |
| 1084 | return 0; |
| 1085 | return REG_INTERSECT; |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1086 | case REGDOM_SET_BY_USER: |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1087 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1088 | return REG_INTERSECT; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1089 | /* If the user knows better the user should set the regdom |
| 1090 | * to their country before the IE is picked up */ |
| 1091 | if (last_request->initiator == REGDOM_SET_BY_USER && |
| 1092 | last_request->intersect) |
| 1093 | return -EOPNOTSUPP; |
Luis R. Rodriguez | 5eebade | 2009-01-22 15:05:47 -0800 | [diff] [blame^] | 1094 | /* Process user requests only after previous user/driver/core |
| 1095 | * requests have been processed */ |
| 1096 | if (last_request->initiator == REGDOM_SET_BY_CORE || |
| 1097 | last_request->initiator == REGDOM_SET_BY_DRIVER || |
| 1098 | last_request->initiator == REGDOM_SET_BY_USER) { |
| 1099 | if (!alpha2_equal(last_request->alpha2, |
| 1100 | cfg80211_regdomain->alpha2)) |
| 1101 | return -EAGAIN; |
| 1102 | } |
| 1103 | |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1110 | /* Caller must hold &cfg80211_drv_mutex */ |
| 1111 | int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by, |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1112 | const char *alpha2, |
| 1113 | u32 country_ie_checksum, |
| 1114 | enum environment_cap env) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct regulatory_request *request; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1117 | bool intersect = false; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1118 | int r = 0; |
| 1119 | |
Johannes Berg | be3d481 | 2008-10-24 20:32:21 +0200 | [diff] [blame] | 1120 | r = ignore_request(wiphy, set_by, alpha2); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1121 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1122 | if (r == REG_INTERSECT) { |
| 1123 | if (set_by == REGDOM_SET_BY_DRIVER) { |
| 1124 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
| 1125 | if (r) |
| 1126 | return r; |
| 1127 | } |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1128 | intersect = true; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1129 | } else if (r) { |
| 1130 | /* If the regulatory domain being requested by the |
| 1131 | * driver has already been set just copy it to the |
| 1132 | * wiphy */ |
| 1133 | if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) { |
| 1134 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
| 1135 | if (r) |
| 1136 | return r; |
| 1137 | r = -EALREADY; |
| 1138 | goto new_request; |
| 1139 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1140 | return r; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1141 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1142 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1143 | new_request: |
Luis R. Rodriguez | 5203cdb | 2008-11-12 14:21:56 -0800 | [diff] [blame] | 1144 | request = kzalloc(sizeof(struct regulatory_request), |
| 1145 | GFP_KERNEL); |
| 1146 | if (!request) |
| 1147 | return -ENOMEM; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1148 | |
Luis R. Rodriguez | 5203cdb | 2008-11-12 14:21:56 -0800 | [diff] [blame] | 1149 | request->alpha2[0] = alpha2[0]; |
| 1150 | request->alpha2[1] = alpha2[1]; |
| 1151 | request->initiator = set_by; |
| 1152 | request->wiphy = wiphy; |
| 1153 | request->intersect = intersect; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1154 | request->country_ie_checksum = country_ie_checksum; |
| 1155 | request->country_ie_env = env; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1156 | |
Luis R. Rodriguez | 5203cdb | 2008-11-12 14:21:56 -0800 | [diff] [blame] | 1157 | kfree(last_request); |
| 1158 | last_request = request; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1159 | |
| 1160 | /* When r == REG_INTERSECT we do need to call CRDA */ |
| 1161 | if (r < 0) |
| 1162 | return r; |
| 1163 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1164 | /* |
| 1165 | * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled |
| 1166 | * AND if CRDA is NOT present nothing will happen, if someone |
| 1167 | * wants to bother with 11d with OLD_REG you can add a timer. |
| 1168 | * If after x amount of time nothing happens you can call: |
| 1169 | * |
| 1170 | * return set_regdom(country_ie_regdomain); |
| 1171 | * |
| 1172 | * to intersect with the static rd |
| 1173 | */ |
Luis R. Rodriguez | 02ba0b3 | 2008-11-12 14:22:00 -0800 | [diff] [blame] | 1174 | return call_crda(alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Johannes Berg | be3d481 | 2008-10-24 20:32:21 +0200 | [diff] [blame] | 1177 | void regulatory_hint(struct wiphy *wiphy, const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1178 | { |
Johannes Berg | be3d481 | 2008-10-24 20:32:21 +0200 | [diff] [blame] | 1179 | BUG_ON(!alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1180 | |
| 1181 | mutex_lock(&cfg80211_drv_mutex); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1182 | __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1183 | mutex_unlock(&cfg80211_drv_mutex); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1184 | } |
| 1185 | EXPORT_SYMBOL(regulatory_hint); |
| 1186 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1187 | static bool reg_same_country_ie_hint(struct wiphy *wiphy, |
| 1188 | u32 country_ie_checksum) |
| 1189 | { |
| 1190 | if (!last_request->wiphy) |
| 1191 | return false; |
| 1192 | if (likely(last_request->wiphy != wiphy)) |
| 1193 | return !country_ie_integrity_changes(country_ie_checksum); |
| 1194 | /* We should not have let these through at this point, they |
| 1195 | * should have been picked up earlier by the first alpha2 check |
| 1196 | * on the device */ |
| 1197 | if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum))) |
| 1198 | return true; |
| 1199 | return false; |
| 1200 | } |
| 1201 | |
| 1202 | void regulatory_hint_11d(struct wiphy *wiphy, |
| 1203 | u8 *country_ie, |
| 1204 | u8 country_ie_len) |
| 1205 | { |
| 1206 | struct ieee80211_regdomain *rd = NULL; |
| 1207 | char alpha2[2]; |
| 1208 | u32 checksum = 0; |
| 1209 | enum environment_cap env = ENVIRON_ANY; |
| 1210 | |
Johannes Berg | 0f70f39 | 2008-12-01 18:13:05 +0100 | [diff] [blame] | 1211 | if (!last_request) |
| 1212 | return; |
| 1213 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1214 | mutex_lock(&cfg80211_drv_mutex); |
| 1215 | |
| 1216 | /* IE len must be evenly divisible by 2 */ |
| 1217 | if (country_ie_len & 0x01) |
| 1218 | goto out; |
| 1219 | |
| 1220 | if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) |
| 1221 | goto out; |
| 1222 | |
| 1223 | /* Pending country IE processing, this can happen after we |
| 1224 | * call CRDA and wait for a response if a beacon was received before |
| 1225 | * we were able to process the last regulatory_hint_11d() call */ |
| 1226 | if (country_ie_regdomain) |
| 1227 | goto out; |
| 1228 | |
| 1229 | alpha2[0] = country_ie[0]; |
| 1230 | alpha2[1] = country_ie[1]; |
| 1231 | |
| 1232 | if (country_ie[2] == 'I') |
| 1233 | env = ENVIRON_INDOOR; |
| 1234 | else if (country_ie[2] == 'O') |
| 1235 | env = ENVIRON_OUTDOOR; |
| 1236 | |
| 1237 | /* We will run this for *every* beacon processed for the BSSID, so |
| 1238 | * we optimize an early check to exit out early if we don't have to |
| 1239 | * do anything */ |
| 1240 | if (likely(last_request->wiphy)) { |
| 1241 | struct cfg80211_registered_device *drv_last_ie; |
| 1242 | |
| 1243 | drv_last_ie = wiphy_to_dev(last_request->wiphy); |
| 1244 | |
| 1245 | /* Lets keep this simple -- we trust the first AP |
| 1246 | * after we intersect with CRDA */ |
| 1247 | if (likely(last_request->wiphy == wiphy)) { |
| 1248 | /* Ignore IEs coming in on this wiphy with |
| 1249 | * the same alpha2 and environment cap */ |
| 1250 | if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, |
| 1251 | alpha2) && |
| 1252 | env == drv_last_ie->env)) { |
| 1253 | goto out; |
| 1254 | } |
| 1255 | /* the wiphy moved on to another BSSID or the AP |
| 1256 | * was reconfigured. XXX: We need to deal with the |
| 1257 | * case where the user suspends and goes to goes |
| 1258 | * to another country, and then gets IEs from an |
| 1259 | * AP with different settings */ |
| 1260 | goto out; |
| 1261 | } else { |
| 1262 | /* Ignore IEs coming in on two separate wiphys with |
| 1263 | * the same alpha2 and environment cap */ |
| 1264 | if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, |
| 1265 | alpha2) && |
| 1266 | env == drv_last_ie->env)) { |
| 1267 | goto out; |
| 1268 | } |
| 1269 | /* We could potentially intersect though */ |
| 1270 | goto out; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | rd = country_ie_2_rd(country_ie, country_ie_len, &checksum); |
| 1275 | if (!rd) |
| 1276 | goto out; |
| 1277 | |
| 1278 | /* This will not happen right now but we leave it here for the |
| 1279 | * the future when we want to add suspend/resume support and having |
| 1280 | * the user move to another country after doing so, or having the user |
| 1281 | * move to another AP. Right now we just trust the first AP. This is why |
| 1282 | * this is marked as likley(). If we hit this before we add this support |
| 1283 | * we want to be informed of it as it would indicate a mistake in the |
| 1284 | * current design */ |
| 1285 | if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))) |
| 1286 | goto out; |
| 1287 | |
| 1288 | /* We keep this around for when CRDA comes back with a response so |
| 1289 | * we can intersect with that */ |
| 1290 | country_ie_regdomain = rd; |
| 1291 | |
| 1292 | __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE, |
| 1293 | country_ie_regdomain->alpha2, checksum, env); |
| 1294 | |
| 1295 | out: |
| 1296 | mutex_unlock(&cfg80211_drv_mutex); |
| 1297 | } |
| 1298 | EXPORT_SYMBOL(regulatory_hint_11d); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1299 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1300 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1301 | { |
| 1302 | unsigned int i; |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1303 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 1304 | const struct ieee80211_freq_range *freq_range = NULL; |
| 1305 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1306 | |
| 1307 | printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), " |
| 1308 | "(max_antenna_gain, max_eirp)\n"); |
| 1309 | |
| 1310 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 1311 | reg_rule = &rd->reg_rules[i]; |
| 1312 | freq_range = ®_rule->freq_range; |
| 1313 | power_rule = ®_rule->power_rule; |
| 1314 | |
| 1315 | /* There may not be documentation for max antenna gain |
| 1316 | * in certain regions */ |
| 1317 | if (power_rule->max_antenna_gain) |
| 1318 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " |
| 1319 | "(%d mBi, %d mBm)\n", |
| 1320 | freq_range->start_freq_khz, |
| 1321 | freq_range->end_freq_khz, |
| 1322 | freq_range->max_bandwidth_khz, |
| 1323 | power_rule->max_antenna_gain, |
| 1324 | power_rule->max_eirp); |
| 1325 | else |
| 1326 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " |
| 1327 | "(N/A, %d mBm)\n", |
| 1328 | freq_range->start_freq_khz, |
| 1329 | freq_range->end_freq_khz, |
| 1330 | freq_range->max_bandwidth_khz, |
| 1331 | power_rule->max_eirp); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1335 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1336 | { |
| 1337 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1338 | if (is_intersected_alpha2(rd->alpha2)) { |
| 1339 | struct wiphy *wiphy = NULL; |
| 1340 | struct cfg80211_registered_device *drv; |
| 1341 | |
| 1342 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { |
| 1343 | if (last_request->wiphy) { |
| 1344 | wiphy = last_request->wiphy; |
| 1345 | drv = wiphy_to_dev(wiphy); |
| 1346 | printk(KERN_INFO "cfg80211: Current regulatory " |
| 1347 | "domain updated by AP to: %c%c\n", |
| 1348 | drv->country_ie_alpha2[0], |
| 1349 | drv->country_ie_alpha2[1]); |
| 1350 | } else |
| 1351 | printk(KERN_INFO "cfg80211: Current regulatory " |
| 1352 | "domain intersected: \n"); |
| 1353 | } else |
| 1354 | printk(KERN_INFO "cfg80211: Current regulatory " |
Luis R. Rodriguez | 039498c | 2009-01-07 17:43:35 -0800 | [diff] [blame] | 1355 | "domain intersected: \n"); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1356 | } else if (is_world_regdom(rd->alpha2)) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1357 | printk(KERN_INFO "cfg80211: World regulatory " |
| 1358 | "domain updated:\n"); |
| 1359 | else { |
| 1360 | if (is_unknown_alpha2(rd->alpha2)) |
| 1361 | printk(KERN_INFO "cfg80211: Regulatory domain " |
| 1362 | "changed to driver built-in settings " |
| 1363 | "(unknown country)\n"); |
| 1364 | else |
| 1365 | printk(KERN_INFO "cfg80211: Regulatory domain " |
| 1366 | "changed to country: %c%c\n", |
| 1367 | rd->alpha2[0], rd->alpha2[1]); |
| 1368 | } |
| 1369 | print_rd_rules(rd); |
| 1370 | } |
| 1371 | |
Johannes Berg | 2df7816 | 2008-10-28 16:49:41 +0100 | [diff] [blame] | 1372 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1373 | { |
| 1374 | printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n", |
| 1375 | rd->alpha2[0], rd->alpha2[1]); |
| 1376 | print_rd_rules(rd); |
| 1377 | } |
| 1378 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1379 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 1380 | static void reg_country_ie_process_debug( |
| 1381 | const struct ieee80211_regdomain *rd, |
| 1382 | const struct ieee80211_regdomain *country_ie_regdomain, |
| 1383 | const struct ieee80211_regdomain *intersected_rd) |
| 1384 | { |
| 1385 | printk(KERN_DEBUG "cfg80211: Received country IE:\n"); |
| 1386 | print_regdomain_info(country_ie_regdomain); |
| 1387 | printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n"); |
| 1388 | print_regdomain_info(rd); |
| 1389 | if (intersected_rd) { |
| 1390 | printk(KERN_DEBUG "cfg80211: We intersect both of these " |
| 1391 | "and get:\n"); |
| 1392 | print_regdomain_info(rd); |
| 1393 | return; |
| 1394 | } |
| 1395 | printk(KERN_DEBUG "cfg80211: Intersection between both failed\n"); |
| 1396 | } |
| 1397 | #else |
| 1398 | static inline void reg_country_ie_process_debug( |
| 1399 | const struct ieee80211_regdomain *rd, |
| 1400 | const struct ieee80211_regdomain *country_ie_regdomain, |
| 1401 | const struct ieee80211_regdomain *intersected_rd) |
| 1402 | { |
| 1403 | } |
| 1404 | #endif |
| 1405 | |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 1406 | /* Takes ownership of rd only if it doesn't fail */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1407 | static int __set_regdom(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1408 | { |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1409 | const struct ieee80211_regdomain *intersected_rd = NULL; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1410 | struct cfg80211_registered_device *drv = NULL; |
| 1411 | struct wiphy *wiphy = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1412 | /* Some basic sanity checks first */ |
| 1413 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1414 | if (is_world_regdom(rd->alpha2)) { |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 1415 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1416 | return -EINVAL; |
| 1417 | update_world_regdomain(rd); |
| 1418 | return 0; |
| 1419 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1420 | |
| 1421 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && |
| 1422 | !is_unknown_alpha2(rd->alpha2)) |
| 1423 | return -EINVAL; |
| 1424 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 1425 | if (!last_request) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1426 | return -EINVAL; |
| 1427 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1428 | /* Lets only bother proceeding on the same alpha2 if the current |
| 1429 | * rd is non static (it means CRDA was present and was used last) |
| 1430 | * and the pending request came in from a country IE */ |
| 1431 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { |
| 1432 | /* If someone else asked us to change the rd lets only bother |
| 1433 | * checking if the alpha2 changes if CRDA was already called */ |
| 1434 | if (!is_old_static_regdom(cfg80211_regdomain) && |
| 1435 | !regdom_changed(rd->alpha2)) |
| 1436 | return -EINVAL; |
| 1437 | } |
| 1438 | |
| 1439 | wiphy = last_request->wiphy; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* Now lets set the regulatory domain, update all driver channels |
| 1442 | * and finally inform them of what we have done, in case they want |
| 1443 | * to review or adjust their own settings based on their own |
| 1444 | * internal EEPROM data */ |
| 1445 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 1446 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1447 | return -EINVAL; |
| 1448 | |
Luis R. Rodriguez | 8375af3 | 2008-11-12 14:21:57 -0800 | [diff] [blame] | 1449 | if (!is_valid_rd(rd)) { |
| 1450 | printk(KERN_ERR "cfg80211: Invalid " |
| 1451 | "regulatory domain detected:\n"); |
| 1452 | print_regdomain_info(rd); |
| 1453 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 1456 | if (!last_request->intersect) { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1457 | int r; |
| 1458 | |
| 1459 | if (last_request->initiator != REGDOM_SET_BY_DRIVER) { |
| 1460 | reset_regdomains(); |
| 1461 | cfg80211_regdomain = rd; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | /* For a driver hint, lets copy the regulatory domain the |
| 1466 | * driver wanted to the wiphy to deal with conflicts */ |
| 1467 | |
| 1468 | BUG_ON(last_request->wiphy->regd); |
| 1469 | |
| 1470 | r = reg_copy_regd(&last_request->wiphy->regd, rd); |
| 1471 | if (r) |
| 1472 | return r; |
| 1473 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 1474 | reset_regdomains(); |
| 1475 | cfg80211_regdomain = rd; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | /* Intersection requires a bit more work */ |
| 1480 | |
| 1481 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { |
| 1482 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1483 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); |
| 1484 | if (!intersected_rd) |
| 1485 | return -EINVAL; |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 1486 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1487 | /* We can trash what CRDA provided now. |
| 1488 | * However if a driver requested this specific regulatory |
| 1489 | * domain we keep it for its private use */ |
| 1490 | if (last_request->initiator == REGDOM_SET_BY_DRIVER) |
| 1491 | last_request->wiphy->regd = rd; |
| 1492 | else |
| 1493 | kfree(rd); |
| 1494 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 1495 | rd = NULL; |
| 1496 | |
| 1497 | reset_regdomains(); |
| 1498 | cfg80211_regdomain = intersected_rd; |
| 1499 | |
| 1500 | return 0; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1503 | /* |
| 1504 | * Country IE requests are handled a bit differently, we intersect |
| 1505 | * the country IE rd with what CRDA believes that country should have |
| 1506 | */ |
| 1507 | |
| 1508 | BUG_ON(!country_ie_regdomain); |
| 1509 | |
| 1510 | if (rd != country_ie_regdomain) { |
| 1511 | /* Intersect what CRDA returned and our what we |
| 1512 | * had built from the Country IE received */ |
| 1513 | |
| 1514 | intersected_rd = regdom_intersect(rd, country_ie_regdomain); |
| 1515 | |
| 1516 | reg_country_ie_process_debug(rd, country_ie_regdomain, |
| 1517 | intersected_rd); |
| 1518 | |
| 1519 | kfree(country_ie_regdomain); |
| 1520 | country_ie_regdomain = NULL; |
| 1521 | } else { |
| 1522 | /* This would happen when CRDA was not present and |
| 1523 | * OLD_REGULATORY was enabled. We intersect our Country |
| 1524 | * IE rd and what was set on cfg80211 originally */ |
| 1525 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); |
| 1526 | } |
| 1527 | |
| 1528 | if (!intersected_rd) |
| 1529 | return -EINVAL; |
| 1530 | |
| 1531 | drv = wiphy_to_dev(wiphy); |
| 1532 | |
| 1533 | drv->country_ie_alpha2[0] = rd->alpha2[0]; |
| 1534 | drv->country_ie_alpha2[1] = rd->alpha2[1]; |
| 1535 | drv->env = last_request->country_ie_env; |
| 1536 | |
| 1537 | BUG_ON(intersected_rd == rd); |
| 1538 | |
| 1539 | kfree(rd); |
| 1540 | rd = NULL; |
| 1541 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 1542 | reset_regdomains(); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1543 | cfg80211_regdomain = intersected_rd; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | /* Use this call to set the current regulatory domain. Conflicts with |
| 1550 | * multiple drivers can be ironed out later. Caller must've already |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 1551 | * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1552 | int set_regdom(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1553 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1554 | int r; |
| 1555 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1556 | /* Note that this doesn't update the wiphys, this is done below */ |
| 1557 | r = __set_regdom(rd); |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 1558 | if (r) { |
| 1559 | kfree(rd); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1560 | return r; |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 1561 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1562 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1563 | /* This would make this whole thing pointless */ |
Luis R. Rodriguez | a01ddaf | 2008-11-12 14:21:59 -0800 | [diff] [blame] | 1564 | if (!last_request->intersect) |
| 1565 | BUG_ON(rd != cfg80211_regdomain); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1566 | |
| 1567 | /* update all wiphys now with the new established regulatory domain */ |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 1568 | update_all_wiphy_regulatory(last_request->initiator); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1569 | |
Luis R. Rodriguez | a01ddaf | 2008-11-12 14:21:59 -0800 | [diff] [blame] | 1570 | print_regdomain(cfg80211_regdomain); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1571 | |
| 1572 | return r; |
| 1573 | } |
| 1574 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1575 | /* Caller must hold cfg80211_drv_mutex */ |
| 1576 | void reg_device_remove(struct wiphy *wiphy) |
| 1577 | { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1578 | kfree(wiphy->regd); |
Johannes Berg | 4a4f4d8 | 2008-12-01 12:07:56 +0100 | [diff] [blame] | 1579 | if (!last_request || !last_request->wiphy) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1580 | return; |
| 1581 | if (last_request->wiphy != wiphy) |
| 1582 | return; |
| 1583 | last_request->wiphy = NULL; |
| 1584 | last_request->country_ie_env = ENVIRON_ANY; |
| 1585 | } |
| 1586 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1587 | int regulatory_init(void) |
| 1588 | { |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1589 | int err; |
| 1590 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1591 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
| 1592 | if (IS_ERR(reg_pdev)) |
| 1593 | return PTR_ERR(reg_pdev); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1594 | |
| 1595 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1596 | cfg80211_regdomain = static_regdom(ieee80211_regdom); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1597 | |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 1598 | printk(KERN_INFO "cfg80211: Using static regulatory domain info\n"); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1599 | print_regdomain_info(cfg80211_regdomain); |
| 1600 | /* The old code still requests for a new regdomain and if |
| 1601 | * you have CRDA you get it updated, otherwise you get |
| 1602 | * stuck with the static values. We ignore "EU" code as |
| 1603 | * that is not a valid ISO / IEC 3166 alpha2 */ |
Johannes Berg | ac9440a | 2008-10-21 11:08:27 +0200 | [diff] [blame] | 1604 | if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U') |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1605 | err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1606 | ieee80211_regdom, 0, ENVIRON_ANY); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1607 | #else |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1608 | cfg80211_regdomain = cfg80211_world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1609 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1610 | err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1611 | if (err) |
| 1612 | printk(KERN_ERR "cfg80211: calling CRDA failed - " |
| 1613 | "unable to update world regulatory domain, " |
| 1614 | "using static definition\n"); |
| 1615 | #endif |
| 1616 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | void regulatory_exit(void) |
| 1621 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1622 | mutex_lock(&cfg80211_drv_mutex); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1623 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1624 | reset_regdomains(); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1625 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1626 | kfree(country_ie_regdomain); |
| 1627 | country_ie_regdomain = NULL; |
| 1628 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 1629 | kfree(last_request); |
| 1630 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1631 | platform_device_unregister(reg_pdev); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 1632 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1633 | mutex_unlock(&cfg80211_drv_mutex); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1634 | } |