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