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