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