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