blob: 4ae12bc0b87b0828a1b8c78d108898be4217a467 [file] [log] [blame]
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07001/*
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
17package com.android.server.connectivity;
18
19import android.app.Notification;
20import android.app.NotificationManager;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.res.Resources;
26import android.graphics.Bitmap;
27import android.graphics.Canvas;
28import android.graphics.drawable.Drawable;
29import android.net.INetworkManagementEventObserver;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070030import android.net.LocalSocket;
31import android.net.LocalSocketAddress;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070032import android.os.Binder;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070033import android.os.ParcelFileDescriptor;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070034import android.os.Process;
35import android.os.SystemClock;
36import android.os.SystemProperties;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070037import android.util.Log;
38
39import com.android.internal.R;
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070040import com.android.internal.net.VpnConfig;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070041import com.android.server.ConnectivityService.VpnCallback;
42
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070043import java.io.OutputStream;
44import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070045import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070046
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070047/**
48 * @hide
49 */
50public 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 Yehe9107902011-07-02 01:48:50 -070058 private String mPackageName = VpnConfig.LEGACY_VPN;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070059 private String mInterfaceName;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070060 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070061
62 public Vpn(Context context, VpnCallback callback) {
63 mContext = context;
64 mCallback = callback;
65 }
66
67 /**
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070068 * Protect a socket from routing changes by binding it to the given
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -070069 * interface. The socket IS closed by this method.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070070 *
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 Yeh3f3337a2011-06-17 16:34:32 -070075 try {
76 mContext.enforceCallingPermission(VPN, "protect");
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -070077 jniProtectSocket(socket.getFd(), name);
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -070078 } finally {
79 try {
80 socket.close();
81 } catch (Exception e) {
82 // ignore
83 }
84 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070085 }
86
87 /**
Chia-chi Yeh100155a2011-07-03 16:52:38 -070088 * 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 Yehff3bdca2011-05-23 17:26:46 -070096 *
Chia-chi Yeh100155a2011-07-03 16:52:38 -070097 * 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 Yehe9107902011-07-02 01:48:50 -0700104 */
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700105 public synchronized boolean prepare(String oldPackage, String newPackage) {
106 // Return false if the package does not match.
107 if (oldPackage != null && !oldPackage.equals(mPackageName)) {
108 return false;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700109 }
110
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700111 // Return true if we do not need to revoke.
112 if (newPackage == null ||
113 (newPackage.equals(mPackageName) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
114 return true;
115 }
116
117 // Only system user can revoke a package.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700118 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 Yeh100155a2011-07-03 16:52:38 -0700124 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 Yehe9107902011-07-02 01:48:50 -0700127 }
128
129 // Reset the interface and hide the notification.
130 if (mInterfaceName != null) {
131 jniResetInterface(mInterfaceName);
132 mCallback.restore();
133 hideNotification();
134 mInterfaceName = null;
135 }
136
137 // Send out the broadcast or stop LegacyVpnRunner.
138 if (!mPackageName.equals(VpnConfig.LEGACY_VPN)) {
139 Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED);
140 intent.setPackage(mPackageName);
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 Yeh100155a2011-07-03 16:52:38 -0700148 Log.i(TAG, "Switched from " + mPackageName + " to " + newPackage);
149 mPackageName = newPackage;
150 return true;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700151 }
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 Yeh100155a2011-07-03 16:52:38 -0700156 * revoked or not prepared.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700157 *
158 * @param config The parameters to configure the network.
159 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700160 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700161 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700162 // 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 {
169 app = pm.getApplicationInfo(mPackageName, 0);
170 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700171 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700172 }
173 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700174 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700175 }
176
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700177 // 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 Yehe9107902011-07-02 01:48:50 -0700193 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh32810342011-07-02 16:16:03 -0700194 ParcelFileDescriptor descriptor = ParcelFileDescriptor.adoptFd(
195 jniConfigure(config.mtu, config.addresses, config.routes));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700196 try {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700197 String name = jniGetInterfaceName(descriptor.getFd());
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700198 if (mInterfaceName != null && !mInterfaceName.equals(name)) {
199 jniResetInterface(mInterfaceName);
200 }
201 mInterfaceName = name;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700202 } catch (RuntimeException e) {
203 try {
204 descriptor.close();
205 } catch (Exception ex) {
206 // ignore
207 }
208 throw e;
209 }
210
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700211 // Override DNS servers and search domains.
212 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700213
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700214 // Fill more values.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700215 config.packagz = mPackageName;
216 config.interfaze = mInterfaceName;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700217
218 // Show the notification!
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700219 showNotification(config, label, bitmap);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700220 return descriptor;
221 }
222
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700223 // INetworkManagementEventObserver.Stub
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700224 public void interfaceStatusChanged(String name, boolean up) {
225 }
226
227 // INetworkManagementEventObserver.Stub
228 public void interfaceLinkStateChanged(String name, boolean up) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700229 }
230
231 // INetworkManagementEventObserver.Stub
232 public void interfaceAdded(String name) {
233 }
234
235 // INetworkManagementEventObserver.Stub
236 public synchronized void interfaceRemoved(String name) {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700237 if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700238 mCallback.restore();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700239 hideNotification();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700240 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700241 }
242 }
243
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700244 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700245 NotificationManager nm = (NotificationManager)
246 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
247
248 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700249 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
250 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700251 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
252 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700253
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700254 long identity = Binder.clearCallingIdentity();
255 Notification notification = new Notification.Builder(mContext)
256 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700257 .setLargeIcon(icon)
258 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700259 .setContentText(text)
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700260 .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700261 .setDefaults(Notification.DEFAULT_ALL)
262 .setOngoing(true)
263 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700264 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 Yeh32810342011-07-02 16:16:03 -0700280 private native int jniConfigure(int mtu, String addresses, String routes);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700281 private native String jniGetInterfaceName(int fd);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700282 private native void jniResetInterface(String name);
283 private native int jniCheckInterface(String name);
284 private native void jniProtectSocket(int fd, String name);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700285
286 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700287 * 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 Yeh85a7ce02011-06-29 16:05:58 -0700290 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700291 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700292 * @param raoocn The arguments to be passed to racoon.
293 * @param mtpd The arguments to be passed to mtpd.
294 */
Chia-chi Yehe9107902011-07-02 01:48:50 -0700295 public synchronized void doLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700296 // Prepare for the new request. This also checks the caller.
297 prepare(null, VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700298
299 // Start a new runner and we are done!
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700300 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
301 mLegacyVpnRunner.start();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700302 }
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 Yeh85a7ce02011-06-29 16:05:58 -0700313 private static final String NONE = "--";
314
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700315 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700316 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700317 private final String[][] mArguments;
318 private long mTimer = -1;
319
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700320 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700321 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700322 mConfig = config;
323 mDaemons = new String[] {"racoon", "mtpd"};
324 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yehe9107902011-07-02 01:48:50 -0700325
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700326 mConfig.packagz = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700327 }
328
329 public void exit() {
Chia-chi Yehe9107902011-07-02 01:48:50 -0700330 // We assume that everything is reset after the daemons die.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700331 for (String daemon : mDaemons) {
332 SystemProperties.set("ctl.stop", daemon);
333 }
334 interrupt();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700335 }
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 Yeh41d16852011-07-01 02:12:06 -0700342 Log.v(TAG, "begin");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700343 execute();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700344 Log.v(TAG, "end");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700345 }
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 Yeh32810342011-07-02 16:16:03 -0700356 throw new InterruptedException("time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700357 }
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 Yeh1f7746b2011-07-01 00:29:06 -0700366 // First stop the daemons.
367 for (String daemon : mDaemons) {
368 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700369 }
370
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700371 // Wait for the daemons to stop.
372 for (String daemon : mDaemons) {
373 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700374 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 Yehe9107902011-07-02 01:48:50 -0700387 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700388 boolean restart = false;
389 for (String[] arguments : mArguments) {
390 restart = restart || (arguments != null);
391 }
392 if (!restart) {
393 return;
394 }
395
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700396 // Start the daemon with arguments.
397 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700398 String[] arguments = mArguments[i];
399 if (arguments == null) {
400 continue;
401 }
402
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700403 // Start the daemon.
404 String daemon = mDaemons[i];
405 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700406
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700407 // Wait for the daemon to start.
408 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700409 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 Yeh1f7746b2011-07-01 00:29:06 -0700416 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700417
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 Yeh1f7746b2011-07-01 00:29:06 -0700431 OutputStream out = socket.getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700432 for (String argument : arguments) {
433 byte[] bytes = argument.getBytes(Charsets.UTF_8);
434 if (bytes.length >= 0xFFFF) {
Chia-chi Yeh32810342011-07-02 16:16:03 -0700435 throw new IllegalArgumentException("argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700436 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700437 out.write(bytes.length >> 8);
438 out.write(bytes.length);
439 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700440 checkpoint(false);
441 }
442
443 // Send End-Of-Arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700444 out.write(0xFF);
445 out.write(0xFF);
446 out.flush();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700447 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 Yeh1f7746b2011-07-01 00:29:06 -0700457 // Check if a running daemon is dead.
458 for (int i = 0; i < mDaemons.length; ++i) {
459 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700460 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700461 SystemProperties.get("init.svc." + daemon))) {
462 throw new IllegalArgumentException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700463 }
464 }
465 checkpoint(true);
466 }
467
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700468 // Now we are connected. Get the interface.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700469 mConfig.interfaze = SystemProperties.get("vpn.via");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700470
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700471 // 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 Yeh32810342011-07-02 16:16:03 -0700479 // TODO: support search domains from ISAKMP mode config.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700480
481 // The final step must be synchronized.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700482 synchronized (Vpn.this) {
483 // Check if the thread is interrupted while we are waiting.
484 checkpoint(false);
485
Chia-chi Yehe9107902011-07-02 01:48:50 -0700486 // Check if the interface is gone while we are waiting.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700487 if (jniCheckInterface(mConfig.interfaze) == 0) {
488 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700489 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700490
491 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700492 mInterfaceName = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700493 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
494 showNotification(mConfig, null, null);
495 }
496 Log.i(TAG, "Connected!");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700497 } catch (Exception e) {
Chia-chi Yeh32810342011-07-02 16:16:03 -0700498 Log.i(TAG, "Abort because " + e.getMessage());
Chia-chi Yehe9107902011-07-02 01:48:50 -0700499 exit();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700500 }
501 }
502 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700503}