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