Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.BIND_VPN_SERVICE; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.NETID_UNSET; |
Jeff Sharkey | 9b2a10f | 2018-01-17 13:27:03 +0900 | [diff] [blame] | 21 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 22 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 23 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 24 | import static android.net.RouteInfo.RTN_THROW; |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 25 | import static android.net.RouteInfo.RTN_UNREACHABLE; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 26 | |
Jeff Davidson | bc19c18 | 2014-11-11 13:20:01 -0800 | [diff] [blame] | 27 | import android.Manifest; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 28 | import android.annotation.NonNull; |
| 29 | import android.annotation.Nullable; |
| 30 | import android.annotation.UserIdInt; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 31 | import android.app.AppGlobals; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 32 | import android.app.AppOpsManager; |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 33 | import android.app.Notification; |
| 34 | import android.app.NotificationManager; |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 35 | import android.app.PendingIntent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 36 | import android.content.BroadcastReceiver; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 37 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 40 | import android.content.IntentFilter; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 42 | import android.content.pm.ApplicationInfo; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 44 | import android.content.pm.PackageManager.NameNotFoundException; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 45 | import android.content.pm.ResolveInfo; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 46 | import android.content.pm.UserInfo; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 47 | import android.net.ConnectivityManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 48 | import android.net.INetworkManagementEventObserver; |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 49 | import android.net.IpPrefix; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 50 | import android.net.LinkAddress; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 51 | import android.net.LinkProperties; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 52 | import android.net.LocalSocket; |
| 53 | import android.net.LocalSocketAddress; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 54 | import android.net.Network; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 55 | import android.net.NetworkAgent; |
| 56 | import android.net.NetworkCapabilities; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 57 | import android.net.NetworkFactory; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 58 | import android.net.NetworkInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 59 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 60 | import android.net.NetworkMisc; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 61 | import android.net.NetworkUtils; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 62 | import android.net.RouteInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 63 | import android.net.UidRange; |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 64 | import android.net.VpnService; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 65 | import android.os.Binder; |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 66 | import android.os.Build.VERSION_CODES; |
| 67 | import android.os.Bundle; |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 68 | import android.os.FileUtils; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 69 | import android.os.IBinder; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 70 | import android.os.INetworkManagementService; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 71 | import android.os.Looper; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 72 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 73 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 74 | import android.os.Process; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 75 | import android.os.RemoteException; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 76 | import android.os.SystemClock; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 77 | import android.os.SystemService; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 78 | import android.os.UserHandle; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 79 | import android.os.UserManager; |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 80 | import android.provider.Settings; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 81 | import android.security.Credentials; |
| 82 | import android.security.KeyStore; |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 83 | import android.text.TextUtils; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 84 | import android.util.ArraySet; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 85 | import android.util.Log; |
| 86 | |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 87 | import com.android.internal.R; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 88 | import com.android.internal.annotations.GuardedBy; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 89 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 90 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 91 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 92 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 93 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 94 | import com.android.internal.net.VpnProfile; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 95 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 96 | import com.android.internal.util.ArrayUtils; |
| 97 | import com.android.server.ConnectivityService; |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 98 | import com.android.server.DeviceIdleController; |
| 99 | import com.android.server.LocalServices; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 100 | import com.android.server.net.BaseNetworkObserver; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 101 | |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 102 | import libcore.io.IoUtils; |
| 103 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 104 | import java.io.File; |
Jeff Davidson | 6bbf39c | 2014-07-23 10:14:53 -0700 | [diff] [blame] | 105 | import java.io.IOException; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 106 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 107 | import java.io.OutputStream; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 108 | import java.math.BigInteger; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 109 | import java.net.Inet4Address; |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 110 | import java.net.Inet6Address; |
| 111 | import java.net.InetAddress; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 112 | import java.nio.charset.StandardCharsets; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 113 | import java.util.ArrayList; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 114 | import java.util.Arrays; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 115 | import java.util.Collection; |
| 116 | import java.util.Collections; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 117 | import java.util.Comparator; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 118 | import java.util.List; |
Lorenzo Colitti | 6fd62b3 | 2017-09-22 21:27:32 +0900 | [diff] [blame] | 119 | import java.util.Objects; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 120 | import java.util.Set; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 121 | import java.util.SortedSet; |
| 122 | import java.util.TreeSet; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 123 | import java.util.concurrent.atomic.AtomicInteger; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 124 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 125 | /** |
| 126 | * @hide |
| 127 | */ |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 128 | public class Vpn { |
| 129 | private static final String NETWORKTYPE = "VPN"; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 130 | private static final String TAG = "Vpn"; |
| 131 | private static final boolean LOGD = true; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 132 | |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 133 | // Length of time (in milliseconds) that an app hosting an always-on VPN is placed on |
| 134 | // the device idle whitelist during service launch and VPN bootstrap. |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 135 | private static final long VPN_LAUNCH_IDLE_WHITELIST_DURATION_MS = 60 * 1000; |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 136 | |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 137 | // Settings for how much of the address space should be routed so that Vpn considers |
| 138 | // "most" of the address space is routed. This is used to determine whether this Vpn |
| 139 | // should be marked with the INTERNET capability. |
| 140 | private static final long MOST_IPV4_ADDRESSES_COUNT; |
| 141 | private static final BigInteger MOST_IPV6_ADDRESSES_COUNT; |
| 142 | static { |
| 143 | // 85% of the address space must be routed for Vpn to consider this VPN to provide |
| 144 | // INTERNET access. |
| 145 | final int howManyPercentIsMost = 85; |
| 146 | |
| 147 | final long twoPower32 = 1L << 32; |
| 148 | MOST_IPV4_ADDRESSES_COUNT = twoPower32 * howManyPercentIsMost / 100; |
| 149 | final BigInteger twoPower128 = BigInteger.ONE.shiftLeft(128); |
| 150 | MOST_IPV6_ADDRESSES_COUNT = twoPower128 |
| 151 | .multiply(BigInteger.valueOf(howManyPercentIsMost)) |
| 152 | .divide(BigInteger.valueOf(100)); |
| 153 | } |
Chalard Jean | e0d26f6 | 2018-03-29 14:10:44 +0900 | [diff] [blame] | 154 | // How many routes to evaluate before bailing and declaring this Vpn should provide |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 155 | // the INTERNET capability. This is necessary because computing the address space is |
Chalard Jean | e0d26f6 | 2018-03-29 14:10:44 +0900 | [diff] [blame] | 156 | // O(n²) and this is running in the system service, so a limit is needed to alleviate |
| 157 | // the risk of attack. |
| 158 | // This is taken as a total of IPv4 + IPV6 routes for simplicity, but the algorithm |
| 159 | // is actually O(n²)+O(n²). |
| 160 | private static final int MAX_ROUTES_TO_EVALUATE = 150; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 161 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 162 | // TODO: create separate trackers for each unique VPN to support |
| 163 | // automated reconnection |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 164 | |
Chalard Jean | 546ec6b | 2018-10-01 12:58:19 +0900 | [diff] [blame] | 165 | private final Context mContext; |
| 166 | private final NetworkInfo mNetworkInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 167 | private String mPackage; |
| 168 | private int mOwnerUID; |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 169 | private boolean mIsPackageTargetingAtLeastQ; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 170 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 171 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 172 | private LegacyVpnRunner mLegacyVpnRunner; |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 173 | private PendingIntent mStatusIntent; |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 174 | private volatile boolean mEnableTeardown = true; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 175 | private final INetworkManagementService mNetd; |
Chalard Jean | f666d0a | 2018-05-18 21:47:45 +0900 | [diff] [blame] | 176 | @VisibleForTesting |
| 177 | protected VpnConfig mConfig; |
| 178 | @VisibleForTesting |
| 179 | protected NetworkAgent mNetworkAgent; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 180 | private final Looper mLooper; |
Chalard Jean | f666d0a | 2018-05-18 21:47:45 +0900 | [diff] [blame] | 181 | @VisibleForTesting |
| 182 | protected final NetworkCapabilities mNetworkCapabilities; |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 183 | private final SystemServices mSystemServices; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 184 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 185 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 186 | * Whether to keep the connection active after rebooting, or upgrading or reinstalling. This |
| 187 | * only applies to {@link VpnService} connections. |
| 188 | */ |
| 189 | private boolean mAlwaysOn = false; |
| 190 | |
| 191 | /** |
| 192 | * Whether to disable traffic outside of this VPN even when the VPN is not connected. System |
| 193 | * apps can still bypass by choosing explicit networks. Has no effect if {@link mAlwaysOn} is |
| 194 | * not set. |
| 195 | */ |
| 196 | private boolean mLockdown = false; |
| 197 | |
| 198 | /** |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 199 | * Set of packages in addition to the VPN app itself that can access the network directly when |
| 200 | * VPN is not connected even if {@code mLockdown} is set. |
| 201 | */ |
| 202 | private @NonNull List<String> mLockdownWhitelist = Collections.emptyList(); |
| 203 | |
| 204 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 205 | * List of UIDs for which networking should be blocked until VPN is ready, during brief periods |
| 206 | * when VPN is not running. For example, during system startup or after a crash. |
| 207 | * @see mLockdown |
| 208 | */ |
| 209 | @GuardedBy("this") |
| 210 | private Set<UidRange> mBlockedUsers = new ArraySet<>(); |
| 211 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 212 | // Handle of the user initiating VPN. |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 213 | private final int mUserHandle; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 214 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 215 | public Vpn(Looper looper, Context context, INetworkManagementService netService, |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 216 | @UserIdInt int userHandle) { |
| 217 | this(looper, context, netService, userHandle, new SystemServices(context)); |
| 218 | } |
| 219 | |
| 220 | @VisibleForTesting |
| 221 | protected Vpn(Looper looper, Context context, INetworkManagementService netService, |
| 222 | int userHandle, SystemServices systemServices) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 223 | mContext = context; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 224 | mNetd = netService; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 225 | mUserHandle = userHandle; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 226 | mLooper = looper; |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 227 | mSystemServices = systemServices; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 228 | |
| 229 | mPackage = VpnConfig.LEGACY_VPN; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 230 | mOwnerUID = getAppUid(mPackage, mUserHandle); |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 231 | mIsPackageTargetingAtLeastQ = doesPackageTargetAtLeastQ(mPackage); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 232 | |
| 233 | try { |
| 234 | netService.registerObserver(mObserver); |
| 235 | } catch (RemoteException e) { |
| 236 | Log.wtf(TAG, "Problem registering observer", e); |
| 237 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 238 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 239 | mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_VPN, 0 /* subtype */, NETWORKTYPE, |
| 240 | "" /* subtypeName */); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 241 | mNetworkCapabilities = new NetworkCapabilities(); |
| 242 | mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_VPN); |
| 243 | mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 244 | updateCapabilities(null /* defaultNetwork */); |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 245 | |
| 246 | loadAlwaysOnPackage(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 249 | /** |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 250 | * Set whether this object is responsible for watching for {@link NetworkInfo} |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 251 | * teardown. When {@code false}, teardown is handled externally by someone |
| 252 | * else. |
| 253 | */ |
| 254 | public void setEnableTeardown(boolean enableTeardown) { |
| 255 | mEnableTeardown = enableTeardown; |
| 256 | } |
| 257 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Update current state, dispaching event to listeners. |
| 260 | */ |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 261 | @VisibleForTesting |
| 262 | protected void updateState(DetailedState detailedState, String reason) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 263 | if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); |
| 264 | mNetworkInfo.setDetailedState(detailedState, reason, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 265 | if (mNetworkAgent != null) { |
| 266 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 267 | } |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 268 | updateAlwaysOnNotification(detailedState); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 271 | /** |
| 272 | * Updates {@link #mNetworkCapabilities} based on current underlying networks and returns a |
| 273 | * defensive copy. |
| 274 | * |
| 275 | * <p>Does not propagate updated capabilities to apps. |
| 276 | * |
| 277 | * @param defaultNetwork underlying network for VPNs following platform's default |
| 278 | */ |
| 279 | public synchronized NetworkCapabilities updateCapabilities( |
| 280 | @Nullable Network defaultNetwork) { |
| 281 | if (mConfig == null) { |
| 282 | // VPN is not running. |
| 283 | return null; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 284 | } |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 285 | |
| 286 | Network[] underlyingNetworks = mConfig.underlyingNetworks; |
| 287 | if (underlyingNetworks == null && defaultNetwork != null) { |
| 288 | // null underlying networks means to track the default. |
| 289 | underlyingNetworks = new Network[] { defaultNetwork }; |
| 290 | } |
| 291 | // Only apps targeting Q and above can explicitly declare themselves as metered. |
| 292 | final boolean isAlwaysMetered = mIsPackageTargetingAtLeastQ && mConfig.isMetered; |
| 293 | |
| 294 | applyUnderlyingCapabilities( |
| 295 | mContext.getSystemService(ConnectivityManager.class), |
| 296 | underlyingNetworks, |
| 297 | mNetworkCapabilities, |
| 298 | isAlwaysMetered); |
| 299 | |
| 300 | return new NetworkCapabilities(mNetworkCapabilities); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @VisibleForTesting |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 304 | public static void applyUnderlyingCapabilities( |
| 305 | ConnectivityManager cm, |
| 306 | Network[] underlyingNetworks, |
| 307 | NetworkCapabilities caps, |
| 308 | boolean isAlwaysMetered) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 309 | int[] transportTypes = new int[] { NetworkCapabilities.TRANSPORT_VPN }; |
| 310 | int downKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; |
| 311 | int upKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 312 | // VPN's meteredness is OR'd with isAlwaysMetered and meteredness of its underlying |
| 313 | // networks. |
| 314 | boolean metered = isAlwaysMetered; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 315 | boolean roaming = false; |
Jeff Sharkey | 9b2a10f | 2018-01-17 13:27:03 +0900 | [diff] [blame] | 316 | boolean congested = false; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 317 | |
Chalard Jean | 3bd5705 | 2018-05-21 15:30:56 +0900 | [diff] [blame] | 318 | boolean hadUnderlyingNetworks = false; |
| 319 | if (null != underlyingNetworks) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 320 | for (Network underlying : underlyingNetworks) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 321 | // TODO(b/124469351): Get capabilities directly from ConnectivityService instead. |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 322 | final NetworkCapabilities underlyingCaps = cm.getNetworkCapabilities(underlying); |
Jeff Sharkey | 4f5e626 | 2018-01-02 11:46:32 -0700 | [diff] [blame] | 323 | if (underlyingCaps == null) continue; |
Chalard Jean | 3bd5705 | 2018-05-21 15:30:56 +0900 | [diff] [blame] | 324 | hadUnderlyingNetworks = true; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 325 | for (int underlyingType : underlyingCaps.getTransportTypes()) { |
| 326 | transportTypes = ArrayUtils.appendInt(transportTypes, underlyingType); |
| 327 | } |
| 328 | |
| 329 | // When we have multiple networks, we have to assume the |
| 330 | // worst-case link speed and restrictions. |
| 331 | downKbps = NetworkCapabilities.minBandwidth(downKbps, |
| 332 | underlyingCaps.getLinkDownstreamBandwidthKbps()); |
| 333 | upKbps = NetworkCapabilities.minBandwidth(upKbps, |
| 334 | underlyingCaps.getLinkUpstreamBandwidthKbps()); |
| 335 | metered |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_METERED); |
| 336 | roaming |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_ROAMING); |
Jeff Sharkey | 9b2a10f | 2018-01-17 13:27:03 +0900 | [diff] [blame] | 337 | congested |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_CONGESTED); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 338 | } |
| 339 | } |
Chalard Jean | 3bd5705 | 2018-05-21 15:30:56 +0900 | [diff] [blame] | 340 | if (!hadUnderlyingNetworks) { |
| 341 | // No idea what the underlying networks are; assume sane defaults |
| 342 | metered = true; |
| 343 | roaming = false; |
| 344 | congested = false; |
| 345 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 346 | |
| 347 | caps.setTransportTypes(transportTypes); |
| 348 | caps.setLinkDownstreamBandwidthKbps(downKbps); |
| 349 | caps.setLinkUpstreamBandwidthKbps(upKbps); |
Jeff Sharkey | 9b2a10f | 2018-01-17 13:27:03 +0900 | [diff] [blame] | 350 | caps.setCapability(NET_CAPABILITY_NOT_METERED, !metered); |
| 351 | caps.setCapability(NET_CAPABILITY_NOT_ROAMING, !roaming); |
| 352 | caps.setCapability(NET_CAPABILITY_NOT_CONGESTED, !congested); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 353 | } |
| 354 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 355 | /** |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 356 | * Chooses whether to force all connections to go though VPN. |
| 357 | * |
| 358 | * Used to enable/disable legacy VPN lockdown. |
| 359 | * |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 360 | * This uses the same ip rule mechanism as |
| 361 | * {@link #setAlwaysOnPackage(String, boolean, List<String>)}; previous settings from calling |
| 362 | * that function will be replaced and saved with the always-on state. |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 363 | * |
| 364 | * @param lockdown whether to prevent all traffic outside of a VPN. |
| 365 | */ |
| 366 | public synchronized void setLockdown(boolean lockdown) { |
| 367 | enforceControlPermissionOrInternalCaller(); |
| 368 | |
| 369 | setVpnForcedLocked(lockdown); |
| 370 | mLockdown = lockdown; |
| 371 | |
| 372 | // Update app lockdown setting if it changed. Legacy VPN lockdown status is controlled by |
| 373 | // LockdownVpnTracker.isEnabled() which keeps track of its own state. |
| 374 | if (mAlwaysOn) { |
| 375 | saveAlwaysOnPackage(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 380 | * Check whether to prevent all traffic outside of a VPN even when the VPN is not connected. |
| 381 | * |
| 382 | * @return {@code true} if VPN lockdown is enabled. |
| 383 | */ |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 384 | public synchronized boolean getLockdown() { |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 385 | return mLockdown; |
| 386 | } |
| 387 | |
| 388 | /** |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 389 | * Returns whether VPN is configured as always-on. |
| 390 | */ |
| 391 | public synchronized boolean getAlwaysOn() { |
| 392 | return mAlwaysOn; |
| 393 | } |
| 394 | |
| 395 | /** |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 396 | * Checks if a VPN app supports always-on mode. |
| 397 | * |
| 398 | * In order to support the always-on feature, an app has to |
| 399 | * <ul> |
| 400 | * <li>target {@link VERSION_CODES#N API 24} or above, and |
Charles He | 5da5ae3 | 2017-08-15 15:30:22 +0100 | [diff] [blame] | 401 | * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON} |
| 402 | * meta-data field. |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 403 | * </ul> |
| 404 | * |
| 405 | * @param packageName the canonical package name of the VPN app |
| 406 | * @return {@code true} if and only if the VPN app exists and supports always-on mode |
| 407 | */ |
| 408 | public boolean isAlwaysOnPackageSupported(String packageName) { |
| 409 | enforceSettingsPermission(); |
| 410 | |
| 411 | if (packageName == null) { |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | PackageManager pm = mContext.getPackageManager(); |
| 416 | ApplicationInfo appInfo = null; |
| 417 | try { |
| 418 | appInfo = pm.getApplicationInfoAsUser(packageName, 0 /*flags*/, mUserHandle); |
| 419 | } catch (NameNotFoundException unused) { |
| 420 | Log.w(TAG, "Can't find \"" + packageName + "\" when checking always-on support"); |
| 421 | } |
| 422 | if (appInfo == null || appInfo.targetSdkVersion < VERSION_CODES.N) { |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | final Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 427 | intent.setPackage(packageName); |
| 428 | List<ResolveInfo> services = |
| 429 | pm.queryIntentServicesAsUser(intent, PackageManager.GET_META_DATA, mUserHandle); |
| 430 | if (services == null || services.size() == 0) { |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | for (ResolveInfo rInfo : services) { |
| 435 | final Bundle metaData = rInfo.serviceInfo.metaData; |
Charles He | 5da5ae3 | 2017-08-15 15:30:22 +0100 | [diff] [blame] | 436 | if (metaData != null && |
| 437 | !metaData.getBoolean(VpnService.SERVICE_META_DATA_SUPPORTS_ALWAYS_ON, true)) { |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | /** |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 446 | * Configures an always-on VPN connection through a specific application. |
| 447 | * This connection is automatically granted and persisted after a reboot. |
| 448 | * |
| 449 | * <p>The designated package should exist and declare a {@link VpnService} in its |
| 450 | * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE}, |
| 451 | * otherwise the call will fail. |
| 452 | * |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 453 | * <p>Note that this method does not check if the VPN app supports always-on mode. The check is |
| 454 | * delayed to {@link #startAlwaysOnVpn()}, which is always called immediately after this |
| 455 | * method in {@link android.net.IConnectivityManager#setAlwaysOnVpnPackage}. |
| 456 | * |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 457 | * @param packageName the package to designate as always-on VPN supplier. |
| 458 | * @param lockdown whether to prevent traffic outside of a VPN, for example while connecting. |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 459 | * @param lockdownWhitelist packages to be whitelisted from lockdown. |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 460 | * @return {@code true} if the package has been set as always-on, {@code false} otherwise. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 461 | */ |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 462 | public synchronized boolean setAlwaysOnPackage( |
| 463 | String packageName, boolean lockdown, List<String> lockdownWhitelist) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 464 | enforceControlPermissionOrInternalCaller(); |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 465 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 466 | if (setAlwaysOnPackageInternal(packageName, lockdown, lockdownWhitelist)) { |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 467 | saveAlwaysOnPackage(); |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Configures an always-on VPN connection through a specific application, the same as |
| 475 | * {@link #setAlwaysOnPackage}. |
| 476 | * |
| 477 | * Does not perform permission checks. Does not persist any of the changes to storage. |
| 478 | * |
| 479 | * @param packageName the package to designate as always-on VPN supplier. |
| 480 | * @param lockdown whether to prevent traffic outside of a VPN, for example while connecting. |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 481 | * @param lockdownWhitelist packages to be whitelisted from lockdown. This is only used if |
| 482 | * {@code lockdown} is {@code true}. Packages must not contain commas. |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 483 | * @return {@code true} if the package has been set as always-on, {@code false} otherwise. |
| 484 | */ |
| 485 | @GuardedBy("this") |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 486 | private boolean setAlwaysOnPackageInternal( |
| 487 | String packageName, boolean lockdown, List<String> lockdownWhitelist) { |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 488 | if (VpnConfig.LEGACY_VPN.equals(packageName)) { |
| 489 | Log.w(TAG, "Not setting legacy VPN \"" + packageName + "\" as always-on."); |
| 490 | return false; |
| 491 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 492 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 493 | if (lockdownWhitelist != null) { |
| 494 | for (String pkg : lockdownWhitelist) { |
| 495 | if (pkg.contains(",")) { |
| 496 | Log.w(TAG, "Not setting always-on vpn, invalid whitelisted package: " + pkg); |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 502 | if (packageName != null) { |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 503 | // Pre-authorize new always-on VPN package. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 504 | if (!setPackageAuthorization(packageName, true)) { |
| 505 | return false; |
| 506 | } |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 507 | mAlwaysOn = true; |
| 508 | } else { |
| 509 | packageName = VpnConfig.LEGACY_VPN; |
| 510 | mAlwaysOn = false; |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 513 | mLockdown = (mAlwaysOn && lockdown); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 514 | mLockdownWhitelist = (mLockdown && lockdownWhitelist != null) |
| 515 | ? Collections.unmodifiableList(new ArrayList<>(lockdownWhitelist)) |
| 516 | : Collections.emptyList(); |
| 517 | |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 518 | if (isCurrentPreparedPackage(packageName)) { |
| 519 | updateAlwaysOnNotification(mNetworkInfo.getDetailedState()); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 520 | setVpnForcedLocked(mLockdown); |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 521 | } else { |
| 522 | // Prepare this app. The notification will update as a side-effect of updateState(). |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 523 | // It also calls setVpnForcedLocked(). |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 524 | prepareInternal(packageName); |
| 525 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 526 | return true; |
| 527 | } |
| 528 | |
Robin Lee | 9ff1a58 | 2016-06-10 16:41:10 +0100 | [diff] [blame] | 529 | private static boolean isNullOrLegacyVpn(String packageName) { |
| 530 | return packageName == null || VpnConfig.LEGACY_VPN.equals(packageName); |
| 531 | } |
| 532 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 533 | /** |
| 534 | * @return the package name of the VPN controller responsible for always-on VPN, |
| 535 | * or {@code null} if none is set or always-on VPN is controlled through |
| 536 | * lockdown instead. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 537 | */ |
| 538 | public synchronized String getAlwaysOnPackage() { |
| 539 | enforceControlPermissionOrInternalCaller(); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 540 | return (mAlwaysOn ? mPackage : null); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 544 | * @return an immutable list of packages whitelisted from always-on VPN lockdown. |
| 545 | */ |
| 546 | public synchronized List<String> getLockdownWhitelist() { |
| 547 | return mLockdown ? mLockdownWhitelist : null; |
| 548 | } |
| 549 | |
| 550 | /** |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 551 | * Save the always-on package and lockdown config into Settings.Secure |
| 552 | */ |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 553 | @GuardedBy("this") |
| 554 | private void saveAlwaysOnPackage() { |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 555 | final long token = Binder.clearCallingIdentity(); |
| 556 | try { |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 557 | mSystemServices.settingsSecurePutStringForUser(Settings.Secure.ALWAYS_ON_VPN_APP, |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 558 | getAlwaysOnPackage(), mUserHandle); |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 559 | mSystemServices.settingsSecurePutIntForUser(Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 560 | (mAlwaysOn && mLockdown ? 1 : 0), mUserHandle); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 561 | mSystemServices.settingsSecurePutStringForUser( |
| 562 | Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST, |
| 563 | String.join(",", mLockdownWhitelist), mUserHandle); |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 564 | } finally { |
| 565 | Binder.restoreCallingIdentity(token); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 570 | * Load the always-on package and lockdown config from Settings.Secure |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 571 | */ |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 572 | @GuardedBy("this") |
| 573 | private void loadAlwaysOnPackage() { |
| 574 | final long token = Binder.clearCallingIdentity(); |
| 575 | try { |
| 576 | final String alwaysOnPackage = mSystemServices.settingsSecureGetStringForUser( |
| 577 | Settings.Secure.ALWAYS_ON_VPN_APP, mUserHandle); |
| 578 | final boolean alwaysOnLockdown = mSystemServices.settingsSecureGetIntForUser( |
| 579 | Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, 0 /*default*/, mUserHandle) != 0; |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 580 | final String whitelistString = mSystemServices.settingsSecureGetStringForUser( |
| 581 | Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST, mUserHandle); |
| 582 | final List<String> whitelistedPackages = TextUtils.isEmpty(whitelistString) |
| 583 | ? Collections.emptyList() : Arrays.asList(whitelistString.split(",")); |
| 584 | setAlwaysOnPackageInternal(alwaysOnPackage, alwaysOnLockdown, whitelistedPackages); |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 585 | } finally { |
| 586 | Binder.restoreCallingIdentity(token); |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * @return {@code true} if the service was started, the service was already connected, or there |
| 592 | * was no always-on VPN to start. {@code false} otherwise. |
| 593 | */ |
| 594 | public boolean startAlwaysOnVpn() { |
| 595 | final String alwaysOnPackage; |
| 596 | synchronized (this) { |
| 597 | alwaysOnPackage = getAlwaysOnPackage(); |
| 598 | // Skip if there is no service to start. |
| 599 | if (alwaysOnPackage == null) { |
| 600 | return true; |
| 601 | } |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 602 | // Remove always-on VPN if it's not supported. |
| 603 | if (!isAlwaysOnPackageSupported(alwaysOnPackage)) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 604 | setAlwaysOnPackage(null, false, null); |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 605 | return false; |
| 606 | } |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 607 | // Skip if the service is already established. This isn't bulletproof: it's not bound |
| 608 | // until after establish(), so if it's mid-setup onStartCommand will be sent twice, |
| 609 | // which may restart the connection. |
| 610 | if (getNetworkInfo().isConnected()) { |
| 611 | return true; |
| 612 | } |
| 613 | } |
| 614 | |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 615 | // Tell the OS that background services in this app need to be allowed for |
| 616 | // a short time, so we can bootstrap the VPN service. |
| 617 | final long oldId = Binder.clearCallingIdentity(); |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 618 | try { |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 619 | DeviceIdleController.LocalService idleController = |
| 620 | LocalServices.getService(DeviceIdleController.LocalService.class); |
| 621 | idleController.addPowerSaveTempWhitelistApp(Process.myUid(), alwaysOnPackage, |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 622 | VPN_LAUNCH_IDLE_WHITELIST_DURATION_MS, mUserHandle, false, "vpn"); |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 623 | |
| 624 | // Start the VPN service declared in the app's manifest. |
| 625 | Intent serviceIntent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 626 | serviceIntent.setPackage(alwaysOnPackage); |
| 627 | try { |
| 628 | return mContext.startServiceAsUser(serviceIntent, UserHandle.of(mUserHandle)) != null; |
| 629 | } catch (RuntimeException e) { |
| 630 | Log.e(TAG, "VpnService " + serviceIntent + " failed to start", e); |
| 631 | return false; |
| 632 | } |
| 633 | } finally { |
| 634 | Binder.restoreCallingIdentity(oldId); |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 639 | * Prepare for a VPN application. This method is designed to solve |
| 640 | * race conditions. It first compares the current prepared package |
| 641 | * with {@code oldPackage}. If they are the same, the prepared |
| 642 | * package is revoked and replaced with {@code newPackage}. If |
| 643 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 644 | * If {@code newPackage} is the same package or {@code null}, the |
| 645 | * revocation is omitted. This method returns {@code true} if the |
| 646 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 647 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 648 | * Legacy VPN is handled specially since it is not a real package. |
| 649 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 650 | * it can be revoked by itself. |
| 651 | * |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 652 | * Note: when we added VPN pre-consent in |
| 653 | * https://android.googlesource.com/platform/frameworks/base/+/0554260 |
| 654 | * the names oldPackage and newPackage became misleading, because when |
| 655 | * an app is pre-consented, we actually prepare oldPackage, not newPackage. |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 656 | * |
| 657 | * Their meanings actually are: |
| 658 | * |
| 659 | * - oldPackage non-null, newPackage null: App calling VpnService#prepare(). |
| 660 | * - oldPackage null, newPackage non-null: ConfirmDialog calling prepareVpn(). |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 661 | * - oldPackage null, newPackage=LEGACY_VPN: Used internally to disconnect |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 662 | * and revoke any current app VPN and re-prepare legacy vpn. |
| 663 | * |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 664 | * TODO: Rename the variables - or split this method into two - and end this confusion. |
| 665 | * TODO: b/29032008 Migrate code from prepare(oldPackage=non-null, newPackage=LEGACY_VPN) |
| 666 | * to prepare(oldPackage=null, newPackage=LEGACY_VPN) |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 667 | * |
| 668 | * @param oldPackage The package name of the old VPN application |
| 669 | * @param newPackage The package name of the new VPN application |
| 670 | * |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 671 | * @return true if the operation succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 672 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 673 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
Jeff Davidson | 0a775ce | 2015-04-27 15:02:48 -0700 | [diff] [blame] | 674 | if (oldPackage != null) { |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 675 | // Stop an existing always-on VPN from being dethroned by other apps. |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 676 | if (mAlwaysOn && !isCurrentPreparedPackage(oldPackage)) { |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 677 | return false; |
| 678 | } |
| 679 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 680 | // Package is not the same or old package was reinstalled. |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 681 | if (!isCurrentPreparedPackage(oldPackage)) { |
Jeff Davidson | 0a775ce | 2015-04-27 15:02:48 -0700 | [diff] [blame] | 682 | // The package doesn't match. We return false (to obtain user consent) unless the |
| 683 | // user has already consented to that VPN package. |
| 684 | if (!oldPackage.equals(VpnConfig.LEGACY_VPN) && isVpnUserPreConsented(oldPackage)) { |
| 685 | prepareInternal(oldPackage); |
| 686 | return true; |
| 687 | } |
| 688 | return false; |
| 689 | } else if (!oldPackage.equals(VpnConfig.LEGACY_VPN) |
| 690 | && !isVpnUserPreConsented(oldPackage)) { |
| 691 | // Currently prepared VPN is revoked, so unprepare it and return false. |
| 692 | prepareInternal(VpnConfig.LEGACY_VPN); |
| 693 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 694 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 697 | // Return true if we do not need to revoke. |
Jeff Davidson | be08587 | 2014-10-24 10:35:53 -0700 | [diff] [blame] | 698 | if (newPackage == null || (!newPackage.equals(VpnConfig.LEGACY_VPN) && |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 699 | isCurrentPreparedPackage(newPackage))) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 700 | return true; |
| 701 | } |
| 702 | |
Robin Lee | 1b1bcd7 | 2016-02-12 18:11:30 +0000 | [diff] [blame] | 703 | // Check that the caller is authorized. |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 704 | enforceControlPermission(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 705 | |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 706 | // Stop an existing always-on VPN from being dethroned by other apps. |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 707 | if (mAlwaysOn && !isCurrentPreparedPackage(newPackage)) { |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 711 | prepareInternal(newPackage); |
| 712 | return true; |
| 713 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 714 | |
Victor Chang | 98a633a | 2016-05-27 17:30:49 +0100 | [diff] [blame] | 715 | private boolean isCurrentPreparedPackage(String packageName) { |
| 716 | // We can't just check that packageName matches mPackage, because if the app was uninstalled |
| 717 | // and reinstalled it will no longer be prepared. Instead check the UID. |
| 718 | return getAppUid(packageName, mUserHandle) == mOwnerUID; |
| 719 | } |
| 720 | |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 721 | /** Prepare the VPN for the given package. Does not perform permission checks. */ |
| 722 | private void prepareInternal(String newPackage) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 723 | long token = Binder.clearCallingIdentity(); |
| 724 | try { |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 725 | // Reset the interface. |
| 726 | if (mInterface != null) { |
| 727 | mStatusIntent = null; |
| 728 | agentDisconnect(); |
| 729 | jniReset(mInterface); |
| 730 | mInterface = null; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 731 | mNetworkCapabilities.setUids(null); |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // Revoke the connection or stop LegacyVpnRunner. |
| 735 | if (mConnection != null) { |
| 736 | try { |
| 737 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 738 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 739 | } catch (Exception e) { |
| 740 | // ignore |
| 741 | } |
| 742 | mContext.unbindService(mConnection); |
| 743 | mConnection = null; |
| 744 | } else if (mLegacyVpnRunner != null) { |
| 745 | mLegacyVpnRunner.exit(); |
| 746 | mLegacyVpnRunner = null; |
| 747 | } |
| 748 | |
| 749 | try { |
| 750 | mNetd.denyProtect(mOwnerUID); |
| 751 | } catch (Exception e) { |
| 752 | Log.wtf(TAG, "Failed to disallow UID " + mOwnerUID + " to call protect() " + e); |
| 753 | } |
| 754 | |
| 755 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 756 | mPackage = newPackage; |
| 757 | mOwnerUID = getAppUid(newPackage, mUserHandle); |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 758 | mIsPackageTargetingAtLeastQ = doesPackageTargetAtLeastQ(newPackage); |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 759 | try { |
| 760 | mNetd.allowProtect(mOwnerUID); |
| 761 | } catch (Exception e) { |
| 762 | Log.wtf(TAG, "Failed to allow UID " + mOwnerUID + " to call protect() " + e); |
| 763 | } |
| 764 | mConfig = null; |
| 765 | |
| 766 | updateState(DetailedState.IDLE, "prepare"); |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 767 | setVpnForcedLocked(mLockdown); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 768 | } finally { |
| 769 | Binder.restoreCallingIdentity(token); |
| 770 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 773 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 774 | * Set whether a package has the ability to launch VPNs without user intervention. |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 775 | */ |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 776 | public boolean setPackageAuthorization(String packageName, boolean authorized) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 777 | // Check if the caller is authorized. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 778 | enforceControlPermissionOrInternalCaller(); |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 779 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 780 | int uid = getAppUid(packageName, mUserHandle); |
| 781 | if (uid == -1 || VpnConfig.LEGACY_VPN.equals(packageName)) { |
| 782 | // Authorization for nonexistent packages (or fake ones) can't be updated. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 783 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | long token = Binder.clearCallingIdentity(); |
| 787 | try { |
| 788 | AppOpsManager appOps = |
| 789 | (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 790 | appOps.setMode(AppOpsManager.OP_ACTIVATE_VPN, uid, packageName, |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 791 | authorized ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 792 | return true; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 793 | } catch (Exception e) { |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 794 | Log.wtf(TAG, "Failed to set app ops for package " + packageName + ", uid " + uid, e); |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 795 | } finally { |
| 796 | Binder.restoreCallingIdentity(token); |
| 797 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 798 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | private boolean isVpnUserPreConsented(String packageName) { |
| 802 | AppOpsManager appOps = |
| 803 | (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 804 | |
| 805 | // Verify that the caller matches the given package and has permission to activate VPNs. |
| 806 | return appOps.noteOpNoThrow(AppOpsManager.OP_ACTIVATE_VPN, Binder.getCallingUid(), |
| 807 | packageName) == AppOpsManager.MODE_ALLOWED; |
| 808 | } |
| 809 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 810 | private int getAppUid(String app, int userHandle) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 811 | if (VpnConfig.LEGACY_VPN.equals(app)) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 812 | return Process.myUid(); |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 813 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 814 | PackageManager pm = mContext.getPackageManager(); |
| 815 | int result; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 816 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 817 | result = pm.getPackageUidAsUser(app, userHandle); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 818 | } catch (NameNotFoundException e) { |
| 819 | result = -1; |
| 820 | } |
| 821 | return result; |
| 822 | } |
| 823 | |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 824 | private boolean doesPackageTargetAtLeastQ(String packageName) { |
| 825 | if (VpnConfig.LEGACY_VPN.equals(packageName)) { |
| 826 | return true; |
| 827 | } |
| 828 | PackageManager pm = mContext.getPackageManager(); |
| 829 | try { |
| 830 | ApplicationInfo appInfo = |
| 831 | pm.getApplicationInfoAsUser(packageName, 0 /*flags*/, mUserHandle); |
| 832 | return appInfo.targetSdkVersion >= VERSION_CODES.Q; |
| 833 | } catch (NameNotFoundException unused) { |
| 834 | Log.w(TAG, "Can't find \"" + packageName + "\""); |
| 835 | return false; |
| 836 | } |
| 837 | } |
| 838 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 839 | public NetworkInfo getNetworkInfo() { |
| 840 | return mNetworkInfo; |
| 841 | } |
| 842 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 843 | public int getNetId() { |
| 844 | return mNetworkAgent != null ? mNetworkAgent.netId : NETID_UNSET; |
| 845 | } |
| 846 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 847 | private LinkProperties makeLinkProperties() { |
| 848 | boolean allowIPv4 = mConfig.allowIPv4; |
| 849 | boolean allowIPv6 = mConfig.allowIPv6; |
| 850 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 851 | LinkProperties lp = new LinkProperties(); |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 852 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 853 | lp.setInterfaceName(mInterface); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 854 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 855 | if (mConfig.addresses != null) { |
| 856 | for (LinkAddress address : mConfig.addresses) { |
| 857 | lp.addLinkAddress(address); |
| 858 | allowIPv4 |= address.getAddress() instanceof Inet4Address; |
| 859 | allowIPv6 |= address.getAddress() instanceof Inet6Address; |
| 860 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 861 | } |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 862 | |
| 863 | if (mConfig.routes != null) { |
| 864 | for (RouteInfo route : mConfig.routes) { |
| 865 | lp.addRoute(route); |
| 866 | InetAddress address = route.getDestination().getAddress(); |
| 867 | allowIPv4 |= address instanceof Inet4Address; |
| 868 | allowIPv6 |= address instanceof Inet6Address; |
| 869 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 870 | } |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 871 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 872 | if (mConfig.dnsServers != null) { |
| 873 | for (String dnsServer : mConfig.dnsServers) { |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 874 | InetAddress address = InetAddress.parseNumericAddress(dnsServer); |
| 875 | lp.addDnsServer(address); |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 876 | allowIPv4 |= address instanceof Inet4Address; |
| 877 | allowIPv6 |= address instanceof Inet6Address; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 878 | } |
| 879 | } |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 880 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 881 | lp.setHttpProxy(mConfig.proxyInfo); |
| 882 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 883 | if (!allowIPv4) { |
| 884 | lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE)); |
| 885 | } |
| 886 | if (!allowIPv6) { |
| 887 | lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE)); |
| 888 | } |
| 889 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 890 | // Concatenate search domains into a string. |
| 891 | StringBuilder buffer = new StringBuilder(); |
| 892 | if (mConfig.searchDomains != null) { |
| 893 | for (String domain : mConfig.searchDomains) { |
| 894 | buffer.append(domain).append(' '); |
| 895 | } |
| 896 | } |
| 897 | lp.setDomains(buffer.toString().trim()); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 898 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 899 | // TODO: Stop setting the MTU in jniCreate and set it here. |
| 900 | |
| 901 | return lp; |
| 902 | } |
| 903 | |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 904 | /** |
| 905 | * Analyzes the passed LinkedProperties to figure out whether it routes to most of the IP space. |
| 906 | * |
| 907 | * This returns true if the passed LinkedProperties contains routes to either most of the IPv4 |
| 908 | * space or to most of the IPv6 address space, where "most" is defined by the value of the |
| 909 | * MOST_IPV{4,6}_ADDRESSES_COUNT constants : if more than this number of addresses are matched |
| 910 | * by any of the routes, then it's decided that most of the space is routed. |
| 911 | * @hide |
| 912 | */ |
| 913 | @VisibleForTesting |
| 914 | static boolean providesRoutesToMostDestinations(LinkProperties lp) { |
Chalard Jean | e0d26f6 | 2018-03-29 14:10:44 +0900 | [diff] [blame] | 915 | final List<RouteInfo> routes = lp.getAllRoutes(); |
| 916 | if (routes.size() > MAX_ROUTES_TO_EVALUATE) return true; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 917 | final Comparator<IpPrefix> prefixLengthComparator = IpPrefix.lengthComparator(); |
| 918 | TreeSet<IpPrefix> ipv4Prefixes = new TreeSet<>(prefixLengthComparator); |
| 919 | TreeSet<IpPrefix> ipv6Prefixes = new TreeSet<>(prefixLengthComparator); |
Chalard Jean | e0d26f6 | 2018-03-29 14:10:44 +0900 | [diff] [blame] | 920 | for (final RouteInfo route : routes) { |
Rubin Xu | c63e48d | 2019-02-19 16:56:47 +0000 | [diff] [blame] | 921 | if (route.getType() == RouteInfo.RTN_UNREACHABLE) continue; |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 922 | IpPrefix destination = route.getDestination(); |
| 923 | if (destination.isIPv4()) { |
| 924 | ipv4Prefixes.add(destination); |
| 925 | } else { |
| 926 | ipv6Prefixes.add(destination); |
| 927 | } |
| 928 | } |
| 929 | if (NetworkUtils.routedIPv4AddressCount(ipv4Prefixes) > MOST_IPV4_ADDRESSES_COUNT) { |
| 930 | return true; |
| 931 | } |
| 932 | return NetworkUtils.routedIPv6AddressCount(ipv6Prefixes) |
| 933 | .compareTo(MOST_IPV6_ADDRESSES_COUNT) >= 0; |
| 934 | } |
| 935 | |
Lorenzo Colitti | 6fd62b3 | 2017-09-22 21:27:32 +0900 | [diff] [blame] | 936 | /** |
| 937 | * Attempt to perform a seamless handover of VPNs by only updating LinkProperties without |
| 938 | * registering a new NetworkAgent. This is not always possible if the new VPN configuration |
| 939 | * has certain changes, in which case this method would just return {@code false}. |
| 940 | */ |
| 941 | private boolean updateLinkPropertiesInPlaceIfPossible(NetworkAgent agent, VpnConfig oldConfig) { |
| 942 | // NetworkMisc cannot be updated without registering a new NetworkAgent. |
| 943 | if (oldConfig.allowBypass != mConfig.allowBypass) { |
| 944 | Log.i(TAG, "Handover not possible due to changes to allowBypass"); |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | // TODO: we currently do not support seamless handover if the allowed or disallowed |
| 949 | // applications have changed. Consider diffing UID ranges and only applying the delta. |
| 950 | if (!Objects.equals(oldConfig.allowedApplications, mConfig.allowedApplications) || |
| 951 | !Objects.equals(oldConfig.disallowedApplications, mConfig.disallowedApplications)) { |
| 952 | Log.i(TAG, "Handover not possible due to changes to whitelisted/blacklisted apps"); |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | LinkProperties lp = makeLinkProperties(); |
| 957 | final boolean hadInternetCapability = mNetworkCapabilities.hasCapability( |
| 958 | NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 959 | final boolean willHaveInternetCapability = providesRoutesToMostDestinations(lp); |
| 960 | if (hadInternetCapability != willHaveInternetCapability) { |
| 961 | // A seamless handover would have led to a change to INTERNET capability, which |
| 962 | // is supposed to be immutable for a given network. In this case bail out and do not |
| 963 | // perform handover. |
| 964 | Log.i(TAG, "Handover not possible due to changes to INTERNET capability"); |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | agent.sendLinkProperties(lp); |
| 969 | return true; |
| 970 | } |
| 971 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 972 | private void agentConnect() { |
| 973 | LinkProperties lp = makeLinkProperties(); |
| 974 | |
Chalard Jean | adbf1d0 | 2018-02-26 11:52:46 +0900 | [diff] [blame] | 975 | if (providesRoutesToMostDestinations(lp)) { |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 976 | mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 977 | } else { |
| 978 | mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 979 | } |
| 980 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 981 | mNetworkInfo.setDetailedState(DetailedState.CONNECTING, null, null); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 982 | |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 983 | NetworkMisc networkMisc = new NetworkMisc(); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 984 | networkMisc.allowBypass = mConfig.allowBypass && !mLockdown; |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 985 | |
Chalard Jean | f474fc3 | 2018-01-17 15:10:05 +0900 | [diff] [blame] | 986 | mNetworkCapabilities.setEstablishingVpnAppUid(Binder.getCallingUid()); |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 987 | mNetworkCapabilities.setUids(createUserAndRestrictedProfilesRanges(mUserHandle, |
| 988 | mConfig.allowedApplications, mConfig.disallowedApplications)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 989 | long token = Binder.clearCallingIdentity(); |
| 990 | try { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 991 | mNetworkAgent = new NetworkAgent(mLooper, mContext, NETWORKTYPE /* logtag */, |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 992 | mNetworkInfo, mNetworkCapabilities, lp, |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 993 | ConnectivityConstants.VPN_DEFAULT_SCORE, networkMisc, |
| 994 | NetworkFactory.SerialNumber.VPN) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 995 | @Override |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 996 | public void unwanted() { |
| 997 | // We are user controlled, not driven by NetworkRequest. |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 998 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 999 | }; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1000 | } finally { |
| 1001 | Binder.restoreCallingIdentity(token); |
| 1002 | } |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 1003 | mNetworkInfo.setIsAvailable(true); |
| 1004 | updateState(DetailedState.CONNECTED, "agentConnect"); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1005 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1006 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1007 | private boolean canHaveRestrictedProfile(int userId) { |
| 1008 | long token = Binder.clearCallingIdentity(); |
| 1009 | try { |
| 1010 | return UserManager.get(mContext).canHaveRestrictedProfile(userId); |
| 1011 | } finally { |
| 1012 | Binder.restoreCallingIdentity(token); |
| 1013 | } |
| 1014 | } |
| 1015 | |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1016 | private void agentDisconnect(NetworkAgent networkAgent) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1017 | if (networkAgent != null) { |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1018 | NetworkInfo networkInfo = new NetworkInfo(mNetworkInfo); |
| 1019 | networkInfo.setIsAvailable(false); |
| 1020 | networkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1021 | networkAgent.sendNetworkInfo(networkInfo); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1025 | private void agentDisconnect() { |
| 1026 | if (mNetworkInfo.isConnected()) { |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1027 | mNetworkInfo.setIsAvailable(false); |
| 1028 | updateState(DetailedState.DISCONNECTED, "agentDisconnect"); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1029 | mNetworkAgent = null; |
| 1030 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1034 | * Establish a VPN network and return the file descriptor of the VPN interface. This methods |
| 1035 | * returns {@code null} if the application is revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1036 | * |
| 1037 | * @param config The parameters to configure the network. |
| 1038 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1039 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 1040 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1041 | // Check if the caller is already prepared. |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1042 | UserManager mgr = UserManager.get(mContext); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1043 | if (Binder.getCallingUid() != mOwnerUID) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 1044 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1045 | } |
Jeff Davidson | 0a775ce | 2015-04-27 15:02:48 -0700 | [diff] [blame] | 1046 | // Check to ensure consent hasn't been revoked since we were prepared. |
| 1047 | if (!isVpnUserPreConsented(mPackage)) { |
| 1048 | return null; |
| 1049 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 1050 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1051 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 1052 | intent.setClassName(mPackage, config.user); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1053 | long token = Binder.clearCallingIdentity(); |
| 1054 | try { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1055 | // Restricted users are not allowed to create VPNs, they are tied to Owner |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1056 | UserInfo user = mgr.getUserInfo(mUserHandle); |
Robin Lee | 628ae0d | 2016-05-20 14:53:48 +0100 | [diff] [blame] | 1057 | if (user.isRestricted()) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1058 | throw new SecurityException("Restricted users cannot establish VPNs"); |
| 1059 | } |
| 1060 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1061 | ResolveInfo info = AppGlobals.getPackageManager().resolveService(intent, |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1062 | null, 0, mUserHandle); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1063 | if (info == null) { |
| 1064 | throw new SecurityException("Cannot find " + config.user); |
| 1065 | } |
| 1066 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 1067 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 1068 | } |
| 1069 | } catch (RemoteException e) { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1070 | throw new SecurityException("Cannot find " + config.user); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1071 | } finally { |
| 1072 | Binder.restoreCallingIdentity(token); |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1073 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 1074 | |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1075 | // Save the old config in case we need to go back. |
| 1076 | VpnConfig oldConfig = mConfig; |
| 1077 | String oldInterface = mInterface; |
| 1078 | Connection oldConnection = mConnection; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1079 | NetworkAgent oldNetworkAgent = mNetworkAgent; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1080 | Set<UidRange> oldUsers = mNetworkCapabilities.getUids(); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1081 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1082 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1083 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1084 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1085 | String interfaze = jniGetName(tun.getFd()); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1086 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1087 | // TEMP use the old jni calls until there is support for netd address setting |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1088 | StringBuilder builder = new StringBuilder(); |
| 1089 | for (LinkAddress address : config.addresses) { |
| 1090 | builder.append(" " + address); |
| 1091 | } |
| 1092 | if (jniSetAddresses(interfaze, builder.toString()) < 1) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1093 | throw new IllegalArgumentException("At least one address must be specified"); |
| 1094 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1095 | Connection connection = new Connection(); |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 1096 | if (!mContext.bindServiceAsUser(intent, connection, |
| 1097 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 1098 | new UserHandle(mUserHandle))) { |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1099 | throw new IllegalStateException("Cannot bind " + config.user); |
| 1100 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1101 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1102 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1103 | mInterface = interfaze; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1104 | |
| 1105 | // Fill more values. |
| 1106 | config.user = mPackage; |
| 1107 | config.interfaze = mInterface; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1108 | config.startTime = SystemClock.elapsedRealtime(); |
| 1109 | mConfig = config; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1110 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1111 | // Set up forwarding and DNS rules. |
Lorenzo Colitti | 6fd62b3 | 2017-09-22 21:27:32 +0900 | [diff] [blame] | 1112 | // First attempt to do a seamless handover that only changes the interface name and |
| 1113 | // parameters. If that fails, disconnect. |
| 1114 | if (oldConfig != null |
| 1115 | && updateLinkPropertiesInPlaceIfPossible(mNetworkAgent, oldConfig)) { |
| 1116 | // Keep mNetworkAgent unchanged |
| 1117 | } else { |
| 1118 | mNetworkAgent = null; |
| 1119 | updateState(DetailedState.CONNECTING, "establish"); |
| 1120 | // Set up forwarding and DNS rules. |
| 1121 | agentConnect(); |
| 1122 | // Remove the old tun's user forwarding rules |
| 1123 | // The new tun's user rules have already been added above so they will take over |
| 1124 | // as rules are deleted. This prevents data leakage as the rules are moved over. |
| 1125 | agentDisconnect(oldNetworkAgent); |
| 1126 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (oldConnection != null) { |
| 1129 | mContext.unbindService(oldConnection); |
| 1130 | } |
Lorenzo Colitti | 6fd62b3 | 2017-09-22 21:27:32 +0900 | [diff] [blame] | 1131 | |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1132 | if (oldInterface != null && !oldInterface.equals(interfaze)) { |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1133 | jniReset(oldInterface); |
| 1134 | } |
Jeff Davidson | 6bbf39c | 2014-07-23 10:14:53 -0700 | [diff] [blame] | 1135 | |
| 1136 | try { |
| 1137 | IoUtils.setBlocking(tun.getFileDescriptor(), config.blocking); |
| 1138 | } catch (IOException e) { |
| 1139 | throw new IllegalStateException( |
| 1140 | "Cannot set tunnel's fd as blocking=" + config.blocking, e); |
| 1141 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1142 | } catch (RuntimeException e) { |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1143 | IoUtils.closeQuietly(tun); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1144 | agentDisconnect(); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1145 | // restore old state |
| 1146 | mConfig = oldConfig; |
| 1147 | mConnection = oldConnection; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1148 | mNetworkCapabilities.setUids(oldUsers); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1149 | mNetworkAgent = oldNetworkAgent; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1150 | mInterface = oldInterface; |
| 1151 | throw e; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1152 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 1153 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1154 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1157 | private boolean isRunningLocked() { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1158 | return mNetworkAgent != null && mInterface != null; |
| 1159 | } |
| 1160 | |
| 1161 | // Returns true if the VPN has been established and the calling UID is its owner. Used to check |
| 1162 | // that a call to mutate VPN state is admissible. |
Chalard Jean | f666d0a | 2018-05-18 21:47:45 +0900 | [diff] [blame] | 1163 | @VisibleForTesting |
| 1164 | protected boolean isCallerEstablishedOwnerLocked() { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1165 | return isRunningLocked() && Binder.getCallingUid() == mOwnerUID; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1168 | // Note: Return type guarantees results are deduped and sorted, which callers require. |
| 1169 | private SortedSet<Integer> getAppsUids(List<String> packageNames, int userHandle) { |
| 1170 | SortedSet<Integer> uids = new TreeSet<Integer>(); |
| 1171 | for (String app : packageNames) { |
| 1172 | int uid = getAppUid(app, userHandle); |
| 1173 | if (uid != -1) uids.add(uid); |
| 1174 | } |
| 1175 | return uids; |
| 1176 | } |
| 1177 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1178 | /** |
| 1179 | * Creates a {@link Set} of non-intersecting {@link UidRange} objects including all UIDs |
| 1180 | * associated with one user, and any restricted profiles attached to that user. |
| 1181 | * |
| 1182 | * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided, |
| 1183 | * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs |
| 1184 | * in each user and profile will be included. |
| 1185 | * |
| 1186 | * @param userHandle The userId to create UID ranges for along with any of its restricted |
| 1187 | * profiles. |
| 1188 | * @param allowedApplications (optional) whitelist of applications to include. |
| 1189 | * @param disallowedApplications (optional) blacklist of applications to exclude. |
| 1190 | */ |
| 1191 | @VisibleForTesting |
| 1192 | Set<UidRange> createUserAndRestrictedProfilesRanges(@UserIdInt int userHandle, |
| 1193 | @Nullable List<String> allowedApplications, |
| 1194 | @Nullable List<String> disallowedApplications) { |
| 1195 | final Set<UidRange> ranges = new ArraySet<>(); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 1196 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1197 | // Assign the top-level user to the set of ranges |
| 1198 | addUserToRanges(ranges, userHandle, allowedApplications, disallowedApplications); |
| 1199 | |
| 1200 | // If the user can have restricted profiles, assign all its restricted profiles too |
| 1201 | if (canHaveRestrictedProfile(userHandle)) { |
| 1202 | final long token = Binder.clearCallingIdentity(); |
| 1203 | List<UserInfo> users; |
| 1204 | try { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1205 | users = UserManager.get(mContext).getUsers(true); |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1206 | } finally { |
| 1207 | Binder.restoreCallingIdentity(token); |
| 1208 | } |
| 1209 | for (UserInfo user : users) { |
| 1210 | if (user.isRestricted() && (user.restrictedProfileParentId == userHandle)) { |
| 1211 | addUserToRanges(ranges, user.id, allowedApplications, disallowedApplications); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | return ranges; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Updates a {@link Set} of non-intersecting {@link UidRange} objects to include all UIDs |
| 1220 | * associated with one user. |
| 1221 | * |
| 1222 | * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided, |
| 1223 | * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs |
| 1224 | * in the user will be included. |
| 1225 | * |
| 1226 | * @param ranges {@link Set} of {@link UidRange}s to which to add. |
| 1227 | * @param userHandle The userId to add to {@param ranges}. |
| 1228 | * @param allowedApplications (optional) whitelist of applications to include. |
| 1229 | * @param disallowedApplications (optional) blacklist of applications to exclude. |
| 1230 | */ |
| 1231 | @VisibleForTesting |
| 1232 | void addUserToRanges(@NonNull Set<UidRange> ranges, @UserIdInt int userHandle, |
| 1233 | @Nullable List<String> allowedApplications, |
| 1234 | @Nullable List<String> disallowedApplications) { |
| 1235 | if (allowedApplications != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1236 | // Add ranges covering all UIDs for allowedApplications. |
| 1237 | int start = -1, stop = -1; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1238 | for (int uid : getAppsUids(allowedApplications, userHandle)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1239 | if (start == -1) { |
| 1240 | start = uid; |
| 1241 | } else if (uid != stop + 1) { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1242 | ranges.add(new UidRange(start, stop)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1243 | start = uid; |
| 1244 | } |
| 1245 | stop = uid; |
| 1246 | } |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1247 | if (start != -1) ranges.add(new UidRange(start, stop)); |
| 1248 | } else if (disallowedApplications != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1249 | // Add all ranges for user skipping UIDs for disallowedApplications. |
| 1250 | final UidRange userRange = UidRange.createForUser(userHandle); |
| 1251 | int start = userRange.start; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1252 | for (int uid : getAppsUids(disallowedApplications, userHandle)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1253 | if (uid == start) { |
| 1254 | start++; |
| 1255 | } else { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1256 | ranges.add(new UidRange(start, uid - 1)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1257 | start = uid + 1; |
| 1258 | } |
| 1259 | } |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1260 | if (start <= userRange.stop) ranges.add(new UidRange(start, userRange.stop)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1261 | } else { |
| 1262 | // Add all UIDs for the user. |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1263 | ranges.add(UidRange.createForUser(userHandle)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1264 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1267 | // Returns the subset of the full list of active UID ranges the VPN applies to (mVpnUsers) that |
| 1268 | // apply to userHandle. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1269 | static private List<UidRange> uidRangesForUser(int userHandle, Set<UidRange> existingRanges) { |
| 1270 | // UidRange#createForUser returns the entire range of UIDs available to a macro-user. |
| 1271 | // This is something like 0-99999 ; {@see UserHandle#PER_USER_RANGE} |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1272 | final UidRange userRange = UidRange.createForUser(userHandle); |
| 1273 | final List<UidRange> ranges = new ArrayList<UidRange>(); |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1274 | for (UidRange range : existingRanges) { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 1275 | if (userRange.containsRange(range)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1276 | ranges.add(range); |
| 1277 | } |
| 1278 | } |
| 1279 | return ranges; |
| 1280 | } |
| 1281 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1282 | /** |
| 1283 | * Updates UID ranges for this VPN and also updates its internal capabilities. |
| 1284 | * |
| 1285 | * <p>Should be called on primary ConnectivityService thread. |
| 1286 | */ |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1287 | public void onUserAdded(int userHandle) { |
| 1288 | // If the user is restricted tie them to the parent user's VPN |
| 1289 | UserInfo user = UserManager.get(mContext).getUserInfo(userHandle); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1290 | if (user.isRestricted() && user.restrictedProfileParentId == mUserHandle) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1291 | synchronized(Vpn.this) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1292 | final Set<UidRange> existingRanges = mNetworkCapabilities.getUids(); |
| 1293 | if (existingRanges != null) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1294 | try { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1295 | addUserToRanges(existingRanges, userHandle, mConfig.allowedApplications, |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1296 | mConfig.disallowedApplications); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1297 | // ConnectivityService will call {@link #updateCapabilities} and apply |
| 1298 | // those for VPN network. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1299 | mNetworkCapabilities.setUids(existingRanges); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1300 | } catch (Exception e) { |
| 1301 | Log.wtf(TAG, "Failed to add restricted user to owner", e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1302 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1303 | } |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1304 | setVpnForcedLocked(mLockdown); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1309 | /** |
| 1310 | * Updates UID ranges for this VPN and also updates its capabilities. |
| 1311 | * |
| 1312 | * <p>Should be called on primary ConnectivityService thread. |
| 1313 | */ |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1314 | public void onUserRemoved(int userHandle) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1315 | // clean up if restricted |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1316 | UserInfo user = UserManager.get(mContext).getUserInfo(userHandle); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1317 | if (user.isRestricted() && user.restrictedProfileParentId == mUserHandle) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1318 | synchronized(Vpn.this) { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1319 | final Set<UidRange> existingRanges = mNetworkCapabilities.getUids(); |
| 1320 | if (existingRanges != null) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1321 | try { |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1322 | final List<UidRange> removedRanges = |
| 1323 | uidRangesForUser(userHandle, existingRanges); |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1324 | existingRanges.removeAll(removedRanges); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1325 | // ConnectivityService will call {@link #updateCapabilities} and |
| 1326 | // apply those for VPN network. |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1327 | mNetworkCapabilities.setUids(existingRanges); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1328 | } catch (Exception e) { |
| 1329 | Log.wtf(TAG, "Failed to remove restricted user to owner", e); |
| 1330 | } |
| 1331 | } |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1332 | setVpnForcedLocked(mLockdown); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 1337 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1338 | * Called when the user associated with this VPN has just been stopped. |
| 1339 | */ |
| 1340 | public synchronized void onUserStopped() { |
| 1341 | // Switch off networking lockdown (if it was enabled) |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1342 | setLockdown(false); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1343 | mAlwaysOn = false; |
| 1344 | |
| 1345 | // Quit any active connections |
| 1346 | agentDisconnect(); |
| 1347 | } |
| 1348 | |
| 1349 | /** |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1350 | * Restricts network access from all UIDs affected by this {@link Vpn}, apart from the VPN |
| 1351 | * service app itself and whitelisted packages, to only sockets that have had {@code protect()} |
| 1352 | * called on them. All non-VPN traffic is blocked via a {@code PROHIBIT} response from the |
| 1353 | * kernel. |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1354 | * |
| 1355 | * The exception for the VPN UID isn't technically necessary -- setup should use protected |
| 1356 | * sockets -- but in practice it saves apps that don't protect their sockets from breaking. |
| 1357 | * |
| 1358 | * Calling multiple times with {@param enforce} = {@code true} will recreate the set of UIDs to |
| 1359 | * block every time, and if anything has changed update using {@link #setAllowOnlyVpnForUids}. |
| 1360 | * |
| 1361 | * @param enforce {@code true} to require that all traffic under the jurisdiction of this |
| 1362 | * {@link Vpn} goes through a VPN connection or is blocked until one is |
| 1363 | * available, {@code false} to lift the requirement. |
| 1364 | * |
| 1365 | * @see #mBlockedUsers |
| 1366 | */ |
| 1367 | @GuardedBy("this") |
| 1368 | private void setVpnForcedLocked(boolean enforce) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1369 | final List<String> exemptedPackages; |
| 1370 | if (isNullOrLegacyVpn(mPackage)) { |
| 1371 | exemptedPackages = null; |
| 1372 | } else { |
| 1373 | exemptedPackages = new ArrayList<>(mLockdownWhitelist); |
| 1374 | exemptedPackages.add(mPackage); |
| 1375 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1376 | final Set<UidRange> removedRanges = new ArraySet<>(mBlockedUsers); |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1377 | |
| 1378 | Set<UidRange> addedRanges = Collections.emptySet(); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1379 | if (enforce) { |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1380 | addedRanges = createUserAndRestrictedProfilesRanges(mUserHandle, |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1381 | /* allowedApplications */ null, |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1382 | /* disallowedApplications */ exemptedPackages); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1383 | |
Bernie Innocenti | 00000fe | 2018-05-28 22:04:37 +0900 | [diff] [blame] | 1384 | // The UID range of the first user (0-99999) would block the IPSec traffic, which comes |
| 1385 | // directly from the kernel and is marked as uid=0. So we adjust the range to allow |
| 1386 | // it through (b/69873852). |
| 1387 | for (UidRange range : addedRanges) { |
| 1388 | if (range.start == 0) { |
| 1389 | addedRanges.remove(range); |
| 1390 | if (range.stop != 0) { |
| 1391 | addedRanges.add(new UidRange(1, range.stop)); |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1396 | removedRanges.removeAll(addedRanges); |
| 1397 | addedRanges.removeAll(mBlockedUsers); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1398 | } |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 1399 | |
| 1400 | setAllowOnlyVpnForUids(false, removedRanges); |
| 1401 | setAllowOnlyVpnForUids(true, addedRanges); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * Either add or remove a list of {@link UidRange}s to the list of UIDs that are only allowed |
| 1406 | * to make connections through sockets that have had {@code protect()} called on them. |
| 1407 | * |
| 1408 | * @param enforce {@code true} to add to the blacklist, {@code false} to remove. |
| 1409 | * @param ranges {@link Collection} of {@link UidRange}s to add (if {@param enforce} is |
| 1410 | * {@code true}) or to remove. |
| 1411 | * @return {@code true} if all of the UIDs were added/removed. {@code false} otherwise, |
| 1412 | * including added ranges that already existed or removed ones that didn't. |
| 1413 | */ |
| 1414 | @GuardedBy("this") |
| 1415 | private boolean setAllowOnlyVpnForUids(boolean enforce, Collection<UidRange> ranges) { |
| 1416 | if (ranges.size() == 0) { |
| 1417 | return true; |
| 1418 | } |
| 1419 | final UidRange[] rangesArray = ranges.toArray(new UidRange[ranges.size()]); |
| 1420 | try { |
| 1421 | mNetd.setAllowOnlyVpnForUids(enforce, rangesArray); |
| 1422 | } catch (RemoteException | RuntimeException e) { |
| 1423 | Log.e(TAG, "Updating blocked=" + enforce |
| 1424 | + " for UIDs " + Arrays.toString(ranges.toArray()) + " failed", e); |
| 1425 | return false; |
| 1426 | } |
| 1427 | if (enforce) { |
| 1428 | mBlockedUsers.addAll(ranges); |
| 1429 | } else { |
| 1430 | mBlockedUsers.removeAll(ranges); |
| 1431 | } |
| 1432 | return true; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 1436 | * Return the configuration of the currently running VPN. |
| 1437 | */ |
| 1438 | public VpnConfig getVpnConfig() { |
| 1439 | enforceControlPermission(); |
| 1440 | return mConfig; |
| 1441 | } |
| 1442 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1443 | @Deprecated |
| 1444 | public synchronized void interfaceStatusChanged(String iface, boolean up) { |
| 1445 | try { |
| 1446 | mObserver.interfaceStatusChanged(iface, up); |
| 1447 | } catch (RemoteException e) { |
| 1448 | // ignored; target is local |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1452 | private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() { |
| 1453 | @Override |
| 1454 | public void interfaceStatusChanged(String interfaze, boolean up) { |
| 1455 | synchronized (Vpn.this) { |
| 1456 | if (!up && mLegacyVpnRunner != null) { |
| 1457 | mLegacyVpnRunner.check(interfaze); |
| 1458 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1459 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1460 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1461 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1462 | @Override |
| 1463 | public void interfaceRemoved(String interfaze) { |
| 1464 | synchronized (Vpn.this) { |
| 1465 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 1466 | mStatusIntent = null; |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1467 | mNetworkCapabilities.setUids(null); |
Paul Jensen | c4c7231 | 2014-12-08 14:04:51 -0500 | [diff] [blame] | 1468 | mConfig = null; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1469 | mInterface = null; |
| 1470 | if (mConnection != null) { |
| 1471 | mContext.unbindService(mConnection); |
| 1472 | mConnection = null; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1473 | agentDisconnect(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1474 | } else if (mLegacyVpnRunner != null) { |
| 1475 | mLegacyVpnRunner.exit(); |
| 1476 | mLegacyVpnRunner = null; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1482 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 1483 | private void enforceControlPermission() { |
Jeff Davidson | bc19c18 | 2014-11-11 13:20:01 -0800 | [diff] [blame] | 1484 | mContext.enforceCallingPermission(Manifest.permission.CONTROL_VPN, "Unauthorized Caller"); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1487 | private void enforceControlPermissionOrInternalCaller() { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1488 | // Require the caller to be either an application with CONTROL_VPN permission or a process |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1489 | // in the system server. |
| 1490 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONTROL_VPN, |
| 1491 | "Unauthorized Caller"); |
| 1492 | } |
| 1493 | |
Charles He | a0a87e8 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1494 | private void enforceSettingsPermission() { |
| 1495 | mContext.enforceCallingOrSelfPermission(Manifest.permission.NETWORK_SETTINGS, |
| 1496 | "Unauthorized Caller"); |
| 1497 | } |
| 1498 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 1499 | private class Connection implements ServiceConnection { |
| 1500 | private IBinder mService; |
| 1501 | |
| 1502 | @Override |
| 1503 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1504 | mService = service; |
| 1505 | } |
| 1506 | |
| 1507 | @Override |
| 1508 | public void onServiceDisconnected(ComponentName name) { |
| 1509 | mService = null; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 1513 | private void prepareStatusIntent() { |
| 1514 | final long token = Binder.clearCallingIdentity(); |
| 1515 | try { |
| 1516 | mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext); |
| 1517 | } finally { |
| 1518 | Binder.restoreCallingIdentity(token); |
| 1519 | } |
| 1520 | } |
| 1521 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1522 | public synchronized boolean addAddress(String address, int prefixLength) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1523 | if (!isCallerEstablishedOwnerLocked()) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1524 | return false; |
| 1525 | } |
| 1526 | boolean success = jniAddAddress(mInterface, address, prefixLength); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1527 | mNetworkAgent.sendLinkProperties(makeLinkProperties()); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1528 | return success; |
| 1529 | } |
| 1530 | |
| 1531 | public synchronized boolean removeAddress(String address, int prefixLength) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1532 | if (!isCallerEstablishedOwnerLocked()) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
| 1535 | boolean success = jniDelAddress(mInterface, address, prefixLength); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1536 | mNetworkAgent.sendLinkProperties(makeLinkProperties()); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1537 | return success; |
| 1538 | } |
| 1539 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 1540 | /** |
| 1541 | * Updates underlying network set. |
| 1542 | * |
| 1543 | * <p>Note: Does not updates capabilities. Call {@link #updateCapabilities} from |
| 1544 | * ConnectivityService thread to get updated capabilities. |
| 1545 | */ |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1546 | public synchronized boolean setUnderlyingNetworks(Network[] networks) { |
| 1547 | if (!isCallerEstablishedOwnerLocked()) { |
| 1548 | return false; |
| 1549 | } |
| 1550 | if (networks == null) { |
| 1551 | mConfig.underlyingNetworks = null; |
| 1552 | } else { |
| 1553 | mConfig.underlyingNetworks = new Network[networks.length]; |
| 1554 | for (int i = 0; i < networks.length; ++i) { |
| 1555 | if (networks[i] == null) { |
| 1556 | mConfig.underlyingNetworks[i] = null; |
| 1557 | } else { |
| 1558 | mConfig.underlyingNetworks[i] = new Network(networks[i].netId); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | public synchronized Network[] getUnderlyingNetworks() { |
| 1566 | if (!isRunningLocked()) { |
| 1567 | return null; |
| 1568 | } |
| 1569 | return mConfig.underlyingNetworks; |
| 1570 | } |
| 1571 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1572 | /** |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1573 | * This method should only be called by ConnectivityService because it doesn't |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1574 | * have enough data to fill VpnInfo.primaryUnderlyingIface field. |
| 1575 | */ |
| 1576 | public synchronized VpnInfo getVpnInfo() { |
| 1577 | if (!isRunningLocked()) { |
| 1578 | return null; |
| 1579 | } |
| 1580 | |
| 1581 | VpnInfo info = new VpnInfo(); |
| 1582 | info.ownerUid = mOwnerUID; |
| 1583 | info.vpnIface = mInterface; |
| 1584 | return info; |
| 1585 | } |
| 1586 | |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1587 | public synchronized boolean appliesToUid(int uid) { |
| 1588 | if (!isRunningLocked()) { |
| 1589 | return false; |
| 1590 | } |
Chalard Jean | ecacd5e | 2017-12-27 14:23:31 +0900 | [diff] [blame] | 1591 | return mNetworkCapabilities.appliesToUid(uid); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1594 | /** |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1595 | * @param uid The target uid. |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1596 | * |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1597 | * @return {@code true} if {@code uid} is included in one of the mBlockedUsers ranges and the |
| 1598 | * VPN is not connected, or if the VPN is connected but does not apply to the {@code uid}. |
| 1599 | * |
| 1600 | * @apiNote This method don't check VPN lockdown status. |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1601 | * @see #mBlockedUsers |
| 1602 | */ |
| 1603 | public synchronized boolean isBlockingUid(int uid) { |
Robin Lee | ebbcb54 | 2016-05-24 16:35:10 +0100 | [diff] [blame] | 1604 | if (mNetworkInfo.isConnected()) { |
| 1605 | return !appliesToUid(uid); |
| 1606 | } else { |
| 1607 | for (UidRange uidRange : mBlockedUsers) { |
| 1608 | if (uidRange.contains(uid)) { |
| 1609 | return true; |
| 1610 | } |
| 1611 | } |
| 1612 | return false; |
| 1613 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1616 | private void updateAlwaysOnNotification(DetailedState networkState) { |
| 1617 | final boolean visible = (mAlwaysOn && networkState != DetailedState.CONNECTED); |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1618 | |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1619 | final UserHandle user = UserHandle.of(mUserHandle); |
| 1620 | final long token = Binder.clearCallingIdentity(); |
| 1621 | try { |
| 1622 | final NotificationManager notificationManager = NotificationManager.from(mContext); |
| 1623 | if (!visible) { |
Charles He | 15297a6 | 2017-04-08 22:03:42 +0100 | [diff] [blame] | 1624 | notificationManager.cancelAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED, user); |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1625 | return; |
| 1626 | } |
Charles He | ab6f2f6 | 2017-07-12 15:30:00 +0100 | [diff] [blame] | 1627 | final Intent intent = new Intent(); |
| 1628 | intent.setComponent(ComponentName.unflattenFromString(mContext.getString( |
| 1629 | R.string.config_customVpnAlwaysOnDisconnectedDialogComponent))); |
| 1630 | intent.putExtra("lockdown", mLockdown); |
| 1631 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 1632 | final PendingIntent configIntent = mSystemServices.pendingIntentGetActivityAsUser( |
| 1633 | intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT, user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1634 | final Notification.Builder builder = |
| 1635 | new Notification.Builder(mContext, SystemNotificationChannels.VPN) |
| 1636 | .setSmallIcon(R.drawable.vpn_connected) |
| 1637 | .setContentTitle(mContext.getString(R.string.vpn_lockdown_disconnected)) |
| 1638 | .setContentText(mContext.getString(R.string.vpn_lockdown_config)) |
| 1639 | .setContentIntent(configIntent) |
| 1640 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 1641 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 1642 | .setOngoing(true) |
| 1643 | .setColor(mContext.getColor(R.color.system_notification_accent_color)); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 1644 | notificationManager.notifyAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED, |
| 1645 | builder.build(), user); |
Tony Mak | 1a405fe | 2016-06-30 11:19:20 +0100 | [diff] [blame] | 1646 | } finally { |
| 1647 | Binder.restoreCallingIdentity(token); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Robin Lee | b8c2a2b | 2017-03-10 16:17:06 +0000 | [diff] [blame] | 1651 | /** |
| 1652 | * Facade for system service calls that change, or depend on, state outside of |
| 1653 | * {@link ConnectivityService} and have hard-to-mock interfaces. |
| 1654 | * |
| 1655 | * @see com.android.server.connectivity.VpnTest |
| 1656 | */ |
| 1657 | @VisibleForTesting |
| 1658 | public static class SystemServices { |
| 1659 | private final Context mContext; |
| 1660 | |
| 1661 | public SystemServices(@NonNull Context context) { |
| 1662 | mContext = context; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * @see PendingIntent#getActivityAsUser() |
| 1667 | */ |
| 1668 | public PendingIntent pendingIntentGetActivityAsUser( |
| 1669 | Intent intent, int flags, UserHandle user) { |
| 1670 | return PendingIntent.getActivityAsUser(mContext, 0 /*request*/, intent, flags, |
| 1671 | null /*options*/, user); |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * @see Settings.Secure#putStringForUser |
| 1676 | */ |
| 1677 | public void settingsSecurePutStringForUser(String key, String value, int userId) { |
| 1678 | Settings.Secure.putStringForUser(mContext.getContentResolver(), key, value, userId); |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * @see Settings.Secure#putIntForUser |
| 1683 | */ |
| 1684 | public void settingsSecurePutIntForUser(String key, int value, int userId) { |
| 1685 | Settings.Secure.putIntForUser(mContext.getContentResolver(), key, value, userId); |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * @see Settings.Secure#getStringForUser |
| 1690 | */ |
| 1691 | public String settingsSecureGetStringForUser(String key, int userId) { |
| 1692 | return Settings.Secure.getStringForUser(mContext.getContentResolver(), key, userId); |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * @see Settings.Secure#getIntForUser |
| 1697 | */ |
| 1698 | public int settingsSecureGetIntForUser(String key, int def, int userId) { |
| 1699 | return Settings.Secure.getIntForUser(mContext.getContentResolver(), key, def, userId); |
| 1700 | } |
| 1701 | } |
| 1702 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1703 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1704 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1705 | private native int jniSetAddresses(String interfaze, String addresses); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1706 | private native void jniReset(String interfaze); |
| 1707 | private native int jniCheck(String interfaze); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 1708 | private native boolean jniAddAddress(String interfaze, String address, int prefixLen); |
| 1709 | private native boolean jniDelAddress(String interfaze, String address, int prefixLen); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1710 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 1711 | private static RouteInfo findIPv4DefaultRoute(LinkProperties prop) { |
| 1712 | for (RouteInfo route : prop.getAllRoutes()) { |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1713 | // Currently legacy VPN only works on IPv4. |
| 1714 | if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) { |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 1715 | return route; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1718 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 1719 | throw new IllegalStateException("Unable to find IPv4 default gateway"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | /** |
| 1723 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 1724 | * secondary thread to perform connection work, returning quickly. |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1725 | * |
| 1726 | * Should only be called to respond to Binder requests as this enforces caller permission. Use |
| 1727 | * {@link #startLegacyVpnPrivileged(VpnProfile, KeyStore, LinkProperties)} to skip the |
| 1728 | * permission check only when the caller is trusted (or the call is initiated by the system). |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1729 | */ |
| 1730 | public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1731 | enforceControlPermission(); |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1732 | long token = Binder.clearCallingIdentity(); |
| 1733 | try { |
| 1734 | startLegacyVpnPrivileged(profile, keyStore, egress); |
| 1735 | } finally { |
| 1736 | Binder.restoreCallingIdentity(token); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | /** |
| 1741 | * Like {@link #startLegacyVpn(VpnProfile, KeyStore, LinkProperties)}, but does not check |
| 1742 | * permissions under the assumption that the caller is the system. |
| 1743 | * |
| 1744 | * Callers are responsible for checking permissions if needed. |
| 1745 | */ |
| 1746 | public void startLegacyVpnPrivileged(VpnProfile profile, KeyStore keyStore, |
| 1747 | LinkProperties egress) { |
Julia Reynolds | f5116d0 | 2014-07-01 11:10:41 -0400 | [diff] [blame] | 1748 | UserManager mgr = UserManager.get(mContext); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1749 | UserInfo user = mgr.getUserInfo(mUserHandle); |
Nicolas Prevot | 95778ff | 2015-01-06 15:43:05 +0000 | [diff] [blame] | 1750 | if (user.isRestricted() || mgr.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN, |
| 1751 | new UserHandle(mUserHandle))) { |
Julia Reynolds | f5116d0 | 2014-07-01 11:10:41 -0400 | [diff] [blame] | 1752 | throw new SecurityException("Restricted users cannot establish VPNs"); |
| 1753 | } |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1754 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 1755 | final RouteInfo ipv4DefaultRoute = findIPv4DefaultRoute(egress); |
| 1756 | final String gateway = ipv4DefaultRoute.getGateway().getHostAddress(); |
| 1757 | final String iface = ipv4DefaultRoute.getInterface(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1758 | |
| 1759 | // Load certificates. |
| 1760 | String privateKey = ""; |
| 1761 | String userCert = ""; |
| 1762 | String caCert = ""; |
| 1763 | String serverCert = ""; |
| 1764 | if (!profile.ipsecUserCert.isEmpty()) { |
| 1765 | privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert; |
| 1766 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1767 | userCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1768 | } |
| 1769 | if (!profile.ipsecCaCert.isEmpty()) { |
| 1770 | byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1771 | caCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1772 | } |
| 1773 | if (!profile.ipsecServerCert.isEmpty()) { |
| 1774 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1775 | serverCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1776 | } |
| 1777 | if (privateKey == null || userCert == null || caCert == null || serverCert == null) { |
| 1778 | throw new IllegalStateException("Cannot load credentials"); |
| 1779 | } |
| 1780 | |
| 1781 | // Prepare arguments for racoon. |
| 1782 | String[] racoon = null; |
| 1783 | switch (profile.type) { |
| 1784 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 1785 | racoon = new String[] { |
| 1786 | iface, profile.server, "udppsk", profile.ipsecIdentifier, |
| 1787 | profile.ipsecSecret, "1701", |
| 1788 | }; |
| 1789 | break; |
| 1790 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 1791 | racoon = new String[] { |
| 1792 | iface, profile.server, "udprsa", privateKey, userCert, |
| 1793 | caCert, serverCert, "1701", |
| 1794 | }; |
| 1795 | break; |
| 1796 | case VpnProfile.TYPE_IPSEC_XAUTH_PSK: |
| 1797 | racoon = new String[] { |
| 1798 | iface, profile.server, "xauthpsk", profile.ipsecIdentifier, |
| 1799 | profile.ipsecSecret, profile.username, profile.password, "", gateway, |
| 1800 | }; |
| 1801 | break; |
| 1802 | case VpnProfile.TYPE_IPSEC_XAUTH_RSA: |
| 1803 | racoon = new String[] { |
| 1804 | iface, profile.server, "xauthrsa", privateKey, userCert, |
| 1805 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 1806 | }; |
| 1807 | break; |
| 1808 | case VpnProfile.TYPE_IPSEC_HYBRID_RSA: |
| 1809 | racoon = new String[] { |
| 1810 | iface, profile.server, "hybridrsa", |
| 1811 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 1812 | }; |
| 1813 | break; |
| 1814 | } |
| 1815 | |
| 1816 | // Prepare arguments for mtpd. |
| 1817 | String[] mtpd = null; |
| 1818 | switch (profile.type) { |
| 1819 | case VpnProfile.TYPE_PPTP: |
| 1820 | mtpd = new String[] { |
| 1821 | iface, "pptp", profile.server, "1723", |
| 1822 | "name", profile.username, "password", profile.password, |
| 1823 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 1824 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 1825 | (profile.mppe ? "+mppe" : "nomppe"), |
| 1826 | }; |
| 1827 | break; |
| 1828 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 1829 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 1830 | mtpd = new String[] { |
| 1831 | iface, "l2tp", profile.server, "1701", profile.l2tpSecret, |
| 1832 | "name", profile.username, "password", profile.password, |
| 1833 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 1834 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 1835 | }; |
| 1836 | break; |
| 1837 | } |
| 1838 | |
| 1839 | VpnConfig config = new VpnConfig(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1840 | config.legacy = true; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1841 | config.user = profile.key; |
| 1842 | config.interfaze = iface; |
| 1843 | config.session = profile.name; |
Varun Anand | 1215f09 | 2019-01-14 11:45:33 -0800 | [diff] [blame] | 1844 | config.isMetered = false; |
Takayuki, Ogura | 5ad260d | 2019-01-25 19:47:05 +0900 | [diff] [blame] | 1845 | config.proxyInfo = profile.proxy; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1846 | |
| 1847 | config.addLegacyRoutes(profile.routes); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1848 | if (!profile.dnsServers.isEmpty()) { |
| 1849 | config.dnsServers = Arrays.asList(profile.dnsServers.split(" +")); |
| 1850 | } |
| 1851 | if (!profile.searchDomains.isEmpty()) { |
| 1852 | config.searchDomains = Arrays.asList(profile.searchDomains.split(" +")); |
| 1853 | } |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1854 | startLegacyVpn(config, racoon, mtpd); |
| 1855 | } |
| 1856 | |
| 1857 | private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1858 | stopLegacyVpnPrivileged(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1859 | |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1860 | // Prepare for the new request. |
| 1861 | prepareInternal(VpnConfig.LEGACY_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1862 | updateState(DetailedState.CONNECTING, "startLegacyVpn"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1863 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1864 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 1865 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 1866 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1869 | /** Stop legacy VPN. Permissions must be checked by callers. */ |
| 1870 | public synchronized void stopLegacyVpnPrivileged() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1871 | if (mLegacyVpnRunner != null) { |
| 1872 | mLegacyVpnRunner.exit(); |
| 1873 | mLegacyVpnRunner = null; |
| 1874 | |
| 1875 | synchronized (LegacyVpnRunner.TAG) { |
| 1876 | // wait for old thread to completely finish before spinning up |
| 1877 | // new instance, otherwise state updates can be out of order. |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1882 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1883 | * Return the information of the current ongoing legacy VPN. |
| 1884 | */ |
| 1885 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 1886 | // Check if the caller is authorized. |
| 1887 | enforceControlPermission(); |
sj.cha | 08bbca0 | 2015-03-23 11:35:24 +0900 | [diff] [blame] | 1888 | return getLegacyVpnInfoPrivileged(); |
| 1889 | } |
| 1890 | |
| 1891 | /** |
| 1892 | * Return the information of the current ongoing legacy VPN. |
| 1893 | * Callers are responsible for checking permissions if needed. |
| 1894 | */ |
| 1895 | public synchronized LegacyVpnInfo getLegacyVpnInfoPrivileged() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1896 | if (mLegacyVpnRunner == null) return null; |
| 1897 | |
| 1898 | final LegacyVpnInfo info = new LegacyVpnInfo(); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1899 | info.key = mConfig.user; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1900 | info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 1901 | if (mNetworkInfo.isConnected()) { |
| 1902 | info.intent = mStatusIntent; |
| 1903 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1904 | return info; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1907 | public VpnConfig getLegacyVpnConfig() { |
| 1908 | if (mLegacyVpnRunner != null) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1909 | return mConfig; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1910 | } else { |
| 1911 | return null; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1915 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1916 | * Bringing up a VPN connection takes time, and that is all this thread |
| 1917 | * does. Here we have plenty of time. The only thing we need to take |
| 1918 | * care of is responding to interruptions as soon as possible. Otherwise |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1919 | * requests will pile up. This could be done in a Handler as a state |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1920 | * machine, but it is much easier to read in the current form. |
| 1921 | */ |
| 1922 | private class LegacyVpnRunner extends Thread { |
| 1923 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1924 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1925 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1926 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1927 | private final LocalSocket[] mSockets; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1928 | private final String mOuterInterface; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1929 | private final AtomicInteger mOuterConnection = |
| 1930 | new AtomicInteger(ConnectivityManager.TYPE_NONE); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1931 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 1932 | private long mBringupStartTime = -1; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1933 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1934 | /** |
| 1935 | * Watch for the outer connection (passing in the constructor) going away. |
| 1936 | */ |
| 1937 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1938 | @Override |
| 1939 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 1940 | if (!mEnableTeardown) return; |
| 1941 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1942 | if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
| 1943 | if (intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, |
| 1944 | ConnectivityManager.TYPE_NONE) == mOuterConnection.get()) { |
| 1945 | NetworkInfo info = (NetworkInfo)intent.getExtra( |
| 1946 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 1947 | if (info != null && !info.isConnectedOrConnecting()) { |
| 1948 | try { |
| 1949 | mObserver.interfaceStatusChanged(mOuterInterface, false); |
| 1950 | } catch (RemoteException e) {} |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | }; |
| 1956 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1957 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1958 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1959 | mConfig = config; |
| 1960 | mDaemons = new String[] {"racoon", "mtpd"}; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1961 | // TODO: clear arguments from memory once launched |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1962 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1963 | mSockets = new LocalSocket[mDaemons.length]; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1964 | |
| 1965 | // This is the interface which VPN is running on, |
| 1966 | // mConfig.interfaze will change to point to OUR |
| 1967 | // internal interface soon. TODO - add inner/outer to mconfig |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1968 | // TODO - we have a race - if the outer iface goes away/disconnects before we hit this |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1969 | // we will leave the VPN up. We should check that it's still there/connected after |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1970 | // registering |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1971 | mOuterInterface = mConfig.interfaze; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1972 | |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1973 | if (!TextUtils.isEmpty(mOuterInterface)) { |
| 1974 | final ConnectivityManager cm = ConnectivityManager.from(mContext); |
| 1975 | for (Network network : cm.getAllNetworks()) { |
| 1976 | final LinkProperties lp = cm.getLinkProperties(network); |
Lorenzo Colitti | 1b60d11 | 2015-07-02 13:03:03 +0900 | [diff] [blame] | 1977 | if (lp != null && lp.getAllInterfaceNames().contains(mOuterInterface)) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1978 | final NetworkInfo networkInfo = cm.getNetworkInfo(network); |
| 1979 | if (networkInfo != null) mOuterConnection.set(networkInfo.getType()); |
| 1980 | } |
| 1981 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | IntentFilter filter = new IntentFilter(); |
| 1985 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
| 1986 | mContext.registerReceiver(mBroadcastReceiver, filter); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1989 | public void check(String interfaze) { |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1990 | if (interfaze.equals(mOuterInterface)) { |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1991 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 1992 | exit(); |
| 1993 | } |
| 1994 | } |
| 1995 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1996 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1997 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1998 | interrupt(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1999 | agentDisconnect(); |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 2000 | try { |
| 2001 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 2002 | } catch (IllegalArgumentException e) {} |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2005 | @Override |
| 2006 | public void run() { |
| 2007 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2008 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2009 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2010 | Log.v(TAG, "Executing"); |
Hisanobu Watanabe | 047454c | 2016-06-07 19:55:41 +0900 | [diff] [blame] | 2011 | try { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2012 | bringup(); |
| 2013 | waitForDaemonsToStop(); |
Hisanobu Watanabe | 047454c | 2016-06-07 19:55:41 +0900 | [diff] [blame] | 2014 | interrupted(); // Clear interrupt flag if execute called exit. |
| 2015 | } catch (InterruptedException e) { |
| 2016 | } finally { |
| 2017 | for (LocalSocket socket : mSockets) { |
| 2018 | IoUtils.closeQuietly(socket); |
| 2019 | } |
| 2020 | // This sleep is necessary for racoon to successfully complete sending delete |
| 2021 | // message to server. |
| 2022 | try { |
| 2023 | Thread.sleep(50); |
| 2024 | } catch (InterruptedException e) { |
| 2025 | } |
| 2026 | for (String daemon : mDaemons) { |
| 2027 | SystemService.stop(daemon); |
| 2028 | } |
| 2029 | } |
| 2030 | agentDisconnect(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2034 | private void checkInterruptAndDelay(boolean sleepLonger) throws InterruptedException { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2035 | long now = SystemClock.elapsedRealtime(); |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2036 | if (now - mBringupStartTime <= 60000) { |
| 2037 | Thread.sleep(sleepLonger ? 200 : 1); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2038 | } else { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2039 | updateState(DetailedState.FAILED, "checkpoint"); |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2040 | throw new IllegalStateException("VPN bringup took too long"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2044 | private void bringup() { |
| 2045 | // Catch all exceptions so we can clean up a few things. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2046 | boolean initFinished = false; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2047 | try { |
| 2048 | // Initialize the timer. |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2049 | mBringupStartTime = SystemClock.elapsedRealtime(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2050 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2051 | // Wait for the daemons to stop. |
| 2052 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 2053 | while (!SystemService.isStopped(daemon)) { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2054 | checkInterruptAndDelay(true); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2058 | // Clear the previous state. |
| 2059 | File state = new File("/data/misc/vpn/state"); |
| 2060 | state.delete(); |
| 2061 | if (state.exists()) { |
| 2062 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2063 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 2064 | new File("/data/misc/vpn/abort").delete(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2065 | initFinished = true; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2066 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 2067 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2068 | boolean restart = false; |
| 2069 | for (String[] arguments : mArguments) { |
| 2070 | restart = restart || (arguments != null); |
| 2071 | } |
| 2072 | if (!restart) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2073 | agentDisconnect(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2074 | return; |
| 2075 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2076 | updateState(DetailedState.CONNECTING, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2077 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2078 | // Start the daemon with arguments. |
| 2079 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2080 | String[] arguments = mArguments[i]; |
| 2081 | if (arguments == null) { |
| 2082 | continue; |
| 2083 | } |
| 2084 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2085 | // Start the daemon. |
| 2086 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 2087 | SystemService.start(daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2088 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2089 | // Wait for the daemon to start. |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 2090 | while (!SystemService.isRunning(daemon)) { |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2091 | checkInterruptAndDelay(true); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2095 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2096 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2097 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2098 | |
| 2099 | // Wait for the socket to connect. |
| 2100 | while (true) { |
| 2101 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2102 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2103 | break; |
| 2104 | } catch (Exception e) { |
| 2105 | // ignore |
| 2106 | } |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2107 | checkInterruptAndDelay(true); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2108 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2109 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2110 | |
| 2111 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2112 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2113 | for (String argument : arguments) { |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 2114 | byte[] bytes = argument.getBytes(StandardCharsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2115 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2116 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2117 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2118 | out.write(bytes.length >> 8); |
| 2119 | out.write(bytes.length); |
| 2120 | out.write(bytes); |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2121 | checkInterruptAndDelay(false); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2122 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2123 | out.write(0xFF); |
| 2124 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2125 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2126 | |
| 2127 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 2128 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2129 | while (true) { |
| 2130 | try { |
| 2131 | if (in.read() == -1) { |
| 2132 | break; |
| 2133 | } |
| 2134 | } catch (Exception e) { |
| 2135 | // ignore |
| 2136 | } |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2137 | checkInterruptAndDelay(true); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2138 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2141 | // Wait for the daemons to create the new state. |
| 2142 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 2143 | // Check if a running daemon is dead. |
| 2144 | for (int i = 0; i < mDaemons.length; ++i) { |
| 2145 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 2146 | if (mArguments[i] != null && !SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2147 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2148 | } |
| 2149 | } |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2150 | checkInterruptAndDelay(true); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2153 | // Now we are connected. Read and parse the new state. |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 2154 | String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1); |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 2155 | if (parameters.length != 7) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2156 | throw new IllegalStateException("Cannot parse the state"); |
| 2157 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2158 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2159 | // Set the interface and the addresses in the config. |
| 2160 | mConfig.interfaze = parameters[0].trim(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2161 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2162 | mConfig.addLegacyAddresses(parameters[1]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2163 | // Set the routes if they are not set in the config. |
| 2164 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2165 | mConfig.addLegacyRoutes(parameters[2]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | // Set the DNS servers if they are not set in the config. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2169 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2170 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2171 | if (!dnsServers.isEmpty()) { |
| 2172 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 2173 | } |
| 2174 | } |
| 2175 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2176 | // Set the search domains if they are not set in the config. |
| 2177 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 2178 | String searchDomains = parameters[4].trim(); |
| 2179 | if (!searchDomains.isEmpty()) { |
| 2180 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 2181 | } |
| 2182 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 2183 | |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 2184 | // Add a throw route for the VPN server endpoint, if one was specified. |
| 2185 | String endpoint = parameters[5]; |
| 2186 | if (!endpoint.isEmpty()) { |
| 2187 | try { |
| 2188 | InetAddress addr = InetAddress.parseNumericAddress(endpoint); |
| 2189 | if (addr instanceof Inet4Address) { |
| 2190 | mConfig.routes.add(new RouteInfo(new IpPrefix(addr, 32), RTN_THROW)); |
| 2191 | } else if (addr instanceof Inet6Address) { |
| 2192 | mConfig.routes.add(new RouteInfo(new IpPrefix(addr, 128), RTN_THROW)); |
| 2193 | } else { |
| 2194 | Log.e(TAG, "Unknown IP address family for VPN endpoint: " + endpoint); |
| 2195 | } |
| 2196 | } catch (IllegalArgumentException e) { |
| 2197 | Log.e(TAG, "Exception constructing throw route to " + endpoint + ": " + e); |
| 2198 | } |
| 2199 | } |
| 2200 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 2201 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2202 | synchronized (Vpn.this) { |
Vinit Deshapnde | 2b862e5 | 2013-10-02 11:50:39 -0700 | [diff] [blame] | 2203 | // Set the start time |
| 2204 | mConfig.startTime = SystemClock.elapsedRealtime(); |
| 2205 | |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2206 | // Check if the thread was interrupted while we were waiting on the lock. |
| 2207 | checkInterruptAndDelay(false); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2208 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 2209 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 2210 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 2211 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2212 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 2213 | |
| 2214 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 2215 | mInterface = mConfig.interfaze; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 2216 | prepareStatusIntent(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2217 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2218 | agentConnect(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2219 | |
| 2220 | Log.i(TAG, "Connected!"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 2221 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2222 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2223 | Log.i(TAG, "Aborting", e); |
Lorenzo Colitti | 4384060 | 2014-08-20 16:01:44 -0700 | [diff] [blame] | 2224 | updateState(DetailedState.FAILED, e.getMessage()); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 2225 | exit(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2228 | |
| 2229 | /** |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2230 | * Check all daemons every two seconds. Return when one of them is stopped. |
| 2231 | * The caller will move to the disconnected state when this function returns, |
| 2232 | * which can happen if a daemon failed or if the VPN was torn down. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2233 | */ |
Chalard Jean | dd59ece | 2017-12-20 13:23:32 +0900 | [diff] [blame] | 2234 | private void waitForDaemonsToStop() throws InterruptedException { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2235 | if (!mNetworkInfo.isConnected()) { |
| 2236 | return; |
| 2237 | } |
Hisanobu Watanabe | 047454c | 2016-06-07 19:55:41 +0900 | [diff] [blame] | 2238 | while (true) { |
| 2239 | Thread.sleep(2000); |
| 2240 | for (int i = 0; i < mDaemons.length; i++) { |
| 2241 | if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) { |
| 2242 | return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2243 | } |
| 2244 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2245 | } |
| 2246 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 2247 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2248 | } |