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