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