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