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