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 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.net.INetworkManagementEventObserver; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 30 | import android.net.LocalSocket; |
| 31 | import android.net.LocalSocketAddress; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 33 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 34 | import android.os.Process; |
| 35 | import android.os.SystemClock; |
| 36 | import android.os.SystemProperties; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| 39 | import com.android.internal.R; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 40 | import com.android.internal.net.VpnConfig; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 41 | import com.android.server.ConnectivityService.VpnCallback; |
| 42 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 43 | import java.io.OutputStream; |
| 44 | import java.nio.charset.Charsets; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 45 | import java.util.Arrays; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 46 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 47 | /** |
| 48 | * @hide |
| 49 | */ |
| 50 | public class Vpn extends INetworkManagementEventObserver.Stub { |
| 51 | |
| 52 | private final static String TAG = "Vpn"; |
| 53 | private final static String VPN = android.Manifest.permission.VPN; |
| 54 | |
| 55 | private final Context mContext; |
| 56 | private final VpnCallback mCallback; |
| 57 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 58 | private String mPackageName = VpnConfig.LEGACY_VPN; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 59 | private String mInterfaceName; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 60 | private LegacyVpnRunner mLegacyVpnRunner; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 61 | |
| 62 | public Vpn(Context context, VpnCallback callback) { |
| 63 | mContext = context; |
| 64 | mCallback = callback; |
| 65 | } |
| 66 | |
| 67 | /** |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 68 | * Protect a socket from routing changes by binding it to the given |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 69 | * interface. The socket IS closed by this method. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 70 | * |
| 71 | * @param socket The socket to be bound. |
| 72 | * @param name The name of the interface. |
| 73 | */ |
| 74 | public void protect(ParcelFileDescriptor socket, String name) { |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 75 | try { |
| 76 | mContext.enforceCallingPermission(VPN, "protect"); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 77 | jniProtectSocket(socket.getFd(), name); |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 78 | } finally { |
| 79 | try { |
| 80 | socket.close(); |
| 81 | } catch (Exception e) { |
| 82 | // ignore |
| 83 | } |
| 84 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 88 | * Prepare for a VPN application. If the new application is valid, |
| 89 | * the previous prepared application is revoked. Since legacy VPN |
| 90 | * is not a real application, it uses {@link VpnConfig#LEGACY_VPN} |
| 91 | * as its package name. Note that this method does not check if |
| 92 | * the applications are the same. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 93 | * |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 94 | * @param packageName The package name of the VPN application. |
| 95 | * @return The package name of the current prepared application. |
| 96 | */ |
| 97 | public synchronized String prepare(String packageName) { |
| 98 | // Return the current prepared application if the new one is null. |
| 99 | if (packageName == null) { |
| 100 | return mPackageName; |
| 101 | } |
| 102 | |
| 103 | // Only system user can call this method. |
| 104 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 105 | throw new SecurityException("Unauthorized Caller"); |
| 106 | } |
| 107 | |
| 108 | // Check the permission of the given package. |
| 109 | PackageManager pm = mContext.getPackageManager(); |
| 110 | if (!packageName.equals(VpnConfig.LEGACY_VPN) && |
| 111 | pm.checkPermission(VPN, packageName) != PackageManager.PERMISSION_GRANTED) { |
| 112 | throw new SecurityException(packageName + " does not have " + VPN); |
| 113 | } |
| 114 | |
| 115 | // Reset the interface and hide the notification. |
| 116 | if (mInterfaceName != null) { |
| 117 | jniResetInterface(mInterfaceName); |
| 118 | mCallback.restore(); |
| 119 | hideNotification(); |
| 120 | mInterfaceName = null; |
| 121 | } |
| 122 | |
| 123 | // Send out the broadcast or stop LegacyVpnRunner. |
| 124 | if (!mPackageName.equals(VpnConfig.LEGACY_VPN)) { |
| 125 | Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED); |
| 126 | intent.setPackage(mPackageName); |
| 127 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 128 | mContext.sendBroadcast(intent); |
| 129 | } else if (mLegacyVpnRunner != null) { |
| 130 | mLegacyVpnRunner.exit(); |
| 131 | mLegacyVpnRunner = null; |
| 132 | } |
| 133 | |
| 134 | Log.i(TAG, "Switched from " + mPackageName + " to " + packageName); |
| 135 | mPackageName = packageName; |
| 136 | return mPackageName; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Establish a VPN network and return the file descriptor of the VPN |
| 141 | * interface. This methods returns {@code null} if the application is |
| 142 | * not prepared or revoked. |
| 143 | * |
| 144 | * @param config The parameters to configure the network. |
| 145 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 146 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 147 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 148 | // Check the permission of the caller. |
| 149 | mContext.enforceCallingPermission(VPN, "establish"); |
| 150 | |
| 151 | // Check if the caller is already prepared. |
| 152 | PackageManager pm = mContext.getPackageManager(); |
| 153 | ApplicationInfo app = null; |
| 154 | try { |
| 155 | app = pm.getApplicationInfo(mPackageName, 0); |
| 156 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 157 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 158 | } |
| 159 | if (Binder.getCallingUid() != app.uid) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 160 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 163 | // Load the label. |
| 164 | String label = app.loadLabel(pm).toString(); |
| 165 | |
| 166 | // Load the icon and convert it into a bitmap. |
| 167 | Drawable icon = app.loadIcon(pm); |
| 168 | Bitmap bitmap = null; |
| 169 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 170 | int width = mContext.getResources().getDimensionPixelSize( |
| 171 | android.R.dimen.notification_large_icon_width); |
| 172 | int height = mContext.getResources().getDimensionPixelSize( |
| 173 | android.R.dimen.notification_large_icon_height); |
| 174 | icon.setBounds(0, 0, width, height); |
| 175 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 176 | icon.draw(new Canvas(bitmap)); |
| 177 | } |
| 178 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 179 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 180 | ParcelFileDescriptor descriptor = ParcelFileDescriptor.adoptFd( |
| 181 | jniConfigure(config.mtu, config.addresses, config.routes)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 182 | try { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 183 | String name = jniGetInterfaceName(descriptor.getFd()); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 184 | if (mInterfaceName != null && !mInterfaceName.equals(name)) { |
| 185 | jniResetInterface(mInterfaceName); |
| 186 | } |
| 187 | mInterfaceName = name; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 188 | } catch (RuntimeException e) { |
| 189 | try { |
| 190 | descriptor.close(); |
| 191 | } catch (Exception ex) { |
| 192 | // ignore |
| 193 | } |
| 194 | throw e; |
| 195 | } |
| 196 | |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 197 | // Override DNS servers and search domains. |
| 198 | mCallback.override(config.dnsServers, config.searchDomains); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 199 | |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 200 | // Fill more values. |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 201 | config.packagz = mPackageName; |
| 202 | config.interfaze = mInterfaceName; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 203 | |
| 204 | // Show the notification! |
Chia-chi Yeh | 383e052 | 2011-07-01 00:13:25 -0700 | [diff] [blame] | 205 | showNotification(config, label, bitmap); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 206 | return descriptor; |
| 207 | } |
| 208 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 209 | // INetworkManagementEventObserver.Stub |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 210 | public void interfaceStatusChanged(String name, boolean up) { |
| 211 | } |
| 212 | |
| 213 | // INetworkManagementEventObserver.Stub |
| 214 | public void interfaceLinkStateChanged(String name, boolean up) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // INetworkManagementEventObserver.Stub |
| 218 | public void interfaceAdded(String name) { |
| 219 | } |
| 220 | |
| 221 | // INetworkManagementEventObserver.Stub |
| 222 | public synchronized void interfaceRemoved(String name) { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 223 | if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 224 | mCallback.restore(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 225 | hideNotification(); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 226 | mInterfaceName = null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 230 | private void showNotification(VpnConfig config, String label, Bitmap icon) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 231 | NotificationManager nm = (NotificationManager) |
| 232 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 233 | |
| 234 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 235 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 236 | mContext.getString(R.string.vpn_title_long, label); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 237 | String text = (config.session == null) ? mContext.getString(R.string.vpn_text) : |
| 238 | mContext.getString(R.string.vpn_text_long, config.session); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 239 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 240 | long identity = Binder.clearCallingIdentity(); |
| 241 | Notification notification = new Notification.Builder(mContext) |
| 242 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 243 | .setLargeIcon(icon) |
| 244 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 245 | .setContentText(text) |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 246 | .setContentIntent(VpnConfig.getIntentForNotification(mContext, config)) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 247 | .setDefaults(Notification.DEFAULT_ALL) |
| 248 | .setOngoing(true) |
| 249 | .getNotification(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 250 | nm.notify(R.drawable.vpn_connected, notification); |
| 251 | Binder.restoreCallingIdentity(identity); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private void hideNotification() { |
| 256 | NotificationManager nm = (NotificationManager) |
| 257 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 258 | |
| 259 | if (nm != null) { |
| 260 | long identity = Binder.clearCallingIdentity(); |
| 261 | nm.cancel(R.drawable.vpn_connected); |
| 262 | Binder.restoreCallingIdentity(identity); |
| 263 | } |
| 264 | } |
| 265 | |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 266 | private native int jniConfigure(int mtu, String addresses, String routes); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 267 | private native String jniGetInterfaceName(int fd); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 268 | private native void jniResetInterface(String name); |
| 269 | private native int jniCheckInterface(String name); |
| 270 | private native void jniProtectSocket(int fd, String name); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 273 | * Handle a legacy VPN request. This method stops the daemons and restart |
| 274 | * them if arguments are not null. Heavy things are offloaded to another |
| 275 | * thread, so callers will not be blocked for a long time. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 276 | * |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 277 | * @param config The parameters to configure the network. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 278 | * @param raoocn The arguments to be passed to racoon. |
| 279 | * @param mtpd The arguments to be passed to mtpd. |
| 280 | */ |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 281 | public synchronized void doLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
| 282 | // There is nothing to stop if another VPN application is prepared. |
| 283 | if (config == null && !mPackageName.equals(VpnConfig.LEGACY_VPN)) { |
| 284 | return; |
| 285 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 286 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 287 | // Reset everything. This also checks the caller. |
| 288 | prepare(VpnConfig.LEGACY_VPN); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 289 | |
| 290 | // Start a new runner and we are done! |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 291 | if (config != null) { |
| 292 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 293 | mLegacyVpnRunner.start(); |
| 294 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Bringing up a VPN connection takes time, and that is all this thread |
| 299 | * does. Here we have plenty of time. The only thing we need to take |
| 300 | * care of is responding to interruptions as soon as possible. Otherwise |
| 301 | * requests will be piled up. This can be done in a Handler as a state |
| 302 | * machine, but it is much easier to read in the current form. |
| 303 | */ |
| 304 | private class LegacyVpnRunner extends Thread { |
| 305 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 306 | private static final String NONE = "--"; |
| 307 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 308 | private final VpnConfig mConfig; |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 309 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 310 | private final String[][] mArguments; |
| 311 | private long mTimer = -1; |
| 312 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 313 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 314 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 315 | mConfig = config; |
| 316 | mDaemons = new String[] {"racoon", "mtpd"}; |
| 317 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 318 | |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 319 | mConfig.packagz = VpnConfig.LEGACY_VPN; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | public void exit() { |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 323 | // We assume that everything is reset after the daemons die. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 324 | for (String daemon : mDaemons) { |
| 325 | SystemProperties.set("ctl.stop", daemon); |
| 326 | } |
| 327 | interrupt(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void run() { |
| 332 | // Wait for the previous thread since it has been interrupted. |
| 333 | Log.v(TAG, "wait"); |
| 334 | synchronized (TAG) { |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 335 | Log.v(TAG, "begin"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 336 | execute(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 337 | Log.v(TAG, "end"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | private void checkpoint(boolean yield) throws InterruptedException { |
| 342 | long now = SystemClock.elapsedRealtime(); |
| 343 | if (mTimer == -1) { |
| 344 | mTimer = now; |
| 345 | Thread.sleep(1); |
| 346 | } else if (now - mTimer <= 30000) { |
| 347 | Thread.sleep(yield ? 200 : 1); |
| 348 | } else { |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 349 | throw new InterruptedException("time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | private void execute() { |
| 354 | // Catch all exceptions so we can clean up few things. |
| 355 | try { |
| 356 | // Initialize the timer. |
| 357 | checkpoint(false); |
| 358 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 359 | // First stop the daemons. |
| 360 | for (String daemon : mDaemons) { |
| 361 | SystemProperties.set("ctl.stop", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 364 | // Wait for the daemons to stop. |
| 365 | for (String daemon : mDaemons) { |
| 366 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 367 | while (!"stopped".equals(SystemProperties.get(key))) { |
| 368 | checkpoint(true); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Reset the properties. |
| 373 | SystemProperties.set("vpn.dns", NONE); |
| 374 | SystemProperties.set("vpn.via", NONE); |
| 375 | while (!NONE.equals(SystemProperties.get("vpn.dns")) || |
| 376 | !NONE.equals(SystemProperties.get("vpn.via"))) { |
| 377 | checkpoint(true); |
| 378 | } |
| 379 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 380 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 381 | boolean restart = false; |
| 382 | for (String[] arguments : mArguments) { |
| 383 | restart = restart || (arguments != null); |
| 384 | } |
| 385 | if (!restart) { |
| 386 | return; |
| 387 | } |
| 388 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 389 | // Start the daemon with arguments. |
| 390 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 391 | String[] arguments = mArguments[i]; |
| 392 | if (arguments == null) { |
| 393 | continue; |
| 394 | } |
| 395 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 396 | // Start the daemon. |
| 397 | String daemon = mDaemons[i]; |
| 398 | SystemProperties.set("ctl.start", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 399 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 400 | // Wait for the daemon to start. |
| 401 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 402 | while (!"running".equals(SystemProperties.get(key))) { |
| 403 | checkpoint(true); |
| 404 | } |
| 405 | |
| 406 | // Create the control socket. |
| 407 | LocalSocket socket = new LocalSocket(); |
| 408 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 409 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 410 | |
| 411 | // Wait for the socket to connect. |
| 412 | while (true) { |
| 413 | try { |
| 414 | socket.connect(address); |
| 415 | break; |
| 416 | } catch (Exception e) { |
| 417 | // ignore |
| 418 | } |
| 419 | checkpoint(true); |
| 420 | } |
| 421 | socket.setSoTimeout(500); |
| 422 | |
| 423 | // Send over the arguments. |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 424 | OutputStream out = socket.getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 425 | for (String argument : arguments) { |
| 426 | byte[] bytes = argument.getBytes(Charsets.UTF_8); |
| 427 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 428 | throw new IllegalArgumentException("argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 429 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 430 | out.write(bytes.length >> 8); |
| 431 | out.write(bytes.length); |
| 432 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 433 | checkpoint(false); |
| 434 | } |
| 435 | |
| 436 | // Send End-Of-Arguments. |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 437 | out.write(0xFF); |
| 438 | out.write(0xFF); |
| 439 | out.flush(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 440 | socket.close(); |
| 441 | } |
| 442 | |
| 443 | // Now here is the beast from the old days. We check few |
| 444 | // properties to figure out the current status. Ideally we |
| 445 | // can read things back from the sockets and get rid of the |
| 446 | // properties, but we have no time... |
| 447 | while (NONE.equals(SystemProperties.get("vpn.dns")) || |
| 448 | NONE.equals(SystemProperties.get("vpn.via"))) { |
| 449 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 450 | // Check if a running daemon is dead. |
| 451 | for (int i = 0; i < mDaemons.length; ++i) { |
| 452 | String daemon = mDaemons[i]; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 453 | if (mArguments[i] != null && !"running".equals( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 454 | SystemProperties.get("init.svc." + daemon))) { |
| 455 | throw new IllegalArgumentException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | checkpoint(true); |
| 459 | } |
| 460 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 461 | // Now we are connected. Get the interface. |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 462 | mConfig.interfaze = SystemProperties.get("vpn.via"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 463 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 464 | // Get the DNS servers if they are not set in config. |
| 465 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
| 466 | String dnsServers = SystemProperties.get("vpn.dns").trim(); |
| 467 | if (!dnsServers.isEmpty()) { |
| 468 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 469 | } |
| 470 | } |
| 471 | |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 472 | // TODO: support search domains from ISAKMP mode config. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 473 | |
| 474 | // The final step must be synchronized. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 475 | synchronized (Vpn.this) { |
| 476 | // Check if the thread is interrupted while we are waiting. |
| 477 | checkpoint(false); |
| 478 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 479 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 480 | if (jniCheckInterface(mConfig.interfaze) == 0) { |
| 481 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 482 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 483 | |
| 484 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame^] | 485 | mInterfaceName = mConfig.interfaze; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 486 | mCallback.override(mConfig.dnsServers, mConfig.searchDomains); |
| 487 | showNotification(mConfig, null, null); |
| 488 | } |
| 489 | Log.i(TAG, "Connected!"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 490 | } catch (Exception e) { |
Chia-chi Yeh | 3281034 | 2011-07-02 16:16:03 -0700 | [diff] [blame] | 491 | Log.i(TAG, "Abort because " + e.getMessage()); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 492 | exit(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 496 | } |