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