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 | 3b77d5e | 2011-12-20 12:23:38 -0800 | [diff] [blame] | 5 | * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com> |
Johannes Berg | 2740f0c | 2014-09-03 15:24:58 +0300 | [diff] [blame] | 6 | * Copyright 2013-2014 Intel Mobile Communications GmbH |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 7 | * Copyright 2017 Intel Deutschland GmbH |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 8 | * |
Luis R. Rodriguez | 3b77d5e | 2011-12-20 12:23:38 -0800 | [diff] [blame] | 9 | * Permission to use, copy, modify, and/or distribute this software for any |
| 10 | * purpose with or without fee is hereby granted, provided that the above |
| 11 | * copyright notice and this permission notice appear in all copies. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 14 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 15 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 16 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 17 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 18 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 19 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
Luis R. Rodriguez | 3b77d5e | 2011-12-20 12:23:38 -0800 | [diff] [blame] | 22 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 23 | /** |
| 24 | * DOC: Wireless regulatory infrastructure |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 25 | * |
| 26 | * The usual implementation is for a driver to read a device EEPROM to |
| 27 | * determine which regulatory domain it should be operating under, then |
| 28 | * looking up the allowable channels in a driver-local table and finally |
| 29 | * registering those channels in the wiphy structure. |
| 30 | * |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 31 | * Another set of compliance enforcement is for drivers to use their |
| 32 | * own compliance limits which can be stored on the EEPROM. The host |
| 33 | * driver or firmware may ensure these are used. |
| 34 | * |
| 35 | * In addition to all this we provide an extra layer of regulatory |
| 36 | * conformance. For drivers which do not have any regulatory |
| 37 | * information CRDA provides the complete regulatory solution. |
| 38 | * For others it provides a community effort on further restrictions |
| 39 | * to enhance compliance. |
| 40 | * |
| 41 | * Note: When number of rules --> infinity we will not be able to |
| 42 | * index on alpha2 any more, instead we'll probably have to |
| 43 | * rely on some SHA1 checksum of the regdomain for example. |
| 44 | * |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 45 | */ |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 46 | |
| 47 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 48 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 49 | #include <linux/kernel.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 50 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 51 | #include <linux/slab.h> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 52 | #include <linux/list.h> |
John W. Linville | c61029c | 2010-08-05 14:26:24 -0400 | [diff] [blame] | 53 | #include <linux/ctype.h> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 54 | #include <linux/nl80211.h> |
| 55 | #include <linux/platform_device.h> |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 56 | #include <linux/verification.h> |
Paul Gortmaker | d9b9384 | 2011-09-18 13:21:27 -0400 | [diff] [blame] | 57 | #include <linux/moduleparam.h> |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 58 | #include <linux/firmware.h> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 59 | #include <net/cfg80211.h> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 60 | #include "core.h" |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 61 | #include "reg.h" |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 62 | #include "rdev-ops.h" |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 63 | #include "nl80211.h" |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 64 | |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 65 | /* |
| 66 | * Grace period we give before making sure all current interfaces reside on |
| 67 | * channels allowed by the current regulatory domain. |
| 68 | */ |
| 69 | #define REG_ENFORCE_GRACE_MS 60000 |
| 70 | |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 71 | /** |
| 72 | * enum reg_request_treatment - regulatory request treatment |
| 73 | * |
| 74 | * @REG_REQ_OK: continue processing the regulatory request |
| 75 | * @REG_REQ_IGNORE: ignore the regulatory request |
| 76 | * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should |
| 77 | * be intersected with the current one. |
| 78 | * @REG_REQ_ALREADY_SET: the regulatory request will not change the current |
| 79 | * regulatory settings, and no further processing is required. |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 80 | */ |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 81 | enum reg_request_treatment { |
| 82 | REG_REQ_OK, |
| 83 | REG_REQ_IGNORE, |
| 84 | REG_REQ_INTERSECT, |
| 85 | REG_REQ_ALREADY_SET, |
| 86 | }; |
| 87 | |
Luis R. Rodriguez | a042994 | 2011-11-28 16:47:15 -0500 | [diff] [blame] | 88 | static struct regulatory_request core_request_world = { |
| 89 | .initiator = NL80211_REGDOM_SET_BY_CORE, |
| 90 | .alpha2[0] = '0', |
| 91 | .alpha2[1] = '0', |
| 92 | .intersect = false, |
| 93 | .processed = true, |
| 94 | .country_ie_env = ENVIRON_ANY, |
| 95 | }; |
| 96 | |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 97 | /* |
| 98 | * Receipt of information from last regulatory request, |
| 99 | * protected by RTNL (and can be accessed with RCU protection) |
| 100 | */ |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 101 | static struct regulatory_request __rcu *last_request = |
Johannes Berg | cec3f0e | 2014-12-12 12:26:25 +0100 | [diff] [blame] | 102 | (void __force __rcu *)&core_request_world; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 103 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 104 | /* To trigger userspace events and load firmware */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 105 | static struct platform_device *reg_pdev; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 106 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 107 | /* |
| 108 | * Central wireless core regulatory domains, we only need two, |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 109 | * the current one and a world regulatory domain in case we have no |
Johannes Berg | e8da2bb | 2012-12-03 23:00:08 +0100 | [diff] [blame] | 110 | * information to give us an alpha2. |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 111 | * (protected by RTNL, can be read under RCU) |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 112 | */ |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 113 | const struct ieee80211_regdomain __rcu *cfg80211_regdomain; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 114 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 115 | /* |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 116 | * Number of devices that registered to the core |
| 117 | * that support cellular base station regulatory hints |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 118 | * (protected by RTNL) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 119 | */ |
| 120 | static int reg_num_devs_support_basehint; |
| 121 | |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 122 | /* |
| 123 | * State variable indicating if the platform on which the devices |
| 124 | * are attached is operating in an indoor environment. The state variable |
| 125 | * is relevant for all registered devices. |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 126 | */ |
| 127 | static bool reg_is_indoor; |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 128 | static spinlock_t reg_indoor_lock; |
| 129 | |
| 130 | /* Used to track the userspace process controlling the indoor setting */ |
| 131 | static u32 reg_is_indoor_portid; |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 132 | |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 133 | static void restore_regulatory_settings(bool reset_user); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 134 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 135 | static const struct ieee80211_regdomain *get_cfg80211_regdom(void) |
| 136 | { |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 137 | return rtnl_dereference(cfg80211_regdomain); |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Arik Nemtsov | ad30ca2 | 2014-12-15 19:25:59 +0200 | [diff] [blame] | 140 | const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy) |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 141 | { |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 142 | return rtnl_dereference(wiphy->regd); |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Luis R. Rodriguez | 3ef121b | 2013-11-13 18:54:05 +0100 | [diff] [blame] | 145 | static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region) |
| 146 | { |
| 147 | switch (dfs_region) { |
| 148 | case NL80211_DFS_UNSET: |
| 149 | return "unset"; |
| 150 | case NL80211_DFS_FCC: |
| 151 | return "FCC"; |
| 152 | case NL80211_DFS_ETSI: |
| 153 | return "ETSI"; |
| 154 | case NL80211_DFS_JP: |
| 155 | return "JP"; |
| 156 | } |
| 157 | return "Unknown"; |
| 158 | } |
| 159 | |
Luis R. Rodriguez | 6c47479 | 2013-11-25 20:56:09 +0100 | [diff] [blame] | 160 | enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy) |
| 161 | { |
| 162 | const struct ieee80211_regdomain *regd = NULL; |
| 163 | const struct ieee80211_regdomain *wiphy_regd = NULL; |
| 164 | |
| 165 | regd = get_cfg80211_regdom(); |
| 166 | if (!wiphy) |
| 167 | goto out; |
| 168 | |
| 169 | wiphy_regd = get_wiphy_regdom(wiphy); |
| 170 | if (!wiphy_regd) |
| 171 | goto out; |
| 172 | |
| 173 | if (wiphy_regd->dfs_region == regd->dfs_region) |
| 174 | goto out; |
| 175 | |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 176 | pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n", |
| 177 | dev_name(&wiphy->dev), |
| 178 | reg_dfs_region_str(wiphy_regd->dfs_region), |
| 179 | reg_dfs_region_str(regd->dfs_region)); |
Luis R. Rodriguez | 6c47479 | 2013-11-25 20:56:09 +0100 | [diff] [blame] | 180 | |
| 181 | out: |
| 182 | return regd->dfs_region; |
| 183 | } |
| 184 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 185 | static void rcu_free_regdom(const struct ieee80211_regdomain *r) |
| 186 | { |
| 187 | if (!r) |
| 188 | return; |
| 189 | kfree_rcu((struct ieee80211_regdomain *)r, rcu_head); |
| 190 | } |
| 191 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 192 | static struct regulatory_request *get_last_request(void) |
| 193 | { |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 194 | return rcu_dereference_rtnl(last_request); |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 197 | /* Used to queue up regulatory hints */ |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 198 | static LIST_HEAD(reg_requests_list); |
| 199 | static spinlock_t reg_requests_lock; |
| 200 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 201 | /* Used to queue up beacon hints for review */ |
| 202 | static LIST_HEAD(reg_pending_beacons); |
| 203 | static spinlock_t reg_pending_beacons_lock; |
| 204 | |
| 205 | /* Used to keep track of processed beacon hints */ |
| 206 | static LIST_HEAD(reg_beacon_list); |
| 207 | |
| 208 | struct reg_beacon { |
| 209 | struct list_head list; |
| 210 | struct ieee80211_channel chan; |
| 211 | }; |
| 212 | |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 213 | static void reg_check_chans_work(struct work_struct *work); |
| 214 | static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work); |
| 215 | |
Luis R. Rodriguez | f333a7a | 2010-11-17 21:46:07 -0800 | [diff] [blame] | 216 | static void reg_todo(struct work_struct *work); |
| 217 | static DECLARE_WORK(reg_work, reg_todo); |
| 218 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 219 | /* We keep a static world regulatory domain in case of the absence of CRDA */ |
| 220 | static const struct ieee80211_regdomain world_regdom = { |
Jason Abele | 28981e5 | 2015-02-17 16:10:41 -0800 | [diff] [blame] | 221 | .n_reg_rules = 8, |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 222 | .alpha2 = "00", |
| 223 | .reg_rules = { |
Luis R. Rodriguez | 68798a6 | 2009-02-21 00:20:37 -0500 | [diff] [blame] | 224 | /* IEEE 802.11b/g, channels 1..11 */ |
| 225 | REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), |
Johannes Berg | 43c771a | 2012-11-12 10:51:34 +0100 | [diff] [blame] | 226 | /* IEEE 802.11b/g, channels 12..13. */ |
Johannes Berg | c382680 | 2015-12-11 17:35:50 +0100 | [diff] [blame] | 227 | REG_RULE(2467-10, 2472+10, 20, 6, 20, |
| 228 | NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW), |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 229 | /* IEEE 802.11 channel 14 - Only JP enables |
| 230 | * this and for 802.11b only */ |
| 231 | REG_RULE(2484-10, 2484+10, 20, 6, 20, |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 232 | NL80211_RRF_NO_IR | |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 233 | NL80211_RRF_NO_OFDM), |
| 234 | /* IEEE 802.11a, channel 36..48 */ |
Johannes Berg | c382680 | 2015-12-11 17:35:50 +0100 | [diff] [blame] | 235 | REG_RULE(5180-10, 5240+10, 80, 6, 20, |
| 236 | NL80211_RRF_NO_IR | |
| 237 | NL80211_RRF_AUTO_BW), |
Luis R. Rodriguez | 3fc71f7 | 2009-02-21 00:20:38 -0500 | [diff] [blame] | 238 | |
Johannes Berg | 131a19b | 2013-03-04 20:54:46 +0100 | [diff] [blame] | 239 | /* IEEE 802.11a, channel 52..64 - DFS required */ |
Johannes Berg | c382680 | 2015-12-11 17:35:50 +0100 | [diff] [blame] | 240 | REG_RULE(5260-10, 5320+10, 80, 6, 20, |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 241 | NL80211_RRF_NO_IR | |
Johannes Berg | c382680 | 2015-12-11 17:35:50 +0100 | [diff] [blame] | 242 | NL80211_RRF_AUTO_BW | |
Johannes Berg | 131a19b | 2013-03-04 20:54:46 +0100 | [diff] [blame] | 243 | NL80211_RRF_DFS), |
| 244 | |
| 245 | /* IEEE 802.11a, channel 100..144 - DFS required */ |
| 246 | REG_RULE(5500-10, 5720+10, 160, 6, 20, |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 247 | NL80211_RRF_NO_IR | |
Johannes Berg | 131a19b | 2013-03-04 20:54:46 +0100 | [diff] [blame] | 248 | NL80211_RRF_DFS), |
Luis R. Rodriguez | 3fc71f7 | 2009-02-21 00:20:38 -0500 | [diff] [blame] | 249 | |
| 250 | /* IEEE 802.11a, channel 149..165 */ |
Johannes Berg | 8ab9d85 | 2012-12-03 22:09:22 +0100 | [diff] [blame] | 251 | REG_RULE(5745-10, 5825+10, 80, 6, 20, |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 252 | NL80211_RRF_NO_IR), |
Vladimir Kondratiev | 90cdc6d | 2012-07-02 09:32:34 +0300 | [diff] [blame] | 253 | |
Johannes Berg | 8047d26 | 2015-10-15 16:16:09 +0200 | [diff] [blame] | 254 | /* IEEE 802.11ad (60GHz), channels 1..3 */ |
Vladimir Kondratiev | 90cdc6d | 2012-07-02 09:32:34 +0300 | [diff] [blame] | 255 | REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 259 | /* protected by RTNL */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 260 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
| 261 | &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 262 | |
Luis R. Rodriguez | 6ee7d33 | 2009-03-20 23:53:06 -0400 | [diff] [blame] | 263 | static char *ieee80211_regdom = "00"; |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 264 | static char user_alpha2[2]; |
Luis R. Rodriguez | 6ee7d33 | 2009-03-20 23:53:06 -0400 | [diff] [blame] | 265 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 266 | module_param(ieee80211_regdom, charp, 0444); |
| 267 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); |
| 268 | |
Arik Nemtsov | c888393 | 2014-04-21 20:39:34 -0700 | [diff] [blame] | 269 | static void reg_free_request(struct regulatory_request *request) |
Luis R. Rodriguez | 5ad6ef5 | 2013-11-05 09:18:08 -0800 | [diff] [blame] | 270 | { |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 271 | if (request == &core_request_world) |
| 272 | return; |
| 273 | |
Arik Nemtsov | c888393 | 2014-04-21 20:39:34 -0700 | [diff] [blame] | 274 | if (request != get_last_request()) |
| 275 | kfree(request); |
| 276 | } |
| 277 | |
| 278 | static void reg_free_last_request(void) |
| 279 | { |
| 280 | struct regulatory_request *lr = get_last_request(); |
| 281 | |
Luis R. Rodriguez | 5ad6ef5 | 2013-11-05 09:18:08 -0800 | [diff] [blame] | 282 | if (lr != &core_request_world && lr) |
| 283 | kfree_rcu(lr, rcu_head); |
| 284 | } |
| 285 | |
Luis R. Rodriguez | 05f1a3e | 2013-11-05 09:18:09 -0800 | [diff] [blame] | 286 | static void reg_update_last_request(struct regulatory_request *request) |
| 287 | { |
Luis R. Rodriguez | 255e25b | 2014-02-25 17:09:40 -0800 | [diff] [blame] | 288 | struct regulatory_request *lr; |
| 289 | |
| 290 | lr = get_last_request(); |
| 291 | if (lr == request) |
| 292 | return; |
| 293 | |
Arik Nemtsov | c888393 | 2014-04-21 20:39:34 -0700 | [diff] [blame] | 294 | reg_free_last_request(); |
Luis R. Rodriguez | 05f1a3e | 2013-11-05 09:18:09 -0800 | [diff] [blame] | 295 | rcu_assign_pointer(last_request, request); |
| 296 | } |
| 297 | |
Johannes Berg | 379b82f | 2012-12-06 15:44:07 +0100 | [diff] [blame] | 298 | static void reset_regdomains(bool full_reset, |
| 299 | const struct ieee80211_regdomain *new_regdom) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 300 | { |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 301 | const struct ieee80211_regdomain *r; |
| 302 | |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 303 | ASSERT_RTNL(); |
Johannes Berg | e8da2bb | 2012-12-03 23:00:08 +0100 | [diff] [blame] | 304 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 305 | r = get_cfg80211_regdom(); |
| 306 | |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 307 | /* avoid freeing static information or freeing something twice */ |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 308 | if (r == cfg80211_world_regdom) |
| 309 | r = NULL; |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 310 | if (cfg80211_world_regdom == &world_regdom) |
| 311 | cfg80211_world_regdom = NULL; |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 312 | if (r == &world_regdom) |
| 313 | r = NULL; |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 314 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 315 | rcu_free_regdom(r); |
| 316 | rcu_free_regdom(cfg80211_world_regdom); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 317 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 318 | cfg80211_world_regdom = &world_regdom; |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 319 | rcu_assign_pointer(cfg80211_regdomain, new_regdom); |
Luis R. Rodriguez | a042994 | 2011-11-28 16:47:15 -0500 | [diff] [blame] | 320 | |
| 321 | if (!full_reset) |
| 322 | return; |
| 323 | |
Luis R. Rodriguez | 05f1a3e | 2013-11-05 09:18:09 -0800 | [diff] [blame] | 324 | reg_update_last_request(&core_request_world); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 327 | /* |
| 328 | * Dynamic world regulatory domain requested by the wireless |
| 329 | * core upon initialization |
| 330 | */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 331 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 332 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 333 | struct regulatory_request *lr; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 334 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 335 | lr = get_last_request(); |
| 336 | |
| 337 | WARN_ON(!lr); |
Johannes Berg | e8da2bb | 2012-12-03 23:00:08 +0100 | [diff] [blame] | 338 | |
Johannes Berg | 379b82f | 2012-12-06 15:44:07 +0100 | [diff] [blame] | 339 | reset_regdomains(false, rd); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 340 | |
| 341 | cfg80211_world_regdom = rd; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 342 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 343 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 344 | bool is_world_regdom(const char *alpha2) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 345 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 346 | if (!alpha2) |
| 347 | return false; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 348 | return alpha2[0] == '0' && alpha2[1] == '0'; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 351 | static bool is_alpha2_set(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 352 | { |
| 353 | if (!alpha2) |
| 354 | return false; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 355 | return alpha2[0] && alpha2[1]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 356 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 357 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 358 | static bool is_unknown_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 359 | { |
| 360 | if (!alpha2) |
| 361 | return false; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 362 | /* |
| 363 | * Special case where regulatory domain was built by driver |
| 364 | * but a specific alpha2 cannot be determined |
| 365 | */ |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 366 | return alpha2[0] == '9' && alpha2[1] == '9'; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 369 | static bool is_intersected_alpha2(const char *alpha2) |
| 370 | { |
| 371 | if (!alpha2) |
| 372 | return false; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 373 | /* |
| 374 | * Special case where regulatory domain is the |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 375 | * result of an intersection between two regulatory domain |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 376 | * structures |
| 377 | */ |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 378 | return alpha2[0] == '9' && alpha2[1] == '8'; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 381 | static bool is_an_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 382 | { |
| 383 | if (!alpha2) |
| 384 | return false; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 385 | return isalpha(alpha2[0]) && isalpha(alpha2[1]); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 388 | 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] | 389 | { |
| 390 | if (!alpha2_x || !alpha2_y) |
| 391 | return false; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 392 | return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Luis R. Rodriguez | 69b1572 | 2009-02-21 00:04:33 -0500 | [diff] [blame] | 395 | static bool regdom_changes(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 396 | { |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 397 | const struct ieee80211_regdomain *r = get_cfg80211_regdom(); |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 398 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 399 | if (!r) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 400 | return true; |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 401 | return !alpha2_equal(r->alpha2, alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 404 | /* |
| 405 | * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets |
| 406 | * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER |
| 407 | * has ever been issued. |
| 408 | */ |
| 409 | static bool is_user_regdom_saved(void) |
| 410 | { |
| 411 | if (user_alpha2[0] == '9' && user_alpha2[1] == '7') |
| 412 | return false; |
| 413 | |
| 414 | /* This would indicate a mistake on the design */ |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 415 | if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2), |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 416 | "Unexpected user alpha2: %c%c\n", |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 417 | user_alpha2[0], user_alpha2[1])) |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 418 | return false; |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 423 | static const struct ieee80211_regdomain * |
| 424 | reg_copy_regd(const struct ieee80211_regdomain *src_regd) |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 425 | { |
| 426 | struct ieee80211_regdomain *regd; |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 427 | int size_of_regd; |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 428 | unsigned int i; |
| 429 | |
Johannes Berg | 82f2085 | 2012-12-04 12:49:16 +0100 | [diff] [blame] | 430 | size_of_regd = |
| 431 | sizeof(struct ieee80211_regdomain) + |
| 432 | src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 433 | |
| 434 | regd = kzalloc(size_of_regd, GFP_KERNEL); |
| 435 | if (!regd) |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 436 | return ERR_PTR(-ENOMEM); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 437 | |
| 438 | memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); |
| 439 | |
| 440 | for (i = 0; i < src_regd->n_reg_rules; i++) |
| 441 | memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 442 | sizeof(struct ieee80211_reg_rule)); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 443 | |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 444 | return regd; |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 447 | struct reg_regdb_apply_request { |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 448 | struct list_head list; |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 449 | const struct ieee80211_regdomain *regdom; |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 450 | }; |
| 451 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 452 | static LIST_HEAD(reg_regdb_apply_list); |
| 453 | static DEFINE_MUTEX(reg_regdb_apply_mutex); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 454 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 455 | static void reg_regdb_apply(struct work_struct *work) |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 456 | { |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 457 | struct reg_regdb_apply_request *request; |
Luis R. Rodriguez | a85d0d7 | 2012-09-14 15:36:57 -0700 | [diff] [blame] | 458 | |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 459 | rtnl_lock(); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 460 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 461 | mutex_lock(®_regdb_apply_mutex); |
| 462 | while (!list_empty(®_regdb_apply_list)) { |
| 463 | request = list_first_entry(®_regdb_apply_list, |
| 464 | struct reg_regdb_apply_request, |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 465 | list); |
| 466 | list_del(&request->list); |
| 467 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 468 | set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 469 | kfree(request); |
| 470 | } |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 471 | mutex_unlock(®_regdb_apply_mutex); |
Luis R. Rodriguez | a85d0d7 | 2012-09-14 15:36:57 -0700 | [diff] [blame] | 472 | |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 473 | rtnl_unlock(); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 476 | static DECLARE_WORK(reg_regdb_work, reg_regdb_apply); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 477 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 478 | static int reg_schedule_apply(const struct ieee80211_regdomain *regdom) |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 479 | { |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 480 | struct reg_regdb_apply_request *request; |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 481 | |
| 482 | request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL); |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 483 | if (!request) { |
| 484 | kfree(regdom); |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 485 | return -ENOMEM; |
| 486 | } |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 487 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 488 | request->regdom = regdom; |
| 489 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 490 | mutex_lock(®_regdb_apply_mutex); |
| 491 | list_add_tail(&request->list, ®_regdb_apply_list); |
| 492 | mutex_unlock(®_regdb_apply_mutex); |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 493 | |
| 494 | schedule_work(®_regdb_work); |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 495 | return 0; |
John W. Linville | 3b377ea | 2009-12-18 17:59:01 -0500 | [diff] [blame] | 496 | } |
Luis R. Rodriguez | 80007ef | 2012-03-23 07:23:31 -0700 | [diff] [blame] | 497 | |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 498 | #ifdef CONFIG_CFG80211_CRDA_SUPPORT |
| 499 | /* Max number of consecutive attempts to communicate with CRDA */ |
| 500 | #define REG_MAX_CRDA_TIMEOUTS 10 |
| 501 | |
| 502 | static u32 reg_crda_timeouts; |
| 503 | |
| 504 | static void crda_timeout_work(struct work_struct *work); |
| 505 | static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work); |
| 506 | |
| 507 | static void crda_timeout_work(struct work_struct *work) |
| 508 | { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 509 | pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n"); |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 510 | rtnl_lock(); |
| 511 | reg_crda_timeouts++; |
| 512 | restore_regulatory_settings(true); |
| 513 | rtnl_unlock(); |
| 514 | } |
| 515 | |
| 516 | static void cancel_crda_timeout(void) |
| 517 | { |
| 518 | cancel_delayed_work(&crda_timeout); |
| 519 | } |
| 520 | |
| 521 | static void cancel_crda_timeout_sync(void) |
| 522 | { |
| 523 | cancel_delayed_work_sync(&crda_timeout); |
| 524 | } |
| 525 | |
| 526 | static void reset_crda_timeouts(void) |
| 527 | { |
| 528 | reg_crda_timeouts = 0; |
| 529 | } |
| 530 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 531 | /* |
| 532 | * This lets us keep regulatory code which is updated on a regulatory |
Johannes Berg | 1226d25 | 2014-02-25 15:43:36 +0100 | [diff] [blame] | 533 | * basis in userspace. |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 534 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 535 | static int call_crda(const char *alpha2) |
| 536 | { |
Johannes Berg | 1226d25 | 2014-02-25 15:43:36 +0100 | [diff] [blame] | 537 | char country[12]; |
| 538 | char *env[] = { country, NULL }; |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 539 | int ret; |
Johannes Berg | 1226d25 | 2014-02-25 15:43:36 +0100 | [diff] [blame] | 540 | |
| 541 | snprintf(country, sizeof(country), "COUNTRY=%c%c", |
| 542 | alpha2[0], alpha2[1]); |
| 543 | |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 544 | if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) { |
Thomas Petazzoni | 042ab5f | 2015-07-09 15:35:15 +0200 | [diff] [blame] | 545 | pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n"); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 546 | return -EINVAL; |
| 547 | } |
| 548 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 549 | if (!is_world_regdom((char *) alpha2)) |
Thomas Petazzoni | 042ab5f | 2015-07-09 15:35:15 +0200 | [diff] [blame] | 550 | pr_debug("Calling CRDA for country: %c%c\n", |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 551 | alpha2[0], alpha2[1]); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 552 | else |
Thomas Petazzoni | 042ab5f | 2015-07-09 15:35:15 +0200 | [diff] [blame] | 553 | pr_debug("Calling CRDA to update world regulatory domain\n"); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 554 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 555 | ret = kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, env); |
| 556 | if (ret) |
| 557 | return ret; |
| 558 | |
| 559 | queue_delayed_work(system_power_efficient_wq, |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 560 | &crda_timeout, msecs_to_jiffies(3142)); |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 561 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 562 | } |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 563 | #else |
| 564 | static inline void cancel_crda_timeout(void) {} |
| 565 | static inline void cancel_crda_timeout_sync(void) {} |
| 566 | static inline void reset_crda_timeouts(void) {} |
| 567 | static inline int call_crda(const char *alpha2) |
| 568 | { |
| 569 | return -ENODATA; |
| 570 | } |
| 571 | #endif /* CONFIG_CFG80211_CRDA_SUPPORT */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 572 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 573 | /* code to directly load a firmware database through request_firmware */ |
| 574 | static const struct fwdb_header *regdb; |
| 575 | |
| 576 | struct fwdb_country { |
| 577 | u8 alpha2[2]; |
| 578 | __be16 coll_ptr; |
| 579 | /* this struct cannot be extended */ |
| 580 | } __packed __aligned(4); |
| 581 | |
| 582 | struct fwdb_collection { |
| 583 | u8 len; |
| 584 | u8 n_rules; |
| 585 | u8 dfs_region; |
| 586 | /* no optional data yet */ |
| 587 | /* aligned to 2, then followed by __be16 array of rule pointers */ |
| 588 | } __packed __aligned(4); |
| 589 | |
| 590 | enum fwdb_flags { |
| 591 | FWDB_FLAG_NO_OFDM = BIT(0), |
| 592 | FWDB_FLAG_NO_OUTDOOR = BIT(1), |
| 593 | FWDB_FLAG_DFS = BIT(2), |
| 594 | FWDB_FLAG_NO_IR = BIT(3), |
| 595 | FWDB_FLAG_AUTO_BW = BIT(4), |
| 596 | }; |
| 597 | |
| 598 | struct fwdb_rule { |
| 599 | u8 len; |
| 600 | u8 flags; |
| 601 | __be16 max_eirp; |
| 602 | __be32 start, end, max_bw; |
| 603 | /* start of optional data */ |
| 604 | __be16 cac_timeout; |
| 605 | } __packed __aligned(4); |
| 606 | |
| 607 | #define FWDB_MAGIC 0x52474442 |
| 608 | #define FWDB_VERSION 20 |
| 609 | |
| 610 | struct fwdb_header { |
| 611 | __be32 magic; |
| 612 | __be32 version; |
| 613 | struct fwdb_country country[]; |
| 614 | } __packed __aligned(4); |
| 615 | |
| 616 | static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr) |
| 617 | { |
| 618 | struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2)); |
| 619 | |
| 620 | if ((u8 *)rule + sizeof(rule->len) > data + size) |
| 621 | return false; |
| 622 | |
| 623 | /* mandatory fields */ |
| 624 | if (rule->len < offsetofend(struct fwdb_rule, max_bw)) |
| 625 | return false; |
| 626 | |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | static bool valid_country(const u8 *data, unsigned int size, |
| 631 | const struct fwdb_country *country) |
| 632 | { |
| 633 | unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2; |
| 634 | struct fwdb_collection *coll = (void *)(data + ptr); |
| 635 | __be16 *rules_ptr; |
| 636 | unsigned int i; |
| 637 | |
| 638 | /* make sure we can read len/n_rules */ |
| 639 | if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size) |
| 640 | return false; |
| 641 | |
| 642 | /* make sure base struct and all rules fit */ |
| 643 | if ((u8 *)coll + ALIGN(coll->len, 2) + |
| 644 | (coll->n_rules * 2) > data + size) |
| 645 | return false; |
| 646 | |
| 647 | /* mandatory fields must exist */ |
| 648 | if (coll->len < offsetofend(struct fwdb_collection, dfs_region)) |
| 649 | return false; |
| 650 | |
| 651 | rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2)); |
| 652 | |
| 653 | for (i = 0; i < coll->n_rules; i++) { |
| 654 | u16 rule_ptr = be16_to_cpu(rules_ptr[i]); |
| 655 | |
| 656 | if (!valid_rule(data, size, rule_ptr)) |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | return true; |
| 661 | } |
| 662 | |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 663 | #ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB |
| 664 | static struct key *builtin_regdb_keys; |
| 665 | |
| 666 | static void __init load_keys_from_buffer(const u8 *p, unsigned int buflen) |
| 667 | { |
| 668 | const u8 *end = p + buflen; |
| 669 | size_t plen; |
| 670 | key_ref_t key; |
| 671 | |
| 672 | while (p < end) { |
| 673 | /* Each cert begins with an ASN.1 SEQUENCE tag and must be more |
| 674 | * than 256 bytes in size. |
| 675 | */ |
| 676 | if (end - p < 4) |
| 677 | goto dodgy_cert; |
| 678 | if (p[0] != 0x30 && |
| 679 | p[1] != 0x82) |
| 680 | goto dodgy_cert; |
| 681 | plen = (p[2] << 8) | p[3]; |
| 682 | plen += 4; |
| 683 | if (plen > end - p) |
| 684 | goto dodgy_cert; |
| 685 | |
| 686 | key = key_create_or_update(make_key_ref(builtin_regdb_keys, 1), |
| 687 | "asymmetric", NULL, p, plen, |
| 688 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 689 | KEY_USR_VIEW | KEY_USR_READ), |
| 690 | KEY_ALLOC_NOT_IN_QUOTA | |
| 691 | KEY_ALLOC_BUILT_IN | |
| 692 | KEY_ALLOC_BYPASS_RESTRICTION); |
| 693 | if (IS_ERR(key)) { |
| 694 | pr_err("Problem loading in-kernel X.509 certificate (%ld)\n", |
| 695 | PTR_ERR(key)); |
| 696 | } else { |
| 697 | pr_notice("Loaded X.509 cert '%s'\n", |
| 698 | key_ref_to_ptr(key)->description); |
| 699 | key_ref_put(key); |
| 700 | } |
| 701 | p += plen; |
| 702 | } |
| 703 | |
| 704 | return; |
| 705 | |
| 706 | dodgy_cert: |
| 707 | pr_err("Problem parsing in-kernel X.509 certificate list\n"); |
| 708 | } |
| 709 | |
| 710 | static int __init load_builtin_regdb_keys(void) |
| 711 | { |
| 712 | builtin_regdb_keys = |
| 713 | keyring_alloc(".builtin_regdb_keys", |
| 714 | KUIDT_INIT(0), KGIDT_INIT(0), current_cred(), |
| 715 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 716 | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH), |
| 717 | KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); |
| 718 | if (IS_ERR(builtin_regdb_keys)) |
| 719 | return PTR_ERR(builtin_regdb_keys); |
| 720 | |
| 721 | pr_notice("Loading compiled-in X.509 certificates for regulatory database\n"); |
| 722 | |
| 723 | #ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS |
| 724 | load_keys_from_buffer(shipped_regdb_certs, shipped_regdb_certs_len); |
| 725 | #endif |
Arnd Bergmann | 88230ef | 2017-10-13 14:04:31 +0200 | [diff] [blame] | 726 | #ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 727 | if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0') |
| 728 | load_keys_from_buffer(extra_regdb_certs, extra_regdb_certs_len); |
| 729 | #endif |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static bool regdb_has_valid_signature(const u8 *data, unsigned int size) |
| 735 | { |
| 736 | const struct firmware *sig; |
| 737 | bool result; |
| 738 | |
| 739 | if (request_firmware(&sig, "regulatory.db.p7s", ®_pdev->dev)) |
| 740 | return false; |
| 741 | |
| 742 | result = verify_pkcs7_signature(data, size, sig->data, sig->size, |
| 743 | builtin_regdb_keys, |
| 744 | VERIFYING_UNSPECIFIED_SIGNATURE, |
| 745 | NULL, NULL) == 0; |
| 746 | |
| 747 | release_firmware(sig); |
| 748 | |
| 749 | return result; |
| 750 | } |
| 751 | |
| 752 | static void free_regdb_keyring(void) |
| 753 | { |
| 754 | key_put(builtin_regdb_keys); |
| 755 | } |
| 756 | #else |
| 757 | static int load_builtin_regdb_keys(void) |
| 758 | { |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static bool regdb_has_valid_signature(const u8 *data, unsigned int size) |
| 763 | { |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | static void free_regdb_keyring(void) |
| 768 | { |
| 769 | } |
| 770 | #endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */ |
| 771 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 772 | static bool valid_regdb(const u8 *data, unsigned int size) |
| 773 | { |
| 774 | const struct fwdb_header *hdr = (void *)data; |
| 775 | const struct fwdb_country *country; |
| 776 | |
| 777 | if (size < sizeof(*hdr)) |
| 778 | return false; |
| 779 | |
| 780 | if (hdr->magic != cpu_to_be32(FWDB_MAGIC)) |
| 781 | return false; |
| 782 | |
| 783 | if (hdr->version != cpu_to_be32(FWDB_VERSION)) |
| 784 | return false; |
| 785 | |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 786 | if (!regdb_has_valid_signature(data, size)) |
| 787 | return false; |
| 788 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 789 | country = &hdr->country[0]; |
| 790 | while ((u8 *)(country + 1) <= data + size) { |
| 791 | if (!country->coll_ptr) |
| 792 | break; |
| 793 | if (!valid_country(data, size, country)) |
| 794 | return false; |
| 795 | country++; |
| 796 | } |
| 797 | |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | static int regdb_query_country(const struct fwdb_header *db, |
| 802 | const struct fwdb_country *country) |
| 803 | { |
| 804 | unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2; |
| 805 | struct fwdb_collection *coll = (void *)((u8 *)db + ptr); |
| 806 | struct ieee80211_regdomain *regdom; |
| 807 | unsigned int size_of_regd; |
| 808 | unsigned int i; |
| 809 | |
| 810 | size_of_regd = |
| 811 | sizeof(struct ieee80211_regdomain) + |
| 812 | coll->n_rules * sizeof(struct ieee80211_reg_rule); |
| 813 | |
| 814 | regdom = kzalloc(size_of_regd, GFP_KERNEL); |
| 815 | if (!regdom) |
| 816 | return -ENOMEM; |
| 817 | |
| 818 | regdom->n_reg_rules = coll->n_rules; |
| 819 | regdom->alpha2[0] = country->alpha2[0]; |
| 820 | regdom->alpha2[1] = country->alpha2[1]; |
| 821 | regdom->dfs_region = coll->dfs_region; |
| 822 | |
| 823 | for (i = 0; i < regdom->n_reg_rules; i++) { |
| 824 | __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2)); |
| 825 | unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2; |
| 826 | struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr); |
| 827 | struct ieee80211_reg_rule *rrule = ®dom->reg_rules[i]; |
| 828 | |
| 829 | rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start); |
| 830 | rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end); |
| 831 | rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw); |
| 832 | |
| 833 | rrule->power_rule.max_antenna_gain = 0; |
| 834 | rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp); |
| 835 | |
| 836 | rrule->flags = 0; |
| 837 | if (rule->flags & FWDB_FLAG_NO_OFDM) |
| 838 | rrule->flags |= NL80211_RRF_NO_OFDM; |
| 839 | if (rule->flags & FWDB_FLAG_NO_OUTDOOR) |
| 840 | rrule->flags |= NL80211_RRF_NO_OUTDOOR; |
| 841 | if (rule->flags & FWDB_FLAG_DFS) |
| 842 | rrule->flags |= NL80211_RRF_DFS; |
| 843 | if (rule->flags & FWDB_FLAG_NO_IR) |
| 844 | rrule->flags |= NL80211_RRF_NO_IR; |
| 845 | if (rule->flags & FWDB_FLAG_AUTO_BW) |
| 846 | rrule->flags |= NL80211_RRF_AUTO_BW; |
| 847 | |
| 848 | rrule->dfs_cac_ms = 0; |
| 849 | |
| 850 | /* handle optional data */ |
| 851 | if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout)) |
| 852 | rrule->dfs_cac_ms = |
| 853 | 1000 * be16_to_cpu(rule->cac_timeout); |
| 854 | } |
| 855 | |
| 856 | return reg_schedule_apply(regdom); |
| 857 | } |
| 858 | |
| 859 | static int query_regdb(const char *alpha2) |
| 860 | { |
| 861 | const struct fwdb_header *hdr = regdb; |
| 862 | const struct fwdb_country *country; |
| 863 | |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 864 | ASSERT_RTNL(); |
| 865 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 866 | if (IS_ERR(regdb)) |
| 867 | return PTR_ERR(regdb); |
| 868 | |
| 869 | country = &hdr->country[0]; |
| 870 | while (country->coll_ptr) { |
| 871 | if (alpha2_equal(alpha2, country->alpha2)) |
| 872 | return regdb_query_country(regdb, country); |
| 873 | country++; |
| 874 | } |
| 875 | |
| 876 | return -ENODATA; |
| 877 | } |
| 878 | |
| 879 | static void regdb_fw_cb(const struct firmware *fw, void *context) |
| 880 | { |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 881 | int set_error = 0; |
| 882 | bool restore = true; |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 883 | void *db; |
| 884 | |
| 885 | if (!fw) { |
| 886 | pr_info("failed to load regulatory.db\n"); |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 887 | set_error = -ENODATA; |
| 888 | } else if (!valid_regdb(fw->data, fw->size)) { |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 889 | pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n"); |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 890 | set_error = -EINVAL; |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 893 | rtnl_lock(); |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 894 | if (WARN_ON(regdb && !IS_ERR(regdb))) { |
| 895 | /* just restore and free new db */ |
| 896 | } else if (set_error) { |
| 897 | regdb = ERR_PTR(set_error); |
| 898 | } else if (fw) { |
| 899 | db = kmemdup(fw->data, fw->size, GFP_KERNEL); |
| 900 | if (db) { |
| 901 | regdb = db; |
| 902 | restore = context && query_regdb(context); |
| 903 | } else { |
| 904 | restore = true; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | if (restore) |
| 909 | restore_regulatory_settings(true); |
| 910 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 911 | rtnl_unlock(); |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 912 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 913 | kfree(context); |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 914 | |
| 915 | release_firmware(fw); |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static int query_regdb_file(const char *alpha2) |
| 919 | { |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 920 | ASSERT_RTNL(); |
| 921 | |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 922 | if (regdb) |
| 923 | return query_regdb(alpha2); |
| 924 | |
| 925 | alpha2 = kmemdup(alpha2, 2, GFP_KERNEL); |
| 926 | if (!alpha2) |
| 927 | return -ENOMEM; |
| 928 | |
| 929 | return request_firmware_nowait(THIS_MODULE, true, "regulatory.db", |
| 930 | ®_pdev->dev, GFP_KERNEL, |
| 931 | (void *)alpha2, regdb_fw_cb); |
| 932 | } |
| 933 | |
Johannes Berg | 1ea4ff3e9 | 2017-09-13 16:07:22 +0200 | [diff] [blame] | 934 | int reg_reload_regdb(void) |
| 935 | { |
| 936 | const struct firmware *fw; |
| 937 | void *db; |
| 938 | int err; |
| 939 | |
| 940 | err = request_firmware(&fw, "regulatory.db", ®_pdev->dev); |
| 941 | if (err) |
| 942 | return err; |
| 943 | |
| 944 | if (!valid_regdb(fw->data, fw->size)) { |
| 945 | err = -ENODATA; |
| 946 | goto out; |
| 947 | } |
| 948 | |
| 949 | db = kmemdup(fw->data, fw->size, GFP_KERNEL); |
| 950 | if (!db) { |
| 951 | err = -ENOMEM; |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | rtnl_lock(); |
| 956 | if (!IS_ERR_OR_NULL(regdb)) |
| 957 | kfree(regdb); |
| 958 | regdb = db; |
| 959 | rtnl_unlock(); |
| 960 | |
| 961 | out: |
| 962 | release_firmware(fw); |
| 963 | return err; |
| 964 | } |
| 965 | |
Johannes Berg | cecbb06 | 2015-10-15 08:47:34 +0200 | [diff] [blame] | 966 | static bool reg_query_database(struct regulatory_request *request) |
Luis R. Rodriguez | fe6631f | 2013-11-05 09:18:10 -0800 | [diff] [blame] | 967 | { |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 968 | if (query_regdb_file(request->alpha2) == 0) |
| 969 | return true; |
| 970 | |
Johannes Berg | c7d319e5 | 2015-10-15 09:03:05 +0200 | [diff] [blame] | 971 | if (call_crda(request->alpha2) == 0) |
| 972 | return true; |
| 973 | |
| 974 | return false; |
Luis R. Rodriguez | fe6631f | 2013-11-05 09:18:10 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Luis R. Rodriguez | e438768 | 2013-11-05 09:18:01 -0800 | [diff] [blame] | 977 | bool reg_is_valid_request(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 978 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 979 | struct regulatory_request *lr = get_last_request(); |
Luis R. Rodriguez | 61405e9 | 2009-05-13 17:04:41 -0400 | [diff] [blame] | 980 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 981 | if (!lr || lr->processed) |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 982 | return false; |
| 983 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 984 | return alpha2_equal(lr->alpha2, alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Janusz Dziedzic | e3961af | 2014-01-25 11:24:11 +0100 | [diff] [blame] | 987 | static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy) |
| 988 | { |
| 989 | struct regulatory_request *lr = get_last_request(); |
| 990 | |
| 991 | /* |
| 992 | * Follow the driver's regulatory domain, if present, unless a country |
| 993 | * IE has been processed or a user wants to help complaince further |
| 994 | */ |
| 995 | if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
| 996 | lr->initiator != NL80211_REGDOM_SET_BY_USER && |
| 997 | wiphy->regd) |
| 998 | return get_wiphy_regdom(wiphy); |
| 999 | |
| 1000 | return get_cfg80211_regdom(); |
| 1001 | } |
| 1002 | |
Arik Nemtsov | a6d4a53 | 2014-10-23 09:37:33 +0300 | [diff] [blame] | 1003 | static unsigned int |
| 1004 | reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd, |
| 1005 | const struct ieee80211_reg_rule *rule) |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1006 | { |
| 1007 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
| 1008 | const struct ieee80211_freq_range *freq_range_tmp; |
| 1009 | const struct ieee80211_reg_rule *tmp; |
| 1010 | u32 start_freq, end_freq, idx, no; |
| 1011 | |
| 1012 | for (idx = 0; idx < rd->n_reg_rules; idx++) |
| 1013 | if (rule == &rd->reg_rules[idx]) |
| 1014 | break; |
| 1015 | |
| 1016 | if (idx == rd->n_reg_rules) |
| 1017 | return 0; |
| 1018 | |
| 1019 | /* get start_freq */ |
| 1020 | no = idx; |
| 1021 | |
| 1022 | while (no) { |
| 1023 | tmp = &rd->reg_rules[--no]; |
| 1024 | freq_range_tmp = &tmp->freq_range; |
| 1025 | |
| 1026 | if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz) |
| 1027 | break; |
| 1028 | |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1029 | freq_range = freq_range_tmp; |
| 1030 | } |
| 1031 | |
| 1032 | start_freq = freq_range->start_freq_khz; |
| 1033 | |
| 1034 | /* get end_freq */ |
| 1035 | freq_range = &rule->freq_range; |
| 1036 | no = idx; |
| 1037 | |
| 1038 | while (no < rd->n_reg_rules - 1) { |
| 1039 | tmp = &rd->reg_rules[++no]; |
| 1040 | freq_range_tmp = &tmp->freq_range; |
| 1041 | |
| 1042 | if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz) |
| 1043 | break; |
| 1044 | |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1045 | freq_range = freq_range_tmp; |
| 1046 | } |
| 1047 | |
| 1048 | end_freq = freq_range->end_freq_khz; |
| 1049 | |
| 1050 | return end_freq - start_freq; |
| 1051 | } |
| 1052 | |
Arik Nemtsov | a6d4a53 | 2014-10-23 09:37:33 +0300 | [diff] [blame] | 1053 | unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd, |
| 1054 | const struct ieee80211_reg_rule *rule) |
| 1055 | { |
| 1056 | unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule); |
| 1057 | |
| 1058 | if (rule->flags & NL80211_RRF_NO_160MHZ) |
| 1059 | bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80)); |
| 1060 | if (rule->flags & NL80211_RRF_NO_80MHZ) |
| 1061 | bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40)); |
| 1062 | |
| 1063 | /* |
| 1064 | * HT40+/HT40- limits are handled per-channel. Only limit BW if both |
| 1065 | * are not allowed. |
| 1066 | */ |
| 1067 | if (rule->flags & NL80211_RRF_NO_HT40MINUS && |
| 1068 | rule->flags & NL80211_RRF_NO_HT40PLUS) |
| 1069 | bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20)); |
| 1070 | |
| 1071 | return bw; |
| 1072 | } |
| 1073 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1074 | /* Sanity check on a regulatory rule */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1075 | 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] | 1076 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1077 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1078 | u32 freq_diff; |
| 1079 | |
Luis R. Rodriguez | 91e9900 | 2008-11-12 14:21:55 -0800 | [diff] [blame] | 1080 | 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] | 1081 | return false; |
| 1082 | |
| 1083 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) |
| 1084 | return false; |
| 1085 | |
| 1086 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 1087 | |
Roel Kluin | bd05f28 | 2009-03-03 22:55:21 +0100 | [diff] [blame] | 1088 | if (freq_range->end_freq_khz <= freq_range->start_freq_khz || |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1089 | freq_range->max_bandwidth_khz > freq_diff) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1090 | return false; |
| 1091 | |
| 1092 | return true; |
| 1093 | } |
| 1094 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1095 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1096 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1097 | const struct ieee80211_reg_rule *reg_rule = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1098 | unsigned int i; |
| 1099 | |
| 1100 | if (!rd->n_reg_rules) |
| 1101 | return false; |
| 1102 | |
Luis R. Rodriguez | 88dc1c3 | 2008-11-12 14:22:01 -0800 | [diff] [blame] | 1103 | if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) |
| 1104 | return false; |
| 1105 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1106 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 1107 | reg_rule = &rd->reg_rules[i]; |
| 1108 | if (!is_valid_reg_rule(reg_rule)) |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | return true; |
| 1113 | } |
| 1114 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1115 | /** |
| 1116 | * freq_in_rule_band - tells us if a frequency is in a frequency band |
| 1117 | * @freq_range: frequency rule we want to query |
| 1118 | * @freq_khz: frequency we are inquiring about |
| 1119 | * |
| 1120 | * This lets us know if a specific frequency rule is or is not relevant to |
| 1121 | * a specific frequency's band. Bands are device specific and artificial |
Vladimir Kondratiev | 64629b9 | 2012-09-23 09:49:54 +0200 | [diff] [blame] | 1122 | * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"), |
| 1123 | * however it is safe for now to assume that a frequency rule should not be |
| 1124 | * part of a frequency's band if the start freq or end freq are off by more |
| 1125 | * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the |
| 1126 | * 60 GHz band. |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1127 | * This resolution can be lowered and should be considered as we add |
| 1128 | * regulatory rule support for other "bands". |
| 1129 | **/ |
| 1130 | static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1131 | u32 freq_khz) |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1132 | { |
| 1133 | #define ONE_GHZ_IN_KHZ 1000000 |
Vladimir Kondratiev | 64629b9 | 2012-09-23 09:49:54 +0200 | [diff] [blame] | 1134 | /* |
| 1135 | * From 802.11ad: directional multi-gigabit (DMG): |
| 1136 | * Pertaining to operation in a frequency band containing a channel |
| 1137 | * with the Channel starting frequency above 45 GHz. |
| 1138 | */ |
| 1139 | u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ? |
| 1140 | 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ; |
| 1141 | if (abs(freq_khz - freq_range->start_freq_khz) <= limit) |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1142 | return true; |
Vladimir Kondratiev | 64629b9 | 2012-09-23 09:49:54 +0200 | [diff] [blame] | 1143 | if (abs(freq_khz - freq_range->end_freq_khz) <= limit) |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1144 | return true; |
| 1145 | return false; |
| 1146 | #undef ONE_GHZ_IN_KHZ |
| 1147 | } |
| 1148 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1149 | /* |
Luis R. Rodriguez | adbfb05 | 2013-11-13 18:54:03 +0100 | [diff] [blame] | 1150 | * Later on we can perhaps use the more restrictive DFS |
| 1151 | * region but we don't have information for that yet so |
| 1152 | * for now simply disallow conflicts. |
| 1153 | */ |
| 1154 | static enum nl80211_dfs_regions |
| 1155 | reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1, |
| 1156 | const enum nl80211_dfs_regions dfs_region2) |
| 1157 | { |
| 1158 | if (dfs_region1 != dfs_region2) |
| 1159 | return NL80211_DFS_UNSET; |
| 1160 | return dfs_region1; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1164 | * Helper for regdom_intersect(), this does the real |
| 1165 | * mathematical intersection fun |
| 1166 | */ |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1167 | static int reg_rules_intersect(const struct ieee80211_regdomain *rd1, |
| 1168 | const struct ieee80211_regdomain *rd2, |
| 1169 | const struct ieee80211_reg_rule *rule1, |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1170 | const struct ieee80211_reg_rule *rule2, |
| 1171 | struct ieee80211_reg_rule *intersected_rule) |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1172 | { |
| 1173 | const struct ieee80211_freq_range *freq_range1, *freq_range2; |
| 1174 | struct ieee80211_freq_range *freq_range; |
| 1175 | const struct ieee80211_power_rule *power_rule1, *power_rule2; |
| 1176 | struct ieee80211_power_rule *power_rule; |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1177 | u32 freq_diff, max_bandwidth1, max_bandwidth2; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1178 | |
| 1179 | freq_range1 = &rule1->freq_range; |
| 1180 | freq_range2 = &rule2->freq_range; |
| 1181 | freq_range = &intersected_rule->freq_range; |
| 1182 | |
| 1183 | power_rule1 = &rule1->power_rule; |
| 1184 | power_rule2 = &rule2->power_rule; |
| 1185 | power_rule = &intersected_rule->power_rule; |
| 1186 | |
| 1187 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1188 | freq_range2->start_freq_khz); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1189 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1190 | freq_range2->end_freq_khz); |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1191 | |
| 1192 | max_bandwidth1 = freq_range1->max_bandwidth_khz; |
| 1193 | max_bandwidth2 = freq_range2->max_bandwidth_khz; |
| 1194 | |
Janusz Dziedzic | b0dfd2e | 2014-02-20 13:52:16 +0100 | [diff] [blame] | 1195 | if (rule1->flags & NL80211_RRF_AUTO_BW) |
| 1196 | max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1); |
| 1197 | if (rule2->flags & NL80211_RRF_AUTO_BW) |
| 1198 | max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2); |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1199 | |
| 1200 | freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1201 | |
Janusz Dziedzic | b0dfd2e | 2014-02-20 13:52:16 +0100 | [diff] [blame] | 1202 | intersected_rule->flags = rule1->flags | rule2->flags; |
| 1203 | |
| 1204 | /* |
| 1205 | * In case NL80211_RRF_AUTO_BW requested for both rules |
| 1206 | * set AUTO_BW in intersected rule also. Next we will |
| 1207 | * calculate BW correctly in handle_channel function. |
| 1208 | * In other case remove AUTO_BW flag while we calculate |
| 1209 | * maximum bandwidth correctly and auto calculation is |
| 1210 | * not required. |
| 1211 | */ |
| 1212 | if ((rule1->flags & NL80211_RRF_AUTO_BW) && |
| 1213 | (rule2->flags & NL80211_RRF_AUTO_BW)) |
| 1214 | intersected_rule->flags |= NL80211_RRF_AUTO_BW; |
| 1215 | else |
| 1216 | intersected_rule->flags &= ~NL80211_RRF_AUTO_BW; |
| 1217 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1218 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 1219 | if (freq_range->max_bandwidth_khz > freq_diff) |
| 1220 | freq_range->max_bandwidth_khz = freq_diff; |
| 1221 | |
| 1222 | power_rule->max_eirp = min(power_rule1->max_eirp, |
| 1223 | power_rule2->max_eirp); |
| 1224 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, |
| 1225 | power_rule2->max_antenna_gain); |
| 1226 | |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 1227 | intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms, |
| 1228 | rule2->dfs_cac_ms); |
| 1229 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1230 | if (!is_valid_reg_rule(intersected_rule)) |
| 1231 | return -EINVAL; |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1236 | /* check whether old rule contains new rule */ |
| 1237 | static bool rule_contains(struct ieee80211_reg_rule *r1, |
| 1238 | struct ieee80211_reg_rule *r2) |
| 1239 | { |
| 1240 | /* for simplicity, currently consider only same flags */ |
| 1241 | if (r1->flags != r2->flags) |
| 1242 | return false; |
| 1243 | |
| 1244 | /* verify r1 is more restrictive */ |
| 1245 | if ((r1->power_rule.max_antenna_gain > |
| 1246 | r2->power_rule.max_antenna_gain) || |
| 1247 | r1->power_rule.max_eirp > r2->power_rule.max_eirp) |
| 1248 | return false; |
| 1249 | |
| 1250 | /* make sure r2's range is contained within r1 */ |
| 1251 | if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz || |
| 1252 | r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz) |
| 1253 | return false; |
| 1254 | |
| 1255 | /* and finally verify that r1.max_bw >= r2.max_bw */ |
| 1256 | if (r1->freq_range.max_bandwidth_khz < |
| 1257 | r2->freq_range.max_bandwidth_khz) |
| 1258 | return false; |
| 1259 | |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | /* add or extend current rules. do nothing if rule is already contained */ |
| 1264 | static void add_rule(struct ieee80211_reg_rule *rule, |
| 1265 | struct ieee80211_reg_rule *reg_rules, u32 *n_rules) |
| 1266 | { |
| 1267 | struct ieee80211_reg_rule *tmp_rule; |
| 1268 | int i; |
| 1269 | |
| 1270 | for (i = 0; i < *n_rules; i++) { |
| 1271 | tmp_rule = ®_rules[i]; |
| 1272 | /* rule is already contained - do nothing */ |
| 1273 | if (rule_contains(tmp_rule, rule)) |
| 1274 | return; |
| 1275 | |
| 1276 | /* extend rule if possible */ |
| 1277 | if (rule_contains(rule, tmp_rule)) { |
| 1278 | memcpy(tmp_rule, rule, sizeof(*rule)); |
| 1279 | return; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | memcpy(®_rules[*n_rules], rule, sizeof(*rule)); |
| 1284 | (*n_rules)++; |
| 1285 | } |
| 1286 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1287 | /** |
| 1288 | * regdom_intersect - do the intersection between two regulatory domains |
| 1289 | * @rd1: first regulatory domain |
| 1290 | * @rd2: second regulatory domain |
| 1291 | * |
| 1292 | * Use this function to get the intersection between two regulatory domains. |
| 1293 | * Once completed we will mark the alpha2 for the rd as intersected, "98", |
| 1294 | * as no one single alpha2 can represent this regulatory domain. |
| 1295 | * |
| 1296 | * Returns a pointer to the regulatory domain structure which will hold the |
| 1297 | * resulting intersection of rules between rd1 and rd2. We will |
| 1298 | * kzalloc() this structure for you. |
| 1299 | */ |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1300 | static struct ieee80211_regdomain * |
| 1301 | regdom_intersect(const struct ieee80211_regdomain *rd1, |
| 1302 | const struct ieee80211_regdomain *rd2) |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1303 | { |
| 1304 | int r, size_of_regd; |
| 1305 | unsigned int x, y; |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1306 | unsigned int num_rules = 0; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1307 | const struct ieee80211_reg_rule *rule1, *rule2; |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1308 | struct ieee80211_reg_rule intersected_rule; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1309 | struct ieee80211_regdomain *rd; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (!rd1 || !rd2) |
| 1312 | return NULL; |
| 1313 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1314 | /* |
| 1315 | * 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] | 1316 | * build them. This is to so we can malloc() and free() a |
| 1317 | * regdomain once. The reason we use reg_rules_intersect() here |
| 1318 | * 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] | 1319 | * All rules that do check out OK are valid. |
| 1320 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1321 | |
| 1322 | for (x = 0; x < rd1->n_reg_rules; x++) { |
| 1323 | rule1 = &rd1->reg_rules[x]; |
| 1324 | for (y = 0; y < rd2->n_reg_rules; y++) { |
| 1325 | rule2 = &rd2->reg_rules[y]; |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1326 | if (!reg_rules_intersect(rd1, rd2, rule1, rule2, |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1327 | &intersected_rule)) |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1328 | num_rules++; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (!num_rules) |
| 1333 | return NULL; |
| 1334 | |
| 1335 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
Johannes Berg | 82f2085 | 2012-12-04 12:49:16 +0100 | [diff] [blame] | 1336 | num_rules * sizeof(struct ieee80211_reg_rule); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1337 | |
| 1338 | rd = kzalloc(size_of_regd, GFP_KERNEL); |
| 1339 | if (!rd) |
| 1340 | return NULL; |
| 1341 | |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1342 | for (x = 0; x < rd1->n_reg_rules; x++) { |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1343 | rule1 = &rd1->reg_rules[x]; |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1344 | for (y = 0; y < rd2->n_reg_rules; y++) { |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1345 | rule2 = &rd2->reg_rules[y]; |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1346 | r = reg_rules_intersect(rd1, rd2, rule1, rule2, |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1347 | &intersected_rule); |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1348 | /* |
| 1349 | * No need to memset here the intersected rule here as |
| 1350 | * we're not using the stack anymore |
| 1351 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1352 | if (r) |
| 1353 | continue; |
Eliad Peller | a62a1ae | 2014-09-03 15:25:03 +0300 | [diff] [blame] | 1354 | |
| 1355 | add_rule(&intersected_rule, rd->reg_rules, |
| 1356 | &rd->n_reg_rules); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1360 | rd->alpha2[0] = '9'; |
| 1361 | rd->alpha2[1] = '8'; |
Luis R. Rodriguez | adbfb05 | 2013-11-13 18:54:03 +0100 | [diff] [blame] | 1362 | rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region, |
| 1363 | rd2->dfs_region); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1364 | |
| 1365 | return rd; |
| 1366 | } |
| 1367 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1368 | /* |
| 1369 | * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may |
| 1370 | * want to just have the channel structure use these |
| 1371 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1372 | static u32 map_regdom_flags(u32 rd_flags) |
| 1373 | { |
| 1374 | u32 channel_flags = 0; |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 1375 | if (rd_flags & NL80211_RRF_NO_IR_ALL) |
| 1376 | channel_flags |= IEEE80211_CHAN_NO_IR; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1377 | if (rd_flags & NL80211_RRF_DFS) |
| 1378 | channel_flags |= IEEE80211_CHAN_RADAR; |
Seth Forshee | 03f6b08 | 2012-08-01 15:58:42 -0500 | [diff] [blame] | 1379 | if (rd_flags & NL80211_RRF_NO_OFDM) |
| 1380 | channel_flags |= IEEE80211_CHAN_NO_OFDM; |
David Spinadel | 570dbde | 2014-02-23 09:12:59 +0200 | [diff] [blame] | 1381 | if (rd_flags & NL80211_RRF_NO_OUTDOOR) |
| 1382 | channel_flags |= IEEE80211_CHAN_INDOOR_ONLY; |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 1383 | if (rd_flags & NL80211_RRF_IR_CONCURRENT) |
| 1384 | channel_flags |= IEEE80211_CHAN_IR_CONCURRENT; |
Arik Nemtsov | a6d4a53 | 2014-10-23 09:37:33 +0300 | [diff] [blame] | 1385 | if (rd_flags & NL80211_RRF_NO_HT40MINUS) |
| 1386 | channel_flags |= IEEE80211_CHAN_NO_HT40MINUS; |
| 1387 | if (rd_flags & NL80211_RRF_NO_HT40PLUS) |
| 1388 | channel_flags |= IEEE80211_CHAN_NO_HT40PLUS; |
| 1389 | if (rd_flags & NL80211_RRF_NO_80MHZ) |
| 1390 | channel_flags |= IEEE80211_CHAN_NO_80MHZ; |
| 1391 | if (rd_flags & NL80211_RRF_NO_160MHZ) |
| 1392 | channel_flags |= IEEE80211_CHAN_NO_160MHZ; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1393 | return channel_flags; |
| 1394 | } |
| 1395 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1396 | static const struct ieee80211_reg_rule * |
Michal Sojka | 4917287 | 2015-11-23 19:27:14 +0100 | [diff] [blame] | 1397 | freq_reg_info_regd(u32 center_freq, |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 1398 | const struct ieee80211_regdomain *regd, u32 bw) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1399 | { |
| 1400 | int i; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1401 | bool band_rule_found = false; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1402 | bool bw_fits = false; |
| 1403 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1404 | if (!regd) |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1405 | return ERR_PTR(-EINVAL); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1406 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1407 | for (i = 0; i < regd->n_reg_rules; i++) { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1408 | const struct ieee80211_reg_rule *rr; |
| 1409 | const struct ieee80211_freq_range *fr = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1410 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1411 | rr = ®d->reg_rules[i]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1412 | fr = &rr->freq_range; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1413 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1414 | /* |
| 1415 | * We only need to know if one frequency rule was |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1416 | * was in center_freq's band, that's enough, so lets |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1417 | * not overwrite it once found |
| 1418 | */ |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1419 | if (!band_rule_found) |
| 1420 | band_rule_found = freq_in_rule_band(fr, center_freq); |
| 1421 | |
Rafał Miłecki | 4787cfa | 2017-01-04 18:58:30 +0100 | [diff] [blame] | 1422 | bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw); |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1423 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1424 | if (band_rule_found && bw_fits) |
| 1425 | return rr; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1428 | if (!band_rule_found) |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1429 | return ERR_PTR(-ERANGE); |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 1430 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1431 | return ERR_PTR(-EINVAL); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Johannes Berg | 8de1c63 | 2015-08-21 14:13:06 +0200 | [diff] [blame] | 1434 | static const struct ieee80211_reg_rule * |
| 1435 | __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw) |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 1436 | { |
| 1437 | const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy); |
| 1438 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 1439 | u32 bw; |
| 1440 | |
| 1441 | for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) { |
Michal Sojka | 4917287 | 2015-11-23 19:27:14 +0100 | [diff] [blame] | 1442 | reg_rule = freq_reg_info_regd(center_freq, regd, bw); |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 1443 | if (!IS_ERR(reg_rule)) |
| 1444 | return reg_rule; |
| 1445 | } |
| 1446 | |
| 1447 | return reg_rule; |
| 1448 | } |
| 1449 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1450 | const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy, |
| 1451 | u32 center_freq) |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1452 | { |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 1453 | return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20)); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1454 | } |
John W. Linville | 4f366c5 | 2010-07-15 14:57:33 -0400 | [diff] [blame] | 1455 | EXPORT_SYMBOL(freq_reg_info); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1456 | |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1457 | const char *reg_initiator_name(enum nl80211_reg_initiator initiator) |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1458 | { |
| 1459 | switch (initiator) { |
| 1460 | case NL80211_REGDOM_SET_BY_CORE: |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1461 | return "core"; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1462 | case NL80211_REGDOM_SET_BY_USER: |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1463 | return "user"; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1464 | case NL80211_REGDOM_SET_BY_DRIVER: |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1465 | return "driver"; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1466 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1467 | return "country IE"; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1468 | default: |
| 1469 | WARN_ON(1); |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1470 | return "bug"; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1471 | } |
| 1472 | } |
Luis R. Rodriguez | 034c6d6 | 2013-10-14 17:42:06 -0700 | [diff] [blame] | 1473 | EXPORT_SYMBOL(reg_initiator_name); |
Luis R. Rodriguez | e702d3c | 2010-10-21 19:17:04 +0530 | [diff] [blame] | 1474 | |
Michal Sojka | 1aeb135 | 2015-11-23 19:27:16 +0100 | [diff] [blame] | 1475 | static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd, |
| 1476 | const struct ieee80211_reg_rule *reg_rule, |
| 1477 | const struct ieee80211_channel *chan) |
| 1478 | { |
| 1479 | const struct ieee80211_freq_range *freq_range = NULL; |
| 1480 | u32 max_bandwidth_khz, bw_flags = 0; |
| 1481 | |
| 1482 | freq_range = ®_rule->freq_range; |
| 1483 | |
| 1484 | max_bandwidth_khz = freq_range->max_bandwidth_khz; |
| 1485 | /* Check if auto calculation requested */ |
| 1486 | if (reg_rule->flags & NL80211_RRF_AUTO_BW) |
| 1487 | max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule); |
| 1488 | |
| 1489 | /* If we get a reg_rule we can assume that at least 5Mhz fit */ |
Rafał Miłecki | 4787cfa | 2017-01-04 18:58:30 +0100 | [diff] [blame] | 1490 | if (!cfg80211_does_bw_fit_range(freq_range, |
| 1491 | MHZ_TO_KHZ(chan->center_freq), |
| 1492 | MHZ_TO_KHZ(10))) |
Michal Sojka | 1aeb135 | 2015-11-23 19:27:16 +0100 | [diff] [blame] | 1493 | bw_flags |= IEEE80211_CHAN_NO_10MHZ; |
Rafał Miłecki | 4787cfa | 2017-01-04 18:58:30 +0100 | [diff] [blame] | 1494 | if (!cfg80211_does_bw_fit_range(freq_range, |
| 1495 | MHZ_TO_KHZ(chan->center_freq), |
| 1496 | MHZ_TO_KHZ(20))) |
Michal Sojka | 1aeb135 | 2015-11-23 19:27:16 +0100 | [diff] [blame] | 1497 | bw_flags |= IEEE80211_CHAN_NO_20MHZ; |
| 1498 | |
| 1499 | if (max_bandwidth_khz < MHZ_TO_KHZ(10)) |
| 1500 | bw_flags |= IEEE80211_CHAN_NO_10MHZ; |
| 1501 | if (max_bandwidth_khz < MHZ_TO_KHZ(20)) |
| 1502 | bw_flags |= IEEE80211_CHAN_NO_20MHZ; |
| 1503 | if (max_bandwidth_khz < MHZ_TO_KHZ(40)) |
| 1504 | bw_flags |= IEEE80211_CHAN_NO_HT40; |
| 1505 | if (max_bandwidth_khz < MHZ_TO_KHZ(80)) |
| 1506 | bw_flags |= IEEE80211_CHAN_NO_80MHZ; |
| 1507 | if (max_bandwidth_khz < MHZ_TO_KHZ(160)) |
| 1508 | bw_flags |= IEEE80211_CHAN_NO_160MHZ; |
| 1509 | return bw_flags; |
| 1510 | } |
| 1511 | |
Johannes Berg | e33e224 | 2014-06-23 11:06:16 +0200 | [diff] [blame] | 1512 | /* |
| 1513 | * Note that right now we assume the desired channel bandwidth |
| 1514 | * is always 20 MHz for each individual channel (HT40 uses 20 MHz |
| 1515 | * per channel, the primary and the extension channel). |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1516 | */ |
Luis R. Rodriguez | 7ca43d0 | 2010-10-20 10:18:53 -0700 | [diff] [blame] | 1517 | static void handle_channel(struct wiphy *wiphy, |
| 1518 | enum nl80211_reg_initiator initiator, |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1519 | struct ieee80211_channel *chan) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1520 | { |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1521 | u32 flags, bw_flags = 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1522 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 1523 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1524 | struct wiphy *request_wiphy = NULL; |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1525 | struct regulatory_request *lr = get_last_request(); |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 1526 | const struct ieee80211_regdomain *regd; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1527 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1528 | request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1529 | |
| 1530 | flags = chan->orig_flags; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1531 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1532 | reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq)); |
| 1533 | if (IS_ERR(reg_rule)) { |
Luis R. Rodriguez | ca4ffe8 | 2010-10-20 10:18:55 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * We will disable all channels that do not match our |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1536 | * received regulatory rule unless the hint is coming |
Luis R. Rodriguez | ca4ffe8 | 2010-10-20 10:18:55 -0700 | [diff] [blame] | 1537 | * from a Country IE and the Country IE had no information |
| 1538 | * about a band. The IEEE 802.11 spec allows for an AP |
| 1539 | * to send only a subset of the regulatory rules allowed, |
| 1540 | * so an AP in the US that only supports 2.4 GHz may only send |
| 1541 | * a country IE with information for the 2.4 GHz band |
| 1542 | * while 5 GHz is still supported. |
| 1543 | */ |
| 1544 | if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 1545 | PTR_ERR(reg_rule) == -ERANGE) |
Luis R. Rodriguez | ca4ffe8 | 2010-10-20 10:18:55 -0700 | [diff] [blame] | 1546 | return; |
| 1547 | |
Luis R. Rodriguez | cc493e4f | 2013-11-06 17:54:44 +0100 | [diff] [blame] | 1548 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
| 1549 | request_wiphy && request_wiphy == wiphy && |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1550 | request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 1551 | pr_debug("Disabling freq %d MHz for good\n", |
| 1552 | chan->center_freq); |
Luis R. Rodriguez | cc493e4f | 2013-11-06 17:54:44 +0100 | [diff] [blame] | 1553 | chan->orig_flags |= IEEE80211_CHAN_DISABLED; |
| 1554 | chan->flags = chan->orig_flags; |
| 1555 | } else { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 1556 | pr_debug("Disabling freq %d MHz\n", |
| 1557 | chan->center_freq); |
Luis R. Rodriguez | cc493e4f | 2013-11-06 17:54:44 +0100 | [diff] [blame] | 1558 | chan->flags |= IEEE80211_CHAN_DISABLED; |
| 1559 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1560 | return; |
Luis R. Rodriguez | ca4ffe8 | 2010-10-20 10:18:55 -0700 | [diff] [blame] | 1561 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1562 | |
Janusz Dziedzic | b0dfd2e | 2014-02-20 13:52:16 +0100 | [diff] [blame] | 1563 | regd = reg_get_regdomain(wiphy); |
Luis R. Rodriguez | e702d3c | 2010-10-21 19:17:04 +0530 | [diff] [blame] | 1564 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1565 | power_rule = ®_rule->power_rule; |
Michal Sojka | 1aeb135 | 2015-11-23 19:27:16 +0100 | [diff] [blame] | 1566 | bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1567 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1568 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1569 | request_wiphy && request_wiphy == wiphy && |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1570 | request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1571 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1572 | * This guarantees the driver's requested regulatory domain |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1573 | * will always be used as a base for further regulatory |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1574 | * settings |
| 1575 | */ |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1576 | chan->flags = chan->orig_flags = |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1577 | map_regdom_flags(reg_rule->flags) | bw_flags; |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1578 | chan->max_antenna_gain = chan->orig_mag = |
| 1579 | (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
Felix Fietkau | 279f0f5 | 2012-10-17 13:56:20 +0200 | [diff] [blame] | 1580 | chan->max_reg_power = chan->max_power = chan->orig_mpwr = |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1581 | (int) MBM_TO_DBM(power_rule->max_eirp); |
Janusz Dziedzic | 4f267c1 | 2014-04-09 13:47:12 +0200 | [diff] [blame] | 1582 | |
| 1583 | if (chan->flags & IEEE80211_CHAN_RADAR) { |
| 1584 | chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; |
| 1585 | if (reg_rule->dfs_cac_ms) |
| 1586 | chan->dfs_cac_ms = reg_rule->dfs_cac_ms; |
| 1587 | } |
| 1588 | |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 1592 | chan->dfs_state = NL80211_DFS_USABLE; |
| 1593 | chan->dfs_state_entered = jiffies; |
| 1594 | |
Rajkumar Manoharan | aa3d7ee | 2011-09-14 14:28:17 +0530 | [diff] [blame] | 1595 | chan->beacon_found = false; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1596 | chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags); |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1597 | chan->max_antenna_gain = |
| 1598 | min_t(int, chan->orig_mag, |
| 1599 | MBI_TO_DBI(power_rule->max_antenna_gain)); |
Hong Wu | eccc068 | 2012-01-11 20:33:39 +0200 | [diff] [blame] | 1600 | chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 1601 | |
| 1602 | if (chan->flags & IEEE80211_CHAN_RADAR) { |
| 1603 | if (reg_rule->dfs_cac_ms) |
| 1604 | chan->dfs_cac_ms = reg_rule->dfs_cac_ms; |
| 1605 | else |
| 1606 | chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; |
| 1607 | } |
| 1608 | |
Stanislaw Gruszka | 5e31fc0 | 2012-07-24 08:35:39 +0200 | [diff] [blame] | 1609 | if (chan->orig_mpwr) { |
| 1610 | /* |
Luis R. Rodriguez | a09a85a | 2013-11-11 22:15:30 +0100 | [diff] [blame] | 1611 | * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER |
| 1612 | * will always follow the passed country IE power settings. |
Stanislaw Gruszka | 5e31fc0 | 2012-07-24 08:35:39 +0200 | [diff] [blame] | 1613 | */ |
| 1614 | if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Luis R. Rodriguez | a09a85a | 2013-11-11 22:15:30 +0100 | [diff] [blame] | 1615 | wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER) |
Stanislaw Gruszka | 5e31fc0 | 2012-07-24 08:35:39 +0200 | [diff] [blame] | 1616 | chan->max_power = chan->max_reg_power; |
| 1617 | else |
| 1618 | chan->max_power = min(chan->orig_mpwr, |
| 1619 | chan->max_reg_power); |
| 1620 | } else |
| 1621 | chan->max_power = chan->max_reg_power; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
Luis R. Rodriguez | 7ca43d0 | 2010-10-20 10:18:53 -0700 | [diff] [blame] | 1624 | static void handle_band(struct wiphy *wiphy, |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1625 | enum nl80211_reg_initiator initiator, |
| 1626 | struct ieee80211_supported_band *sband) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1627 | { |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1628 | unsigned int i; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1629 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1630 | if (!sband) |
| 1631 | return; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1632 | |
| 1633 | for (i = 0; i < sband->n_channels; i++) |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1634 | handle_channel(wiphy, initiator, &sband->channels[i]); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1637 | static bool reg_request_cell_base(struct regulatory_request *request) |
| 1638 | { |
| 1639 | if (request->initiator != NL80211_REGDOM_SET_BY_USER) |
| 1640 | return false; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1641 | return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE; |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | bool reg_last_request_cell_base(void) |
| 1645 | { |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 1646 | return reg_request_cell_base(get_last_request()); |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Ilan Peer | 94fc661 | 2014-02-23 09:13:00 +0200 | [diff] [blame] | 1649 | #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1650 | /* Core specific check */ |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 1651 | static enum reg_request_treatment |
| 1652 | reg_ignore_cell_hint(struct regulatory_request *pending_request) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1653 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1654 | struct regulatory_request *lr = get_last_request(); |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1655 | |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1656 | if (!reg_num_devs_support_basehint) |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 1657 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1658 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1659 | if (reg_request_cell_base(lr) && |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1660 | !regdom_changes(pending_request->alpha2)) |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 1661 | return REG_REQ_ALREADY_SET; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1662 | |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 1663 | return REG_REQ_OK; |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* Device specific check */ |
| 1667 | static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) |
| 1668 | { |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1669 | return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS); |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1670 | } |
| 1671 | #else |
Johannes Berg | a515de6 | 2015-10-15 14:28:56 +0200 | [diff] [blame] | 1672 | static enum reg_request_treatment |
| 1673 | reg_ignore_cell_hint(struct regulatory_request *pending_request) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1674 | { |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 1675 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1676 | } |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1677 | |
| 1678 | static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1679 | { |
| 1680 | return true; |
| 1681 | } |
| 1682 | #endif |
| 1683 | |
Luis R. Rodriguez | fa1fb9c | 2013-10-02 18:33:10 -0700 | [diff] [blame] | 1684 | static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy) |
| 1685 | { |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1686 | if (wiphy->regulatory_flags & REGULATORY_STRICT_REG && |
| 1687 | !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)) |
Luis R. Rodriguez | fa1fb9c | 2013-10-02 18:33:10 -0700 | [diff] [blame] | 1688 | return true; |
| 1689 | return false; |
| 1690 | } |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1691 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1692 | static bool ignore_reg_update(struct wiphy *wiphy, |
| 1693 | enum nl80211_reg_initiator initiator) |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1694 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1695 | struct regulatory_request *lr = get_last_request(); |
| 1696 | |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 1697 | if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) |
| 1698 | return true; |
| 1699 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1700 | if (!lr) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 1701 | pr_debug("Ignoring regulatory request set by %s since last_request is not set\n", |
| 1702 | reg_initiator_name(initiator)); |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1703 | return true; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1704 | } |
| 1705 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1706 | if (initiator == NL80211_REGDOM_SET_BY_CORE && |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1707 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 1708 | pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n", |
| 1709 | reg_initiator_name(initiator)); |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1710 | return true; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1711 | } |
| 1712 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1713 | /* |
| 1714 | * wiphy->regd will be set once the device has its own |
| 1715 | * desired regulatory domain set |
| 1716 | */ |
Luis R. Rodriguez | fa1fb9c | 2013-10-02 18:33:10 -0700 | [diff] [blame] | 1717 | if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd && |
Luis R. Rodriguez | 749b527 | 2010-10-20 10:18:54 -0700 | [diff] [blame] | 1718 | initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1719 | !is_world_regdom(lr->alpha2)) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 1720 | pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n", |
| 1721 | reg_initiator_name(initiator)); |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1722 | return true; |
Luis R. Rodriguez | 926a0a0 | 2010-10-21 19:17:03 +0530 | [diff] [blame] | 1723 | } |
| 1724 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 1725 | if (reg_request_cell_base(lr)) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 1726 | return reg_dev_ignore_cell_hint(wiphy); |
| 1727 | |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1728 | return false; |
| 1729 | } |
| 1730 | |
Luis R. Rodriguez | 3195e48 | 2012-12-19 10:53:03 -0800 | [diff] [blame] | 1731 | static bool reg_is_world_roaming(struct wiphy *wiphy) |
| 1732 | { |
| 1733 | const struct ieee80211_regdomain *cr = get_cfg80211_regdom(); |
| 1734 | const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy); |
| 1735 | struct regulatory_request *lr = get_last_request(); |
| 1736 | |
| 1737 | if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2))) |
| 1738 | return true; |
| 1739 | |
| 1740 | if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1741 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) |
Luis R. Rodriguez | 3195e48 | 2012-12-19 10:53:03 -0800 | [diff] [blame] | 1742 | return true; |
| 1743 | |
| 1744 | return false; |
| 1745 | } |
| 1746 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1747 | static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx, |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1748 | struct reg_beacon *reg_beacon) |
| 1749 | { |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1750 | struct ieee80211_supported_band *sband; |
| 1751 | struct ieee80211_channel *chan; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1752 | bool channel_changed = false; |
| 1753 | struct ieee80211_channel chan_before; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1754 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1755 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1756 | chan = &sband->channels[chan_idx]; |
| 1757 | |
| 1758 | if (likely(chan->center_freq != reg_beacon->chan.center_freq)) |
| 1759 | return; |
| 1760 | |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1761 | if (chan->beacon_found) |
| 1762 | return; |
| 1763 | |
| 1764 | chan->beacon_found = true; |
| 1765 | |
Luis R. Rodriguez | 0f500a5 | 2012-12-19 10:53:04 -0800 | [diff] [blame] | 1766 | if (!reg_is_world_roaming(wiphy)) |
| 1767 | return; |
| 1768 | |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 1769 | if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS) |
Luis R. Rodriguez | 3718424 | 2009-07-30 17:43:48 -0700 | [diff] [blame] | 1770 | return; |
| 1771 | |
Johannes Berg | a48a52b | 2018-01-15 09:12:05 +0100 | [diff] [blame] | 1772 | chan_before = *chan; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1773 | |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 1774 | if (chan->flags & IEEE80211_CHAN_NO_IR) { |
| 1775 | chan->flags &= ~IEEE80211_CHAN_NO_IR; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1776 | channel_changed = true; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1777 | } |
| 1778 | |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1779 | if (channel_changed) |
| 1780 | nl80211_send_beacon_hint_event(wiphy, &chan_before, chan); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * Called when a scan on a wiphy finds a beacon on |
| 1785 | * new channel |
| 1786 | */ |
| 1787 | static void wiphy_update_new_beacon(struct wiphy *wiphy, |
| 1788 | struct reg_beacon *reg_beacon) |
| 1789 | { |
| 1790 | unsigned int i; |
| 1791 | struct ieee80211_supported_band *sband; |
| 1792 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1793 | if (!wiphy->bands[reg_beacon->chan.band]) |
| 1794 | return; |
| 1795 | |
| 1796 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1797 | |
| 1798 | for (i = 0; i < sband->n_channels; i++) |
| 1799 | handle_reg_beacon(wiphy, i, reg_beacon); |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Called upon reg changes or a new wiphy is added |
| 1804 | */ |
| 1805 | static void wiphy_update_beacon_reg(struct wiphy *wiphy) |
| 1806 | { |
| 1807 | unsigned int i; |
| 1808 | struct ieee80211_supported_band *sband; |
| 1809 | struct reg_beacon *reg_beacon; |
| 1810 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1811 | list_for_each_entry(reg_beacon, ®_beacon_list, list) { |
| 1812 | if (!wiphy->bands[reg_beacon->chan.band]) |
| 1813 | continue; |
| 1814 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1815 | for (i = 0; i < sband->n_channels; i++) |
| 1816 | handle_reg_beacon(wiphy, i, reg_beacon); |
| 1817 | } |
| 1818 | } |
| 1819 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1820 | /* Reap the advantages of previously found beacons */ |
| 1821 | static void reg_process_beacons(struct wiphy *wiphy) |
| 1822 | { |
Luis R. Rodriguez | b1ed8dd | 2009-05-02 00:34:15 -0400 | [diff] [blame] | 1823 | /* |
| 1824 | * Means we are just firing up cfg80211, so no beacons would |
| 1825 | * have been processed yet. |
| 1826 | */ |
| 1827 | if (!last_request) |
| 1828 | return; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1829 | wiphy_update_beacon_reg(wiphy); |
| 1830 | } |
| 1831 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1832 | static bool is_ht40_allowed(struct ieee80211_channel *chan) |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1833 | { |
| 1834 | if (!chan) |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1835 | return false; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1836 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1837 | return false; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1838 | /* This would happen when regulatory rules disallow HT40 completely */ |
Felix Fietkau | 55b183a | 2013-01-11 14:22:58 +0100 | [diff] [blame] | 1839 | if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40) |
| 1840 | return false; |
| 1841 | return true; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static void reg_process_ht_flags_channel(struct wiphy *wiphy, |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1845 | struct ieee80211_channel *channel) |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1846 | { |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1847 | struct ieee80211_supported_band *sband = wiphy->bands[channel->band]; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1848 | struct ieee80211_channel *channel_before = NULL, *channel_after = NULL; |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 1849 | const struct ieee80211_regdomain *regd; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1850 | unsigned int i; |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 1851 | u32 flags; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1852 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1853 | if (!is_ht40_allowed(channel)) { |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1854 | channel->flags |= IEEE80211_CHAN_NO_HT40; |
| 1855 | return; |
| 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * We need to ensure the extension channels exist to |
| 1860 | * be able to use HT40- or HT40+, this finds them (or not) |
| 1861 | */ |
| 1862 | for (i = 0; i < sband->n_channels; i++) { |
| 1863 | struct ieee80211_channel *c = &sband->channels[i]; |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 1864 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1865 | if (c->center_freq == (channel->center_freq - 20)) |
| 1866 | channel_before = c; |
| 1867 | if (c->center_freq == (channel->center_freq + 20)) |
| 1868 | channel_after = c; |
| 1869 | } |
| 1870 | |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 1871 | flags = 0; |
| 1872 | regd = get_wiphy_regdom(wiphy); |
| 1873 | if (regd) { |
| 1874 | const struct ieee80211_reg_rule *reg_rule = |
| 1875 | freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq), |
| 1876 | regd, MHZ_TO_KHZ(20)); |
| 1877 | |
| 1878 | if (!IS_ERR(reg_rule)) |
| 1879 | flags = reg_rule->flags; |
| 1880 | } |
| 1881 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1882 | /* |
| 1883 | * Please note that this assumes target bandwidth is 20 MHz, |
| 1884 | * if that ever changes we also need to change the below logic |
| 1885 | * to include that as well. |
| 1886 | */ |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 1887 | if (!is_ht40_allowed(channel_before) || |
| 1888 | flags & NL80211_RRF_NO_HT40MINUS) |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1889 | channel->flags |= IEEE80211_CHAN_NO_HT40MINUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1890 | else |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1891 | channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1892 | |
Emmanuel Grumbach | 4e0854a | 2017-09-06 13:45:40 +0300 | [diff] [blame] | 1893 | if (!is_ht40_allowed(channel_after) || |
| 1894 | flags & NL80211_RRF_NO_HT40PLUS) |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1895 | channel->flags |= IEEE80211_CHAN_NO_HT40PLUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1896 | else |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1897 | channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static void reg_process_ht_flags_band(struct wiphy *wiphy, |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1901 | struct ieee80211_supported_band *sband) |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1902 | { |
| 1903 | unsigned int i; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1904 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1905 | if (!sband) |
| 1906 | return; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1907 | |
| 1908 | for (i = 0; i < sband->n_channels; i++) |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1909 | reg_process_ht_flags_channel(wiphy, &sband->channels[i]); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static void reg_process_ht_flags(struct wiphy *wiphy) |
| 1913 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1914 | enum nl80211_band band; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1915 | |
| 1916 | if (!wiphy) |
| 1917 | return; |
| 1918 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1919 | for (band = 0; band < NUM_NL80211_BANDS; band++) |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 1920 | reg_process_ht_flags_band(wiphy, wiphy->bands[band]); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1921 | } |
| 1922 | |
Luis R. Rodriguez | 0e3802d | 2013-11-05 09:18:12 -0800 | [diff] [blame] | 1923 | static void reg_call_notifier(struct wiphy *wiphy, |
| 1924 | struct regulatory_request *request) |
| 1925 | { |
| 1926 | if (wiphy->reg_notifier) |
| 1927 | wiphy->reg_notifier(wiphy, request); |
| 1928 | } |
| 1929 | |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1930 | static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev) |
| 1931 | { |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1932 | struct cfg80211_chan_def chandef; |
| 1933 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1934 | enum nl80211_iftype iftype; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1935 | |
| 1936 | wdev_lock(wdev); |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1937 | iftype = wdev->iftype; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1938 | |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1939 | /* make sure the interface is active */ |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1940 | if (!wdev->netdev || !netif_running(wdev->netdev)) |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1941 | goto wdev_inactive_unlock; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1942 | |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1943 | switch (iftype) { |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1944 | case NL80211_IFTYPE_AP: |
| 1945 | case NL80211_IFTYPE_P2P_GO: |
| 1946 | if (!wdev->beacon_interval) |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1947 | goto wdev_inactive_unlock; |
| 1948 | chandef = wdev->chandef; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1949 | break; |
Arik Nemtsov | 185076d | 2014-12-03 18:08:17 +0200 | [diff] [blame] | 1950 | case NL80211_IFTYPE_ADHOC: |
| 1951 | if (!wdev->ssid_len) |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1952 | goto wdev_inactive_unlock; |
| 1953 | chandef = wdev->chandef; |
Arik Nemtsov | 185076d | 2014-12-03 18:08:17 +0200 | [diff] [blame] | 1954 | break; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1955 | case NL80211_IFTYPE_STATION: |
| 1956 | case NL80211_IFTYPE_P2P_CLIENT: |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1957 | if (!wdev->current_bss || |
| 1958 | !wdev->current_bss->pub.channel) |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1959 | goto wdev_inactive_unlock; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1960 | |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1961 | if (!rdev->ops->get_channel || |
| 1962 | rdev_get_channel(rdev, wdev, &chandef)) |
| 1963 | cfg80211_chandef_create(&chandef, |
| 1964 | wdev->current_bss->pub.channel, |
| 1965 | NL80211_CHAN_NO_HT); |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1966 | break; |
| 1967 | case NL80211_IFTYPE_MONITOR: |
| 1968 | case NL80211_IFTYPE_AP_VLAN: |
| 1969 | case NL80211_IFTYPE_P2P_DEVICE: |
| 1970 | /* no enforcement required */ |
| 1971 | break; |
| 1972 | default: |
| 1973 | /* others not implemented for now */ |
| 1974 | WARN_ON(1); |
| 1975 | break; |
| 1976 | } |
| 1977 | |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1978 | wdev_unlock(wdev); |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1979 | |
| 1980 | switch (iftype) { |
| 1981 | case NL80211_IFTYPE_AP: |
| 1982 | case NL80211_IFTYPE_P2P_GO: |
| 1983 | case NL80211_IFTYPE_ADHOC: |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 1984 | return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype); |
Arik Nemtsov | 2065870 | 2014-12-29 11:59:59 +0200 | [diff] [blame] | 1985 | case NL80211_IFTYPE_STATION: |
| 1986 | case NL80211_IFTYPE_P2P_CLIENT: |
| 1987 | return cfg80211_chandef_usable(wiphy, &chandef, |
| 1988 | IEEE80211_CHAN_DISABLED); |
| 1989 | default: |
| 1990 | break; |
| 1991 | } |
| 1992 | |
| 1993 | return true; |
| 1994 | |
| 1995 | wdev_inactive_unlock: |
| 1996 | wdev_unlock(wdev); |
| 1997 | return true; |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | static void reg_leave_invalid_chans(struct wiphy *wiphy) |
| 2001 | { |
| 2002 | struct wireless_dev *wdev; |
| 2003 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); |
| 2004 | |
| 2005 | ASSERT_RTNL(); |
| 2006 | |
Johannes Berg | 53873f1 | 2016-05-03 16:52:04 +0300 | [diff] [blame] | 2007 | list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 2008 | if (!reg_wdev_chan_valid(wiphy, wdev)) |
| 2009 | cfg80211_leave(rdev, wdev); |
| 2010 | } |
| 2011 | |
| 2012 | static void reg_check_chans_work(struct work_struct *work) |
| 2013 | { |
| 2014 | struct cfg80211_registered_device *rdev; |
| 2015 | |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2016 | pr_debug("Verifying active interfaces after reg change\n"); |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 2017 | rtnl_lock(); |
| 2018 | |
| 2019 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) |
| 2020 | if (!(rdev->wiphy.regulatory_flags & |
| 2021 | REGULATORY_IGNORE_STALE_KICKOFF)) |
| 2022 | reg_leave_invalid_chans(&rdev->wiphy); |
| 2023 | |
| 2024 | rtnl_unlock(); |
| 2025 | } |
| 2026 | |
| 2027 | static void reg_check_channels(void) |
| 2028 | { |
| 2029 | /* |
| 2030 | * Give usermode a chance to do something nicer (move to another |
| 2031 | * channel, orderly disconnection), before forcing a disconnection. |
| 2032 | */ |
| 2033 | mod_delayed_work(system_power_efficient_wq, |
| 2034 | ®_check_chans, |
| 2035 | msecs_to_jiffies(REG_ENFORCE_GRACE_MS)); |
| 2036 | } |
| 2037 | |
Sven Neumann | eac03e3 | 2011-08-30 23:38:53 +0200 | [diff] [blame] | 2038 | static void wiphy_update_regulatory(struct wiphy *wiphy, |
| 2039 | enum nl80211_reg_initiator initiator) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 2040 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2041 | enum nl80211_band band; |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2042 | struct regulatory_request *lr = get_last_request(); |
Sven Neumann | eac03e3 | 2011-08-30 23:38:53 +0200 | [diff] [blame] | 2043 | |
Luis R. Rodriguez | 0e3802d | 2013-11-05 09:18:12 -0800 | [diff] [blame] | 2044 | if (ignore_reg_update(wiphy, initiator)) { |
| 2045 | /* |
| 2046 | * Regulatory updates set by CORE are ignored for custom |
| 2047 | * regulatory cards. Let us notify the changes to the driver, |
| 2048 | * as some drivers used this to restore its orig_* reg domain. |
| 2049 | */ |
| 2050 | if (initiator == NL80211_REGDOM_SET_BY_CORE && |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 2051 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) |
Luis R. Rodriguez | 0e3802d | 2013-11-05 09:18:12 -0800 | [diff] [blame] | 2052 | reg_call_notifier(wiphy, lr); |
Sven Neumann | a203c2a | 2011-07-12 15:52:07 +0200 | [diff] [blame] | 2053 | return; |
Luis R. Rodriguez | 0e3802d | 2013-11-05 09:18:12 -0800 | [diff] [blame] | 2054 | } |
Sven Neumann | a203c2a | 2011-07-12 15:52:07 +0200 | [diff] [blame] | 2055 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2056 | lr->dfs_region = get_cfg80211_regdom()->dfs_region; |
Luis R. Rodriguez | b68e6b3 | 2011-10-11 10:59:03 -0700 | [diff] [blame] | 2057 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2058 | for (band = 0; band < NUM_NL80211_BANDS; band++) |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2059 | handle_band(wiphy, initiator, wiphy->bands[band]); |
Sven Neumann | a203c2a | 2011-07-12 15:52:07 +0200 | [diff] [blame] | 2060 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2061 | reg_process_beacons(wiphy); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 2062 | reg_process_ht_flags(wiphy); |
Luis R. Rodriguez | 0e3802d | 2013-11-05 09:18:12 -0800 | [diff] [blame] | 2063 | reg_call_notifier(wiphy, lr); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Sven Neumann | d7549cb | 2011-08-30 23:38:54 +0200 | [diff] [blame] | 2066 | static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) |
| 2067 | { |
| 2068 | struct cfg80211_registered_device *rdev; |
Rajkumar Manoharan | 4a38994 | 2011-12-08 23:59:26 +0530 | [diff] [blame] | 2069 | struct wiphy *wiphy; |
Sven Neumann | d7549cb | 2011-08-30 23:38:54 +0200 | [diff] [blame] | 2070 | |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2071 | ASSERT_RTNL(); |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 2072 | |
Rajkumar Manoharan | 4a38994 | 2011-12-08 23:59:26 +0530 | [diff] [blame] | 2073 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 2074 | wiphy = &rdev->wiphy; |
| 2075 | wiphy_update_regulatory(wiphy, initiator); |
Rajkumar Manoharan | 4a38994 | 2011-12-08 23:59:26 +0530 | [diff] [blame] | 2076 | } |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 2077 | |
| 2078 | reg_check_channels(); |
Sven Neumann | d7549cb | 2011-08-30 23:38:54 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2081 | static void handle_channel_custom(struct wiphy *wiphy, |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2082 | struct ieee80211_channel *chan, |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2083 | const struct ieee80211_regdomain *regd) |
| 2084 | { |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 2085 | u32 bw_flags = 0; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2086 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 2087 | const struct ieee80211_power_rule *power_rule = NULL; |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 2088 | u32 bw; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2089 | |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 2090 | for (bw = MHZ_TO_KHZ(20); bw >= MHZ_TO_KHZ(5); bw = bw / 2) { |
Michal Sojka | 4917287 | 2015-11-23 19:27:14 +0100 | [diff] [blame] | 2091 | reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq), |
Matthias May | 4edd569 | 2015-07-17 15:28:39 +0200 | [diff] [blame] | 2092 | regd, bw); |
| 2093 | if (!IS_ERR(reg_rule)) |
| 2094 | break; |
| 2095 | } |
Luis R. Rodriguez | ac46d48 | 2009-05-01 18:44:50 -0400 | [diff] [blame] | 2096 | |
Johannes Berg | 361c9c8 | 2012-12-06 15:57:14 +0100 | [diff] [blame] | 2097 | if (IS_ERR(reg_rule)) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2098 | pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n", |
| 2099 | chan->center_freq); |
Arik Nemtsov | db8dfee | 2014-12-15 19:26:02 +0200 | [diff] [blame] | 2100 | if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { |
| 2101 | chan->flags |= IEEE80211_CHAN_DISABLED; |
| 2102 | } else { |
| 2103 | chan->orig_flags |= IEEE80211_CHAN_DISABLED; |
| 2104 | chan->flags = chan->orig_flags; |
| 2105 | } |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | power_rule = ®_rule->power_rule; |
Michal Sojka | 1aeb135 | 2015-11-23 19:27:16 +0100 | [diff] [blame] | 2110 | bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 2111 | |
Arik Nemtsov | 2e18b38 | 2014-11-16 16:37:46 +0200 | [diff] [blame] | 2112 | chan->dfs_state_entered = jiffies; |
Arik Nemtsov | c7ab508 | 2014-11-16 16:37:47 +0200 | [diff] [blame] | 2113 | chan->dfs_state = NL80211_DFS_USABLE; |
| 2114 | |
| 2115 | chan->beacon_found = false; |
Arik Nemtsov | db8dfee | 2014-12-15 19:26:02 +0200 | [diff] [blame] | 2116 | |
| 2117 | if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) |
| 2118 | chan->flags = chan->orig_flags | bw_flags | |
| 2119 | map_regdom_flags(reg_rule->flags); |
| 2120 | else |
| 2121 | chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; |
| 2122 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2123 | chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
Felix Fietkau | 279f0f5 | 2012-10-17 13:56:20 +0200 | [diff] [blame] | 2124 | chan->max_reg_power = chan->max_power = |
| 2125 | (int) MBM_TO_DBM(power_rule->max_eirp); |
Arik Nemtsov | 2e18b38 | 2014-11-16 16:37:46 +0200 | [diff] [blame] | 2126 | |
| 2127 | if (chan->flags & IEEE80211_CHAN_RADAR) { |
| 2128 | if (reg_rule->dfs_cac_ms) |
| 2129 | chan->dfs_cac_ms = reg_rule->dfs_cac_ms; |
| 2130 | else |
| 2131 | chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; |
| 2132 | } |
| 2133 | |
| 2134 | chan->max_power = chan->max_reg_power; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2137 | static void handle_band_custom(struct wiphy *wiphy, |
| 2138 | struct ieee80211_supported_band *sband, |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2139 | const struct ieee80211_regdomain *regd) |
| 2140 | { |
| 2141 | unsigned int i; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2142 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2143 | if (!sband) |
| 2144 | return; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2145 | |
| 2146 | for (i = 0; i < sband->n_channels; i++) |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2147 | handle_channel_custom(wiphy, &sband->channels[i], regd); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* Used by drivers prior to wiphy registration */ |
| 2151 | void wiphy_apply_custom_regulatory(struct wiphy *wiphy, |
| 2152 | const struct ieee80211_regdomain *regd) |
| 2153 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2154 | enum nl80211_band band; |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 2155 | unsigned int bands_set = 0; |
Luis R. Rodriguez | ac46d48 | 2009-05-01 18:44:50 -0400 | [diff] [blame] | 2156 | |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 2157 | WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG), |
| 2158 | "wiphy should have REGULATORY_CUSTOM_REG\n"); |
| 2159 | wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG; |
Luis R. Rodriguez | 222ea58 | 2013-11-05 09:18:00 -0800 | [diff] [blame] | 2160 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2161 | for (band = 0; band < NUM_NL80211_BANDS; band++) { |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 2162 | if (!wiphy->bands[band]) |
| 2163 | continue; |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2164 | handle_band_custom(wiphy, wiphy->bands[band], regd); |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 2165 | bands_set++; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2166 | } |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 2167 | |
| 2168 | /* |
| 2169 | * no point in calling this if it won't have any effect |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 2170 | * on your device's supported bands. |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 2171 | */ |
| 2172 | WARN_ON(!bands_set); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 2173 | } |
| 2174 | EXPORT_SYMBOL(wiphy_apply_custom_regulatory); |
| 2175 | |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2176 | static void reg_set_request_processed(void) |
| 2177 | { |
| 2178 | bool need_more_processing = false; |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2179 | struct regulatory_request *lr = get_last_request(); |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2180 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2181 | lr->processed = true; |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2182 | |
| 2183 | spin_lock(®_requests_lock); |
| 2184 | if (!list_empty(®_requests_list)) |
| 2185 | need_more_processing = true; |
| 2186 | spin_unlock(®_requests_lock); |
| 2187 | |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 2188 | cancel_crda_timeout(); |
Luis R. Rodriguez | a90c7a3 | 2011-04-05 10:49:04 -0700 | [diff] [blame] | 2189 | |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2190 | if (need_more_processing) |
| 2191 | schedule_work(®_work); |
| 2192 | } |
| 2193 | |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 2194 | /** |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2195 | * reg_process_hint_core - process core regulatory requests |
| 2196 | * @pending_request: a pending core regulatory request |
| 2197 | * |
| 2198 | * The wireless subsystem can use this function to process |
| 2199 | * a regulatory request issued by the regulatory core. |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2200 | */ |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2201 | static enum reg_request_treatment |
| 2202 | reg_process_hint_core(struct regulatory_request *core_request) |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2203 | { |
Johannes Berg | cecbb06 | 2015-10-15 08:47:34 +0200 | [diff] [blame] | 2204 | if (reg_query_database(core_request)) { |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2205 | core_request->intersect = false; |
| 2206 | core_request->processed = false; |
| 2207 | reg_update_last_request(core_request); |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2208 | return REG_REQ_OK; |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2209 | } |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2210 | |
| 2211 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2214 | static enum reg_request_treatment |
| 2215 | __reg_process_hint_user(struct regulatory_request *user_request) |
| 2216 | { |
| 2217 | struct regulatory_request *lr = get_last_request(); |
| 2218 | |
| 2219 | if (reg_request_cell_base(user_request)) |
| 2220 | return reg_ignore_cell_hint(user_request); |
| 2221 | |
| 2222 | if (reg_request_cell_base(lr)) |
| 2223 | return REG_REQ_IGNORE; |
| 2224 | |
| 2225 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) |
| 2226 | return REG_REQ_INTERSECT; |
| 2227 | /* |
| 2228 | * If the user knows better the user should set the regdom |
| 2229 | * to their country before the IE is picked up |
| 2230 | */ |
| 2231 | if (lr->initiator == NL80211_REGDOM_SET_BY_USER && |
| 2232 | lr->intersect) |
| 2233 | return REG_REQ_IGNORE; |
| 2234 | /* |
| 2235 | * Process user requests only after previous user/driver/core |
| 2236 | * requests have been processed |
| 2237 | */ |
| 2238 | if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE || |
| 2239 | lr->initiator == NL80211_REGDOM_SET_BY_DRIVER || |
| 2240 | lr->initiator == NL80211_REGDOM_SET_BY_USER) && |
| 2241 | regdom_changes(lr->alpha2)) |
| 2242 | return REG_REQ_IGNORE; |
| 2243 | |
| 2244 | if (!regdom_changes(user_request->alpha2)) |
| 2245 | return REG_REQ_ALREADY_SET; |
| 2246 | |
| 2247 | return REG_REQ_OK; |
| 2248 | } |
| 2249 | |
| 2250 | /** |
| 2251 | * reg_process_hint_user - process user regulatory requests |
| 2252 | * @user_request: a pending user regulatory request |
| 2253 | * |
| 2254 | * The wireless subsystem can use this function to process |
| 2255 | * a regulatory request initiated by userspace. |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2256 | */ |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2257 | static enum reg_request_treatment |
| 2258 | reg_process_hint_user(struct regulatory_request *user_request) |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2259 | { |
| 2260 | enum reg_request_treatment treatment; |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2261 | |
| 2262 | treatment = __reg_process_hint_user(user_request); |
| 2263 | if (treatment == REG_REQ_IGNORE || |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2264 | treatment == REG_REQ_ALREADY_SET) |
| 2265 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2266 | |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2267 | user_request->intersect = treatment == REG_REQ_INTERSECT; |
| 2268 | user_request->processed = false; |
Luis R. Rodriguez | 5ad6ef5 | 2013-11-05 09:18:08 -0800 | [diff] [blame] | 2269 | |
Johannes Berg | cecbb06 | 2015-10-15 08:47:34 +0200 | [diff] [blame] | 2270 | if (reg_query_database(user_request)) { |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2271 | reg_update_last_request(user_request); |
| 2272 | user_alpha2[0] = user_request->alpha2[0]; |
| 2273 | user_alpha2[1] = user_request->alpha2[1]; |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2274 | return REG_REQ_OK; |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2275 | } |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2276 | |
| 2277 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 0d97a61 | 2013-11-05 09:18:04 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2280 | static enum reg_request_treatment |
| 2281 | __reg_process_hint_driver(struct regulatory_request *driver_request) |
| 2282 | { |
| 2283 | struct regulatory_request *lr = get_last_request(); |
| 2284 | |
| 2285 | if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) { |
| 2286 | if (regdom_changes(driver_request->alpha2)) |
| 2287 | return REG_REQ_OK; |
| 2288 | return REG_REQ_ALREADY_SET; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * This would happen if you unplug and plug your card |
| 2293 | * back in or if you add a new device for which the previously |
| 2294 | * loaded card also agrees on the regulatory domain. |
| 2295 | */ |
| 2296 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
| 2297 | !regdom_changes(driver_request->alpha2)) |
| 2298 | return REG_REQ_ALREADY_SET; |
| 2299 | |
| 2300 | return REG_REQ_INTERSECT; |
| 2301 | } |
| 2302 | |
| 2303 | /** |
| 2304 | * reg_process_hint_driver - process driver regulatory requests |
| 2305 | * @driver_request: a pending driver regulatory request |
| 2306 | * |
| 2307 | * The wireless subsystem can use this function to process |
| 2308 | * a regulatory request issued by an 802.11 driver. |
| 2309 | * |
| 2310 | * Returns one of the different reg request treatment values. |
| 2311 | */ |
| 2312 | static enum reg_request_treatment |
| 2313 | reg_process_hint_driver(struct wiphy *wiphy, |
| 2314 | struct regulatory_request *driver_request) |
| 2315 | { |
Arik Nemtsov | 34f05f5 | 2014-12-04 12:22:16 +0200 | [diff] [blame] | 2316 | const struct ieee80211_regdomain *regd, *tmp; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2317 | enum reg_request_treatment treatment; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2318 | |
| 2319 | treatment = __reg_process_hint_driver(driver_request); |
| 2320 | |
| 2321 | switch (treatment) { |
| 2322 | case REG_REQ_OK: |
| 2323 | break; |
| 2324 | case REG_REQ_IGNORE: |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2325 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2326 | case REG_REQ_INTERSECT: |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2327 | case REG_REQ_ALREADY_SET: |
| 2328 | regd = reg_copy_regd(get_cfg80211_regdom()); |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2329 | if (IS_ERR(regd)) |
| 2330 | return REG_REQ_IGNORE; |
Arik Nemtsov | 34f05f5 | 2014-12-04 12:22:16 +0200 | [diff] [blame] | 2331 | |
| 2332 | tmp = get_wiphy_regdom(wiphy); |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2333 | rcu_assign_pointer(wiphy->regd, regd); |
Arik Nemtsov | 34f05f5 | 2014-12-04 12:22:16 +0200 | [diff] [blame] | 2334 | rcu_free_regdom(tmp); |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2337 | |
| 2338 | driver_request->intersect = treatment == REG_REQ_INTERSECT; |
| 2339 | driver_request->processed = false; |
Luis R. Rodriguez | 5ad6ef5 | 2013-11-05 09:18:08 -0800 | [diff] [blame] | 2340 | |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2341 | /* |
| 2342 | * Since CRDA will not be called in this case as we already |
| 2343 | * have applied the requested regulatory domain before we just |
| 2344 | * inform userspace we have processed the request |
| 2345 | */ |
| 2346 | if (treatment == REG_REQ_ALREADY_SET) { |
| 2347 | nl80211_send_reg_change_event(driver_request); |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2348 | reg_update_last_request(driver_request); |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2349 | reg_set_request_processed(); |
Johannes Berg | 480908a | 2015-10-15 12:58:58 +0200 | [diff] [blame] | 2350 | return REG_REQ_ALREADY_SET; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2353 | if (reg_query_database(driver_request)) { |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2354 | reg_update_last_request(driver_request); |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2355 | return REG_REQ_OK; |
| 2356 | } |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2357 | |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2358 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2361 | static enum reg_request_treatment |
| 2362 | __reg_process_hint_country_ie(struct wiphy *wiphy, |
| 2363 | struct regulatory_request *country_ie_request) |
| 2364 | { |
| 2365 | struct wiphy *last_wiphy = NULL; |
| 2366 | struct regulatory_request *lr = get_last_request(); |
| 2367 | |
| 2368 | if (reg_request_cell_base(lr)) { |
| 2369 | /* Trust a Cell base station over the AP's country IE */ |
| 2370 | if (regdom_changes(country_ie_request->alpha2)) |
| 2371 | return REG_REQ_IGNORE; |
| 2372 | return REG_REQ_ALREADY_SET; |
Luis R. Rodriguez | 2a90146 | 2013-11-11 22:15:31 +0100 | [diff] [blame] | 2373 | } else { |
| 2374 | if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE) |
| 2375 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2376 | } |
| 2377 | |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2378 | if (unlikely(!is_an_alpha2(country_ie_request->alpha2))) |
| 2379 | return -EINVAL; |
Luis R. Rodriguez | 2f1c6c5 | 2013-11-05 09:18:07 -0800 | [diff] [blame] | 2380 | |
| 2381 | if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) |
| 2382 | return REG_REQ_OK; |
| 2383 | |
| 2384 | last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); |
| 2385 | |
| 2386 | if (last_wiphy != wiphy) { |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2387 | /* |
Luis R. Rodriguez | 2f1c6c5 | 2013-11-05 09:18:07 -0800 | [diff] [blame] | 2388 | * Two cards with two APs claiming different |
| 2389 | * Country IE alpha2s. We could |
| 2390 | * intersect them, but that seems unlikely |
| 2391 | * to be correct. Reject second one for now. |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2392 | */ |
Luis R. Rodriguez | 2f1c6c5 | 2013-11-05 09:18:07 -0800 | [diff] [blame] | 2393 | if (regdom_changes(country_ie_request->alpha2)) |
| 2394 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2395 | return REG_REQ_ALREADY_SET; |
| 2396 | } |
Emmanuel Grumbach | 70dcec5 | 2014-12-02 09:53:25 +0200 | [diff] [blame] | 2397 | |
| 2398 | if (regdom_changes(country_ie_request->alpha2)) |
Luis R. Rodriguez | 2f1c6c5 | 2013-11-05 09:18:07 -0800 | [diff] [blame] | 2399 | return REG_REQ_OK; |
| 2400 | return REG_REQ_ALREADY_SET; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2403 | /** |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2404 | * reg_process_hint_country_ie - process regulatory requests from country IEs |
| 2405 | * @country_ie_request: a regulatory request from a country IE |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 2406 | * |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2407 | * The wireless subsystem can use this function to process |
| 2408 | * a regulatory request issued by a country Information Element. |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 2409 | * |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 2410 | * Returns one of the different reg request treatment values. |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 2411 | */ |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 2412 | static enum reg_request_treatment |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2413 | reg_process_hint_country_ie(struct wiphy *wiphy, |
| 2414 | struct regulatory_request *country_ie_request) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2415 | { |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 2416 | enum reg_request_treatment treatment; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2417 | |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2418 | treatment = __reg_process_hint_country_ie(wiphy, country_ie_request); |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 2419 | |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 2420 | switch (treatment) { |
Johannes Berg | 2f92212 | 2012-12-03 17:54:55 +0100 | [diff] [blame] | 2421 | case REG_REQ_OK: |
| 2422 | break; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2423 | case REG_REQ_IGNORE: |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2424 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2425 | case REG_REQ_ALREADY_SET: |
Arik Nemtsov | c888393 | 2014-04-21 20:39:34 -0700 | [diff] [blame] | 2426 | reg_free_request(country_ie_request); |
Johannes Berg | 480908a | 2015-10-15 12:58:58 +0200 | [diff] [blame] | 2427 | return REG_REQ_ALREADY_SET; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2428 | case REG_REQ_INTERSECT: |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2429 | /* |
| 2430 | * This doesn't happen yet, not sure we |
| 2431 | * ever want to support it for this case. |
| 2432 | */ |
| 2433 | WARN_ONCE(1, "Unexpected intersection for country IEs"); |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2434 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2435 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2436 | |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2437 | country_ie_request->intersect = false; |
| 2438 | country_ie_request->processed = false; |
Luis R. Rodriguez | 5ad6ef5 | 2013-11-05 09:18:08 -0800 | [diff] [blame] | 2439 | |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2440 | if (reg_query_database(country_ie_request)) { |
Johannes Berg | 25b20db | 2015-10-15 12:05:05 +0200 | [diff] [blame] | 2441 | reg_update_last_request(country_ie_request); |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2442 | return REG_REQ_OK; |
| 2443 | } |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 2444 | |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2445 | return REG_REQ_IGNORE; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 2448 | bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2) |
| 2449 | { |
| 2450 | const struct ieee80211_regdomain *wiphy1_regd = NULL; |
| 2451 | const struct ieee80211_regdomain *wiphy2_regd = NULL; |
| 2452 | const struct ieee80211_regdomain *cfg80211_regd = NULL; |
| 2453 | bool dfs_domain_same; |
| 2454 | |
| 2455 | rcu_read_lock(); |
| 2456 | |
| 2457 | cfg80211_regd = rcu_dereference(cfg80211_regdomain); |
| 2458 | wiphy1_regd = rcu_dereference(wiphy1->regd); |
| 2459 | if (!wiphy1_regd) |
| 2460 | wiphy1_regd = cfg80211_regd; |
| 2461 | |
| 2462 | wiphy2_regd = rcu_dereference(wiphy2->regd); |
| 2463 | if (!wiphy2_regd) |
| 2464 | wiphy2_regd = cfg80211_regd; |
| 2465 | |
| 2466 | dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region; |
| 2467 | |
| 2468 | rcu_read_unlock(); |
| 2469 | |
| 2470 | return dfs_domain_same; |
| 2471 | } |
| 2472 | |
| 2473 | static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan, |
| 2474 | struct ieee80211_channel *src_chan) |
| 2475 | { |
| 2476 | if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) || |
| 2477 | !(src_chan->flags & IEEE80211_CHAN_RADAR)) |
| 2478 | return; |
| 2479 | |
| 2480 | if (dst_chan->flags & IEEE80211_CHAN_DISABLED || |
| 2481 | src_chan->flags & IEEE80211_CHAN_DISABLED) |
| 2482 | return; |
| 2483 | |
| 2484 | if (src_chan->center_freq == dst_chan->center_freq && |
| 2485 | dst_chan->dfs_state == NL80211_DFS_USABLE) { |
| 2486 | dst_chan->dfs_state = src_chan->dfs_state; |
| 2487 | dst_chan->dfs_state_entered = src_chan->dfs_state_entered; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy, |
| 2492 | struct wiphy *src_wiphy) |
| 2493 | { |
| 2494 | struct ieee80211_supported_band *src_sband, *dst_sband; |
| 2495 | struct ieee80211_channel *src_chan, *dst_chan; |
| 2496 | int i, j, band; |
| 2497 | |
| 2498 | if (!reg_dfs_domain_same(dst_wiphy, src_wiphy)) |
| 2499 | return; |
| 2500 | |
| 2501 | for (band = 0; band < NUM_NL80211_BANDS; band++) { |
| 2502 | dst_sband = dst_wiphy->bands[band]; |
| 2503 | src_sband = src_wiphy->bands[band]; |
| 2504 | if (!dst_sband || !src_sband) |
| 2505 | continue; |
| 2506 | |
| 2507 | for (i = 0; i < dst_sband->n_channels; i++) { |
| 2508 | dst_chan = &dst_sband->channels[i]; |
| 2509 | for (j = 0; j < src_sband->n_channels; j++) { |
| 2510 | src_chan = &src_sband->channels[j]; |
| 2511 | reg_copy_dfs_chan_state(dst_chan, src_chan); |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy) |
| 2518 | { |
| 2519 | struct cfg80211_registered_device *rdev; |
| 2520 | |
| 2521 | ASSERT_RTNL(); |
| 2522 | |
| 2523 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 2524 | if (wiphy == &rdev->wiphy) |
| 2525 | continue; |
| 2526 | wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy); |
| 2527 | } |
| 2528 | } |
| 2529 | |
Luis R. Rodriguez | 30a548c | 2009-05-02 01:17:27 -0400 | [diff] [blame] | 2530 | /* This processes *all* regulatory hints */ |
Luis R. Rodriguez | 1daa37c | 2013-11-05 09:18:02 -0800 | [diff] [blame] | 2531 | static void reg_process_hint(struct regulatory_request *reg_request) |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2532 | { |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2533 | struct wiphy *wiphy = NULL; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2534 | enum reg_request_treatment treatment; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2535 | |
Johannes Berg | f417376 | 2012-12-03 18:23:37 +0100 | [diff] [blame] | 2536 | if (reg_request->wiphy_idx != WIPHY_IDX_INVALID) |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2537 | wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); |
| 2538 | |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2539 | switch (reg_request->initiator) { |
| 2540 | case NL80211_REGDOM_SET_BY_CORE: |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2541 | treatment = reg_process_hint_core(reg_request); |
| 2542 | break; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2543 | case NL80211_REGDOM_SET_BY_USER: |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2544 | treatment = reg_process_hint_user(reg_request); |
| 2545 | break; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2546 | case NL80211_REGDOM_SET_BY_DRIVER: |
Ilan Peer | 772f038 | 2014-01-14 15:17:23 +0200 | [diff] [blame] | 2547 | if (!wiphy) |
| 2548 | goto out_free; |
Luis R. Rodriguez | 21636c7 | 2013-11-05 09:18:05 -0800 | [diff] [blame] | 2549 | treatment = reg_process_hint_driver(wiphy, reg_request); |
| 2550 | break; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2551 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
Ilan Peer | 772f038 | 2014-01-14 15:17:23 +0200 | [diff] [blame] | 2552 | if (!wiphy) |
| 2553 | goto out_free; |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2554 | treatment = reg_process_hint_country_ie(wiphy, reg_request); |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2555 | break; |
| 2556 | default: |
| 2557 | WARN(1, "invalid initiator %d\n", reg_request->initiator); |
Ilan Peer | 772f038 | 2014-01-14 15:17:23 +0200 | [diff] [blame] | 2558 | goto out_free; |
Luis R. Rodriguez | b3eb7f3 | 2013-11-05 09:18:03 -0800 | [diff] [blame] | 2559 | } |
| 2560 | |
Johannes Berg | d34265a | 2015-10-15 13:05:55 +0200 | [diff] [blame] | 2561 | if (treatment == REG_REQ_IGNORE) |
| 2562 | goto out_free; |
| 2563 | |
Johannes Berg | 480908a | 2015-10-15 12:58:58 +0200 | [diff] [blame] | 2564 | WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET, |
| 2565 | "unexpected treatment value %d\n", treatment); |
| 2566 | |
John Linville | 841b351 | 2015-06-24 11:42:25 -0400 | [diff] [blame] | 2567 | /* This is required so that the orig_* parameters are saved. |
| 2568 | * NOTE: treatment must be set for any case that reaches here! |
| 2569 | */ |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2570 | if (treatment == REG_REQ_ALREADY_SET && wiphy && |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 2571 | wiphy->regulatory_flags & REGULATORY_STRICT_REG) { |
Luis R. Rodriguez | b23e7a9 | 2013-11-05 09:18:06 -0800 | [diff] [blame] | 2572 | wiphy_update_regulatory(wiphy, reg_request->initiator); |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 2573 | wiphy_all_share_dfs_chan_state(wiphy); |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 2574 | reg_check_channels(); |
| 2575 | } |
Ilan Peer | 772f038 | 2014-01-14 15:17:23 +0200 | [diff] [blame] | 2576 | |
| 2577 | return; |
| 2578 | |
| 2579 | out_free: |
Arik Nemtsov | c888393 | 2014-04-21 20:39:34 -0700 | [diff] [blame] | 2580 | reg_free_request(reg_request); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2581 | } |
| 2582 | |
Arik Nemtsov | ef51fb1 | 2015-01-07 16:47:20 +0200 | [diff] [blame] | 2583 | static bool reg_only_self_managed_wiphys(void) |
| 2584 | { |
| 2585 | struct cfg80211_registered_device *rdev; |
| 2586 | struct wiphy *wiphy; |
| 2587 | bool self_managed_found = false; |
| 2588 | |
| 2589 | ASSERT_RTNL(); |
| 2590 | |
| 2591 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 2592 | wiphy = &rdev->wiphy; |
| 2593 | if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) |
| 2594 | self_managed_found = true; |
| 2595 | else |
| 2596 | return false; |
| 2597 | } |
| 2598 | |
| 2599 | /* make sure at least one self-managed wiphy exists */ |
| 2600 | return self_managed_found; |
| 2601 | } |
| 2602 | |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2603 | /* |
| 2604 | * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* |
| 2605 | * Regulatory hints come on a first come first serve basis and we |
| 2606 | * must process each one atomically. |
| 2607 | */ |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2608 | static void reg_process_pending_hints(void) |
Luis R. Rodriguez | b0e2880 | 2010-11-17 21:46:08 -0800 | [diff] [blame] | 2609 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2610 | struct regulatory_request *reg_request, *lr; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2611 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2612 | lr = get_last_request(); |
Luis R. Rodriguez | b0e2880 | 2010-11-17 21:46:08 -0800 | [diff] [blame] | 2613 | |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2614 | /* When last_request->processed becomes true this will be rescheduled */ |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2615 | if (lr && !lr->processed) { |
Luis R. Rodriguez | 96cce12 | 2014-04-21 20:39:35 -0700 | [diff] [blame] | 2616 | reg_process_hint(lr); |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2617 | return; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2618 | } |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2619 | |
| 2620 | spin_lock(®_requests_lock); |
| 2621 | |
| 2622 | if (list_empty(®_requests_list)) { |
| 2623 | spin_unlock(®_requests_lock); |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2624 | return; |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | reg_request = list_first_entry(®_requests_list, |
| 2628 | struct regulatory_request, |
| 2629 | list); |
| 2630 | list_del_init(®_request->list); |
| 2631 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2632 | spin_unlock(®_requests_lock); |
Luis R. Rodriguez | b0e2880 | 2010-11-17 21:46:08 -0800 | [diff] [blame] | 2633 | |
Arik Nemtsov | ef51fb1 | 2015-01-07 16:47:20 +0200 | [diff] [blame] | 2634 | if (reg_only_self_managed_wiphys()) { |
| 2635 | reg_free_request(reg_request); |
| 2636 | return; |
| 2637 | } |
| 2638 | |
Luis R. Rodriguez | 1daa37c | 2013-11-05 09:18:02 -0800 | [diff] [blame] | 2639 | reg_process_hint(reg_request); |
Ben | 2e54a68 | 2015-03-12 09:37:34 -0400 | [diff] [blame] | 2640 | |
| 2641 | lr = get_last_request(); |
| 2642 | |
| 2643 | spin_lock(®_requests_lock); |
| 2644 | if (!list_empty(®_requests_list) && lr && lr->processed) |
| 2645 | schedule_work(®_work); |
| 2646 | spin_unlock(®_requests_lock); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2647 | } |
| 2648 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2649 | /* Processes beacon hints -- this has nothing to do with country IEs */ |
| 2650 | static void reg_process_pending_beacon_hints(void) |
| 2651 | { |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 2652 | struct cfg80211_registered_device *rdev; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2653 | struct reg_beacon *pending_beacon, *tmp; |
| 2654 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2655 | /* This goes through the _pending_ beacon list */ |
| 2656 | spin_lock_bh(®_pending_beacons_lock); |
| 2657 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2658 | list_for_each_entry_safe(pending_beacon, tmp, |
| 2659 | ®_pending_beacons, list) { |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2660 | list_del_init(&pending_beacon->list); |
| 2661 | |
| 2662 | /* Applies the beacon hint to current wiphys */ |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 2663 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) |
| 2664 | wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2665 | |
| 2666 | /* Remembers the beacon hint for new wiphys or reg changes */ |
| 2667 | list_add_tail(&pending_beacon->list, ®_beacon_list); |
| 2668 | } |
| 2669 | |
| 2670 | spin_unlock_bh(®_pending_beacons_lock); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2671 | } |
| 2672 | |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 2673 | static void reg_process_self_managed_hints(void) |
| 2674 | { |
| 2675 | struct cfg80211_registered_device *rdev; |
| 2676 | struct wiphy *wiphy; |
| 2677 | const struct ieee80211_regdomain *tmp; |
| 2678 | const struct ieee80211_regdomain *regd; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2679 | enum nl80211_band band; |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 2680 | struct regulatory_request request = {}; |
| 2681 | |
| 2682 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 2683 | wiphy = &rdev->wiphy; |
| 2684 | |
| 2685 | spin_lock(®_requests_lock); |
| 2686 | regd = rdev->requested_regd; |
| 2687 | rdev->requested_regd = NULL; |
| 2688 | spin_unlock(®_requests_lock); |
| 2689 | |
| 2690 | if (regd == NULL) |
| 2691 | continue; |
| 2692 | |
| 2693 | tmp = get_wiphy_regdom(wiphy); |
| 2694 | rcu_assign_pointer(wiphy->regd, regd); |
| 2695 | rcu_free_regdom(tmp); |
| 2696 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2697 | for (band = 0; band < NUM_NL80211_BANDS; band++) |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 2698 | handle_band_custom(wiphy, wiphy->bands[band], regd); |
| 2699 | |
| 2700 | reg_process_ht_flags(wiphy); |
| 2701 | |
| 2702 | request.wiphy_idx = get_wiphy_idx(wiphy); |
| 2703 | request.alpha2[0] = regd->alpha2[0]; |
| 2704 | request.alpha2[1] = regd->alpha2[1]; |
| 2705 | request.initiator = NL80211_REGDOM_SET_BY_DRIVER; |
| 2706 | |
| 2707 | nl80211_send_wiphy_reg_change_event(&request); |
| 2708 | } |
| 2709 | |
| 2710 | reg_check_channels(); |
| 2711 | } |
| 2712 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2713 | static void reg_todo(struct work_struct *work) |
| 2714 | { |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2715 | rtnl_lock(); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2716 | reg_process_pending_hints(); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2717 | reg_process_pending_beacon_hints(); |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 2718 | reg_process_self_managed_hints(); |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2719 | rtnl_unlock(); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2720 | } |
| 2721 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2722 | static void queue_regulatory_request(struct regulatory_request *request) |
| 2723 | { |
Johannes Berg | d4f2c88 | 2012-12-03 18:59:58 +0100 | [diff] [blame] | 2724 | request->alpha2[0] = toupper(request->alpha2[0]); |
| 2725 | request->alpha2[1] = toupper(request->alpha2[1]); |
John W. Linville | c61029c | 2010-08-05 14:26:24 -0400 | [diff] [blame] | 2726 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2727 | spin_lock(®_requests_lock); |
| 2728 | list_add_tail(&request->list, ®_requests_list); |
| 2729 | spin_unlock(®_requests_lock); |
| 2730 | |
| 2731 | schedule_work(®_work); |
| 2732 | } |
| 2733 | |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2734 | /* |
| 2735 | * Core regulatory hint -- happens during cfg80211_init() |
| 2736 | * and when we restore regulatory settings. |
| 2737 | */ |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 2738 | static int regulatory_hint_core(const char *alpha2) |
| 2739 | { |
| 2740 | struct regulatory_request *request; |
| 2741 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 2742 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 2743 | if (!request) |
| 2744 | return -ENOMEM; |
| 2745 | |
| 2746 | request->alpha2[0] = alpha2[0]; |
| 2747 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2748 | request->initiator = NL80211_REGDOM_SET_BY_CORE; |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 2749 | |
Luis R. Rodriguez | 31e9972 | 2010-11-17 21:46:06 -0800 | [diff] [blame] | 2750 | queue_regulatory_request(request); |
Luis R. Rodriguez | 5078b2e | 2009-05-13 17:04:42 -0400 | [diff] [blame] | 2751 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2752 | return 0; |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 2753 | } |
| 2754 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2755 | /* User hints */ |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 2756 | int regulatory_hint_user(const char *alpha2, |
| 2757 | enum nl80211_user_reg_hint_type user_reg_hint_type) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2758 | { |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2759 | struct regulatory_request *request; |
| 2760 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2761 | if (WARN_ON(!alpha2)) |
| 2762 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2763 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2764 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
| 2765 | if (!request) |
| 2766 | return -ENOMEM; |
| 2767 | |
Johannes Berg | f417376 | 2012-12-03 18:23:37 +0100 | [diff] [blame] | 2768 | request->wiphy_idx = WIPHY_IDX_INVALID; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2769 | request->alpha2[0] = alpha2[0]; |
| 2770 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | e12822e | 2010-01-04 11:37:39 -0500 | [diff] [blame] | 2771 | request->initiator = NL80211_REGDOM_SET_BY_USER; |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 2772 | request->user_reg_hint_type = user_reg_hint_type; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2773 | |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2774 | /* Allow calling CRDA again */ |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 2775 | reset_crda_timeouts(); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2776 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2777 | queue_regulatory_request(request); |
| 2778 | |
| 2779 | return 0; |
| 2780 | } |
| 2781 | |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2782 | int regulatory_hint_indoor(bool is_indoor, u32 portid) |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 2783 | { |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2784 | spin_lock(®_indoor_lock); |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 2785 | |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2786 | /* It is possible that more than one user space process is trying to |
| 2787 | * configure the indoor setting. To handle such cases, clear the indoor |
| 2788 | * setting in case that some process does not think that the device |
| 2789 | * is operating in an indoor environment. In addition, if a user space |
| 2790 | * process indicates that it is controlling the indoor setting, save its |
| 2791 | * portid, i.e., make it the owner. |
| 2792 | */ |
| 2793 | reg_is_indoor = is_indoor; |
| 2794 | if (reg_is_indoor) { |
| 2795 | if (!reg_is_indoor_portid) |
| 2796 | reg_is_indoor_portid = portid; |
| 2797 | } else { |
| 2798 | reg_is_indoor_portid = 0; |
| 2799 | } |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 2800 | |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2801 | spin_unlock(®_indoor_lock); |
| 2802 | |
| 2803 | if (!is_indoor) |
| 2804 | reg_check_channels(); |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 2805 | |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2809 | void regulatory_netlink_notify(u32 portid) |
| 2810 | { |
| 2811 | spin_lock(®_indoor_lock); |
| 2812 | |
| 2813 | if (reg_is_indoor_portid != portid) { |
| 2814 | spin_unlock(®_indoor_lock); |
| 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | reg_is_indoor = false; |
| 2819 | reg_is_indoor_portid = 0; |
| 2820 | |
| 2821 | spin_unlock(®_indoor_lock); |
| 2822 | |
| 2823 | reg_check_channels(); |
| 2824 | } |
| 2825 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2826 | /* Driver hints */ |
| 2827 | int regulatory_hint(struct wiphy *wiphy, const char *alpha2) |
| 2828 | { |
| 2829 | struct regulatory_request *request; |
| 2830 | |
Johannes Berg | fdc9d7b | 2012-12-03 18:36:09 +0100 | [diff] [blame] | 2831 | if (WARN_ON(!alpha2 || !wiphy)) |
| 2832 | return -EINVAL; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2833 | |
Luis R. Rodriguez | 4f7b914 | 2013-12-14 20:09:06 +0100 | [diff] [blame] | 2834 | wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG; |
| 2835 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2836 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
| 2837 | if (!request) |
| 2838 | return -ENOMEM; |
| 2839 | |
| 2840 | request->wiphy_idx = get_wiphy_idx(wiphy); |
| 2841 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2842 | request->alpha2[0] = alpha2[0]; |
| 2843 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2844 | request->initiator = NL80211_REGDOM_SET_BY_DRIVER; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2845 | |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2846 | /* Allow calling CRDA again */ |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 2847 | reset_crda_timeouts(); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2848 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2849 | queue_regulatory_request(request); |
| 2850 | |
| 2851 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2852 | } |
| 2853 | EXPORT_SYMBOL(regulatory_hint); |
| 2854 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2855 | void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band, |
Luis R. Rodriguez | 789fd03 | 2013-10-04 18:07:24 -0700 | [diff] [blame] | 2856 | const u8 *country_ie, u8 country_ie_len) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2857 | { |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2858 | char alpha2[2]; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2859 | enum environment_cap env = ENVIRON_ANY; |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2860 | struct regulatory_request *request = NULL, *lr; |
Luis R. Rodriguez | d335fe6 | 2009-02-21 00:04:27 -0500 | [diff] [blame] | 2861 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2862 | /* IE len must be evenly divisible by 2 */ |
| 2863 | if (country_ie_len & 0x01) |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2864 | return; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2865 | |
| 2866 | if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2867 | return; |
| 2868 | |
| 2869 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 2870 | if (!request) |
| 2871 | return; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2872 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2873 | alpha2[0] = country_ie[0]; |
| 2874 | alpha2[1] = country_ie[1]; |
| 2875 | |
| 2876 | if (country_ie[2] == 'I') |
| 2877 | env = ENVIRON_INDOOR; |
| 2878 | else if (country_ie[2] == 'O') |
| 2879 | env = ENVIRON_OUTDOOR; |
| 2880 | |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2881 | rcu_read_lock(); |
| 2882 | lr = get_last_request(); |
| 2883 | |
| 2884 | if (unlikely(!lr)) |
| 2885 | goto out; |
| 2886 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2887 | /* |
Luis R. Rodriguez | 8b19e6c | 2009-07-30 17:38:09 -0700 | [diff] [blame] | 2888 | * We will run this only upon a successful connection on cfg80211. |
Luis R. Rodriguez | 4b44c8b | 2009-07-30 17:38:07 -0700 | [diff] [blame] | 2889 | * We leave conflict resolution to the workqueue, where can hold |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2890 | * the RTNL. |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2891 | */ |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 2892 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && |
| 2893 | lr->wiphy_idx != WIPHY_IDX_INVALID) |
Luis R. Rodriguez | 4b44c8b | 2009-07-30 17:38:07 -0700 | [diff] [blame] | 2894 | goto out; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2895 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2896 | request->wiphy_idx = get_wiphy_idx(wiphy); |
John W. Linville | 4f366c5 | 2010-07-15 14:57:33 -0400 | [diff] [blame] | 2897 | request->alpha2[0] = alpha2[0]; |
| 2898 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2899 | request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2900 | request->country_ie_env = env; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2901 | |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2902 | /* Allow calling CRDA again */ |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 2903 | reset_crda_timeouts(); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 2904 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2905 | queue_regulatory_request(request); |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2906 | request = NULL; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2907 | out: |
Johannes Berg | db2424c | 2013-05-10 19:07:52 +0200 | [diff] [blame] | 2908 | kfree(request); |
| 2909 | rcu_read_unlock(); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2910 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2911 | |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2912 | static void restore_alpha2(char *alpha2, bool reset_user) |
| 2913 | { |
| 2914 | /* indicates there is no alpha2 to consider for restoration */ |
| 2915 | alpha2[0] = '9'; |
| 2916 | alpha2[1] = '7'; |
| 2917 | |
| 2918 | /* The user setting has precedence over the module parameter */ |
| 2919 | if (is_user_regdom_saved()) { |
| 2920 | /* Unless we're asked to ignore it and reset it */ |
| 2921 | if (reset_user) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2922 | pr_debug("Restoring regulatory settings including user preference\n"); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2923 | user_alpha2[0] = '9'; |
| 2924 | user_alpha2[1] = '7'; |
| 2925 | |
| 2926 | /* |
| 2927 | * If we're ignoring user settings, we still need to |
| 2928 | * check the module parameter to ensure we put things |
| 2929 | * back as they were for a full restore. |
| 2930 | */ |
| 2931 | if (!is_world_regdom(ieee80211_regdom)) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2932 | pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", |
| 2933 | ieee80211_regdom[0], ieee80211_regdom[1]); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2934 | alpha2[0] = ieee80211_regdom[0]; |
| 2935 | alpha2[1] = ieee80211_regdom[1]; |
| 2936 | } |
| 2937 | } else { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2938 | pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n", |
| 2939 | user_alpha2[0], user_alpha2[1]); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2940 | alpha2[0] = user_alpha2[0]; |
| 2941 | alpha2[1] = user_alpha2[1]; |
| 2942 | } |
| 2943 | } else if (!is_world_regdom(ieee80211_regdom)) { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2944 | pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", |
| 2945 | ieee80211_regdom[0], ieee80211_regdom[1]); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2946 | alpha2[0] = ieee80211_regdom[0]; |
| 2947 | alpha2[1] = ieee80211_regdom[1]; |
| 2948 | } else |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 2949 | pr_debug("Restoring regulatory settings\n"); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2950 | } |
| 2951 | |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 2952 | static void restore_custom_reg_settings(struct wiphy *wiphy) |
| 2953 | { |
| 2954 | struct ieee80211_supported_band *sband; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2955 | enum nl80211_band band; |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 2956 | struct ieee80211_channel *chan; |
| 2957 | int i; |
| 2958 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 2959 | for (band = 0; band < NUM_NL80211_BANDS; band++) { |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 2960 | sband = wiphy->bands[band]; |
| 2961 | if (!sband) |
| 2962 | continue; |
| 2963 | for (i = 0; i < sband->n_channels; i++) { |
| 2964 | chan = &sband->channels[i]; |
| 2965 | chan->flags = chan->orig_flags; |
| 2966 | chan->max_antenna_gain = chan->orig_mag; |
| 2967 | chan->max_power = chan->orig_mpwr; |
Paul Stewart | 899852a | 2012-08-01 16:54:42 -0700 | [diff] [blame] | 2968 | chan->beacon_found = false; |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2973 | /* |
| 2974 | * Restoring regulatory settings involves ingoring any |
| 2975 | * possibly stale country IE information and user regulatory |
| 2976 | * settings if so desired, this includes any beacon hints |
| 2977 | * learned as we could have traveled outside to another country |
| 2978 | * after disconnection. To restore regulatory settings we do |
| 2979 | * exactly what we did at bootup: |
| 2980 | * |
| 2981 | * - send a core regulatory hint |
| 2982 | * - send a user regulatory hint if applicable |
| 2983 | * |
| 2984 | * Device drivers that send a regulatory hint for a specific country |
| 2985 | * keep their own regulatory domain on wiphy->regd so that does does |
| 2986 | * not need to be remembered. |
| 2987 | */ |
| 2988 | static void restore_regulatory_settings(bool reset_user) |
| 2989 | { |
| 2990 | char alpha2[2]; |
Dmitry Shmidt | cee0bec | 2011-12-19 12:32:21 -0800 | [diff] [blame] | 2991 | char world_alpha2[2]; |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2992 | struct reg_beacon *reg_beacon, *btmp; |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 2993 | LIST_HEAD(tmp_reg_req_list); |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 2994 | struct cfg80211_registered_device *rdev; |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 2995 | |
Johannes Berg | 5fe231e | 2013-05-08 21:45:15 +0200 | [diff] [blame] | 2996 | ASSERT_RTNL(); |
| 2997 | |
Ilan peer | 0505075 | 2015-03-04 00:32:06 -0500 | [diff] [blame] | 2998 | /* |
| 2999 | * Clear the indoor setting in case that it is not controlled by user |
| 3000 | * space, as otherwise there is no guarantee that the device is still |
| 3001 | * operating in an indoor environment. |
| 3002 | */ |
| 3003 | spin_lock(®_indoor_lock); |
| 3004 | if (reg_is_indoor && !reg_is_indoor_portid) { |
| 3005 | reg_is_indoor = false; |
| 3006 | reg_check_channels(); |
| 3007 | } |
| 3008 | spin_unlock(®_indoor_lock); |
Ilan Peer | 52616f2 | 2014-02-25 16:26:00 +0200 | [diff] [blame] | 3009 | |
Johannes Berg | 2d31986 | 2013-01-09 12:01:38 +0100 | [diff] [blame] | 3010 | reset_regdomains(true, &world_regdom); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3011 | restore_alpha2(alpha2, reset_user); |
| 3012 | |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 3013 | /* |
| 3014 | * If there's any pending requests we simply |
| 3015 | * stash them to a temporary pending queue and |
| 3016 | * add then after we've restored regulatory |
| 3017 | * settings. |
| 3018 | */ |
| 3019 | spin_lock(®_requests_lock); |
Ilan peer | eeca9fc | 2015-03-04 00:32:07 -0500 | [diff] [blame] | 3020 | list_splice_tail_init(®_requests_list, &tmp_reg_req_list); |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 3021 | spin_unlock(®_requests_lock); |
| 3022 | |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3023 | /* Clear beacon hints */ |
| 3024 | spin_lock_bh(®_pending_beacons_lock); |
Johannes Berg | fea9bce | 2012-12-03 17:32:01 +0100 | [diff] [blame] | 3025 | list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { |
| 3026 | list_del(®_beacon->list); |
| 3027 | kfree(reg_beacon); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3028 | } |
| 3029 | spin_unlock_bh(®_pending_beacons_lock); |
| 3030 | |
Johannes Berg | fea9bce | 2012-12-03 17:32:01 +0100 | [diff] [blame] | 3031 | list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { |
| 3032 | list_del(®_beacon->list); |
| 3033 | kfree(reg_beacon); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | /* First restore to the basic regulatory settings */ |
Johannes Berg | 379b82f | 2012-12-06 15:44:07 +0100 | [diff] [blame] | 3037 | world_alpha2[0] = cfg80211_world_regdom->alpha2[0]; |
| 3038 | world_alpha2[1] = cfg80211_world_regdom->alpha2[1]; |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3039 | |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 3040 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 3041 | if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) |
| 3042 | continue; |
Luis R. Rodriguez | a2f73b6 | 2013-11-11 22:15:29 +0100 | [diff] [blame] | 3043 | if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG) |
Rajkumar Manoharan | 5ce543d | 2011-12-07 21:50:08 +0530 | [diff] [blame] | 3044 | restore_custom_reg_settings(&rdev->wiphy); |
| 3045 | } |
| 3046 | |
Dmitry Shmidt | cee0bec | 2011-12-19 12:32:21 -0800 | [diff] [blame] | 3047 | regulatory_hint_core(world_alpha2); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3048 | |
| 3049 | /* |
| 3050 | * This restores the ieee80211_regdom module parameter |
| 3051 | * preference or the last user requested regulatory |
| 3052 | * settings, user regulatory settings takes precedence. |
| 3053 | */ |
| 3054 | if (is_an_alpha2(alpha2)) |
Maciej S. Szmigiero | 549cc1c | 2015-09-02 19:00:31 +0200 | [diff] [blame] | 3055 | regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3056 | |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 3057 | spin_lock(®_requests_lock); |
Johannes Berg | 11cff96 | 2012-12-03 18:56:41 +0100 | [diff] [blame] | 3058 | list_splice_tail_init(&tmp_reg_req_list, ®_requests_list); |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 3059 | spin_unlock(®_requests_lock); |
| 3060 | |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 3061 | pr_debug("Kicking the queue\n"); |
Luis R. Rodriguez | 1460955 | 2011-04-05 10:49:03 -0700 | [diff] [blame] | 3062 | |
| 3063 | schedule_work(®_work); |
| 3064 | } |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3065 | |
| 3066 | void regulatory_hint_disconnect(void) |
| 3067 | { |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 3068 | pr_debug("All devices are disconnected, going to restore regulatory settings\n"); |
Luis R. Rodriguez | 09d989d | 2010-01-29 19:58:57 -0500 | [diff] [blame] | 3069 | restore_regulatory_settings(false); |
| 3070 | } |
| 3071 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3072 | static bool freq_is_chan_12_13_14(u16 freq) |
| 3073 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 3074 | if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) || |
| 3075 | freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) || |
| 3076 | freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ)) |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3077 | return true; |
| 3078 | return false; |
| 3079 | } |
| 3080 | |
Luis R. Rodriguez | 3ebfa6e | 2012-12-19 10:53:02 -0800 | [diff] [blame] | 3081 | static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan) |
| 3082 | { |
| 3083 | struct reg_beacon *pending_beacon; |
| 3084 | |
| 3085 | list_for_each_entry(pending_beacon, ®_pending_beacons, list) |
| 3086 | if (beacon_chan->center_freq == |
| 3087 | pending_beacon->chan.center_freq) |
| 3088 | return true; |
| 3089 | return false; |
| 3090 | } |
| 3091 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3092 | int regulatory_hint_found_beacon(struct wiphy *wiphy, |
| 3093 | struct ieee80211_channel *beacon_chan, |
| 3094 | gfp_t gfp) |
| 3095 | { |
| 3096 | struct reg_beacon *reg_beacon; |
Luis R. Rodriguez | 3ebfa6e | 2012-12-19 10:53:02 -0800 | [diff] [blame] | 3097 | bool processing; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3098 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3099 | if (beacon_chan->beacon_found || |
| 3100 | beacon_chan->flags & IEEE80211_CHAN_RADAR || |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 3101 | (beacon_chan->band == NL80211_BAND_2GHZ && |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3102 | !freq_is_chan_12_13_14(beacon_chan->center_freq))) |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3103 | return 0; |
| 3104 | |
Luis R. Rodriguez | 3ebfa6e | 2012-12-19 10:53:02 -0800 | [diff] [blame] | 3105 | spin_lock_bh(®_pending_beacons_lock); |
| 3106 | processing = pending_reg_beacon(beacon_chan); |
| 3107 | spin_unlock_bh(®_pending_beacons_lock); |
| 3108 | |
| 3109 | if (processing) |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3110 | return 0; |
| 3111 | |
| 3112 | reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp); |
| 3113 | if (!reg_beacon) |
| 3114 | return -ENOMEM; |
| 3115 | |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 3116 | pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n", |
| 3117 | beacon_chan->center_freq, |
| 3118 | ieee80211_frequency_to_channel(beacon_chan->center_freq), |
| 3119 | wiphy_name(wiphy)); |
Luis R. Rodriguez | 4113f75 | 2010-01-04 11:50:11 -0500 | [diff] [blame] | 3120 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3121 | memcpy(®_beacon->chan, beacon_chan, |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3122 | sizeof(struct ieee80211_channel)); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3123 | |
| 3124 | /* |
| 3125 | * Since we can be called from BH or and non-BH context |
| 3126 | * we must use spin_lock_bh() |
| 3127 | */ |
| 3128 | spin_lock_bh(®_pending_beacons_lock); |
| 3129 | list_add_tail(®_beacon->list, ®_pending_beacons); |
| 3130 | spin_unlock_bh(®_pending_beacons_lock); |
| 3131 | |
| 3132 | schedule_work(®_work); |
| 3133 | |
| 3134 | return 0; |
| 3135 | } |
| 3136 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 3137 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3138 | { |
| 3139 | unsigned int i; |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 3140 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 3141 | const struct ieee80211_freq_range *freq_range = NULL; |
| 3142 | const struct ieee80211_power_rule *power_rule = NULL; |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 3143 | char bw[32], cac_time[32]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3144 | |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3145 | pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n"); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3146 | |
| 3147 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 3148 | reg_rule = &rd->reg_rules[i]; |
| 3149 | freq_range = ®_rule->freq_range; |
| 3150 | power_rule = ®_rule->power_rule; |
| 3151 | |
Janusz Dziedzic | b0dfd2e | 2014-02-20 13:52:16 +0100 | [diff] [blame] | 3152 | if (reg_rule->flags & NL80211_RRF_AUTO_BW) |
| 3153 | snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO", |
| 3154 | freq_range->max_bandwidth_khz, |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 3155 | reg_get_max_bandwidth(rd, reg_rule)); |
| 3156 | else |
Janusz Dziedzic | b0dfd2e | 2014-02-20 13:52:16 +0100 | [diff] [blame] | 3157 | snprintf(bw, sizeof(bw), "%d KHz", |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 3158 | freq_range->max_bandwidth_khz); |
| 3159 | |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 3160 | if (reg_rule->flags & NL80211_RRF_DFS) |
| 3161 | scnprintf(cac_time, sizeof(cac_time), "%u s", |
| 3162 | reg_rule->dfs_cac_ms/1000); |
| 3163 | else |
| 3164 | scnprintf(cac_time, sizeof(cac_time), "N/A"); |
| 3165 | |
| 3166 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 3167 | /* |
| 3168 | * There may not be documentation for max antenna gain |
| 3169 | * in certain regions |
| 3170 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3171 | if (power_rule->max_antenna_gain) |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3172 | pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n", |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3173 | freq_range->start_freq_khz, |
| 3174 | freq_range->end_freq_khz, |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 3175 | bw, |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3176 | power_rule->max_antenna_gain, |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 3177 | power_rule->max_eirp, |
| 3178 | cac_time); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3179 | else |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3180 | pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n", |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3181 | freq_range->start_freq_khz, |
| 3182 | freq_range->end_freq_khz, |
Janusz Dziedzic | 9752482 | 2014-01-30 09:52:20 +0100 | [diff] [blame] | 3183 | bw, |
Janusz Dziedzic | 089027e | 2014-02-21 19:46:12 +0100 | [diff] [blame] | 3184 | power_rule->max_eirp, |
| 3185 | cac_time); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | |
Luis R. Rodriguez | 4c7d398 | 2013-11-13 18:54:02 +0100 | [diff] [blame] | 3189 | bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region) |
Luis R. Rodriguez | 8b60b07 | 2011-10-11 10:59:02 -0700 | [diff] [blame] | 3190 | { |
| 3191 | switch (dfs_region) { |
| 3192 | case NL80211_DFS_UNSET: |
| 3193 | case NL80211_DFS_FCC: |
| 3194 | case NL80211_DFS_ETSI: |
| 3195 | case NL80211_DFS_JP: |
| 3196 | return true; |
| 3197 | default: |
Johannes Berg | c799ba6 | 2015-12-11 15:31:10 +0100 | [diff] [blame] | 3198 | pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region); |
Luis R. Rodriguez | 8b60b07 | 2011-10-11 10:59:02 -0700 | [diff] [blame] | 3199 | return false; |
| 3200 | } |
| 3201 | } |
| 3202 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 3203 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3204 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3205 | struct regulatory_request *lr = get_last_request(); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3206 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 3207 | if (is_intersected_alpha2(rd->alpha2)) { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3208 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 3209 | struct cfg80211_registered_device *rdev; |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3210 | rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx); |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 3211 | if (rdev) { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3212 | pr_debug("Current regulatory domain updated by AP to: %c%c\n", |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 3213 | rdev->country_ie_alpha2[0], |
| 3214 | rdev->country_ie_alpha2[1]); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 3215 | } else |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3216 | pr_debug("Current regulatory domain intersected:\n"); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 3217 | } else |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3218 | pr_debug("Current regulatory domain intersected:\n"); |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3219 | } else if (is_world_regdom(rd->alpha2)) { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3220 | pr_debug("World regulatory domain updated:\n"); |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3221 | } else { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3222 | if (is_unknown_alpha2(rd->alpha2)) |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3223 | pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n"); |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3224 | else { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3225 | if (reg_request_cell_base(lr)) |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3226 | pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n", |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3227 | rd->alpha2[0], rd->alpha2[1]); |
| 3228 | else |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3229 | pr_debug("Regulatory domain changed to country: %c%c\n", |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3230 | rd->alpha2[0], rd->alpha2[1]); |
| 3231 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3232 | } |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3233 | |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3234 | pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region)); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3235 | print_rd_rules(rd); |
| 3236 | } |
| 3237 | |
Johannes Berg | 2df7816 | 2008-10-28 16:49:41 +0100 | [diff] [blame] | 3238 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3239 | { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3240 | pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3241 | print_rd_rules(rd); |
| 3242 | } |
| 3243 | |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3244 | static int reg_set_rd_core(const struct ieee80211_regdomain *rd) |
| 3245 | { |
| 3246 | if (!is_world_regdom(rd->alpha2)) |
| 3247 | return -EINVAL; |
| 3248 | update_world_regdomain(rd); |
| 3249 | return 0; |
| 3250 | } |
| 3251 | |
Luis R. Rodriguez | 84721d4 | 2013-11-05 09:18:15 -0800 | [diff] [blame] | 3252 | static int reg_set_rd_user(const struct ieee80211_regdomain *rd, |
| 3253 | struct regulatory_request *user_request) |
| 3254 | { |
| 3255 | const struct ieee80211_regdomain *intersected_rd = NULL; |
| 3256 | |
Luis R. Rodriguez | 84721d4 | 2013-11-05 09:18:15 -0800 | [diff] [blame] | 3257 | if (!regdom_changes(rd->alpha2)) |
| 3258 | return -EALREADY; |
| 3259 | |
| 3260 | if (!is_valid_rd(rd)) { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3261 | pr_err("Invalid regulatory domain detected: %c%c\n", |
| 3262 | rd->alpha2[0], rd->alpha2[1]); |
Luis R. Rodriguez | 84721d4 | 2013-11-05 09:18:15 -0800 | [diff] [blame] | 3263 | print_regdomain_info(rd); |
| 3264 | return -EINVAL; |
| 3265 | } |
| 3266 | |
| 3267 | if (!user_request->intersect) { |
| 3268 | reset_regdomains(false, rd); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); |
| 3273 | if (!intersected_rd) |
| 3274 | return -EINVAL; |
| 3275 | |
| 3276 | kfree(rd); |
| 3277 | rd = NULL; |
| 3278 | reset_regdomains(false, intersected_rd); |
| 3279 | |
| 3280 | return 0; |
| 3281 | } |
| 3282 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3283 | static int reg_set_rd_driver(const struct ieee80211_regdomain *rd, |
| 3284 | struct regulatory_request *driver_request) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3285 | { |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 3286 | const struct ieee80211_regdomain *regd; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 3287 | const struct ieee80211_regdomain *intersected_rd = NULL; |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3288 | const struct ieee80211_regdomain *tmp; |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 3289 | struct wiphy *request_wiphy; |
Johannes Berg | 6913b49 | 2012-12-04 00:48:59 +0100 | [diff] [blame] | 3290 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3291 | if (is_world_regdom(rd->alpha2)) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3292 | return -EINVAL; |
| 3293 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3294 | if (!regdom_changes(rd->alpha2)) |
| 3295 | return -EALREADY; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3296 | |
Luis R. Rodriguez | 8375af3 | 2008-11-12 14:21:57 -0800 | [diff] [blame] | 3297 | if (!is_valid_rd(rd)) { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3298 | pr_err("Invalid regulatory domain detected: %c%c\n", |
| 3299 | rd->alpha2[0], rd->alpha2[1]); |
Luis R. Rodriguez | 8375af3 | 2008-11-12 14:21:57 -0800 | [diff] [blame] | 3300 | print_regdomain_info(rd); |
| 3301 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3304 | request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx); |
Johannes Berg | 922ec58 | 2015-10-15 09:12:49 +0200 | [diff] [blame] | 3305 | if (!request_wiphy) |
Johannes Berg | de3584b | 2011-11-21 10:44:00 +0100 | [diff] [blame] | 3306 | return -ENODEV; |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 3307 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3308 | if (!driver_request->intersect) { |
Luis R. Rodriguez | 558f6d3 | 2009-06-08 18:54:37 -0700 | [diff] [blame] | 3309 | if (request_wiphy->regd) |
| 3310 | return -EALREADY; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 3311 | |
Johannes Berg | e9763c3 | 2012-12-03 16:59:46 +0100 | [diff] [blame] | 3312 | regd = reg_copy_regd(rd); |
| 3313 | if (IS_ERR(regd)) |
| 3314 | return PTR_ERR(regd); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 3315 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 3316 | rcu_assign_pointer(request_wiphy->regd, regd); |
Johannes Berg | 379b82f | 2012-12-06 15:44:07 +0100 | [diff] [blame] | 3317 | reset_regdomains(false, rd); |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 3318 | return 0; |
| 3319 | } |
| 3320 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3321 | intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); |
| 3322 | if (!intersected_rd) |
| 3323 | return -EINVAL; |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 3324 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3325 | /* |
| 3326 | * We can trash what CRDA provided now. |
| 3327 | * However if a driver requested this specific regulatory |
| 3328 | * domain we keep it for its private use |
| 3329 | */ |
| 3330 | tmp = get_wiphy_regdom(request_wiphy); |
| 3331 | rcu_assign_pointer(request_wiphy->regd, rd); |
| 3332 | rcu_free_regdom(tmp); |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 3333 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3334 | rd = NULL; |
Larry Finger | b7566fc | 2013-02-04 15:33:44 -0600 | [diff] [blame] | 3335 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3336 | reset_regdomains(false, intersected_rd); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 3337 | |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3338 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
Luis R. Rodriguez | 0199240 | 2013-11-05 09:18:17 -0800 | [diff] [blame] | 3341 | static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd, |
| 3342 | struct regulatory_request *country_ie_request) |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3343 | { |
| 3344 | struct wiphy *request_wiphy; |
| 3345 | |
| 3346 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && |
| 3347 | !is_unknown_alpha2(rd->alpha2)) |
| 3348 | return -EINVAL; |
| 3349 | |
| 3350 | /* |
| 3351 | * Lets only bother proceeding on the same alpha2 if the current |
| 3352 | * rd is non static (it means CRDA was present and was used last) |
| 3353 | * and the pending request came in from a country IE |
| 3354 | */ |
| 3355 | |
| 3356 | if (!is_valid_rd(rd)) { |
Dave Young | 94c4fd6 | 2015-11-15 15:31:05 +0800 | [diff] [blame] | 3357 | pr_err("Invalid regulatory domain detected: %c%c\n", |
| 3358 | rd->alpha2[0], rd->alpha2[1]); |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3359 | print_regdomain_info(rd); |
| 3360 | return -EINVAL; |
| 3361 | } |
| 3362 | |
Luis R. Rodriguez | 0199240 | 2013-11-05 09:18:17 -0800 | [diff] [blame] | 3363 | request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx); |
Johannes Berg | 922ec58 | 2015-10-15 09:12:49 +0200 | [diff] [blame] | 3364 | if (!request_wiphy) |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3365 | return -ENODEV; |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3366 | |
Luis R. Rodriguez | 0199240 | 2013-11-05 09:18:17 -0800 | [diff] [blame] | 3367 | if (country_ie_request->intersect) |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3368 | return -EINVAL; |
| 3369 | |
| 3370 | reset_regdomains(false, rd); |
| 3371 | return 0; |
| 3372 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3373 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 3374 | /* |
| 3375 | * Use this call to set the current regulatory domain. Conflicts with |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3376 | * multiple drivers can be ironed out later. Caller must've already |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 3377 | * kmalloc'd the rd structure. |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 3378 | */ |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 3379 | int set_regdom(const struct ieee80211_regdomain *rd, |
| 3380 | enum ieee80211_regd_source regd_src) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3381 | { |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3382 | struct regulatory_request *lr; |
Janusz Dziedzic | 092008a | 2014-02-14 08:54:00 +0100 | [diff] [blame] | 3383 | bool user_reset = false; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3384 | int r; |
| 3385 | |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3386 | if (!reg_is_valid_request(rd->alpha2)) { |
| 3387 | kfree(rd); |
| 3388 | return -EINVAL; |
| 3389 | } |
| 3390 | |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 3391 | if (regd_src == REGD_SOURCE_CRDA) |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 3392 | reset_crda_timeouts(); |
Ilan peer | c37722b | 2015-03-30 15:15:49 +0300 | [diff] [blame] | 3393 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3394 | lr = get_last_request(); |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 3395 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3396 | /* Note that this doesn't update the wiphys, this is done below */ |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3397 | switch (lr->initiator) { |
| 3398 | case NL80211_REGDOM_SET_BY_CORE: |
| 3399 | r = reg_set_rd_core(rd); |
| 3400 | break; |
| 3401 | case NL80211_REGDOM_SET_BY_USER: |
Luis R. Rodriguez | 84721d4 | 2013-11-05 09:18:15 -0800 | [diff] [blame] | 3402 | r = reg_set_rd_user(rd, lr); |
Janusz Dziedzic | 092008a | 2014-02-14 08:54:00 +0100 | [diff] [blame] | 3403 | user_reset = true; |
Luis R. Rodriguez | 84721d4 | 2013-11-05 09:18:15 -0800 | [diff] [blame] | 3404 | break; |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3405 | case NL80211_REGDOM_SET_BY_DRIVER: |
Luis R. Rodriguez | f5fe324 | 2013-11-05 09:18:16 -0800 | [diff] [blame] | 3406 | r = reg_set_rd_driver(rd, lr); |
| 3407 | break; |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3408 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
Luis R. Rodriguez | 0199240 | 2013-11-05 09:18:17 -0800 | [diff] [blame] | 3409 | r = reg_set_rd_country_ie(rd, lr); |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3410 | break; |
| 3411 | default: |
| 3412 | WARN(1, "invalid initiator %d\n", lr->initiator); |
Ola Olsson | 09d1180 | 2015-12-13 19:12:03 +0100 | [diff] [blame] | 3413 | kfree(rd); |
Luis R. Rodriguez | 3b9e5ac | 2013-11-05 09:18:14 -0800 | [diff] [blame] | 3414 | return -EINVAL; |
| 3415 | } |
| 3416 | |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 3417 | if (r) { |
Janusz Dziedzic | 092008a | 2014-02-14 08:54:00 +0100 | [diff] [blame] | 3418 | switch (r) { |
| 3419 | case -EALREADY: |
Kalle Valo | 9590853 | 2012-07-12 15:33:58 +0300 | [diff] [blame] | 3420 | reg_set_request_processed(); |
Janusz Dziedzic | 092008a | 2014-02-14 08:54:00 +0100 | [diff] [blame] | 3421 | break; |
| 3422 | default: |
| 3423 | /* Back to world regulatory in case of errors */ |
| 3424 | restore_regulatory_settings(user_reset); |
| 3425 | } |
Kalle Valo | 9590853 | 2012-07-12 15:33:58 +0300 | [diff] [blame] | 3426 | |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 3427 | kfree(rd); |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3428 | return r; |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 3429 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3430 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3431 | /* This would make this whole thing pointless */ |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3432 | if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom())) |
| 3433 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3434 | |
| 3435 | /* update all wiphys now with the new established regulatory domain */ |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3436 | update_all_wiphy_regulatory(lr->initiator); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3437 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 3438 | print_regdomain(get_cfg80211_regdom()); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3439 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3440 | nl80211_send_reg_change_event(lr); |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 3441 | |
Luis R. Rodriguez | b2e253c | 2010-11-17 21:46:09 -0800 | [diff] [blame] | 3442 | reg_set_request_processed(); |
| 3443 | |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3444 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
Arik Nemtsov | 2c3e861 | 2015-01-07 16:47:19 +0200 | [diff] [blame] | 3447 | static int __regulatory_set_wiphy_regd(struct wiphy *wiphy, |
| 3448 | struct ieee80211_regdomain *rd) |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 3449 | { |
| 3450 | const struct ieee80211_regdomain *regd; |
| 3451 | const struct ieee80211_regdomain *prev_regd; |
| 3452 | struct cfg80211_registered_device *rdev; |
| 3453 | |
| 3454 | if (WARN_ON(!wiphy || !rd)) |
| 3455 | return -EINVAL; |
| 3456 | |
| 3457 | if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED), |
| 3458 | "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n")) |
| 3459 | return -EPERM; |
| 3460 | |
| 3461 | if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) { |
| 3462 | print_regdomain_info(rd); |
| 3463 | return -EINVAL; |
| 3464 | } |
| 3465 | |
| 3466 | regd = reg_copy_regd(rd); |
| 3467 | if (IS_ERR(regd)) |
| 3468 | return PTR_ERR(regd); |
| 3469 | |
| 3470 | rdev = wiphy_to_rdev(wiphy); |
| 3471 | |
| 3472 | spin_lock(®_requests_lock); |
| 3473 | prev_regd = rdev->requested_regd; |
| 3474 | rdev->requested_regd = regd; |
| 3475 | spin_unlock(®_requests_lock); |
| 3476 | |
| 3477 | kfree(prev_regd); |
Arik Nemtsov | 2c3e861 | 2015-01-07 16:47:19 +0200 | [diff] [blame] | 3478 | return 0; |
| 3479 | } |
| 3480 | |
| 3481 | int regulatory_set_wiphy_regd(struct wiphy *wiphy, |
| 3482 | struct ieee80211_regdomain *rd) |
| 3483 | { |
| 3484 | int ret = __regulatory_set_wiphy_regd(wiphy, rd); |
| 3485 | |
| 3486 | if (ret) |
| 3487 | return ret; |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 3488 | |
| 3489 | schedule_work(®_work); |
| 3490 | return 0; |
| 3491 | } |
| 3492 | EXPORT_SYMBOL(regulatory_set_wiphy_regd); |
| 3493 | |
Arik Nemtsov | 2c3e861 | 2015-01-07 16:47:19 +0200 | [diff] [blame] | 3494 | int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy, |
| 3495 | struct ieee80211_regdomain *rd) |
| 3496 | { |
| 3497 | int ret; |
| 3498 | |
| 3499 | ASSERT_RTNL(); |
| 3500 | |
| 3501 | ret = __regulatory_set_wiphy_regd(wiphy, rd); |
| 3502 | if (ret) |
| 3503 | return ret; |
| 3504 | |
| 3505 | /* process the request immediately */ |
| 3506 | reg_process_self_managed_hints(); |
| 3507 | return 0; |
| 3508 | } |
| 3509 | EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl); |
| 3510 | |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3511 | void wiphy_regulatory_register(struct wiphy *wiphy) |
| 3512 | { |
Arik Nemtsov | 23df0b7 | 2013-07-21 16:36:48 +0300 | [diff] [blame] | 3513 | struct regulatory_request *lr; |
| 3514 | |
Jonathan Doron | b0d7aa5 | 2014-12-15 19:26:00 +0200 | [diff] [blame] | 3515 | /* self-managed devices ignore external hints */ |
| 3516 | if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) |
| 3517 | wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS | |
| 3518 | REGULATORY_COUNTRY_IE_IGNORE; |
| 3519 | |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3520 | if (!reg_dev_ignore_cell_hint(wiphy)) |
| 3521 | reg_num_devs_support_basehint++; |
| 3522 | |
Arik Nemtsov | 23df0b7 | 2013-07-21 16:36:48 +0300 | [diff] [blame] | 3523 | lr = get_last_request(); |
| 3524 | wiphy_update_regulatory(wiphy, lr->initiator); |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 3525 | wiphy_all_share_dfs_chan_state(wiphy); |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
Luis R. Rodriguez | bfead08 | 2012-07-12 11:49:19 -0700 | [diff] [blame] | 3528 | void wiphy_regulatory_deregister(struct wiphy *wiphy) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 3529 | { |
Luis R. Rodriguez | 0ad8aca | 2009-03-24 21:21:08 -0400 | [diff] [blame] | 3530 | struct wiphy *request_wiphy = NULL; |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3531 | struct regulatory_request *lr; |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 3532 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3533 | lr = get_last_request(); |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 3534 | |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3535 | if (!reg_dev_ignore_cell_hint(wiphy)) |
| 3536 | reg_num_devs_support_basehint--; |
| 3537 | |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 3538 | rcu_free_regdom(get_wiphy_regdom(wiphy)); |
Monam Agarwal | 34dd886 | 2014-03-24 00:53:40 +0530 | [diff] [blame] | 3539 | RCU_INIT_POINTER(wiphy->regd, NULL); |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 3540 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3541 | if (lr) |
| 3542 | request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 3543 | |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 3544 | if (!request_wiphy || request_wiphy != wiphy) |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3545 | return; |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 3546 | |
Johannes Berg | c492db3 | 2012-12-06 16:29:25 +0100 | [diff] [blame] | 3547 | lr->wiphy_idx = WIPHY_IDX_INVALID; |
| 3548 | lr->country_ie_env = ENVIRON_ANY; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 3549 | } |
| 3550 | |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 3551 | /* |
| 3552 | * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for |
| 3553 | * UNII band definitions |
| 3554 | */ |
| 3555 | int cfg80211_get_unii(int freq) |
| 3556 | { |
| 3557 | /* UNII-1 */ |
| 3558 | if (freq >= 5150 && freq <= 5250) |
| 3559 | return 0; |
| 3560 | |
| 3561 | /* UNII-2A */ |
| 3562 | if (freq > 5250 && freq <= 5350) |
| 3563 | return 1; |
| 3564 | |
| 3565 | /* UNII-2B */ |
| 3566 | if (freq > 5350 && freq <= 5470) |
| 3567 | return 2; |
| 3568 | |
| 3569 | /* UNII-2C */ |
| 3570 | if (freq > 5470 && freq <= 5725) |
| 3571 | return 3; |
| 3572 | |
| 3573 | /* UNII-3 */ |
| 3574 | if (freq > 5725 && freq <= 5825) |
| 3575 | return 4; |
| 3576 | |
| 3577 | return -EINVAL; |
| 3578 | } |
| 3579 | |
Ilan Peer | c8866e5 | 2014-02-23 09:13:03 +0200 | [diff] [blame] | 3580 | bool regulatory_indoor_allowed(void) |
| 3581 | { |
| 3582 | return reg_is_indoor; |
| 3583 | } |
| 3584 | |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 3585 | bool regulatory_pre_cac_allowed(struct wiphy *wiphy) |
| 3586 | { |
| 3587 | const struct ieee80211_regdomain *regd = NULL; |
| 3588 | const struct ieee80211_regdomain *wiphy_regd = NULL; |
| 3589 | bool pre_cac_allowed = false; |
| 3590 | |
| 3591 | rcu_read_lock(); |
| 3592 | |
| 3593 | regd = rcu_dereference(cfg80211_regdomain); |
| 3594 | wiphy_regd = rcu_dereference(wiphy->regd); |
| 3595 | if (!wiphy_regd) { |
| 3596 | if (regd->dfs_region == NL80211_DFS_ETSI) |
| 3597 | pre_cac_allowed = true; |
| 3598 | |
| 3599 | rcu_read_unlock(); |
| 3600 | |
| 3601 | return pre_cac_allowed; |
| 3602 | } |
| 3603 | |
| 3604 | if (regd->dfs_region == wiphy_regd->dfs_region && |
| 3605 | wiphy_regd->dfs_region == NL80211_DFS_ETSI) |
| 3606 | pre_cac_allowed = true; |
| 3607 | |
| 3608 | rcu_read_unlock(); |
| 3609 | |
| 3610 | return pre_cac_allowed; |
| 3611 | } |
| 3612 | |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 3613 | void regulatory_propagate_dfs_state(struct wiphy *wiphy, |
| 3614 | struct cfg80211_chan_def *chandef, |
| 3615 | enum nl80211_dfs_state dfs_state, |
| 3616 | enum nl80211_radar_event event) |
| 3617 | { |
| 3618 | struct cfg80211_registered_device *rdev; |
| 3619 | |
| 3620 | ASSERT_RTNL(); |
| 3621 | |
| 3622 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 3623 | return; |
| 3624 | |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 3625 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 3626 | if (wiphy == &rdev->wiphy) |
| 3627 | continue; |
| 3628 | |
| 3629 | if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) |
| 3630 | continue; |
| 3631 | |
| 3632 | if (!ieee80211_get_channel(&rdev->wiphy, |
| 3633 | chandef->chan->center_freq)) |
| 3634 | continue; |
| 3635 | |
| 3636 | cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state); |
| 3637 | |
| 3638 | if (event == NL80211_RADAR_DETECTED || |
| 3639 | event == NL80211_RADAR_CAC_FINISHED) |
| 3640 | cfg80211_sched_dfs_chan_update(rdev); |
| 3641 | |
| 3642 | nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL); |
| 3643 | } |
| 3644 | } |
| 3645 | |
Johannes Berg | d7be102 | 2017-10-26 11:24:27 +0200 | [diff] [blame] | 3646 | static int __init regulatory_init_db(void) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3647 | { |
Johannes Berg | d7be102 | 2017-10-26 11:24:27 +0200 | [diff] [blame] | 3648 | int err; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 3649 | |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 3650 | err = load_builtin_regdb_keys(); |
| 3651 | if (err) |
| 3652 | return err; |
| 3653 | |
Luis R. Rodriguez | ae9e4b0 | 2009-07-14 20:23:15 -0400 | [diff] [blame] | 3654 | /* We always try to get an update for the static regdomain */ |
Johannes Berg | 458f4f9 | 2012-12-06 15:47:38 +0100 | [diff] [blame] | 3655 | err = regulatory_hint_core(cfg80211_world_regdom->alpha2); |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 3656 | if (err) { |
Ola Olsson | 09d1180 | 2015-12-13 19:12:03 +0100 | [diff] [blame] | 3657 | if (err == -ENOMEM) { |
| 3658 | platform_device_unregister(reg_pdev); |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 3659 | return err; |
Ola Olsson | 09d1180 | 2015-12-13 19:12:03 +0100 | [diff] [blame] | 3660 | } |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 3661 | /* |
| 3662 | * N.B. kobject_uevent_env() can fail mainly for when we're out |
| 3663 | * memory which is handled and propagated appropriately above |
| 3664 | * but it can also fail during a netlink_broadcast() or during |
| 3665 | * early boot for call_usermodehelper(). For now treat these |
| 3666 | * errors as non-fatal. |
| 3667 | */ |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 3668 | pr_err("kobject_uevent_env() was unable to call CRDA during init\n"); |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 3669 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 3670 | |
Luis R. Rodriguez | ae9e4b0 | 2009-07-14 20:23:15 -0400 | [diff] [blame] | 3671 | /* |
| 3672 | * Finally, if the user set the module parameter treat it |
| 3673 | * as a user hint. |
| 3674 | */ |
| 3675 | if (!is_world_regdom(ieee80211_regdom)) |
Luis R. Rodriguez | 57b5ce0 | 2012-07-12 11:49:18 -0700 | [diff] [blame] | 3676 | regulatory_hint_user(ieee80211_regdom, |
| 3677 | NL80211_USER_REG_HINT_USER); |
Luis R. Rodriguez | ae9e4b0 | 2009-07-14 20:23:15 -0400 | [diff] [blame] | 3678 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3679 | return 0; |
| 3680 | } |
Johannes Berg | d7be102 | 2017-10-26 11:24:27 +0200 | [diff] [blame] | 3681 | #ifndef MODULE |
| 3682 | late_initcall(regulatory_init_db); |
| 3683 | #endif |
| 3684 | |
| 3685 | int __init regulatory_init(void) |
| 3686 | { |
| 3687 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
| 3688 | if (IS_ERR(reg_pdev)) |
| 3689 | return PTR_ERR(reg_pdev); |
| 3690 | |
| 3691 | spin_lock_init(®_requests_lock); |
| 3692 | spin_lock_init(®_pending_beacons_lock); |
| 3693 | spin_lock_init(®_indoor_lock); |
| 3694 | |
| 3695 | rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom); |
| 3696 | |
| 3697 | user_alpha2[0] = '9'; |
| 3698 | user_alpha2[1] = '7'; |
| 3699 | |
| 3700 | #ifdef MODULE |
| 3701 | return regulatory_init_db(); |
| 3702 | #else |
| 3703 | return 0; |
| 3704 | #endif |
| 3705 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3706 | |
Johannes Berg | 1a91931 | 2012-12-03 17:21:11 +0100 | [diff] [blame] | 3707 | void regulatory_exit(void) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3708 | { |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 3709 | struct regulatory_request *reg_request, *tmp; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3710 | struct reg_beacon *reg_beacon, *btmp; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 3711 | |
| 3712 | cancel_work_sync(®_work); |
Johannes Berg | b686303 | 2015-10-15 09:25:18 +0200 | [diff] [blame] | 3713 | cancel_crda_timeout_sync(); |
Arik Nemtsov | ad932f0 | 2014-11-27 09:44:55 +0200 | [diff] [blame] | 3714 | cancel_delayed_work_sync(®_check_chans); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 3715 | |
Johannes Berg | 9027b14 | 2012-12-03 17:59:24 +0100 | [diff] [blame] | 3716 | /* Lock to suppress warnings */ |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3717 | rtnl_lock(); |
Johannes Berg | 379b82f | 2012-12-06 15:44:07 +0100 | [diff] [blame] | 3718 | reset_regdomains(true, NULL); |
Johannes Berg | 38fd214 | 2013-05-10 19:17:17 +0200 | [diff] [blame] | 3719 | rtnl_unlock(); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 3720 | |
Luis R. Rodriguez | 58ebacc | 2011-11-08 14:28:06 -0800 | [diff] [blame] | 3721 | dev_set_uevent_suppress(®_pdev->dev, true); |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 3722 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 3723 | platform_device_unregister(reg_pdev); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 3724 | |
Johannes Berg | fea9bce | 2012-12-03 17:32:01 +0100 | [diff] [blame] | 3725 | list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { |
| 3726 | list_del(®_beacon->list); |
| 3727 | kfree(reg_beacon); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 3728 | } |
| 3729 | |
Johannes Berg | fea9bce | 2012-12-03 17:32:01 +0100 | [diff] [blame] | 3730 | list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { |
| 3731 | list_del(®_beacon->list); |
| 3732 | kfree(reg_beacon); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 3733 | } |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 3734 | |
Johannes Berg | fea9bce | 2012-12-03 17:32:01 +0100 | [diff] [blame] | 3735 | list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) { |
| 3736 | list_del(®_request->list); |
| 3737 | kfree(reg_request); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 3738 | } |
Johannes Berg | 007f6c5 | 2015-10-15 11:22:58 +0200 | [diff] [blame] | 3739 | |
| 3740 | if (!IS_ERR_OR_NULL(regdb)) |
| 3741 | kfree(regdb); |
Johannes Berg | 90a53e4 | 2017-09-13 22:21:08 +0200 | [diff] [blame] | 3742 | |
| 3743 | free_regdb_keyring(); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 3744 | } |