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