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