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