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 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 21 | import android.app.Notification; |
| 22 | import android.app.NotificationManager; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.Canvas; |
| 33 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 34 | import android.net.BaseNetworkStateTracker; |
| 35 | import android.net.ConnectivityManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 36 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 37 | import android.net.LinkProperties; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 38 | import android.net.LocalSocket; |
| 39 | import android.net.LocalSocketAddress; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 40 | import android.net.NetworkInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 41 | import android.net.RouteInfo; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 42 | import android.net.NetworkInfo.DetailedState; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 43 | import android.os.Binder; |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 44 | import android.os.FileUtils; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 45 | import android.os.IBinder; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 46 | import android.os.INetworkManagementService; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 47 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 48 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 49 | import android.os.Process; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 50 | import android.os.RemoteException; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 51 | import android.os.SystemClock; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 52 | import android.os.SystemService; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 53 | import android.os.UserHandle; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 54 | import android.security.Credentials; |
| 55 | import android.security.KeyStore; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 56 | import android.util.Log; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 57 | import android.widget.Toast; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 58 | |
| 59 | import com.android.internal.R; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 60 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 61 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 62 | import com.android.internal.net.VpnProfile; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 63 | import com.android.internal.util.Preconditions; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 64 | import com.android.server.ConnectivityService.VpnCallback; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 65 | import com.android.server.net.BaseNetworkObserver; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 66 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 67 | import java.io.File; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 68 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 69 | import java.io.OutputStream; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 70 | import java.net.Inet4Address; |
| 71 | import java.net.InetAddress; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 72 | import java.nio.charset.Charsets; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 73 | import java.util.Arrays; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 74 | |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 75 | import libcore.io.IoUtils; |
| 76 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 77 | /** |
| 78 | * @hide |
| 79 | */ |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 80 | public class Vpn extends BaseNetworkStateTracker { |
| 81 | private static final String TAG = "Vpn"; |
| 82 | private static final boolean LOGD = true; |
| 83 | |
| 84 | // TODO: create separate trackers for each unique VPN to support |
| 85 | // automated reconnection |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 86 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 87 | private final VpnCallback mCallback; |
| 88 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 89 | private String mPackage = VpnConfig.LEGACY_VPN; |
| 90 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 91 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 92 | private LegacyVpnRunner mLegacyVpnRunner; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 93 | private PendingIntent mStatusIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 94 | private boolean mEnableNotif = true; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 95 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 96 | public Vpn(Context context, VpnCallback callback, INetworkManagementService netService) { |
| 97 | // TODO: create dedicated TYPE_VPN network type |
| 98 | super(ConnectivityManager.TYPE_DUMMY); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 99 | mContext = context; |
| 100 | mCallback = callback; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 101 | |
| 102 | try { |
| 103 | netService.registerObserver(mObserver); |
| 104 | } catch (RemoteException e) { |
| 105 | Log.wtf(TAG, "Problem registering observer", e); |
| 106 | } |
| 107 | } |
| 108 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 109 | public void setEnableNotifications(boolean enableNotif) { |
| 110 | mEnableNotif = enableNotif; |
| 111 | } |
| 112 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 113 | @Override |
| 114 | protected void startMonitoringInternal() { |
| 115 | // Ignored; events are sent through callbacks for now |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean teardown() { |
| 120 | // TODO: finish migration to unique tracker for each VPN |
| 121 | throw new UnsupportedOperationException(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean reconnect() { |
| 126 | // TODO: finish migration to unique tracker for each VPN |
| 127 | throw new UnsupportedOperationException(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String getTcpBufferSizesPropName() { |
| 132 | return PROP_TCP_BUFFER_UNKNOWN; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Update current state, dispaching event to listeners. |
| 137 | */ |
| 138 | private void updateState(DetailedState detailedState, String reason) { |
| 139 | if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); |
| 140 | mNetworkInfo.setDetailedState(detailedState, reason, null); |
| 141 | mCallback.onStateChanged(new NetworkInfo(mNetworkInfo)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 145 | * Prepare for a VPN application. This method is designed to solve |
| 146 | * race conditions. It first compares the current prepared package |
| 147 | * with {@code oldPackage}. If they are the same, the prepared |
| 148 | * package is revoked and replaced with {@code newPackage}. If |
| 149 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 150 | * If {@code newPackage} is the same package or {@code null}, the |
| 151 | * revocation is omitted. This method returns {@code true} if the |
| 152 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 153 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 154 | * Legacy VPN is handled specially since it is not a real package. |
| 155 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 156 | * it can be revoked by itself. |
| 157 | * |
| 158 | * @param oldPackage The package name of the old VPN application. |
| 159 | * @param newPackage The package name of the new VPN application. |
| 160 | * @return true if the operation is succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 161 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 162 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
| 163 | // Return false if the package does not match. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 164 | if (oldPackage != null && !oldPackage.equals(mPackage)) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 165 | return false; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 168 | // Return true if we do not need to revoke. |
| 169 | if (newPackage == null || |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 170 | (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 174 | // Check if the caller is authorized. |
| 175 | enforceControlPermission(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 176 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 177 | // Reset the interface and hide the notification. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 178 | if (mInterface != null) { |
| 179 | jniReset(mInterface); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 180 | final long token = Binder.clearCallingIdentity(); |
| 181 | try { |
| 182 | mCallback.restore(); |
| 183 | hideNotification(); |
| 184 | } finally { |
| 185 | Binder.restoreCallingIdentity(token); |
| 186 | } |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 187 | mInterface = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 190 | // Revoke the connection or stop LegacyVpnRunner. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 191 | if (mConnection != null) { |
| 192 | try { |
| 193 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 194 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 195 | } catch (Exception e) { |
| 196 | // ignore |
| 197 | } |
| 198 | mContext.unbindService(mConnection); |
| 199 | mConnection = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 200 | } else if (mLegacyVpnRunner != null) { |
| 201 | mLegacyVpnRunner.exit(); |
| 202 | mLegacyVpnRunner = null; |
| 203 | } |
| 204 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 205 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 206 | mPackage = newPackage; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 207 | updateState(DetailedState.IDLE, "prepare"); |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 208 | return true; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 212 | * Protect a socket from routing changes by binding it to the given |
| 213 | * interface. The socket is NOT closed by this method. |
| 214 | * |
| 215 | * @param socket The socket to be bound. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 216 | * @param interfaze The name of the interface. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 217 | */ |
| 218 | public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception { |
| 219 | PackageManager pm = mContext.getPackageManager(); |
| 220 | ApplicationInfo app = pm.getApplicationInfo(mPackage, 0); |
| 221 | if (Binder.getCallingUid() != app.uid) { |
| 222 | throw new SecurityException("Unauthorized Caller"); |
| 223 | } |
| 224 | jniProtect(socket.getFd(), interfaze); |
| 225 | } |
| 226 | |
| 227 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 228 | * Establish a VPN network and return the file descriptor of the VPN |
| 229 | * interface. This methods returns {@code null} if the application is |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 230 | * revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 231 | * |
| 232 | * @param config The parameters to configure the network. |
| 233 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 234 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 235 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 236 | // Check if the caller is already prepared. |
| 237 | PackageManager pm = mContext.getPackageManager(); |
| 238 | ApplicationInfo app = null; |
| 239 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 240 | app = pm.getApplicationInfo(mPackage, 0); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 241 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 242 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 243 | } |
| 244 | if (Binder.getCallingUid() != app.uid) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 245 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 248 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 249 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 250 | intent.setClassName(mPackage, config.user); |
| 251 | ResolveInfo info = pm.resolveService(intent, 0); |
| 252 | if (info == null) { |
| 253 | throw new SecurityException("Cannot find " + config.user); |
| 254 | } |
| 255 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 256 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 257 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 258 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 259 | // Load the label. |
| 260 | String label = app.loadLabel(pm).toString(); |
| 261 | |
| 262 | // Load the icon and convert it into a bitmap. |
| 263 | Drawable icon = app.loadIcon(pm); |
| 264 | Bitmap bitmap = null; |
| 265 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 266 | int width = mContext.getResources().getDimensionPixelSize( |
| 267 | android.R.dimen.notification_large_icon_width); |
| 268 | int height = mContext.getResources().getDimensionPixelSize( |
| 269 | android.R.dimen.notification_large_icon_height); |
| 270 | icon.setBounds(0, 0, width, height); |
| 271 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 272 | Canvas c = new Canvas(bitmap); |
| 273 | icon.draw(c); |
| 274 | c.setBitmap(null); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 277 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 278 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 279 | try { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 280 | updateState(DetailedState.CONNECTING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 281 | String interfaze = jniGetName(tun.getFd()); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 282 | if (jniSetAddresses(interfaze, config.addresses) < 1) { |
| 283 | throw new IllegalArgumentException("At least one address must be specified"); |
| 284 | } |
| 285 | if (config.routes != null) { |
| 286 | jniSetRoutes(interfaze, config.routes); |
| 287 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 288 | Connection connection = new Connection(); |
| 289 | if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) { |
| 290 | throw new IllegalStateException("Cannot bind " + config.user); |
| 291 | } |
| 292 | if (mConnection != null) { |
| 293 | mContext.unbindService(mConnection); |
| 294 | } |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 295 | if (mInterface != null && !mInterface.equals(interfaze)) { |
| 296 | jniReset(mInterface); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 297 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 298 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 299 | mInterface = interfaze; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 300 | } catch (RuntimeException e) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 301 | updateState(DetailedState.FAILED, "establish"); |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 302 | IoUtils.closeQuietly(tun); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 303 | throw e; |
| 304 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 305 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 306 | |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 307 | // Fill more values. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 308 | config.user = mPackage; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 309 | config.interfaze = mInterface; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 310 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 311 | // Override DNS servers and show the notification. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 312 | final long token = Binder.clearCallingIdentity(); |
| 313 | try { |
| 314 | mCallback.override(config.dnsServers, config.searchDomains); |
| 315 | showNotification(config, label, bitmap); |
| 316 | } finally { |
| 317 | Binder.restoreCallingIdentity(token); |
| 318 | } |
| 319 | // TODO: ensure that contract class eventually marks as connected |
| 320 | updateState(DetailedState.AUTHENTICATING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 321 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 324 | @Deprecated |
| 325 | public synchronized void interfaceStatusChanged(String iface, boolean up) { |
| 326 | try { |
| 327 | mObserver.interfaceStatusChanged(iface, up); |
| 328 | } catch (RemoteException e) { |
| 329 | // ignored; target is local |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 333 | private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() { |
| 334 | @Override |
| 335 | public void interfaceStatusChanged(String interfaze, boolean up) { |
| 336 | synchronized (Vpn.this) { |
| 337 | if (!up && mLegacyVpnRunner != null) { |
| 338 | mLegacyVpnRunner.check(interfaze); |
| 339 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 340 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 341 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 342 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public void interfaceRemoved(String interfaze) { |
| 345 | synchronized (Vpn.this) { |
| 346 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
| 347 | final long token = Binder.clearCallingIdentity(); |
| 348 | try { |
| 349 | mCallback.restore(); |
| 350 | hideNotification(); |
| 351 | } finally { |
| 352 | Binder.restoreCallingIdentity(token); |
| 353 | } |
| 354 | mInterface = null; |
| 355 | if (mConnection != null) { |
| 356 | mContext.unbindService(mConnection); |
| 357 | mConnection = null; |
| 358 | updateState(DetailedState.DISCONNECTED, "interfaceRemoved"); |
| 359 | } else if (mLegacyVpnRunner != null) { |
| 360 | mLegacyVpnRunner.exit(); |
| 361 | mLegacyVpnRunner = null; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 367 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 368 | private void enforceControlPermission() { |
| 369 | // System user is allowed to control VPN. |
| 370 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | try { |
| 375 | // System dialogs are also allowed to control VPN. |
| 376 | PackageManager pm = mContext.getPackageManager(); |
| 377 | ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0); |
| 378 | if (Binder.getCallingUid() == app.uid) { |
| 379 | return; |
| 380 | } |
| 381 | } catch (Exception e) { |
| 382 | // ignore |
| 383 | } |
| 384 | |
| 385 | throw new SecurityException("Unauthorized Caller"); |
| 386 | } |
| 387 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 388 | private class Connection implements ServiceConnection { |
| 389 | private IBinder mService; |
| 390 | |
| 391 | @Override |
| 392 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 393 | mService = service; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void onServiceDisconnected(ComponentName name) { |
| 398 | mService = null; |
| 399 | } |
| 400 | } |
| 401 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 402 | private void showNotification(VpnConfig config, String label, Bitmap icon) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 403 | if (!mEnableNotif) return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 404 | mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext, config); |
| 405 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 406 | NotificationManager nm = (NotificationManager) |
| 407 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 408 | |
| 409 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 410 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 411 | mContext.getString(R.string.vpn_title_long, label); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 412 | String text = (config.session == null) ? mContext.getString(R.string.vpn_text) : |
| 413 | mContext.getString(R.string.vpn_text_long, config.session); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 414 | config.startTime = SystemClock.elapsedRealtime(); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 415 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 416 | Notification notification = new Notification.Builder(mContext) |
| 417 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 418 | .setLargeIcon(icon) |
| 419 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 420 | .setContentText(text) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 421 | .setContentIntent(mStatusIntent) |
Chia-chi Yeh | 50fe709 | 2012-01-11 14:26:24 -0800 | [diff] [blame] | 422 | .setDefaults(0) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 423 | .setOngoing(true) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 424 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 425 | nm.notifyAsUser(null, R.drawable.vpn_connected, notification, UserHandle.ALL); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | private void hideNotification() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 430 | if (!mEnableNotif) return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 431 | mStatusIntent = null; |
| 432 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 433 | NotificationManager nm = (NotificationManager) |
| 434 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 435 | |
| 436 | if (nm != null) { |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 437 | nm.cancelAsUser(null, R.drawable.vpn_connected, UserHandle.ALL); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 441 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 442 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 443 | private native int jniSetAddresses(String interfaze, String addresses); |
| 444 | private native int jniSetRoutes(String interfaze, String routes); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 445 | private native void jniReset(String interfaze); |
| 446 | private native int jniCheck(String interfaze); |
| 447 | private native void jniProtect(int socket, String interfaze); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 448 | |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 449 | private static String findLegacyVpnGateway(LinkProperties prop) { |
| 450 | for (RouteInfo route : prop.getRoutes()) { |
| 451 | // Currently legacy VPN only works on IPv4. |
| 452 | if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) { |
| 453 | return route.getGateway().getHostAddress(); |
| 454 | } |
| 455 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 456 | |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 457 | throw new IllegalStateException("Unable to find suitable gateway"); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 462 | * secondary thread to perform connection work, returning quickly. |
| 463 | */ |
| 464 | public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) { |
| 465 | if (keyStore.state() != KeyStore.State.UNLOCKED) { |
| 466 | throw new IllegalStateException("KeyStore isn't unlocked"); |
| 467 | } |
| 468 | |
| 469 | final String iface = egress.getInterfaceName(); |
| 470 | final String gateway = findLegacyVpnGateway(egress); |
| 471 | |
| 472 | // Load certificates. |
| 473 | String privateKey = ""; |
| 474 | String userCert = ""; |
| 475 | String caCert = ""; |
| 476 | String serverCert = ""; |
| 477 | if (!profile.ipsecUserCert.isEmpty()) { |
| 478 | privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert; |
| 479 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert); |
| 480 | userCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 481 | } |
| 482 | if (!profile.ipsecCaCert.isEmpty()) { |
| 483 | byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert); |
| 484 | caCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 485 | } |
| 486 | if (!profile.ipsecServerCert.isEmpty()) { |
| 487 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert); |
| 488 | serverCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 489 | } |
| 490 | if (privateKey == null || userCert == null || caCert == null || serverCert == null) { |
| 491 | throw new IllegalStateException("Cannot load credentials"); |
| 492 | } |
| 493 | |
| 494 | // Prepare arguments for racoon. |
| 495 | String[] racoon = null; |
| 496 | switch (profile.type) { |
| 497 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 498 | racoon = new String[] { |
| 499 | iface, profile.server, "udppsk", profile.ipsecIdentifier, |
| 500 | profile.ipsecSecret, "1701", |
| 501 | }; |
| 502 | break; |
| 503 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 504 | racoon = new String[] { |
| 505 | iface, profile.server, "udprsa", privateKey, userCert, |
| 506 | caCert, serverCert, "1701", |
| 507 | }; |
| 508 | break; |
| 509 | case VpnProfile.TYPE_IPSEC_XAUTH_PSK: |
| 510 | racoon = new String[] { |
| 511 | iface, profile.server, "xauthpsk", profile.ipsecIdentifier, |
| 512 | profile.ipsecSecret, profile.username, profile.password, "", gateway, |
| 513 | }; |
| 514 | break; |
| 515 | case VpnProfile.TYPE_IPSEC_XAUTH_RSA: |
| 516 | racoon = new String[] { |
| 517 | iface, profile.server, "xauthrsa", privateKey, userCert, |
| 518 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 519 | }; |
| 520 | break; |
| 521 | case VpnProfile.TYPE_IPSEC_HYBRID_RSA: |
| 522 | racoon = new String[] { |
| 523 | iface, profile.server, "hybridrsa", |
| 524 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 525 | }; |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | // Prepare arguments for mtpd. |
| 530 | String[] mtpd = null; |
| 531 | switch (profile.type) { |
| 532 | case VpnProfile.TYPE_PPTP: |
| 533 | mtpd = new String[] { |
| 534 | iface, "pptp", profile.server, "1723", |
| 535 | "name", profile.username, "password", profile.password, |
| 536 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 537 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 538 | (profile.mppe ? "+mppe" : "nomppe"), |
| 539 | }; |
| 540 | break; |
| 541 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 542 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 543 | mtpd = new String[] { |
| 544 | iface, "l2tp", profile.server, "1701", profile.l2tpSecret, |
| 545 | "name", profile.username, "password", profile.password, |
| 546 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 547 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 548 | }; |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | VpnConfig config = new VpnConfig(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 553 | config.legacy = true; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 554 | config.user = profile.key; |
| 555 | config.interfaze = iface; |
| 556 | config.session = profile.name; |
| 557 | config.routes = profile.routes; |
| 558 | if (!profile.dnsServers.isEmpty()) { |
| 559 | config.dnsServers = Arrays.asList(profile.dnsServers.split(" +")); |
| 560 | } |
| 561 | if (!profile.searchDomains.isEmpty()) { |
| 562 | config.searchDomains = Arrays.asList(profile.searchDomains.split(" +")); |
| 563 | } |
| 564 | |
| 565 | startLegacyVpn(config, racoon, mtpd); |
| 566 | } |
| 567 | |
| 568 | private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
| 569 | stopLegacyVpn(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 570 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 571 | // Prepare for the new request. This also checks the caller. |
| 572 | prepare(null, VpnConfig.LEGACY_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 573 | updateState(DetailedState.CONNECTING, "startLegacyVpn"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 574 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 575 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 576 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 577 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 580 | public synchronized void stopLegacyVpn() { |
| 581 | if (mLegacyVpnRunner != null) { |
| 582 | mLegacyVpnRunner.exit(); |
| 583 | mLegacyVpnRunner = null; |
| 584 | |
| 585 | synchronized (LegacyVpnRunner.TAG) { |
| 586 | // wait for old thread to completely finish before spinning up |
| 587 | // new instance, otherwise state updates can be out of order. |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 592 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 593 | * Return the information of the current ongoing legacy VPN. |
| 594 | */ |
| 595 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 596 | // Check if the caller is authorized. |
| 597 | enforceControlPermission(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 598 | if (mLegacyVpnRunner == null) return null; |
| 599 | |
| 600 | final LegacyVpnInfo info = new LegacyVpnInfo(); |
| 601 | info.key = mLegacyVpnRunner.mConfig.user; |
| 602 | info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); |
| 603 | if (mNetworkInfo.isConnected()) { |
| 604 | info.intent = mStatusIntent; |
| 605 | } |
| 606 | return info; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 609 | public VpnConfig getLegacyVpnConfig() { |
| 610 | if (mLegacyVpnRunner != null) { |
| 611 | return mLegacyVpnRunner.mConfig; |
| 612 | } else { |
| 613 | return null; |
| 614 | } |
| 615 | } |
| 616 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 617 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 618 | * Bringing up a VPN connection takes time, and that is all this thread |
| 619 | * does. Here we have plenty of time. The only thing we need to take |
| 620 | * care of is responding to interruptions as soon as possible. Otherwise |
| 621 | * requests will be piled up. This can be done in a Handler as a state |
| 622 | * machine, but it is much easier to read in the current form. |
| 623 | */ |
| 624 | private class LegacyVpnRunner extends Thread { |
| 625 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 626 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 627 | private final VpnConfig mConfig; |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 628 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 629 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 630 | private final LocalSocket[] mSockets; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 631 | private final String mOuterInterface; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 632 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 633 | private long mTimer = -1; |
| 634 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 635 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 636 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 637 | mConfig = config; |
| 638 | mDaemons = new String[] {"racoon", "mtpd"}; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 639 | // TODO: clear arguments from memory once launched |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 640 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 641 | mSockets = new LocalSocket[mDaemons.length]; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 642 | |
| 643 | // This is the interface which VPN is running on, |
| 644 | // mConfig.interfaze will change to point to OUR |
| 645 | // internal interface soon. TODO - add inner/outer to mconfig |
| 646 | mOuterInterface = mConfig.interfaze; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 649 | public void check(String interfaze) { |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 650 | if (interfaze.equals(mOuterInterface)) { |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 651 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 652 | exit(); |
| 653 | } |
| 654 | } |
| 655 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 656 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 657 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 658 | interrupt(); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 659 | for (LocalSocket socket : mSockets) { |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 660 | IoUtils.closeQuietly(socket); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 661 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 662 | updateState(DetailedState.DISCONNECTED, "exit"); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 665 | @Override |
| 666 | public void run() { |
| 667 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 668 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 669 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 670 | Log.v(TAG, "Executing"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 671 | execute(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 672 | monitorDaemons(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | private void checkpoint(boolean yield) throws InterruptedException { |
| 677 | long now = SystemClock.elapsedRealtime(); |
| 678 | if (mTimer == -1) { |
| 679 | mTimer = now; |
| 680 | Thread.sleep(1); |
Chia-chi Yeh | 7ef8611 | 2011-07-22 15:46:52 -0700 | [diff] [blame] | 681 | } else if (now - mTimer <= 60000) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 682 | Thread.sleep(yield ? 200 : 1); |
| 683 | } else { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 684 | updateState(DetailedState.FAILED, "checkpoint"); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 685 | throw new IllegalStateException("Time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | private void execute() { |
| 690 | // Catch all exceptions so we can clean up few things. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 691 | boolean initFinished = false; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 692 | try { |
| 693 | // Initialize the timer. |
| 694 | checkpoint(false); |
| 695 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 696 | // Wait for the daemons to stop. |
| 697 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 698 | while (!SystemService.isStopped(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 699 | checkpoint(true); |
| 700 | } |
| 701 | } |
| 702 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 703 | // Clear the previous state. |
| 704 | File state = new File("/data/misc/vpn/state"); |
| 705 | state.delete(); |
| 706 | if (state.exists()) { |
| 707 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 708 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 709 | new File("/data/misc/vpn/abort").delete(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 710 | initFinished = true; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 711 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 712 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 713 | boolean restart = false; |
| 714 | for (String[] arguments : mArguments) { |
| 715 | restart = restart || (arguments != null); |
| 716 | } |
| 717 | if (!restart) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 718 | updateState(DetailedState.DISCONNECTED, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 719 | return; |
| 720 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 721 | updateState(DetailedState.CONNECTING, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 722 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 723 | // Start the daemon with arguments. |
| 724 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 725 | String[] arguments = mArguments[i]; |
| 726 | if (arguments == null) { |
| 727 | continue; |
| 728 | } |
| 729 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 730 | // Start the daemon. |
| 731 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 732 | SystemService.start(daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 733 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 734 | // Wait for the daemon to start. |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 735 | while (!SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 736 | checkpoint(true); |
| 737 | } |
| 738 | |
| 739 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 740 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 741 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 742 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 743 | |
| 744 | // Wait for the socket to connect. |
| 745 | while (true) { |
| 746 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 747 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 748 | break; |
| 749 | } catch (Exception e) { |
| 750 | // ignore |
| 751 | } |
| 752 | checkpoint(true); |
| 753 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 754 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 755 | |
| 756 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 757 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 758 | for (String argument : arguments) { |
| 759 | byte[] bytes = argument.getBytes(Charsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 760 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 761 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 762 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 763 | out.write(bytes.length >> 8); |
| 764 | out.write(bytes.length); |
| 765 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 766 | checkpoint(false); |
| 767 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 768 | out.write(0xFF); |
| 769 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 770 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 771 | |
| 772 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 773 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 774 | while (true) { |
| 775 | try { |
| 776 | if (in.read() == -1) { |
| 777 | break; |
| 778 | } |
| 779 | } catch (Exception e) { |
| 780 | // ignore |
| 781 | } |
| 782 | checkpoint(true); |
| 783 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 786 | // Wait for the daemons to create the new state. |
| 787 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 788 | // Check if a running daemon is dead. |
| 789 | for (int i = 0; i < mDaemons.length; ++i) { |
| 790 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 791 | if (mArguments[i] != null && !SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 792 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | checkpoint(true); |
| 796 | } |
| 797 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 798 | // Now we are connected. Read and parse the new state. |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 799 | String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 800 | if (parameters.length != 6) { |
| 801 | throw new IllegalStateException("Cannot parse the state"); |
| 802 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 803 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 804 | // Set the interface and the addresses in the config. |
| 805 | mConfig.interfaze = parameters[0].trim(); |
| 806 | mConfig.addresses = parameters[1].trim(); |
| 807 | |
| 808 | // Set the routes if they are not set in the config. |
| 809 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
| 810 | mConfig.routes = parameters[2].trim(); |
| 811 | } |
| 812 | |
| 813 | // 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] | 814 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 815 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 816 | if (!dnsServers.isEmpty()) { |
| 817 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 818 | } |
| 819 | } |
| 820 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 821 | // Set the search domains if they are not set in the config. |
| 822 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 823 | String searchDomains = parameters[4].trim(); |
| 824 | if (!searchDomains.isEmpty()) { |
| 825 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 826 | } |
| 827 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 828 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 829 | // Set the routes. |
| 830 | jniSetRoutes(mConfig.interfaze, mConfig.routes); |
| 831 | |
| 832 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 833 | synchronized (Vpn.this) { |
| 834 | // Check if the thread is interrupted while we are waiting. |
| 835 | checkpoint(false); |
| 836 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 837 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 838 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 839 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 840 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 841 | |
| 842 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 843 | mInterface = mConfig.interfaze; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 844 | mCallback.override(mConfig.dnsServers, mConfig.searchDomains); |
| 845 | showNotification(mConfig, null, null); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 846 | |
| 847 | Log.i(TAG, "Connected!"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 848 | updateState(DetailedState.CONNECTED, "execute"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 849 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 850 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 851 | Log.i(TAG, "Aborting", e); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 852 | exit(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 853 | } finally { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 854 | // Kill the daemons if they fail to stop. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 855 | if (!initFinished) { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 856 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 857 | SystemService.stop(daemon); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 861 | // Do not leave an unstable state. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 862 | if (!initFinished || mNetworkInfo.getDetailedState() == DetailedState.CONNECTING) { |
| 863 | updateState(DetailedState.FAILED, "execute"); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 864 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 867 | |
| 868 | /** |
| 869 | * Monitor the daemons we started, moving to disconnected state if the |
| 870 | * underlying services fail. |
| 871 | */ |
| 872 | private void monitorDaemons() { |
| 873 | if (!mNetworkInfo.isConnected()) { |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | try { |
| 878 | while (true) { |
| 879 | Thread.sleep(2000); |
| 880 | for (int i = 0; i < mDaemons.length; i++) { |
| 881 | if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) { |
| 882 | return; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } catch (InterruptedException e) { |
| 887 | Log.d(TAG, "interrupted during monitorDaemons(); stopping services"); |
| 888 | } finally { |
| 889 | for (String daemon : mDaemons) { |
| 890 | SystemService.stop(daemon); |
| 891 | } |
| 892 | |
| 893 | updateState(DetailedState.DISCONNECTED, "babysit"); |
| 894 | } |
| 895 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 896 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 897 | } |