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