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; |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 21 | import static android.net.RouteInfo.RTN_THROW; |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 22 | import static android.net.RouteInfo.RTN_UNREACHABLE; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 23 | |
Jeff Davidson | bc19c18 | 2014-11-11 13:20:01 -0800 | [diff] [blame] | 24 | import android.Manifest; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 25 | import android.annotation.NonNull; |
| 26 | import android.annotation.Nullable; |
| 27 | import android.annotation.UserIdInt; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 28 | import android.app.AppGlobals; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 29 | import android.app.AppOpsManager; |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 32 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 35 | import android.content.IntentFilter; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 36 | import android.content.ServiceConnection; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 38 | import android.content.pm.PackageManager.NameNotFoundException; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 39 | import android.content.pm.ResolveInfo; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 40 | import android.content.pm.UserInfo; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 41 | import android.net.ConnectivityManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 42 | import android.net.INetworkManagementEventObserver; |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 43 | import android.net.IpPrefix; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 44 | import android.net.LinkAddress; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 45 | import android.net.LinkProperties; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 46 | import android.net.LocalSocket; |
| 47 | import android.net.LocalSocketAddress; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 48 | import android.net.Network; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 49 | import android.net.NetworkAgent; |
| 50 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 51 | import android.net.NetworkInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 52 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 53 | import android.net.NetworkMisc; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 54 | import android.net.RouteInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 55 | import android.net.UidRange; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 56 | import android.os.Binder; |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 57 | import android.os.FileUtils; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 58 | import android.os.IBinder; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 59 | import android.os.INetworkManagementService; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 60 | import android.os.Looper; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 61 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 62 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 63 | import android.os.Process; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 64 | import android.os.RemoteException; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 65 | import android.os.SystemClock; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 66 | import android.os.SystemService; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 68 | import android.os.UserManager; |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 69 | import android.provider.Settings; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 70 | import android.security.Credentials; |
| 71 | import android.security.KeyStore; |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 72 | import android.text.TextUtils; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 73 | import android.util.ArraySet; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 74 | import android.util.Log; |
| 75 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 76 | import com.android.internal.annotations.GuardedBy; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 77 | import com.android.internal.annotations.VisibleForTesting; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 78 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 79 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 80 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 81 | import com.android.internal.net.VpnProfile; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 82 | import com.android.server.net.BaseNetworkObserver; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 83 | |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 84 | import libcore.io.IoUtils; |
| 85 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 86 | import java.io.File; |
Jeff Davidson | 6bbf39c | 2014-07-23 10:14:53 -0700 | [diff] [blame] | 87 | import java.io.IOException; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 88 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 89 | import java.io.OutputStream; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 90 | import java.net.Inet4Address; |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 91 | import java.net.Inet6Address; |
| 92 | import java.net.InetAddress; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 93 | import java.nio.charset.StandardCharsets; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 94 | import java.util.ArrayList; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 95 | import java.util.Arrays; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 96 | import java.util.Collection; |
| 97 | import java.util.Collections; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 98 | import java.util.List; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 99 | import java.util.Set; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 100 | import java.util.SortedSet; |
| 101 | import java.util.TreeSet; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 102 | import java.util.concurrent.atomic.AtomicInteger; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 103 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @hide |
| 106 | */ |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 107 | public class Vpn { |
| 108 | private static final String NETWORKTYPE = "VPN"; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 109 | private static final String TAG = "Vpn"; |
| 110 | private static final boolean LOGD = true; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 111 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 112 | // TODO: create separate trackers for each unique VPN to support |
| 113 | // automated reconnection |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 114 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 115 | private Context mContext; |
| 116 | private NetworkInfo mNetworkInfo; |
| 117 | private String mPackage; |
| 118 | private int mOwnerUID; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 119 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 120 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 121 | private LegacyVpnRunner mLegacyVpnRunner; |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 122 | private PendingIntent mStatusIntent; |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 123 | private volatile boolean mEnableTeardown = true; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 124 | private final INetworkManagementService mNetd; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 125 | private VpnConfig mConfig; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 126 | private NetworkAgent mNetworkAgent; |
| 127 | private final Looper mLooper; |
| 128 | private final NetworkCapabilities mNetworkCapabilities; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 129 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 130 | /** |
| 131 | * List of UIDs that are set to use this VPN by default. Normally, every UID in the user is |
| 132 | * added to this set but that can be changed by adding allowed or disallowed applications. It |
| 133 | * is non-null iff the VPN is connected. |
| 134 | * |
| 135 | * Unless the VPN has set allowBypass=true, these UIDs are forced into the VPN. |
| 136 | * |
| 137 | * @see VpnService.Builder#addAllowedApplication(String) |
| 138 | * @see VpnService.Builder#addDisallowedApplication(String) |
| 139 | */ |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 140 | @GuardedBy("this") |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 141 | private Set<UidRange> mVpnUsers = null; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 142 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 143 | // Handle of user initiating VPN. |
| 144 | private final int mUserHandle; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 145 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 146 | public Vpn(Looper looper, Context context, INetworkManagementService netService, |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 147 | int userHandle) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 148 | mContext = context; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 149 | mNetd = netService; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 150 | mUserHandle = userHandle; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 151 | mLooper = looper; |
| 152 | |
| 153 | mPackage = VpnConfig.LEGACY_VPN; |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 154 | mOwnerUID = getAppUid(mPackage, mUserHandle); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 155 | |
| 156 | try { |
| 157 | netService.registerObserver(mObserver); |
| 158 | } catch (RemoteException e) { |
| 159 | Log.wtf(TAG, "Problem registering observer", e); |
| 160 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 161 | |
| 162 | mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_VPN, 0, NETWORKTYPE, ""); |
| 163 | // TODO: Copy metered attribute and bandwidths from physical transport, b/16207332 |
| 164 | mNetworkCapabilities = new NetworkCapabilities(); |
| 165 | mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_VPN); |
| 166 | mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 169 | /** |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 170 | * Set if this object is responsible for watching for {@link NetworkInfo} |
| 171 | * teardown. When {@code false}, teardown is handled externally by someone |
| 172 | * else. |
| 173 | */ |
| 174 | public void setEnableTeardown(boolean enableTeardown) { |
| 175 | mEnableTeardown = enableTeardown; |
| 176 | } |
| 177 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Update current state, dispaching event to listeners. |
| 180 | */ |
| 181 | private void updateState(DetailedState detailedState, String reason) { |
| 182 | if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); |
| 183 | mNetworkInfo.setDetailedState(detailedState, reason, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 184 | if (mNetworkAgent != null) { |
| 185 | mNetworkAgent.sendNetworkInfo(mNetworkInfo); |
| 186 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 190 | * Configures an always-on VPN connection through a specific application. |
| 191 | * This connection is automatically granted and persisted after a reboot. |
| 192 | * |
| 193 | * <p>The designated package should exist and declare a {@link VpnService} in its |
| 194 | * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE}, |
| 195 | * otherwise the call will fail. |
| 196 | * |
| 197 | * @param newPackage the package to designate as always-on VPN supplier. |
| 198 | */ |
| 199 | public synchronized boolean setAlwaysOnPackage(String packageName) { |
| 200 | enforceControlPermissionOrInternalCaller(); |
| 201 | |
| 202 | // Disconnect current VPN. |
| 203 | prepareInternal(VpnConfig.LEGACY_VPN); |
| 204 | |
| 205 | // Pre-authorize new always-on VPN package. |
| 206 | if (packageName != null) { |
| 207 | if (!setPackageAuthorization(packageName, true)) { |
| 208 | return false; |
| 209 | } |
Robin Lee | 7837884 | 2016-02-12 11:51:33 +0000 | [diff] [blame] | 210 | prepareInternal(packageName); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Save the new package name in Settings.Secure. |
| 214 | final long token = Binder.clearCallingIdentity(); |
| 215 | try { |
| 216 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 217 | Settings.Secure.ALWAYS_ON_VPN_APP, packageName, mUserHandle); |
| 218 | } finally { |
| 219 | Binder.restoreCallingIdentity(token); |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @return the package name of the VPN controller responsible for always-on VPN, |
| 226 | * or {@code null} if none is set or always-on VPN is controlled through |
| 227 | * lockdown instead. |
| 228 | * @hide |
| 229 | */ |
| 230 | public synchronized String getAlwaysOnPackage() { |
| 231 | enforceControlPermissionOrInternalCaller(); |
| 232 | |
| 233 | final long token = Binder.clearCallingIdentity(); |
| 234 | try { |
| 235 | return Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 236 | Settings.Secure.ALWAYS_ON_VPN_APP, mUserHandle); |
| 237 | } finally { |
| 238 | Binder.restoreCallingIdentity(token); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 243 | * Prepare for a VPN application. This method is designed to solve |
| 244 | * race conditions. It first compares the current prepared package |
| 245 | * with {@code oldPackage}. If they are the same, the prepared |
| 246 | * package is revoked and replaced with {@code newPackage}. If |
| 247 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 248 | * If {@code newPackage} is the same package or {@code null}, the |
| 249 | * revocation is omitted. This method returns {@code true} if the |
| 250 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 251 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 252 | * Legacy VPN is handled specially since it is not a real package. |
| 253 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 254 | * it can be revoked by itself. |
| 255 | * |
| 256 | * @param oldPackage The package name of the old VPN application. |
| 257 | * @param newPackage The package name of the new VPN application. |
| 258 | * @return true if the operation is succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 259 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 260 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
Jeff Davidson | 0a775ce | 2015-04-27 15:02:48 -0700 | [diff] [blame] | 261 | if (oldPackage != null) { |
| 262 | if (getAppUid(oldPackage, mUserHandle) != mOwnerUID) { |
| 263 | // The package doesn't match. We return false (to obtain user consent) unless the |
| 264 | // user has already consented to that VPN package. |
| 265 | if (!oldPackage.equals(VpnConfig.LEGACY_VPN) && isVpnUserPreConsented(oldPackage)) { |
| 266 | prepareInternal(oldPackage); |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } else if (!oldPackage.equals(VpnConfig.LEGACY_VPN) |
| 271 | && !isVpnUserPreConsented(oldPackage)) { |
| 272 | // Currently prepared VPN is revoked, so unprepare it and return false. |
| 273 | prepareInternal(VpnConfig.LEGACY_VPN); |
| 274 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 275 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 278 | // Return true if we do not need to revoke. |
Jeff Davidson | be08587 | 2014-10-24 10:35:53 -0700 | [diff] [blame] | 279 | if (newPackage == null || (!newPackage.equals(VpnConfig.LEGACY_VPN) && |
| 280 | getAppUid(newPackage, mUserHandle) == mOwnerUID)) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
Robin Lee | 1b1bcd7 | 2016-02-12 18:11:30 +0000 | [diff] [blame] | 284 | // Stop an existing always-on VPN from being dethroned by other apps. |
| 285 | if (getAlwaysOnPackage() != null) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // Check that the caller is authorized. |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 290 | enforceControlPermission(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 291 | |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 292 | prepareInternal(newPackage); |
| 293 | return true; |
| 294 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 295 | |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 296 | /** Prepare the VPN for the given package. Does not perform permission checks. */ |
| 297 | private void prepareInternal(String newPackage) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 298 | long token = Binder.clearCallingIdentity(); |
| 299 | try { |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 300 | // Reset the interface. |
| 301 | if (mInterface != null) { |
| 302 | mStatusIntent = null; |
| 303 | agentDisconnect(); |
| 304 | jniReset(mInterface); |
| 305 | mInterface = null; |
| 306 | mVpnUsers = null; |
| 307 | } |
| 308 | |
| 309 | // Revoke the connection or stop LegacyVpnRunner. |
| 310 | if (mConnection != null) { |
| 311 | try { |
| 312 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 313 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 314 | } catch (Exception e) { |
| 315 | // ignore |
| 316 | } |
| 317 | mContext.unbindService(mConnection); |
| 318 | mConnection = null; |
| 319 | } else if (mLegacyVpnRunner != null) { |
| 320 | mLegacyVpnRunner.exit(); |
| 321 | mLegacyVpnRunner = null; |
| 322 | } |
| 323 | |
| 324 | try { |
| 325 | mNetd.denyProtect(mOwnerUID); |
| 326 | } catch (Exception e) { |
| 327 | Log.wtf(TAG, "Failed to disallow UID " + mOwnerUID + " to call protect() " + e); |
| 328 | } |
| 329 | |
| 330 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 331 | mPackage = newPackage; |
| 332 | mOwnerUID = getAppUid(newPackage, mUserHandle); |
| 333 | try { |
| 334 | mNetd.allowProtect(mOwnerUID); |
| 335 | } catch (Exception e) { |
| 336 | Log.wtf(TAG, "Failed to allow UID " + mOwnerUID + " to call protect() " + e); |
| 337 | } |
| 338 | mConfig = null; |
| 339 | |
| 340 | updateState(DetailedState.IDLE, "prepare"); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 341 | } finally { |
| 342 | Binder.restoreCallingIdentity(token); |
| 343 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 346 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 347 | * 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] | 348 | */ |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 349 | public boolean setPackageAuthorization(String packageName, boolean authorized) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 350 | // Check if the caller is authorized. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 351 | enforceControlPermissionOrInternalCaller(); |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 352 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 353 | int uid = getAppUid(packageName, mUserHandle); |
| 354 | if (uid == -1 || VpnConfig.LEGACY_VPN.equals(packageName)) { |
| 355 | // Authorization for nonexistent packages (or fake ones) can't be updated. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 356 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | long token = Binder.clearCallingIdentity(); |
| 360 | try { |
| 361 | AppOpsManager appOps = |
| 362 | (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 363 | appOps.setMode(AppOpsManager.OP_ACTIVATE_VPN, uid, packageName, |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 364 | authorized ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 365 | return true; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 366 | } catch (Exception e) { |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 367 | 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] | 368 | } finally { |
| 369 | Binder.restoreCallingIdentity(token); |
| 370 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 371 | return false; |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | private boolean isVpnUserPreConsented(String packageName) { |
| 375 | AppOpsManager appOps = |
| 376 | (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 377 | |
| 378 | // Verify that the caller matches the given package and has permission to activate VPNs. |
| 379 | return appOps.noteOpNoThrow(AppOpsManager.OP_ACTIVATE_VPN, Binder.getCallingUid(), |
| 380 | packageName) == AppOpsManager.MODE_ALLOWED; |
| 381 | } |
| 382 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 383 | private int getAppUid(String app, int userHandle) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 384 | if (VpnConfig.LEGACY_VPN.equals(app)) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 385 | return Process.myUid(); |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 386 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 387 | PackageManager pm = mContext.getPackageManager(); |
| 388 | int result; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 389 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 390 | result = pm.getPackageUidAsUser(app, userHandle); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 391 | } catch (NameNotFoundException e) { |
| 392 | result = -1; |
| 393 | } |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | public NetworkInfo getNetworkInfo() { |
| 398 | return mNetworkInfo; |
| 399 | } |
| 400 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 401 | public int getNetId() { |
| 402 | return mNetworkAgent != null ? mNetworkAgent.netId : NETID_UNSET; |
| 403 | } |
| 404 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 405 | private LinkProperties makeLinkProperties() { |
| 406 | boolean allowIPv4 = mConfig.allowIPv4; |
| 407 | boolean allowIPv6 = mConfig.allowIPv6; |
| 408 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 409 | LinkProperties lp = new LinkProperties(); |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 410 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 411 | lp.setInterfaceName(mInterface); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 412 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 413 | if (mConfig.addresses != null) { |
| 414 | for (LinkAddress address : mConfig.addresses) { |
| 415 | lp.addLinkAddress(address); |
| 416 | allowIPv4 |= address.getAddress() instanceof Inet4Address; |
| 417 | allowIPv6 |= address.getAddress() instanceof Inet6Address; |
| 418 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 419 | } |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 420 | |
| 421 | if (mConfig.routes != null) { |
| 422 | for (RouteInfo route : mConfig.routes) { |
| 423 | lp.addRoute(route); |
| 424 | InetAddress address = route.getDestination().getAddress(); |
| 425 | allowIPv4 |= address instanceof Inet4Address; |
| 426 | allowIPv6 |= address instanceof Inet6Address; |
| 427 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 428 | } |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 429 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 430 | if (mConfig.dnsServers != null) { |
| 431 | for (String dnsServer : mConfig.dnsServers) { |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 432 | InetAddress address = InetAddress.parseNumericAddress(dnsServer); |
| 433 | lp.addDnsServer(address); |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 434 | allowIPv4 |= address instanceof Inet4Address; |
| 435 | allowIPv6 |= address instanceof Inet6Address; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 436 | } |
| 437 | } |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 438 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 439 | if (!allowIPv4) { |
| 440 | lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE)); |
| 441 | } |
| 442 | if (!allowIPv6) { |
| 443 | lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE)); |
| 444 | } |
| 445 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 446 | // Concatenate search domains into a string. |
| 447 | StringBuilder buffer = new StringBuilder(); |
| 448 | if (mConfig.searchDomains != null) { |
| 449 | for (String domain : mConfig.searchDomains) { |
| 450 | buffer.append(domain).append(' '); |
| 451 | } |
| 452 | } |
| 453 | lp.setDomains(buffer.toString().trim()); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 454 | |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 455 | // TODO: Stop setting the MTU in jniCreate and set it here. |
| 456 | |
| 457 | return lp; |
| 458 | } |
| 459 | |
| 460 | private void agentConnect() { |
| 461 | LinkProperties lp = makeLinkProperties(); |
| 462 | |
| 463 | if (lp.hasIPv4DefaultRoute() || lp.hasIPv6DefaultRoute()) { |
| 464 | mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 465 | } else { |
| 466 | mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 467 | } |
| 468 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 469 | mNetworkInfo.setDetailedState(DetailedState.CONNECTING, null, null); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 470 | |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 471 | NetworkMisc networkMisc = new NetworkMisc(); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 472 | networkMisc.allowBypass = mConfig.allowBypass; |
| 473 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 474 | long token = Binder.clearCallingIdentity(); |
| 475 | try { |
| 476 | mNetworkAgent = new NetworkAgent(mLooper, mContext, NETWORKTYPE, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 477 | mNetworkInfo, mNetworkCapabilities, lp, 0, networkMisc) { |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 478 | @Override |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 479 | public void unwanted() { |
| 480 | // We are user controlled, not driven by NetworkRequest. |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 481 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 482 | }; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 483 | } finally { |
| 484 | Binder.restoreCallingIdentity(token); |
| 485 | } |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 486 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 487 | mVpnUsers = createUserAndRestrictedProfilesRanges(mUserHandle, |
| 488 | mConfig.allowedApplications, mConfig.disallowedApplications); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 489 | mNetworkAgent.addUidRanges(mVpnUsers.toArray(new UidRange[mVpnUsers.size()])); |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 490 | |
| 491 | mNetworkInfo.setIsAvailable(true); |
| 492 | updateState(DetailedState.CONNECTED, "agentConnect"); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 493 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 494 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 495 | private boolean canHaveRestrictedProfile(int userId) { |
| 496 | long token = Binder.clearCallingIdentity(); |
| 497 | try { |
| 498 | return UserManager.get(mContext).canHaveRestrictedProfile(userId); |
| 499 | } finally { |
| 500 | Binder.restoreCallingIdentity(token); |
| 501 | } |
| 502 | } |
| 503 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 504 | private void agentDisconnect(NetworkInfo networkInfo, NetworkAgent networkAgent) { |
| 505 | networkInfo.setIsAvailable(false); |
| 506 | networkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null); |
| 507 | if (networkAgent != null) { |
| 508 | networkAgent.sendNetworkInfo(networkInfo); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | private void agentDisconnect(NetworkAgent networkAgent) { |
| 513 | NetworkInfo networkInfo = new NetworkInfo(mNetworkInfo); |
| 514 | agentDisconnect(networkInfo, networkAgent); |
| 515 | } |
| 516 | |
| 517 | private void agentDisconnect() { |
| 518 | if (mNetworkInfo.isConnected()) { |
| 519 | agentDisconnect(mNetworkInfo, mNetworkAgent); |
| 520 | mNetworkAgent = null; |
| 521 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 525 | * Establish a VPN network and return the file descriptor of the VPN |
| 526 | * interface. This methods returns {@code null} if the application is |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 527 | * revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 528 | * |
| 529 | * @param config The parameters to configure the network. |
| 530 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 531 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 532 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 533 | // Check if the caller is already prepared. |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 534 | UserManager mgr = UserManager.get(mContext); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 535 | if (Binder.getCallingUid() != mOwnerUID) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 536 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 537 | } |
Jeff Davidson | 0a775ce | 2015-04-27 15:02:48 -0700 | [diff] [blame] | 538 | // Check to ensure consent hasn't been revoked since we were prepared. |
| 539 | if (!isVpnUserPreConsented(mPackage)) { |
| 540 | return null; |
| 541 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 542 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 543 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 544 | intent.setClassName(mPackage, config.user); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 545 | long token = Binder.clearCallingIdentity(); |
| 546 | try { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 547 | // 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] | 548 | UserInfo user = mgr.getUserInfo(mUserHandle); |
Nicolas Prevot | 95778ff | 2015-01-06 15:43:05 +0000 | [diff] [blame] | 549 | if (user.isRestricted() || mgr.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN, |
| 550 | new UserHandle(mUserHandle))) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 551 | throw new SecurityException("Restricted users cannot establish VPNs"); |
| 552 | } |
| 553 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 554 | ResolveInfo info = AppGlobals.getPackageManager().resolveService(intent, |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 555 | null, 0, mUserHandle); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 556 | if (info == null) { |
| 557 | throw new SecurityException("Cannot find " + config.user); |
| 558 | } |
| 559 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 560 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 561 | } |
| 562 | } catch (RemoteException e) { |
| 563 | throw new SecurityException("Cannot find " + config.user); |
| 564 | } finally { |
| 565 | Binder.restoreCallingIdentity(token); |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 566 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 567 | |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 568 | // Save the old config in case we need to go back. |
| 569 | VpnConfig oldConfig = mConfig; |
| 570 | String oldInterface = mInterface; |
| 571 | Connection oldConnection = mConnection; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 572 | NetworkAgent oldNetworkAgent = mNetworkAgent; |
| 573 | mNetworkAgent = null; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 574 | Set<UidRange> oldUsers = mVpnUsers; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 575 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 576 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 577 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 578 | try { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 579 | updateState(DetailedState.CONNECTING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 580 | String interfaze = jniGetName(tun.getFd()); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 581 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 582 | // 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] | 583 | StringBuilder builder = new StringBuilder(); |
| 584 | for (LinkAddress address : config.addresses) { |
| 585 | builder.append(" " + address); |
| 586 | } |
| 587 | if (jniSetAddresses(interfaze, builder.toString()) < 1) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 588 | throw new IllegalArgumentException("At least one address must be specified"); |
| 589 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 590 | Connection connection = new Connection(); |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 591 | if (!mContext.bindServiceAsUser(intent, connection, |
| 592 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 593 | new UserHandle(mUserHandle))) { |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 594 | throw new IllegalStateException("Cannot bind " + config.user); |
| 595 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 596 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 597 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 598 | mInterface = interfaze; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 599 | |
| 600 | // Fill more values. |
| 601 | config.user = mPackage; |
| 602 | config.interfaze = mInterface; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 603 | config.startTime = SystemClock.elapsedRealtime(); |
| 604 | mConfig = config; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 605 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 606 | // Set up forwarding and DNS rules. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 607 | agentConnect(); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 608 | |
| 609 | if (oldConnection != null) { |
| 610 | mContext.unbindService(oldConnection); |
| 611 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 612 | // Remove the old tun's user forwarding rules |
| 613 | // The new tun's user rules have already been added so they will take over |
| 614 | // as rules are deleted. This prevents data leakage as the rules are moved over. |
| 615 | agentDisconnect(oldNetworkAgent); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 616 | if (oldInterface != null && !oldInterface.equals(interfaze)) { |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 617 | jniReset(oldInterface); |
| 618 | } |
Jeff Davidson | 6bbf39c | 2014-07-23 10:14:53 -0700 | [diff] [blame] | 619 | |
| 620 | try { |
| 621 | IoUtils.setBlocking(tun.getFileDescriptor(), config.blocking); |
| 622 | } catch (IOException e) { |
| 623 | throw new IllegalStateException( |
| 624 | "Cannot set tunnel's fd as blocking=" + config.blocking, e); |
| 625 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 626 | } catch (RuntimeException e) { |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 627 | IoUtils.closeQuietly(tun); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 628 | agentDisconnect(); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 629 | // restore old state |
| 630 | mConfig = oldConfig; |
| 631 | mConnection = oldConnection; |
| 632 | mVpnUsers = oldUsers; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 633 | mNetworkAgent = oldNetworkAgent; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 634 | mInterface = oldInterface; |
| 635 | throw e; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 636 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame] | 637 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 638 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 641 | private boolean isRunningLocked() { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 642 | return mNetworkAgent != null && mInterface != null; |
| 643 | } |
| 644 | |
| 645 | // Returns true if the VPN has been established and the calling UID is its owner. Used to check |
| 646 | // that a call to mutate VPN state is admissible. |
| 647 | private boolean isCallerEstablishedOwnerLocked() { |
| 648 | return isRunningLocked() && Binder.getCallingUid() == mOwnerUID; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 651 | // Note: Return type guarantees results are deduped and sorted, which callers require. |
| 652 | private SortedSet<Integer> getAppsUids(List<String> packageNames, int userHandle) { |
| 653 | SortedSet<Integer> uids = new TreeSet<Integer>(); |
| 654 | for (String app : packageNames) { |
| 655 | int uid = getAppUid(app, userHandle); |
| 656 | if (uid != -1) uids.add(uid); |
| 657 | } |
| 658 | return uids; |
| 659 | } |
| 660 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 661 | /** |
| 662 | * Creates a {@link Set} of non-intersecting {@link UidRange} objects including all UIDs |
| 663 | * associated with one user, and any restricted profiles attached to that user. |
| 664 | * |
| 665 | * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided, |
| 666 | * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs |
| 667 | * in each user and profile will be included. |
| 668 | * |
| 669 | * @param userHandle The userId to create UID ranges for along with any of its restricted |
| 670 | * profiles. |
| 671 | * @param allowedApplications (optional) whitelist of applications to include. |
| 672 | * @param disallowedApplications (optional) blacklist of applications to exclude. |
| 673 | */ |
| 674 | @VisibleForTesting |
| 675 | Set<UidRange> createUserAndRestrictedProfilesRanges(@UserIdInt int userHandle, |
| 676 | @Nullable List<String> allowedApplications, |
| 677 | @Nullable List<String> disallowedApplications) { |
| 678 | final Set<UidRange> ranges = new ArraySet<>(); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 679 | |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 680 | // Assign the top-level user to the set of ranges |
| 681 | addUserToRanges(ranges, userHandle, allowedApplications, disallowedApplications); |
| 682 | |
| 683 | // If the user can have restricted profiles, assign all its restricted profiles too |
| 684 | if (canHaveRestrictedProfile(userHandle)) { |
| 685 | final long token = Binder.clearCallingIdentity(); |
| 686 | List<UserInfo> users; |
| 687 | try { |
| 688 | users = UserManager.get(mContext).getUsers(); |
| 689 | } finally { |
| 690 | Binder.restoreCallingIdentity(token); |
| 691 | } |
| 692 | for (UserInfo user : users) { |
| 693 | if (user.isRestricted() && (user.restrictedProfileParentId == userHandle)) { |
| 694 | addUserToRanges(ranges, user.id, allowedApplications, disallowedApplications); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | return ranges; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Updates a {@link Set} of non-intersecting {@link UidRange} objects to include all UIDs |
| 703 | * associated with one user. |
| 704 | * |
| 705 | * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided, |
| 706 | * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs |
| 707 | * in the user will be included. |
| 708 | * |
| 709 | * @param ranges {@link Set} of {@link UidRange}s to which to add. |
| 710 | * @param userHandle The userId to add to {@param ranges}. |
| 711 | * @param allowedApplications (optional) whitelist of applications to include. |
| 712 | * @param disallowedApplications (optional) blacklist of applications to exclude. |
| 713 | */ |
| 714 | @VisibleForTesting |
| 715 | void addUserToRanges(@NonNull Set<UidRange> ranges, @UserIdInt int userHandle, |
| 716 | @Nullable List<String> allowedApplications, |
| 717 | @Nullable List<String> disallowedApplications) { |
| 718 | if (allowedApplications != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 719 | // Add ranges covering all UIDs for allowedApplications. |
| 720 | int start = -1, stop = -1; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 721 | for (int uid : getAppsUids(allowedApplications, userHandle)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 722 | if (start == -1) { |
| 723 | start = uid; |
| 724 | } else if (uid != stop + 1) { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 725 | ranges.add(new UidRange(start, stop)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 726 | start = uid; |
| 727 | } |
| 728 | stop = uid; |
| 729 | } |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 730 | if (start != -1) ranges.add(new UidRange(start, stop)); |
| 731 | } else if (disallowedApplications != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 732 | // Add all ranges for user skipping UIDs for disallowedApplications. |
| 733 | final UidRange userRange = UidRange.createForUser(userHandle); |
| 734 | int start = userRange.start; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 735 | for (int uid : getAppsUids(disallowedApplications, userHandle)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 736 | if (uid == start) { |
| 737 | start++; |
| 738 | } else { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 739 | ranges.add(new UidRange(start, uid - 1)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 740 | start = uid + 1; |
| 741 | } |
| 742 | } |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 743 | if (start <= userRange.stop) ranges.add(new UidRange(start, userRange.stop)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 744 | } else { |
| 745 | // Add all UIDs for the user. |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 746 | ranges.add(UidRange.createForUser(userHandle)); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 747 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 750 | // Returns the subset of the full list of active UID ranges the VPN applies to (mVpnUsers) that |
| 751 | // apply to userHandle. |
| 752 | private List<UidRange> uidRangesForUser(int userHandle) { |
| 753 | final UidRange userRange = UidRange.createForUser(userHandle); |
| 754 | final List<UidRange> ranges = new ArrayList<UidRange>(); |
| 755 | for (UidRange range : mVpnUsers) { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 756 | if (userRange.containsRange(range)) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 757 | ranges.add(range); |
| 758 | } |
| 759 | } |
| 760 | return ranges; |
| 761 | } |
| 762 | |
| 763 | private void removeVpnUserLocked(int userHandle) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 764 | if (mVpnUsers == null) { |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 765 | throw new IllegalStateException("VPN is not active"); |
| 766 | } |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 767 | final List<UidRange> ranges = uidRangesForUser(userHandle); |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 768 | if (mNetworkAgent != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 769 | mNetworkAgent.removeUidRanges(ranges.toArray(new UidRange[ranges.size()])); |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 770 | } |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 771 | mVpnUsers.removeAll(ranges); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 774 | public void onUserAdded(int userHandle) { |
| 775 | // If the user is restricted tie them to the parent user's VPN |
| 776 | UserInfo user = UserManager.get(mContext).getUserInfo(userHandle); |
Fyodor Kupolov | 49d5a01 | 2016-03-04 13:41:58 -0800 | [diff] [blame] | 777 | if (user.isRestricted() && user.restrictedProfileParentId == mUserHandle |
| 778 | && mVpnUsers != null) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 779 | synchronized(Vpn.this) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 780 | try { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 781 | addUserToRanges(mVpnUsers, userHandle, mConfig.allowedApplications, |
| 782 | mConfig.disallowedApplications); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 783 | if (mNetworkAgent != null) { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 784 | final List<UidRange> ranges = uidRangesForUser(userHandle); |
| 785 | mNetworkAgent.addUidRanges(ranges.toArray(new UidRange[ranges.size()])); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 786 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 787 | } catch (Exception e) { |
| 788 | Log.wtf(TAG, "Failed to add restricted user to owner", e); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 794 | public void onUserRemoved(int userHandle) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 795 | // clean up if restricted |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 796 | UserInfo user = UserManager.get(mContext).getUserInfo(userHandle); |
Fyodor Kupolov | 49d5a01 | 2016-03-04 13:41:58 -0800 | [diff] [blame] | 797 | if (user.isRestricted() && user.restrictedProfileParentId == mUserHandle |
| 798 | && mVpnUsers != null) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 799 | synchronized(Vpn.this) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 800 | try { |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 801 | removeVpnUserLocked(userHandle); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 802 | } catch (Exception e) { |
| 803 | Log.wtf(TAG, "Failed to remove restricted user to owner", e); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 809 | /** |
| 810 | * Return the configuration of the currently running VPN. |
| 811 | */ |
| 812 | public VpnConfig getVpnConfig() { |
| 813 | enforceControlPermission(); |
| 814 | return mConfig; |
| 815 | } |
| 816 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 817 | @Deprecated |
| 818 | public synchronized void interfaceStatusChanged(String iface, boolean up) { |
| 819 | try { |
| 820 | mObserver.interfaceStatusChanged(iface, up); |
| 821 | } catch (RemoteException e) { |
| 822 | // ignored; target is local |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 826 | private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() { |
| 827 | @Override |
| 828 | public void interfaceStatusChanged(String interfaze, boolean up) { |
| 829 | synchronized (Vpn.this) { |
| 830 | if (!up && mLegacyVpnRunner != null) { |
| 831 | mLegacyVpnRunner.check(interfaze); |
| 832 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 833 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 834 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 835 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 836 | @Override |
| 837 | public void interfaceRemoved(String interfaze) { |
| 838 | synchronized (Vpn.this) { |
| 839 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 840 | mStatusIntent = null; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 841 | mVpnUsers = null; |
Paul Jensen | c4c7231 | 2014-12-08 14:04:51 -0500 | [diff] [blame] | 842 | mConfig = null; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 843 | mInterface = null; |
| 844 | if (mConnection != null) { |
| 845 | mContext.unbindService(mConnection); |
| 846 | mConnection = null; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 847 | agentDisconnect(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 848 | } else if (mLegacyVpnRunner != null) { |
| 849 | mLegacyVpnRunner.exit(); |
| 850 | mLegacyVpnRunner = null; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 856 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 857 | private void enforceControlPermission() { |
Jeff Davidson | bc19c18 | 2014-11-11 13:20:01 -0800 | [diff] [blame] | 858 | mContext.enforceCallingPermission(Manifest.permission.CONTROL_VPN, "Unauthorized Caller"); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 861 | private void enforceControlPermissionOrInternalCaller() { |
| 862 | // Require caller to be either an application with CONTROL_VPN permission or a process |
| 863 | // in the system server. |
| 864 | mContext.enforceCallingOrSelfPermission(Manifest.permission.CONTROL_VPN, |
| 865 | "Unauthorized Caller"); |
| 866 | } |
| 867 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 868 | private class Connection implements ServiceConnection { |
| 869 | private IBinder mService; |
| 870 | |
| 871 | @Override |
| 872 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 873 | mService = service; |
| 874 | } |
| 875 | |
| 876 | @Override |
| 877 | public void onServiceDisconnected(ComponentName name) { |
| 878 | mService = null; |
| 879 | } |
| 880 | } |
| 881 | |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 882 | private void prepareStatusIntent() { |
| 883 | final long token = Binder.clearCallingIdentity(); |
| 884 | try { |
| 885 | mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext); |
| 886 | } finally { |
| 887 | Binder.restoreCallingIdentity(token); |
| 888 | } |
| 889 | } |
| 890 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 891 | public synchronized boolean addAddress(String address, int prefixLength) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 892 | if (!isCallerEstablishedOwnerLocked()) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 893 | return false; |
| 894 | } |
| 895 | boolean success = jniAddAddress(mInterface, address, prefixLength); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 896 | mNetworkAgent.sendLinkProperties(makeLinkProperties()); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 897 | return success; |
| 898 | } |
| 899 | |
| 900 | public synchronized boolean removeAddress(String address, int prefixLength) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 901 | if (!isCallerEstablishedOwnerLocked()) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 902 | return false; |
| 903 | } |
| 904 | boolean success = jniDelAddress(mInterface, address, prefixLength); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 905 | mNetworkAgent.sendLinkProperties(makeLinkProperties()); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 906 | return success; |
| 907 | } |
| 908 | |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 909 | public synchronized boolean setUnderlyingNetworks(Network[] networks) { |
| 910 | if (!isCallerEstablishedOwnerLocked()) { |
| 911 | return false; |
| 912 | } |
| 913 | if (networks == null) { |
| 914 | mConfig.underlyingNetworks = null; |
| 915 | } else { |
| 916 | mConfig.underlyingNetworks = new Network[networks.length]; |
| 917 | for (int i = 0; i < networks.length; ++i) { |
| 918 | if (networks[i] == null) { |
| 919 | mConfig.underlyingNetworks[i] = null; |
| 920 | } else { |
| 921 | mConfig.underlyingNetworks[i] = new Network(networks[i].netId); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | public synchronized Network[] getUnderlyingNetworks() { |
| 929 | if (!isRunningLocked()) { |
| 930 | return null; |
| 931 | } |
| 932 | return mConfig.underlyingNetworks; |
| 933 | } |
| 934 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 935 | /** |
| 936 | * This method should only be called by ConnectivityService. Because it doesn't |
| 937 | * have enough data to fill VpnInfo.primaryUnderlyingIface field. |
| 938 | */ |
| 939 | public synchronized VpnInfo getVpnInfo() { |
| 940 | if (!isRunningLocked()) { |
| 941 | return null; |
| 942 | } |
| 943 | |
| 944 | VpnInfo info = new VpnInfo(); |
| 945 | info.ownerUid = mOwnerUID; |
| 946 | info.vpnIface = mInterface; |
| 947 | return info; |
| 948 | } |
| 949 | |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 950 | public synchronized boolean appliesToUid(int uid) { |
| 951 | if (!isRunningLocked()) { |
| 952 | return false; |
| 953 | } |
| 954 | for (UidRange uidRange : mVpnUsers) { |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 955 | if (uidRange.contains(uid)) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 956 | return true; |
| 957 | } |
| 958 | } |
| 959 | return false; |
| 960 | } |
| 961 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 962 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 963 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 964 | private native int jniSetAddresses(String interfaze, String addresses); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 965 | private native void jniReset(String interfaze); |
| 966 | private native int jniCheck(String interfaze); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 967 | private native boolean jniAddAddress(String interfaze, String address, int prefixLen); |
| 968 | private native boolean jniDelAddress(String interfaze, String address, int prefixLen); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 969 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 970 | private static RouteInfo findIPv4DefaultRoute(LinkProperties prop) { |
| 971 | for (RouteInfo route : prop.getAllRoutes()) { |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 972 | // Currently legacy VPN only works on IPv4. |
| 973 | if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) { |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 974 | return route; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 977 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 978 | throw new IllegalStateException("Unable to find IPv4 default gateway"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 983 | * secondary thread to perform connection work, returning quickly. |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 984 | * |
| 985 | * Should only be called to respond to Binder requests as this enforces caller permission. Use |
| 986 | * {@link #startLegacyVpnPrivileged(VpnProfile, KeyStore, LinkProperties)} to skip the |
| 987 | * 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] | 988 | */ |
| 989 | public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 990 | enforceControlPermission(); |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 991 | long token = Binder.clearCallingIdentity(); |
| 992 | try { |
| 993 | startLegacyVpnPrivileged(profile, keyStore, egress); |
| 994 | } finally { |
| 995 | Binder.restoreCallingIdentity(token); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Like {@link #startLegacyVpn(VpnProfile, KeyStore, LinkProperties)}, but does not check |
| 1001 | * permissions under the assumption that the caller is the system. |
| 1002 | * |
| 1003 | * Callers are responsible for checking permissions if needed. |
| 1004 | */ |
| 1005 | public void startLegacyVpnPrivileged(VpnProfile profile, KeyStore keyStore, |
| 1006 | LinkProperties egress) { |
Julia Reynolds | f5116d0 | 2014-07-01 11:10:41 -0400 | [diff] [blame] | 1007 | UserManager mgr = UserManager.get(mContext); |
Paul Jensen | 0784eea | 2014-08-19 16:00:24 -0400 | [diff] [blame] | 1008 | UserInfo user = mgr.getUserInfo(mUserHandle); |
Nicolas Prevot | 95778ff | 2015-01-06 15:43:05 +0000 | [diff] [blame] | 1009 | if (user.isRestricted() || mgr.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN, |
| 1010 | new UserHandle(mUserHandle))) { |
Julia Reynolds | f5116d0 | 2014-07-01 11:10:41 -0400 | [diff] [blame] | 1011 | throw new SecurityException("Restricted users cannot establish VPNs"); |
| 1012 | } |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1013 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 1014 | final RouteInfo ipv4DefaultRoute = findIPv4DefaultRoute(egress); |
| 1015 | final String gateway = ipv4DefaultRoute.getGateway().getHostAddress(); |
| 1016 | final String iface = ipv4DefaultRoute.getInterface(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1017 | |
| 1018 | // Load certificates. |
| 1019 | String privateKey = ""; |
| 1020 | String userCert = ""; |
| 1021 | String caCert = ""; |
| 1022 | String serverCert = ""; |
| 1023 | if (!profile.ipsecUserCert.isEmpty()) { |
| 1024 | privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert; |
| 1025 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1026 | userCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1027 | } |
| 1028 | if (!profile.ipsecCaCert.isEmpty()) { |
| 1029 | byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1030 | caCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1031 | } |
| 1032 | if (!profile.ipsecServerCert.isEmpty()) { |
| 1033 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1034 | serverCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1035 | } |
| 1036 | if (privateKey == null || userCert == null || caCert == null || serverCert == null) { |
| 1037 | throw new IllegalStateException("Cannot load credentials"); |
| 1038 | } |
| 1039 | |
| 1040 | // Prepare arguments for racoon. |
| 1041 | String[] racoon = null; |
| 1042 | switch (profile.type) { |
| 1043 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 1044 | racoon = new String[] { |
| 1045 | iface, profile.server, "udppsk", profile.ipsecIdentifier, |
| 1046 | profile.ipsecSecret, "1701", |
| 1047 | }; |
| 1048 | break; |
| 1049 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 1050 | racoon = new String[] { |
| 1051 | iface, profile.server, "udprsa", privateKey, userCert, |
| 1052 | caCert, serverCert, "1701", |
| 1053 | }; |
| 1054 | break; |
| 1055 | case VpnProfile.TYPE_IPSEC_XAUTH_PSK: |
| 1056 | racoon = new String[] { |
| 1057 | iface, profile.server, "xauthpsk", profile.ipsecIdentifier, |
| 1058 | profile.ipsecSecret, profile.username, profile.password, "", gateway, |
| 1059 | }; |
| 1060 | break; |
| 1061 | case VpnProfile.TYPE_IPSEC_XAUTH_RSA: |
| 1062 | racoon = new String[] { |
| 1063 | iface, profile.server, "xauthrsa", privateKey, userCert, |
| 1064 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 1065 | }; |
| 1066 | break; |
| 1067 | case VpnProfile.TYPE_IPSEC_HYBRID_RSA: |
| 1068 | racoon = new String[] { |
| 1069 | iface, profile.server, "hybridrsa", |
| 1070 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 1071 | }; |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | // Prepare arguments for mtpd. |
| 1076 | String[] mtpd = null; |
| 1077 | switch (profile.type) { |
| 1078 | case VpnProfile.TYPE_PPTP: |
| 1079 | mtpd = new String[] { |
| 1080 | iface, "pptp", profile.server, "1723", |
| 1081 | "name", profile.username, "password", profile.password, |
| 1082 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 1083 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 1084 | (profile.mppe ? "+mppe" : "nomppe"), |
| 1085 | }; |
| 1086 | break; |
| 1087 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 1088 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 1089 | mtpd = new String[] { |
| 1090 | iface, "l2tp", profile.server, "1701", profile.l2tpSecret, |
| 1091 | "name", profile.username, "password", profile.password, |
| 1092 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 1093 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 1094 | }; |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | VpnConfig config = new VpnConfig(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1099 | config.legacy = true; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1100 | config.user = profile.key; |
| 1101 | config.interfaze = iface; |
| 1102 | config.session = profile.name; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1103 | |
| 1104 | config.addLegacyRoutes(profile.routes); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1105 | if (!profile.dnsServers.isEmpty()) { |
| 1106 | config.dnsServers = Arrays.asList(profile.dnsServers.split(" +")); |
| 1107 | } |
| 1108 | if (!profile.searchDomains.isEmpty()) { |
| 1109 | config.searchDomains = Arrays.asList(profile.searchDomains.split(" +")); |
| 1110 | } |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 1111 | startLegacyVpn(config, racoon, mtpd); |
| 1112 | } |
| 1113 | |
| 1114 | private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1115 | stopLegacyVpnPrivileged(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1116 | |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1117 | // Prepare for the new request. |
| 1118 | prepareInternal(VpnConfig.LEGACY_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1119 | updateState(DetailedState.CONNECTING, "startLegacyVpn"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1120 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1121 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 1122 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 1123 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 1126 | /** Stop legacy VPN. Permissions must be checked by callers. */ |
| 1127 | public synchronized void stopLegacyVpnPrivileged() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1128 | if (mLegacyVpnRunner != null) { |
| 1129 | mLegacyVpnRunner.exit(); |
| 1130 | mLegacyVpnRunner = null; |
| 1131 | |
| 1132 | synchronized (LegacyVpnRunner.TAG) { |
| 1133 | // wait for old thread to completely finish before spinning up |
| 1134 | // new instance, otherwise state updates can be out of order. |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1139 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1140 | * Return the information of the current ongoing legacy VPN. |
| 1141 | */ |
| 1142 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 1143 | // Check if the caller is authorized. |
| 1144 | enforceControlPermission(); |
sj.cha | 08bbca0 | 2015-03-23 11:35:24 +0900 | [diff] [blame] | 1145 | return getLegacyVpnInfoPrivileged(); |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * Return the information of the current ongoing legacy VPN. |
| 1150 | * Callers are responsible for checking permissions if needed. |
| 1151 | */ |
| 1152 | public synchronized LegacyVpnInfo getLegacyVpnInfoPrivileged() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1153 | if (mLegacyVpnRunner == null) return null; |
| 1154 | |
| 1155 | final LegacyVpnInfo info = new LegacyVpnInfo(); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1156 | info.key = mConfig.user; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1157 | info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); |
Jeff Davidson | 90b1b9f | 2014-08-22 13:05:43 -0700 | [diff] [blame] | 1158 | if (mNetworkInfo.isConnected()) { |
| 1159 | info.intent = mStatusIntent; |
| 1160 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1161 | return info; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1164 | public VpnConfig getLegacyVpnConfig() { |
| 1165 | if (mLegacyVpnRunner != null) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1166 | return mConfig; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | return null; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1172 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1173 | * Bringing up a VPN connection takes time, and that is all this thread |
| 1174 | * does. Here we have plenty of time. The only thing we need to take |
| 1175 | * care of is responding to interruptions as soon as possible. Otherwise |
| 1176 | * requests will be piled up. This can be done in a Handler as a state |
| 1177 | * machine, but it is much easier to read in the current form. |
| 1178 | */ |
| 1179 | private class LegacyVpnRunner extends Thread { |
| 1180 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1181 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1182 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1183 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1184 | private final LocalSocket[] mSockets; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1185 | private final String mOuterInterface; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1186 | private final AtomicInteger mOuterConnection = |
| 1187 | new AtomicInteger(ConnectivityManager.TYPE_NONE); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1188 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1189 | private long mTimer = -1; |
| 1190 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1191 | /** |
| 1192 | * Watch for the outer connection (passing in the constructor) going away. |
| 1193 | */ |
| 1194 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1195 | @Override |
| 1196 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 1197 | if (!mEnableTeardown) return; |
| 1198 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1199 | if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
| 1200 | if (intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, |
| 1201 | ConnectivityManager.TYPE_NONE) == mOuterConnection.get()) { |
| 1202 | NetworkInfo info = (NetworkInfo)intent.getExtra( |
| 1203 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 1204 | if (info != null && !info.isConnectedOrConnecting()) { |
| 1205 | try { |
| 1206 | mObserver.interfaceStatusChanged(mOuterInterface, false); |
| 1207 | } catch (RemoteException e) {} |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | }; |
| 1213 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1214 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1215 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1216 | mConfig = config; |
| 1217 | mDaemons = new String[] {"racoon", "mtpd"}; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1218 | // TODO: clear arguments from memory once launched |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1219 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1220 | mSockets = new LocalSocket[mDaemons.length]; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1221 | |
| 1222 | // This is the interface which VPN is running on, |
| 1223 | // mConfig.interfaze will change to point to OUR |
| 1224 | // internal interface soon. TODO - add inner/outer to mconfig |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1225 | // 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] | 1226 | // 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] | 1227 | // registering |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1228 | mOuterInterface = mConfig.interfaze; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1229 | |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1230 | if (!TextUtils.isEmpty(mOuterInterface)) { |
| 1231 | final ConnectivityManager cm = ConnectivityManager.from(mContext); |
| 1232 | for (Network network : cm.getAllNetworks()) { |
| 1233 | final LinkProperties lp = cm.getLinkProperties(network); |
Lorenzo Colitti | 1b60d11 | 2015-07-02 13:03:03 +0900 | [diff] [blame] | 1234 | if (lp != null && lp.getAllInterfaceNames().contains(mOuterInterface)) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1235 | final NetworkInfo networkInfo = cm.getNetworkInfo(network); |
| 1236 | if (networkInfo != null) mOuterConnection.set(networkInfo.getType()); |
| 1237 | } |
| 1238 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | IntentFilter filter = new IntentFilter(); |
| 1242 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
| 1243 | mContext.registerReceiver(mBroadcastReceiver, filter); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1246 | public void check(String interfaze) { |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 1247 | if (interfaze.equals(mOuterInterface)) { |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 1248 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 1249 | exit(); |
| 1250 | } |
| 1251 | } |
| 1252 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1253 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1254 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1255 | interrupt(); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1256 | for (LocalSocket socket : mSockets) { |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 1257 | IoUtils.closeQuietly(socket); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1258 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1259 | agentDisconnect(); |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 1260 | try { |
| 1261 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 1262 | } catch (IllegalArgumentException e) {} |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1265 | @Override |
| 1266 | public void run() { |
| 1267 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1268 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1269 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1270 | Log.v(TAG, "Executing"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1271 | execute(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1272 | monitorDaemons(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | private void checkpoint(boolean yield) throws InterruptedException { |
| 1277 | long now = SystemClock.elapsedRealtime(); |
| 1278 | if (mTimer == -1) { |
| 1279 | mTimer = now; |
| 1280 | Thread.sleep(1); |
Chia-chi Yeh | 7ef8611 | 2011-07-22 15:46:52 -0700 | [diff] [blame] | 1281 | } else if (now - mTimer <= 60000) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1282 | Thread.sleep(yield ? 200 : 1); |
| 1283 | } else { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1284 | updateState(DetailedState.FAILED, "checkpoint"); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1285 | throw new IllegalStateException("Time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | private void execute() { |
| 1290 | // Catch all exceptions so we can clean up few things. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1291 | boolean initFinished = false; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1292 | try { |
| 1293 | // Initialize the timer. |
| 1294 | checkpoint(false); |
| 1295 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1296 | // Wait for the daemons to stop. |
| 1297 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1298 | while (!SystemService.isStopped(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1299 | checkpoint(true); |
| 1300 | } |
| 1301 | } |
| 1302 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1303 | // Clear the previous state. |
| 1304 | File state = new File("/data/misc/vpn/state"); |
| 1305 | state.delete(); |
| 1306 | if (state.exists()) { |
| 1307 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1308 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 1309 | new File("/data/misc/vpn/abort").delete(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1310 | initFinished = true; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1311 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1312 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1313 | boolean restart = false; |
| 1314 | for (String[] arguments : mArguments) { |
| 1315 | restart = restart || (arguments != null); |
| 1316 | } |
| 1317 | if (!restart) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1318 | agentDisconnect(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1319 | return; |
| 1320 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1321 | updateState(DetailedState.CONNECTING, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1322 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1323 | // Start the daemon with arguments. |
| 1324 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1325 | String[] arguments = mArguments[i]; |
| 1326 | if (arguments == null) { |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1330 | // Start the daemon. |
| 1331 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1332 | SystemService.start(daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1333 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1334 | // Wait for the daemon to start. |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1335 | while (!SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1336 | checkpoint(true); |
| 1337 | } |
| 1338 | |
| 1339 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1340 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1341 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1342 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1343 | |
| 1344 | // Wait for the socket to connect. |
| 1345 | while (true) { |
| 1346 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1347 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1348 | break; |
| 1349 | } catch (Exception e) { |
| 1350 | // ignore |
| 1351 | } |
| 1352 | checkpoint(true); |
| 1353 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1354 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1355 | |
| 1356 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1357 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1358 | for (String argument : arguments) { |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1359 | byte[] bytes = argument.getBytes(StandardCharsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1360 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1361 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1362 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1363 | out.write(bytes.length >> 8); |
| 1364 | out.write(bytes.length); |
| 1365 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1366 | checkpoint(false); |
| 1367 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1368 | out.write(0xFF); |
| 1369 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1370 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1371 | |
| 1372 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1373 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1374 | while (true) { |
| 1375 | try { |
| 1376 | if (in.read() == -1) { |
| 1377 | break; |
| 1378 | } |
| 1379 | } catch (Exception e) { |
| 1380 | // ignore |
| 1381 | } |
| 1382 | checkpoint(true); |
| 1383 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1386 | // Wait for the daemons to create the new state. |
| 1387 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1388 | // Check if a running daemon is dead. |
| 1389 | for (int i = 0; i < mDaemons.length; ++i) { |
| 1390 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1391 | if (mArguments[i] != null && !SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1392 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | checkpoint(true); |
| 1396 | } |
| 1397 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1398 | // Now we are connected. Read and parse the new state. |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 1399 | String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1); |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 1400 | if (parameters.length != 7) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1401 | throw new IllegalStateException("Cannot parse the state"); |
| 1402 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1403 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1404 | // Set the interface and the addresses in the config. |
| 1405 | mConfig.interfaze = parameters[0].trim(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1406 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1407 | mConfig.addLegacyAddresses(parameters[1]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1408 | // Set the routes if they are not set in the config. |
| 1409 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1410 | mConfig.addLegacyRoutes(parameters[2]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | // 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] | 1414 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1415 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1416 | if (!dnsServers.isEmpty()) { |
| 1417 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 1418 | } |
| 1419 | } |
| 1420 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1421 | // Set the search domains if they are not set in the config. |
| 1422 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 1423 | String searchDomains = parameters[4].trim(); |
| 1424 | if (!searchDomains.isEmpty()) { |
| 1425 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 1426 | } |
| 1427 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1428 | |
Lorenzo Colitti | 5026279 | 2014-09-19 01:53:35 +0900 | [diff] [blame] | 1429 | // Add a throw route for the VPN server endpoint, if one was specified. |
| 1430 | String endpoint = parameters[5]; |
| 1431 | if (!endpoint.isEmpty()) { |
| 1432 | try { |
| 1433 | InetAddress addr = InetAddress.parseNumericAddress(endpoint); |
| 1434 | if (addr instanceof Inet4Address) { |
| 1435 | mConfig.routes.add(new RouteInfo(new IpPrefix(addr, 32), RTN_THROW)); |
| 1436 | } else if (addr instanceof Inet6Address) { |
| 1437 | mConfig.routes.add(new RouteInfo(new IpPrefix(addr, 128), RTN_THROW)); |
| 1438 | } else { |
| 1439 | Log.e(TAG, "Unknown IP address family for VPN endpoint: " + endpoint); |
| 1440 | } |
| 1441 | } catch (IllegalArgumentException e) { |
| 1442 | Log.e(TAG, "Exception constructing throw route to " + endpoint + ": " + e); |
| 1443 | } |
| 1444 | } |
| 1445 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1446 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1447 | synchronized (Vpn.this) { |
Vinit Deshapnde | 2b862e5 | 2013-10-02 11:50:39 -0700 | [diff] [blame] | 1448 | // Set the start time |
| 1449 | mConfig.startTime = SystemClock.elapsedRealtime(); |
| 1450 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1451 | // Check if the thread is interrupted while we are waiting. |
| 1452 | checkpoint(false); |
| 1453 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1454 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1455 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 1456 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1457 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1458 | |
| 1459 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1460 | mInterface = mConfig.interfaze; |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame^] | 1461 | prepareStatusIntent(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1462 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1463 | agentConnect(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1464 | |
| 1465 | Log.i(TAG, "Connected!"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1466 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1467 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1468 | Log.i(TAG, "Aborting", e); |
Lorenzo Colitti | 4384060 | 2014-08-20 16:01:44 -0700 | [diff] [blame] | 1469 | updateState(DetailedState.FAILED, e.getMessage()); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1470 | exit(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1471 | } finally { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1472 | // Kill the daemons if they fail to stop. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1473 | if (!initFinished) { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1474 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1475 | SystemService.stop(daemon); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1479 | // Do not leave an unstable state. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1480 | if (!initFinished || mNetworkInfo.getDetailedState() == DetailedState.CONNECTING) { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1481 | agentDisconnect(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1482 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1483 | } |
| 1484 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1485 | |
| 1486 | /** |
| 1487 | * Monitor the daemons we started, moving to disconnected state if the |
| 1488 | * underlying services fail. |
| 1489 | */ |
| 1490 | private void monitorDaemons() { |
| 1491 | if (!mNetworkInfo.isConnected()) { |
| 1492 | return; |
| 1493 | } |
| 1494 | |
| 1495 | try { |
| 1496 | while (true) { |
| 1497 | Thread.sleep(2000); |
| 1498 | for (int i = 0; i < mDaemons.length; i++) { |
| 1499 | if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) { |
| 1500 | return; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | } catch (InterruptedException e) { |
| 1505 | Log.d(TAG, "interrupted during monitorDaemons(); stopping services"); |
| 1506 | } finally { |
| 1507 | for (String daemon : mDaemons) { |
| 1508 | SystemService.stop(daemon); |
| 1509 | } |
| 1510 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1511 | agentDisconnect(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1512 | } |
| 1513 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1514 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1515 | } |