Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
| 19 | import static android.net.ConnectivityManager.PRIVATE_DNS_DEFAULT_MODE; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OFF; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_OPPORTUNISTIC; |
| 22 | import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_PROVIDER_HOSTNAME; |
| 23 | import static android.provider.Settings.Global.DNS_RESOLVER_MIN_SAMPLES; |
| 24 | import static android.provider.Settings.Global.DNS_RESOLVER_MAX_SAMPLES; |
| 25 | import static android.provider.Settings.Global.DNS_RESOLVER_SAMPLE_VALIDITY_SECONDS; |
| 26 | import static android.provider.Settings.Global.DNS_RESOLVER_SUCCESS_THRESHOLD_PERCENT; |
| 27 | import static android.provider.Settings.Global.PRIVATE_DNS_MODE; |
| 28 | import static android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER; |
| 29 | |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 33 | import android.net.LinkProperties; |
| 34 | import android.net.Network; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 35 | import android.net.NetworkUtils; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 36 | import android.net.Uri; |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 37 | import android.net.dns.ResolvUtil; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 38 | import android.os.Binder; |
| 39 | import android.os.INetworkManagementService; |
| 40 | import android.os.Handler; |
| 41 | import android.os.UserHandle; |
| 42 | import android.provider.Settings; |
| 43 | import android.text.TextUtils; |
| 44 | import android.util.Slog; |
| 45 | |
| 46 | import com.android.server.connectivity.MockableSystemProperties; |
| 47 | |
| 48 | import java.net.InetAddress; |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 49 | import java.net.UnknownHostException; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 50 | import java.util.Arrays; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 51 | import java.util.Collection; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 52 | import java.util.HashMap; |
| 53 | import java.util.Map; |
| 54 | import java.util.stream.Collectors; |
| 55 | import java.util.StringJoiner; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /** |
| 59 | * Encapsulate the management of DNS settings for networks. |
| 60 | * |
| 61 | * This class it NOT designed for concurrent access. Furthermore, all non-static |
| 62 | * methods MUST be called from ConnectivityService's thread. |
| 63 | * |
Erik Kline | 79c6d05 | 2018-03-21 07:18:33 -0700 | [diff] [blame^] | 64 | * [ Private DNS ] |
| 65 | * The code handling Private DNS is spread across several components, but this |
| 66 | * seems like the least bad place to collect all the observations. |
| 67 | * |
| 68 | * Private DNS handling and updating occurs in response to several different |
| 69 | * events. Each is described here with its corresponding intended handling. |
| 70 | * |
| 71 | * [A] Event: A new network comes up. |
| 72 | * Mechanics: |
| 73 | * [1] ConnectivityService gets notifications from NetworkAgents. |
| 74 | * [2] in updateNetworkInfo(), the first time the NetworkAgent goes into |
| 75 | * into CONNECTED state, the Private DNS configuration is retrieved, |
| 76 | * programmed, and strict mode hostname resolution (if applicable) is |
| 77 | * enqueued in NetworkAgent's NetworkMonitor, via a call to |
| 78 | * handlePerNetworkPrivateDnsConfig(). |
| 79 | * [3] Re-resolution of strict mode hostnames that fail to return any |
| 80 | * IP addresses happens inside NetworkMonitor; it sends itself a |
| 81 | * delayed CMD_EVALUATE_PRIVATE_DNS message in a simple backoff |
| 82 | * schedule. |
| 83 | * [4] Successfully resolved hostnames are sent to ConnectivityService |
| 84 | * inside an EVENT_PRIVATE_DNS_CONFIG_RESOLVED message. The resolved |
| 85 | * IP addresses are programmed into netd via: |
| 86 | * |
| 87 | * updatePrivateDns() -> updateDnses() |
| 88 | * |
| 89 | * both of which make calls into DnsManager. |
| 90 | * [5] Upon a successful hostname resolution NetworkMonitor initiates a |
| 91 | * validation attempt in the form of a lookup for a one-time hostname |
| 92 | * that uses Private DNS. |
| 93 | * |
| 94 | * [B] Event: Private DNS settings are changed. |
| 95 | * Mechanics: |
| 96 | * [1] ConnectivityService gets notifications from its SettingsObserver. |
| 97 | * [2] handlePrivateDnsSettingsChanged() is called, which calls |
| 98 | * handlePerNetworkPrivateDnsConfig() and the process proceeds |
| 99 | * as if from A.3 above. |
| 100 | * |
| 101 | * [C] Event: An application calls ConnectivityManager#reportBadNetwork(). |
| 102 | * Mechanics: |
| 103 | * [1] NetworkMonitor is notified and initiates a reevaluation, which |
| 104 | * always bypasses Private DNS. |
| 105 | * [2] Once completed, NetworkMonitor checks if strict mode is in operation |
| 106 | * and if so enqueues another evaluation of Private DNS, as if from |
| 107 | * step A.5 above. |
| 108 | * |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 109 | * @hide |
| 110 | */ |
| 111 | public class DnsManager { |
| 112 | private static final String TAG = DnsManager.class.getSimpleName(); |
| 113 | |
| 114 | /* Defaults for resolver parameters. */ |
| 115 | private static final int DNS_RESOLVER_DEFAULT_SAMPLE_VALIDITY_SECONDS = 1800; |
| 116 | private static final int DNS_RESOLVER_DEFAULT_SUCCESS_THRESHOLD_PERCENT = 25; |
| 117 | private static final int DNS_RESOLVER_DEFAULT_MIN_SAMPLES = 8; |
| 118 | private static final int DNS_RESOLVER_DEFAULT_MAX_SAMPLES = 64; |
| 119 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 120 | public static class PrivateDnsConfig { |
| 121 | public final boolean useTls; |
| 122 | public final String hostname; |
| 123 | public final InetAddress[] ips; |
| 124 | |
| 125 | public PrivateDnsConfig() { |
| 126 | this(false); |
| 127 | } |
| 128 | |
| 129 | public PrivateDnsConfig(boolean useTls) { |
| 130 | this.useTls = useTls; |
| 131 | this.hostname = ""; |
| 132 | this.ips = new InetAddress[0]; |
| 133 | } |
| 134 | |
| 135 | public PrivateDnsConfig(String hostname, InetAddress[] ips) { |
| 136 | this.useTls = !TextUtils.isEmpty(hostname); |
| 137 | this.hostname = useTls ? hostname : ""; |
| 138 | this.ips = (ips != null) ? ips : new InetAddress[0]; |
| 139 | } |
| 140 | |
| 141 | public PrivateDnsConfig(PrivateDnsConfig cfg) { |
| 142 | useTls = cfg.useTls; |
| 143 | hostname = cfg.hostname; |
| 144 | ips = cfg.ips; |
| 145 | } |
| 146 | |
| 147 | public boolean inStrictMode() { |
| 148 | return useTls && !TextUtils.isEmpty(hostname); |
| 149 | } |
| 150 | |
| 151 | public String toString() { |
| 152 | return PrivateDnsConfig.class.getSimpleName() + |
| 153 | "{" + useTls + ":" + hostname + "/" + Arrays.toString(ips) + "}"; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public static PrivateDnsConfig getPrivateDnsConfig(ContentResolver cr) { |
| 158 | final String mode = getPrivateDnsMode(cr); |
| 159 | |
| 160 | final boolean useTls = !TextUtils.isEmpty(mode) && !PRIVATE_DNS_MODE_OFF.equals(mode); |
| 161 | |
| 162 | if (PRIVATE_DNS_MODE_PROVIDER_HOSTNAME.equals(mode)) { |
| 163 | final String specifier = getStringSetting(cr, PRIVATE_DNS_SPECIFIER); |
| 164 | return new PrivateDnsConfig(specifier, null); |
| 165 | } |
| 166 | |
| 167 | return new PrivateDnsConfig(useTls); |
| 168 | } |
| 169 | |
| 170 | public static PrivateDnsConfig tryBlockingResolveOf(Network network, String name) { |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 171 | try { |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 172 | final InetAddress[] ips = ResolvUtil.blockingResolveAllLocally(network, name); |
| 173 | return new PrivateDnsConfig(name, ips); |
| 174 | } catch (UnknownHostException uhe) { |
| 175 | return new PrivateDnsConfig(name, null); |
| 176 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | public static Uri[] getPrivateDnsSettingsUris() { |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 180 | return new Uri[]{ |
| 181 | Settings.Global.getUriFor(PRIVATE_DNS_MODE), |
| 182 | Settings.Global.getUriFor(PRIVATE_DNS_SPECIFIER), |
| 183 | }; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 184 | } |
| 185 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 186 | private final Context mContext; |
| 187 | private final ContentResolver mContentResolver; |
| 188 | private final INetworkManagementService mNMS; |
| 189 | private final MockableSystemProperties mSystemProperties; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 190 | private final Map<Integer, PrivateDnsConfig> mPrivateDnsMap; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 191 | |
| 192 | private int mNumDnsEntries; |
| 193 | private int mSampleValidity; |
| 194 | private int mSuccessThreshold; |
| 195 | private int mMinSamples; |
| 196 | private int mMaxSamples; |
| 197 | private String mPrivateDnsMode; |
| 198 | private String mPrivateDnsSpecifier; |
| 199 | |
| 200 | public DnsManager(Context ctx, INetworkManagementService nms, MockableSystemProperties sp) { |
| 201 | mContext = ctx; |
| 202 | mContentResolver = mContext.getContentResolver(); |
| 203 | mNMS = nms; |
| 204 | mSystemProperties = sp; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 205 | mPrivateDnsMap = new HashMap<>(); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 206 | |
| 207 | // TODO: Create and register ContentObservers to track every setting |
| 208 | // used herein, posting messages to respond to changes. |
| 209 | } |
| 210 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 211 | public PrivateDnsConfig getPrivateDnsConfig() { |
| 212 | return getPrivateDnsConfig(mContentResolver); |
| 213 | } |
| 214 | |
| 215 | public void removeNetwork(Network network) { |
| 216 | mPrivateDnsMap.remove(network.netId); |
| 217 | } |
| 218 | |
| 219 | public PrivateDnsConfig updatePrivateDns(Network network, PrivateDnsConfig cfg) { |
| 220 | Slog.w(TAG, "updatePrivateDns(" + network + ", " + cfg + ")"); |
| 221 | return (cfg != null) |
| 222 | ? mPrivateDnsMap.put(network.netId, cfg) |
Andreas Gampe | ba06133 | 2018-03-05 17:23:07 -0800 | [diff] [blame] | 223 | : mPrivateDnsMap.remove(network.netId); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | public void setDnsConfigurationForNetwork( |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 227 | int netId, LinkProperties lp, boolean isDefaultNetwork) { |
Erik Kline | 117e7f3 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 228 | final String[] assignedServers = NetworkUtils.makeStrings(lp.getDnsServers()); |
| 229 | final String[] domainStrs = getDomainStrings(lp.getDomains()); |
| 230 | |
| 231 | updateParametersSettings(); |
| 232 | final int[] params = { mSampleValidity, mSuccessThreshold, mMinSamples, mMaxSamples }; |
| 233 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 234 | // We only use the PrivateDnsConfig data pushed to this class instance |
| 235 | // from ConnectivityService because it works in coordination with |
| 236 | // NetworkMonitor to decide which networks need validation and runs the |
| 237 | // blocking calls to resolve Private DNS strict mode hostnames. |
| 238 | // |
Erik Kline | c43d2f5 | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 239 | // At this time we do not attempt to enable Private DNS on non-Internet |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 240 | // networks like IMS. |
| 241 | final PrivateDnsConfig privateDnsCfg = mPrivateDnsMap.get(netId); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 242 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 243 | final boolean useTls = (privateDnsCfg != null) && privateDnsCfg.useTls; |
| 244 | final boolean strictMode = (privateDnsCfg != null) && privateDnsCfg.inStrictMode(); |
| 245 | final String tlsHostname = strictMode ? privateDnsCfg.hostname : ""; |
Erik Kline | 117e7f3 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 246 | final String[] tlsServers = |
| 247 | strictMode ? NetworkUtils.makeStrings( |
| 248 | Arrays.stream(privateDnsCfg.ips) |
| 249 | .filter((ip) -> lp.isReachable(ip)) |
| 250 | .collect(Collectors.toList())) |
| 251 | : useTls ? assignedServers // Opportunistic |
| 252 | : new String[0]; // Off |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 253 | |
| 254 | Slog.d(TAG, String.format("setDnsConfigurationForNetwork(%d, %s, %s, %s, %s, %s)", |
Erik Kline | 117e7f3 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 255 | netId, Arrays.toString(assignedServers), Arrays.toString(domainStrs), |
| 256 | Arrays.toString(params), tlsHostname, Arrays.toString(tlsServers))); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 257 | try { |
| 258 | mNMS.setDnsConfigurationForNetwork( |
Erik Kline | 117e7f3 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 259 | netId, assignedServers, domainStrs, params, tlsHostname, tlsServers); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 260 | } catch (Exception e) { |
| 261 | Slog.e(TAG, "Error setting DNS configuration: " + e); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // TODO: netd should listen on [::1]:53 and proxy queries to the current |
| 266 | // default network, and we should just set net.dns1 to ::1, not least |
| 267 | // because applications attempting to use net.dns resolvers will bypass |
| 268 | // the privacy protections of things like DNS-over-TLS. |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 269 | if (isDefaultNetwork) setDefaultDnsSystemProperties(lp.getDnsServers()); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 270 | flushVmDnsCache(); |
| 271 | } |
| 272 | |
| 273 | public void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 274 | int last = 0; |
| 275 | for (InetAddress dns : dnses) { |
| 276 | ++last; |
| 277 | setNetDnsProperty(last, dns.getHostAddress()); |
| 278 | } |
| 279 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 280 | setNetDnsProperty(i, ""); |
| 281 | } |
| 282 | mNumDnsEntries = last; |
| 283 | } |
| 284 | |
| 285 | private void flushVmDnsCache() { |
| 286 | /* |
| 287 | * Tell the VMs to toss their DNS caches |
| 288 | */ |
| 289 | final Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 290 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 291 | /* |
| 292 | * Connectivity events can happen before boot has completed ... |
| 293 | */ |
| 294 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 295 | final long ident = Binder.clearCallingIdentity(); |
| 296 | try { |
| 297 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 298 | } finally { |
| 299 | Binder.restoreCallingIdentity(ident); |
| 300 | } |
| 301 | } |
| 302 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 303 | private void updateParametersSettings() { |
| 304 | mSampleValidity = getIntSetting( |
| 305 | DNS_RESOLVER_SAMPLE_VALIDITY_SECONDS, |
| 306 | DNS_RESOLVER_DEFAULT_SAMPLE_VALIDITY_SECONDS); |
| 307 | if (mSampleValidity < 0 || mSampleValidity > 65535) { |
| 308 | Slog.w(TAG, "Invalid sampleValidity=" + mSampleValidity + ", using default=" + |
| 309 | DNS_RESOLVER_DEFAULT_SAMPLE_VALIDITY_SECONDS); |
| 310 | mSampleValidity = DNS_RESOLVER_DEFAULT_SAMPLE_VALIDITY_SECONDS; |
| 311 | } |
| 312 | |
| 313 | mSuccessThreshold = getIntSetting( |
| 314 | DNS_RESOLVER_SUCCESS_THRESHOLD_PERCENT, |
| 315 | DNS_RESOLVER_DEFAULT_SUCCESS_THRESHOLD_PERCENT); |
| 316 | if (mSuccessThreshold < 0 || mSuccessThreshold > 100) { |
| 317 | Slog.w(TAG, "Invalid successThreshold=" + mSuccessThreshold + ", using default=" + |
| 318 | DNS_RESOLVER_DEFAULT_SUCCESS_THRESHOLD_PERCENT); |
| 319 | mSuccessThreshold = DNS_RESOLVER_DEFAULT_SUCCESS_THRESHOLD_PERCENT; |
| 320 | } |
| 321 | |
| 322 | mMinSamples = getIntSetting(DNS_RESOLVER_MIN_SAMPLES, DNS_RESOLVER_DEFAULT_MIN_SAMPLES); |
| 323 | mMaxSamples = getIntSetting(DNS_RESOLVER_MAX_SAMPLES, DNS_RESOLVER_DEFAULT_MAX_SAMPLES); |
| 324 | if (mMinSamples < 0 || mMinSamples > mMaxSamples || mMaxSamples > 64) { |
| 325 | Slog.w(TAG, "Invalid sample count (min, max)=(" + mMinSamples + ", " + mMaxSamples + |
| 326 | "), using default=(" + DNS_RESOLVER_DEFAULT_MIN_SAMPLES + ", " + |
| 327 | DNS_RESOLVER_DEFAULT_MAX_SAMPLES + ")"); |
| 328 | mMinSamples = DNS_RESOLVER_DEFAULT_MIN_SAMPLES; |
| 329 | mMaxSamples = DNS_RESOLVER_DEFAULT_MAX_SAMPLES; |
| 330 | } |
| 331 | } |
| 332 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 333 | private int getIntSetting(String which, int dflt) { |
| 334 | return Settings.Global.getInt(mContentResolver, which, dflt); |
| 335 | } |
| 336 | |
| 337 | private void setNetDnsProperty(int which, String value) { |
| 338 | final String key = "net.dns" + which; |
| 339 | // Log and forget errors setting unsupported properties. |
| 340 | try { |
| 341 | mSystemProperties.set(key, value); |
| 342 | } catch (Exception e) { |
| 343 | Slog.e(TAG, "Error setting unsupported net.dns property: ", e); |
| 344 | } |
| 345 | } |
| 346 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 347 | private static String getPrivateDnsMode(ContentResolver cr) { |
| 348 | final String mode = getStringSetting(cr, PRIVATE_DNS_MODE); |
| 349 | return !TextUtils.isEmpty(mode) ? mode : PRIVATE_DNS_DEFAULT_MODE; |
| 350 | } |
| 351 | |
| 352 | private static String getStringSetting(ContentResolver cr, String which) { |
| 353 | return Settings.Global.getString(cr, which); |
| 354 | } |
| 355 | |
| 356 | private static String[] getDomainStrings(String domains) { |
| 357 | return (TextUtils.isEmpty(domains)) ? new String[0] : domains.split(" "); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 358 | } |
| 359 | } |