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