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