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