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