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