blob: 3ba52d9f0949b3706d28188e0e44aab24f3aba04 [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 Yehe9107902011-07-02 01:48:50 -070088 * 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 Yehff3bdca2011-05-23 17:26:46 -070093 *
Chia-chi Yehe9107902011-07-02 01:48:50 -070094 * @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 Yehff3bdca2011-05-23 17:26:46 -0700146 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700147 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700148 // 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 Yeh7b0b8342011-06-17 14:34:11 -0700157 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700158 }
159 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700160 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700161 }
162
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700163 // 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 Yehe9107902011-07-02 01:48:50 -0700179 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh32810342011-07-02 16:16:03 -0700180 ParcelFileDescriptor descriptor = ParcelFileDescriptor.adoptFd(
181 jniConfigure(config.mtu, config.addresses, config.routes));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700182 try {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700183 String name = jniGetInterfaceName(descriptor.getFd());
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700184 if (mInterfaceName != null && !mInterfaceName.equals(name)) {
185 jniResetInterface(mInterfaceName);
186 }
187 mInterfaceName = name;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700188 } catch (RuntimeException e) {
189 try {
190 descriptor.close();
191 } catch (Exception ex) {
192 // ignore
193 }
194 throw e;
195 }
196
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700197 // Override DNS servers and search domains.
198 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700199
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700200 // Fill more values.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700201 config.packagz = mPackageName;
202 config.interfaze = mInterfaceName;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700203
204 // Show the notification!
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700205 showNotification(config, label, bitmap);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700206 return descriptor;
207 }
208
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700209 // INetworkManagementEventObserver.Stub
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700210 public void interfaceStatusChanged(String name, boolean up) {
211 }
212
213 // INetworkManagementEventObserver.Stub
214 public void interfaceLinkStateChanged(String name, boolean up) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700215 }
216
217 // INetworkManagementEventObserver.Stub
218 public void interfaceAdded(String name) {
219 }
220
221 // INetworkManagementEventObserver.Stub
222 public synchronized void interfaceRemoved(String name) {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700223 if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700224 mCallback.restore();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700225 hideNotification();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700226 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700227 }
228 }
229
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700230 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700231 NotificationManager nm = (NotificationManager)
232 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
233
234 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700235 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
236 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700237 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
238 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700239
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700240 long identity = Binder.clearCallingIdentity();
241 Notification notification = new Notification.Builder(mContext)
242 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700243 .setLargeIcon(icon)
244 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700245 .setContentText(text)
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700246 .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700247 .setDefaults(Notification.DEFAULT_ALL)
248 .setOngoing(true)
249 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700250 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 Yeh32810342011-07-02 16:16:03 -0700266 private native int jniConfigure(int mtu, String addresses, String routes);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700267 private native String jniGetInterfaceName(int fd);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700268 private native void jniResetInterface(String name);
269 private native int jniCheckInterface(String name);
270 private native void jniProtectSocket(int fd, String name);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700271
272 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700273 * 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 Yeh85a7ce02011-06-29 16:05:58 -0700276 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700277 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700278 * @param raoocn The arguments to be passed to racoon.
279 * @param mtpd The arguments to be passed to mtpd.
280 */
Chia-chi Yehe9107902011-07-02 01:48:50 -0700281 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 Yeh85a7ce02011-06-29 16:05:58 -0700286
Chia-chi Yehe9107902011-07-02 01:48:50 -0700287 // Reset everything. This also checks the caller.
288 prepare(VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700289
290 // Start a new runner and we are done!
Chia-chi Yehe9107902011-07-02 01:48:50 -0700291 if (config != null) {
292 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
293 mLegacyVpnRunner.start();
294 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700295 }
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 Yeh85a7ce02011-06-29 16:05:58 -0700306 private static final String NONE = "--";
307
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700308 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700309 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700310 private final String[][] mArguments;
311 private long mTimer = -1;
312
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700313 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700314 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700315 mConfig = config;
316 mDaemons = new String[] {"racoon", "mtpd"};
317 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yehe9107902011-07-02 01:48:50 -0700318
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700319 mConfig.packagz = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700320 }
321
322 public void exit() {
Chia-chi Yehe9107902011-07-02 01:48:50 -0700323 // We assume that everything is reset after the daemons die.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700324 for (String daemon : mDaemons) {
325 SystemProperties.set("ctl.stop", daemon);
326 }
327 interrupt();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700328 }
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 Yeh41d16852011-07-01 02:12:06 -0700335 Log.v(TAG, "begin");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700336 execute();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700337 Log.v(TAG, "end");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700338 }
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 Yeh32810342011-07-02 16:16:03 -0700349 throw new InterruptedException("time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700350 }
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 Yeh1f7746b2011-07-01 00:29:06 -0700359 // First stop the daemons.
360 for (String daemon : mDaemons) {
361 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700362 }
363
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700364 // Wait for the daemons to stop.
365 for (String daemon : mDaemons) {
366 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700367 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 Yehe9107902011-07-02 01:48:50 -0700380 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700381 boolean restart = false;
382 for (String[] arguments : mArguments) {
383 restart = restart || (arguments != null);
384 }
385 if (!restart) {
386 return;
387 }
388
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700389 // Start the daemon with arguments.
390 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700391 String[] arguments = mArguments[i];
392 if (arguments == null) {
393 continue;
394 }
395
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700396 // Start the daemon.
397 String daemon = mDaemons[i];
398 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700399
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700400 // Wait for the daemon to start.
401 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700402 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 Yeh1f7746b2011-07-01 00:29:06 -0700409 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700410
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 Yeh1f7746b2011-07-01 00:29:06 -0700424 OutputStream out = socket.getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700425 for (String argument : arguments) {
426 byte[] bytes = argument.getBytes(Charsets.UTF_8);
427 if (bytes.length >= 0xFFFF) {
Chia-chi Yeh32810342011-07-02 16:16:03 -0700428 throw new IllegalArgumentException("argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700429 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700430 out.write(bytes.length >> 8);
431 out.write(bytes.length);
432 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700433 checkpoint(false);
434 }
435
436 // Send End-Of-Arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700437 out.write(0xFF);
438 out.write(0xFF);
439 out.flush();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700440 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 Yeh1f7746b2011-07-01 00:29:06 -0700450 // Check if a running daemon is dead.
451 for (int i = 0; i < mDaemons.length; ++i) {
452 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700453 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700454 SystemProperties.get("init.svc." + daemon))) {
455 throw new IllegalArgumentException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700456 }
457 }
458 checkpoint(true);
459 }
460
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700461 // Now we are connected. Get the interface.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700462 mConfig.interfaze = SystemProperties.get("vpn.via");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700463
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700464 // 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 Yeh32810342011-07-02 16:16:03 -0700472 // TODO: support search domains from ISAKMP mode config.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700473
474 // The final step must be synchronized.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700475 synchronized (Vpn.this) {
476 // Check if the thread is interrupted while we are waiting.
477 checkpoint(false);
478
Chia-chi Yehe9107902011-07-02 01:48:50 -0700479 // Check if the interface is gone while we are waiting.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700480 if (jniCheckInterface(mConfig.interfaze) == 0) {
481 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700482 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700483
484 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700485 mInterfaceName = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700486 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
487 showNotification(mConfig, null, null);
488 }
489 Log.i(TAG, "Connected!");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700490 } catch (Exception e) {
Chia-chi Yeh32810342011-07-02 16:16:03 -0700491 Log.i(TAG, "Abort because " + e.getMessage());
Chia-chi Yehe9107902011-07-02 01:48:50 -0700492 exit();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700493 }
494 }
495 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700496}