blob: d96bd0dab52a2504ac78237b009c62dbdb32c976 [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
Jeff Sharkey899223b2012-08-04 15:24:58 -070019import static android.Manifest.permission.BIND_VPN_SERVICE;
20
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070021import android.app.Notification;
22import android.app.NotificationManager;
Jeff Sharkey899223b2012-08-04 15:24:58 -070023import android.app.PendingIntent;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070024import android.content.ComponentName;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070025import android.content.Context;
26import android.content.Intent;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070027import android.content.ServiceConnection;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070028import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070030import android.content.pm.ResolveInfo;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070031import android.graphics.Bitmap;
32import android.graphics.Canvas;
33import android.graphics.drawable.Drawable;
Jeff Sharkey899223b2012-08-04 15:24:58 -070034import android.net.BaseNetworkStateTracker;
35import android.net.ConnectivityManager;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070036import android.net.INetworkManagementEventObserver;
Jeff Sharkey82f85212012-08-24 11:17:25 -070037import android.net.LinkProperties;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070038import android.net.LocalSocket;
39import android.net.LocalSocketAddress;
Jeff Sharkey899223b2012-08-04 15:24:58 -070040import android.net.NetworkInfo;
Jeff Sharkey82f85212012-08-24 11:17:25 -070041import android.net.RouteInfo;
Jeff Sharkey899223b2012-08-04 15:24:58 -070042import android.net.NetworkInfo.DetailedState;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070043import android.os.Binder;
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -080044import android.os.FileUtils;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070045import android.os.IBinder;
Jeff Sharkey899223b2012-08-04 15:24:58 -070046import android.os.INetworkManagementService;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070047import android.os.Parcel;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070048import android.os.ParcelFileDescriptor;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070049import android.os.Process;
Jeff Sharkey899223b2012-08-04 15:24:58 -070050import android.os.RemoteException;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070051import android.os.SystemClock;
Jeff Sharkey088f29f2012-08-05 14:55:04 -070052import android.os.SystemService;
Jeff Sharkey82f85212012-08-24 11:17:25 -070053import android.security.Credentials;
54import android.security.KeyStore;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070055import android.util.Log;
Jeff Sharkey82f85212012-08-24 11:17:25 -070056import android.widget.Toast;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070057
58import com.android.internal.R;
Chia-chi Yeh2e467642011-07-04 03:23:12 -070059import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070060import com.android.internal.net.VpnConfig;
Jeff Sharkey82f85212012-08-24 11:17:25 -070061import com.android.internal.net.VpnProfile;
Jeff Sharkey899223b2012-08-04 15:24:58 -070062import com.android.internal.util.Preconditions;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070063import com.android.server.ConnectivityService.VpnCallback;
Jeff Sharkey899223b2012-08-04 15:24:58 -070064import com.android.server.net.BaseNetworkObserver;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070065
Chia-chi Yeh97a61562011-07-14 15:05:05 -070066import java.io.File;
Chia-chi Yeh97a61562011-07-14 15:05:05 -070067import java.io.InputStream;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070068import java.io.OutputStream;
Jeff Sharkey82f85212012-08-24 11:17:25 -070069import java.net.Inet4Address;
70import java.net.InetAddress;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070071import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070072import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070073
Jeff Sharkey065b2992012-08-05 14:16:48 -070074import libcore.io.IoUtils;
75
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070076/**
77 * @hide
78 */
Jeff Sharkey899223b2012-08-04 15:24:58 -070079public class Vpn extends BaseNetworkStateTracker {
80 private static final String TAG = "Vpn";
81 private static final boolean LOGD = true;
82
83 // TODO: create separate trackers for each unique VPN to support
84 // automated reconnection
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070085
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070086 private final VpnCallback mCallback;
87
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070088 private String mPackage = VpnConfig.LEGACY_VPN;
89 private String mInterface;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070090 private Connection mConnection;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070091 private LegacyVpnRunner mLegacyVpnRunner;
Jeff Sharkey899223b2012-08-04 15:24:58 -070092 private PendingIntent mStatusIntent;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070093
Jeff Sharkey899223b2012-08-04 15:24:58 -070094 public Vpn(Context context, VpnCallback callback, INetworkManagementService netService) {
95 // TODO: create dedicated TYPE_VPN network type
96 super(ConnectivityManager.TYPE_DUMMY);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070097 mContext = context;
98 mCallback = callback;
Jeff Sharkey899223b2012-08-04 15:24:58 -070099
100 try {
101 netService.registerObserver(mObserver);
102 } catch (RemoteException e) {
103 Log.wtf(TAG, "Problem registering observer", e);
104 }
105 }
106
107 @Override
108 protected void startMonitoringInternal() {
109 // Ignored; events are sent through callbacks for now
110 }
111
112 @Override
113 public boolean teardown() {
114 // TODO: finish migration to unique tracker for each VPN
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public boolean reconnect() {
120 // TODO: finish migration to unique tracker for each VPN
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public String getTcpBufferSizesPropName() {
126 return PROP_TCP_BUFFER_UNKNOWN;
127 }
128
129 /**
130 * Update current state, dispaching event to listeners.
131 */
132 private void updateState(DetailedState detailedState, String reason) {
133 if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason);
134 mNetworkInfo.setDetailedState(detailedState, reason, null);
135 mCallback.onStateChanged(new NetworkInfo(mNetworkInfo));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700136 }
137
138 /**
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700139 * Prepare for a VPN application. This method is designed to solve
140 * race conditions. It first compares the current prepared package
141 * with {@code oldPackage}. If they are the same, the prepared
142 * package is revoked and replaced with {@code newPackage}. If
143 * {@code oldPackage} is {@code null}, the comparison is omitted.
144 * If {@code newPackage} is the same package or {@code null}, the
145 * revocation is omitted. This method returns {@code true} if the
146 * operation is succeeded.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700147 *
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700148 * Legacy VPN is handled specially since it is not a real package.
149 * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and
150 * it can be revoked by itself.
151 *
152 * @param oldPackage The package name of the old VPN application.
153 * @param newPackage The package name of the new VPN application.
154 * @return true if the operation is succeeded.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700155 */
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700156 public synchronized boolean prepare(String oldPackage, String newPackage) {
157 // Return false if the package does not match.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700158 if (oldPackage != null && !oldPackage.equals(mPackage)) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700159 return false;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700160 }
161
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700162 // Return true if we do not need to revoke.
163 if (newPackage == null ||
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700164 (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700165 return true;
166 }
167
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700168 // Check if the caller is authorized.
169 enforceControlPermission();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700170
Chia-chi Yehe9107902011-07-02 01:48:50 -0700171 // Reset the interface and hide the notification.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700172 if (mInterface != null) {
173 jniReset(mInterface);
Jeff Sharkey899223b2012-08-04 15:24:58 -0700174 final long token = Binder.clearCallingIdentity();
175 try {
176 mCallback.restore();
177 hideNotification();
178 } finally {
179 Binder.restoreCallingIdentity(token);
180 }
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700181 mInterface = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700182 }
183
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700184 // Revoke the connection or stop LegacyVpnRunner.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700185 if (mConnection != null) {
186 try {
187 mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION,
188 Parcel.obtain(), null, IBinder.FLAG_ONEWAY);
189 } catch (Exception e) {
190 // ignore
191 }
192 mContext.unbindService(mConnection);
193 mConnection = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700194 } else if (mLegacyVpnRunner != null) {
195 mLegacyVpnRunner.exit();
196 mLegacyVpnRunner = null;
197 }
198
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700199 Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
200 mPackage = newPackage;
Jeff Sharkey899223b2012-08-04 15:24:58 -0700201 updateState(DetailedState.IDLE, "prepare");
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700202 return true;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700203 }
204
205 /**
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700206 * Protect a socket from routing changes by binding it to the given
207 * interface. The socket is NOT closed by this method.
208 *
209 * @param socket The socket to be bound.
Jeff Sharkey899223b2012-08-04 15:24:58 -0700210 * @param interfaze The name of the interface.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700211 */
212 public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception {
213 PackageManager pm = mContext.getPackageManager();
214 ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
215 if (Binder.getCallingUid() != app.uid) {
216 throw new SecurityException("Unauthorized Caller");
217 }
218 jniProtect(socket.getFd(), interfaze);
219 }
220
221 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700222 * Establish a VPN network and return the file descriptor of the VPN
223 * interface. This methods returns {@code null} if the application is
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700224 * revoked or not prepared.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700225 *
226 * @param config The parameters to configure the network.
227 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700228 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700229 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700230 // Check if the caller is already prepared.
231 PackageManager pm = mContext.getPackageManager();
232 ApplicationInfo app = null;
233 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700234 app = pm.getApplicationInfo(mPackage, 0);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700235 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700236 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700237 }
238 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700239 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700240 }
241
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700242 // Check if the service is properly declared.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700243 Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
244 intent.setClassName(mPackage, config.user);
245 ResolveInfo info = pm.resolveService(intent, 0);
246 if (info == null) {
247 throw new SecurityException("Cannot find " + config.user);
248 }
249 if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
250 throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
251 }
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700252
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700253 // Load the label.
254 String label = app.loadLabel(pm).toString();
255
256 // Load the icon and convert it into a bitmap.
257 Drawable icon = app.loadIcon(pm);
258 Bitmap bitmap = null;
259 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
260 int width = mContext.getResources().getDimensionPixelSize(
261 android.R.dimen.notification_large_icon_width);
262 int height = mContext.getResources().getDimensionPixelSize(
263 android.R.dimen.notification_large_icon_height);
264 icon.setBounds(0, 0, width, height);
265 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -0700266 Canvas c = new Canvas(bitmap);
267 icon.draw(c);
268 c.setBitmap(null);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700269 }
270
Chia-chi Yehe9107902011-07-02 01:48:50 -0700271 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700272 ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700273 try {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700274 updateState(DetailedState.CONNECTING, "establish");
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700275 String interfaze = jniGetName(tun.getFd());
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700276 if (jniSetAddresses(interfaze, config.addresses) < 1) {
277 throw new IllegalArgumentException("At least one address must be specified");
278 }
279 if (config.routes != null) {
280 jniSetRoutes(interfaze, config.routes);
281 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700282 Connection connection = new Connection();
283 if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
284 throw new IllegalStateException("Cannot bind " + config.user);
285 }
286 if (mConnection != null) {
287 mContext.unbindService(mConnection);
288 }
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700289 if (mInterface != null && !mInterface.equals(interfaze)) {
290 jniReset(mInterface);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700291 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700292 mConnection = connection;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700293 mInterface = interfaze;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700294 } catch (RuntimeException e) {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700295 updateState(DetailedState.FAILED, "establish");
Jeff Sharkey065b2992012-08-05 14:16:48 -0700296 IoUtils.closeQuietly(tun);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700297 throw e;
298 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700299 Log.i(TAG, "Established by " + config.user + " on " + mInterface);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700300
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700301 // Fill more values.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700302 config.user = mPackage;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700303 config.interfaze = mInterface;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700304
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700305 // Override DNS servers and show the notification.
Jeff Sharkey899223b2012-08-04 15:24:58 -0700306 final long token = Binder.clearCallingIdentity();
307 try {
308 mCallback.override(config.dnsServers, config.searchDomains);
309 showNotification(config, label, bitmap);
310 } finally {
311 Binder.restoreCallingIdentity(token);
312 }
313 // TODO: ensure that contract class eventually marks as connected
314 updateState(DetailedState.AUTHENTICATING, "establish");
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700315 return tun;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700316 }
317
Jeff Sharkey899223b2012-08-04 15:24:58 -0700318 @Deprecated
319 public synchronized void interfaceStatusChanged(String iface, boolean up) {
320 try {
321 mObserver.interfaceStatusChanged(iface, up);
322 } catch (RemoteException e) {
323 // ignored; target is local
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700324 }
325 }
326
Jeff Sharkey899223b2012-08-04 15:24:58 -0700327 private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() {
328 @Override
329 public void interfaceStatusChanged(String interfaze, boolean up) {
330 synchronized (Vpn.this) {
331 if (!up && mLegacyVpnRunner != null) {
332 mLegacyVpnRunner.check(interfaze);
333 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700334 }
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700335 }
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700336
Jeff Sharkey899223b2012-08-04 15:24:58 -0700337 @Override
338 public void interfaceRemoved(String interfaze) {
339 synchronized (Vpn.this) {
340 if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) {
341 final long token = Binder.clearCallingIdentity();
342 try {
343 mCallback.restore();
344 hideNotification();
345 } finally {
346 Binder.restoreCallingIdentity(token);
347 }
348 mInterface = null;
349 if (mConnection != null) {
350 mContext.unbindService(mConnection);
351 mConnection = null;
352 updateState(DetailedState.DISCONNECTED, "interfaceRemoved");
353 } else if (mLegacyVpnRunner != null) {
354 mLegacyVpnRunner.exit();
355 mLegacyVpnRunner = null;
356 }
357 }
358 }
359 }
360 };
Haoyu Baidb3c8672012-06-20 14:29:57 -0700361
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700362 private void enforceControlPermission() {
363 // System user is allowed to control VPN.
364 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
365 return;
366 }
367
368 try {
369 // System dialogs are also allowed to control VPN.
370 PackageManager pm = mContext.getPackageManager();
371 ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0);
372 if (Binder.getCallingUid() == app.uid) {
373 return;
374 }
375 } catch (Exception e) {
376 // ignore
377 }
378
379 throw new SecurityException("Unauthorized Caller");
380 }
381
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700382 private class Connection implements ServiceConnection {
383 private IBinder mService;
384
385 @Override
386 public void onServiceConnected(ComponentName name, IBinder service) {
387 mService = service;
388 }
389
390 @Override
391 public void onServiceDisconnected(ComponentName name) {
392 mService = null;
393 }
394 }
395
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700396 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700397 mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext, config);
398
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700399 NotificationManager nm = (NotificationManager)
400 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
401
402 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700403 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
404 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700405 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
406 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700407 config.startTime = SystemClock.elapsedRealtime();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700408
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700409 Notification notification = new Notification.Builder(mContext)
410 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700411 .setLargeIcon(icon)
412 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700413 .setContentText(text)
Jeff Sharkey899223b2012-08-04 15:24:58 -0700414 .setContentIntent(mStatusIntent)
Chia-chi Yeh50fe7092012-01-11 14:26:24 -0800415 .setDefaults(0)
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700416 .setOngoing(true)
Jeff Sharkey899223b2012-08-04 15:24:58 -0700417 .build();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700418 nm.notify(R.drawable.vpn_connected, notification);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700419 }
420 }
421
422 private void hideNotification() {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700423 mStatusIntent = null;
424
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700425 NotificationManager nm = (NotificationManager)
426 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
427
428 if (nm != null) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700429 nm.cancel(R.drawable.vpn_connected);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700430 }
431 }
432
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700433 private native int jniCreate(int mtu);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700434 private native String jniGetName(int tun);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700435 private native int jniSetAddresses(String interfaze, String addresses);
436 private native int jniSetRoutes(String interfaze, String routes);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700437 private native void jniReset(String interfaze);
438 private native int jniCheck(String interfaze);
439 private native void jniProtect(int socket, String interfaze);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700440
Jeff Sharkey82f85212012-08-24 11:17:25 -0700441 private static String findLegacyVpnGateway(LinkProperties prop) {
442 for (RouteInfo route : prop.getRoutes()) {
443 // Currently legacy VPN only works on IPv4.
444 if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) {
445 return route.getGateway().getHostAddress();
446 }
447 }
Jeff Sharkey899223b2012-08-04 15:24:58 -0700448
Jeff Sharkey82f85212012-08-24 11:17:25 -0700449 throw new IllegalStateException("Unable to find suitable gateway");
450 }
451
452 /**
453 * Start legacy VPN, controlling native daemons as needed. Creates a
454 * secondary thread to perform connection work, returning quickly.
455 */
456 public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) {
457 if (keyStore.state() != KeyStore.State.UNLOCKED) {
458 throw new IllegalStateException("KeyStore isn't unlocked");
459 }
460
461 final String iface = egress.getInterfaceName();
462 final String gateway = findLegacyVpnGateway(egress);
463
464 // Load certificates.
465 String privateKey = "";
466 String userCert = "";
467 String caCert = "";
468 String serverCert = "";
469 if (!profile.ipsecUserCert.isEmpty()) {
470 privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert;
471 byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert);
472 userCert = (value == null) ? null : new String(value, Charsets.UTF_8);
473 }
474 if (!profile.ipsecCaCert.isEmpty()) {
475 byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert);
476 caCert = (value == null) ? null : new String(value, Charsets.UTF_8);
477 }
478 if (!profile.ipsecServerCert.isEmpty()) {
479 byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert);
480 serverCert = (value == null) ? null : new String(value, Charsets.UTF_8);
481 }
482 if (privateKey == null || userCert == null || caCert == null || serverCert == null) {
483 throw new IllegalStateException("Cannot load credentials");
484 }
485
486 // Prepare arguments for racoon.
487 String[] racoon = null;
488 switch (profile.type) {
489 case VpnProfile.TYPE_L2TP_IPSEC_PSK:
490 racoon = new String[] {
491 iface, profile.server, "udppsk", profile.ipsecIdentifier,
492 profile.ipsecSecret, "1701",
493 };
494 break;
495 case VpnProfile.TYPE_L2TP_IPSEC_RSA:
496 racoon = new String[] {
497 iface, profile.server, "udprsa", privateKey, userCert,
498 caCert, serverCert, "1701",
499 };
500 break;
501 case VpnProfile.TYPE_IPSEC_XAUTH_PSK:
502 racoon = new String[] {
503 iface, profile.server, "xauthpsk", profile.ipsecIdentifier,
504 profile.ipsecSecret, profile.username, profile.password, "", gateway,
505 };
506 break;
507 case VpnProfile.TYPE_IPSEC_XAUTH_RSA:
508 racoon = new String[] {
509 iface, profile.server, "xauthrsa", privateKey, userCert,
510 caCert, serverCert, profile.username, profile.password, "", gateway,
511 };
512 break;
513 case VpnProfile.TYPE_IPSEC_HYBRID_RSA:
514 racoon = new String[] {
515 iface, profile.server, "hybridrsa",
516 caCert, serverCert, profile.username, profile.password, "", gateway,
517 };
518 break;
519 }
520
521 // Prepare arguments for mtpd.
522 String[] mtpd = null;
523 switch (profile.type) {
524 case VpnProfile.TYPE_PPTP:
525 mtpd = new String[] {
526 iface, "pptp", profile.server, "1723",
527 "name", profile.username, "password", profile.password,
528 "linkname", "vpn", "refuse-eap", "nodefaultroute",
529 "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400",
530 (profile.mppe ? "+mppe" : "nomppe"),
531 };
532 break;
533 case VpnProfile.TYPE_L2TP_IPSEC_PSK:
534 case VpnProfile.TYPE_L2TP_IPSEC_RSA:
535 mtpd = new String[] {
536 iface, "l2tp", profile.server, "1701", profile.l2tpSecret,
537 "name", profile.username, "password", profile.password,
538 "linkname", "vpn", "refuse-eap", "nodefaultroute",
539 "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400",
540 };
541 break;
542 }
543
544 VpnConfig config = new VpnConfig();
Jeff Sharkey899223b2012-08-04 15:24:58 -0700545 config.legacy = true;
Jeff Sharkey82f85212012-08-24 11:17:25 -0700546 config.user = profile.key;
547 config.interfaze = iface;
548 config.session = profile.name;
549 config.routes = profile.routes;
550 if (!profile.dnsServers.isEmpty()) {
551 config.dnsServers = Arrays.asList(profile.dnsServers.split(" +"));
552 }
553 if (!profile.searchDomains.isEmpty()) {
554 config.searchDomains = Arrays.asList(profile.searchDomains.split(" +"));
555 }
556
557 startLegacyVpn(config, racoon, mtpd);
558 }
559
560 private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
561 stopLegacyVpn();
Jeff Sharkey899223b2012-08-04 15:24:58 -0700562
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700563 // Prepare for the new request. This also checks the caller.
564 prepare(null, VpnConfig.LEGACY_VPN);
Jeff Sharkey899223b2012-08-04 15:24:58 -0700565 updateState(DetailedState.CONNECTING, "startLegacyVpn");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700566
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700567 // Start a new LegacyVpnRunner and we are done!
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700568 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
569 mLegacyVpnRunner.start();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700570 }
571
Jeff Sharkey899223b2012-08-04 15:24:58 -0700572 public synchronized void stopLegacyVpn() {
573 if (mLegacyVpnRunner != null) {
574 mLegacyVpnRunner.exit();
575 mLegacyVpnRunner = null;
576
577 synchronized (LegacyVpnRunner.TAG) {
578 // wait for old thread to completely finish before spinning up
579 // new instance, otherwise state updates can be out of order.
580 }
581 }
582 }
583
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700584 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700585 * Return the information of the current ongoing legacy VPN.
586 */
587 public synchronized LegacyVpnInfo getLegacyVpnInfo() {
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700588 // Check if the caller is authorized.
589 enforceControlPermission();
Jeff Sharkey899223b2012-08-04 15:24:58 -0700590 if (mLegacyVpnRunner == null) return null;
591
592 final LegacyVpnInfo info = new LegacyVpnInfo();
593 info.key = mLegacyVpnRunner.mConfig.user;
594 info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo);
595 if (mNetworkInfo.isConnected()) {
596 info.intent = mStatusIntent;
597 }
598 return info;
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700599 }
600
601 /**
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700602 * Bringing up a VPN connection takes time, and that is all this thread
603 * does. Here we have plenty of time. The only thing we need to take
604 * care of is responding to interruptions as soon as possible. Otherwise
605 * requests will be piled up. This can be done in a Handler as a state
606 * machine, but it is much easier to read in the current form.
607 */
608 private class LegacyVpnRunner extends Thread {
609 private static final String TAG = "LegacyVpnRunner";
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700610
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700611 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700612 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700613 private final String[][] mArguments;
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700614 private final LocalSocket[] mSockets;
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700615
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700616 private long mTimer = -1;
617
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700618 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700619 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700620 mConfig = config;
621 mDaemons = new String[] {"racoon", "mtpd"};
Jeff Sharkey899223b2012-08-04 15:24:58 -0700622 // TODO: clear arguments from memory once launched
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700623 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700624 mSockets = new LocalSocket[mDaemons.length];
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700625 }
626
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700627 public void check(String interfaze) {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700628 if (interfaze.equals(mConfig.interfaze)) {
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700629 Log.i(TAG, "Legacy VPN is going down with " + interfaze);
630 exit();
631 }
632 }
633
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700634 public void exit() {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700635 // We assume that everything is reset after stopping the daemons.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700636 interrupt();
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700637 for (LocalSocket socket : mSockets) {
Jeff Sharkey065b2992012-08-05 14:16:48 -0700638 IoUtils.closeQuietly(socket);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700639 }
Jeff Sharkey899223b2012-08-04 15:24:58 -0700640 updateState(DetailedState.DISCONNECTED, "exit");
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700641 }
642
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700643 @Override
644 public void run() {
645 // Wait for the previous thread since it has been interrupted.
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700646 Log.v(TAG, "Waiting");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700647 synchronized (TAG) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700648 Log.v(TAG, "Executing");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700649 execute();
Jeff Sharkey899223b2012-08-04 15:24:58 -0700650 monitorDaemons();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700651 }
652 }
653
654 private void checkpoint(boolean yield) throws InterruptedException {
655 long now = SystemClock.elapsedRealtime();
656 if (mTimer == -1) {
657 mTimer = now;
658 Thread.sleep(1);
Chia-chi Yeh7ef86112011-07-22 15:46:52 -0700659 } else if (now - mTimer <= 60000) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700660 Thread.sleep(yield ? 200 : 1);
661 } else {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700662 updateState(DetailedState.FAILED, "checkpoint");
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700663 throw new IllegalStateException("Time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700664 }
665 }
666
667 private void execute() {
668 // Catch all exceptions so we can clean up few things.
Jeff Sharkey899223b2012-08-04 15:24:58 -0700669 boolean initFinished = false;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700670 try {
671 // Initialize the timer.
672 checkpoint(false);
673
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700674 // Wait for the daemons to stop.
675 for (String daemon : mDaemons) {
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700676 while (!SystemService.isStopped(daemon)) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700677 checkpoint(true);
678 }
679 }
680
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700681 // Clear the previous state.
682 File state = new File("/data/misc/vpn/state");
683 state.delete();
684 if (state.exists()) {
685 throw new IllegalStateException("Cannot delete the state");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700686 }
Chia-chi Yehc1872732011-12-08 16:51:41 -0800687 new File("/data/misc/vpn/abort").delete();
Jeff Sharkey899223b2012-08-04 15:24:58 -0700688 initFinished = true;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700689
Chia-chi Yehe9107902011-07-02 01:48:50 -0700690 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700691 boolean restart = false;
692 for (String[] arguments : mArguments) {
693 restart = restart || (arguments != null);
694 }
695 if (!restart) {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700696 updateState(DetailedState.DISCONNECTED, "execute");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700697 return;
698 }
Jeff Sharkey899223b2012-08-04 15:24:58 -0700699 updateState(DetailedState.CONNECTING, "execute");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700700
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700701 // Start the daemon with arguments.
702 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700703 String[] arguments = mArguments[i];
704 if (arguments == null) {
705 continue;
706 }
707
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700708 // Start the daemon.
709 String daemon = mDaemons[i];
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700710 SystemService.start(daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700711
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700712 // Wait for the daemon to start.
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700713 while (!SystemService.isRunning(daemon)) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700714 checkpoint(true);
715 }
716
717 // Create the control socket.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700718 mSockets[i] = new LocalSocket();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700719 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700720 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700721
722 // Wait for the socket to connect.
723 while (true) {
724 try {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700725 mSockets[i].connect(address);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700726 break;
727 } catch (Exception e) {
728 // ignore
729 }
730 checkpoint(true);
731 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700732 mSockets[i].setSoTimeout(500);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700733
734 // Send over the arguments.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700735 OutputStream out = mSockets[i].getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700736 for (String argument : arguments) {
737 byte[] bytes = argument.getBytes(Charsets.UTF_8);
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700738 if (bytes.length >= 0xFFFF) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700739 throw new IllegalArgumentException("Argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700740 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700741 out.write(bytes.length >> 8);
742 out.write(bytes.length);
743 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700744 checkpoint(false);
745 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700746 out.write(0xFF);
747 out.write(0xFF);
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700748 out.flush();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700749
750 // Wait for End-of-File.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700751 InputStream in = mSockets[i].getInputStream();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700752 while (true) {
753 try {
754 if (in.read() == -1) {
755 break;
756 }
757 } catch (Exception e) {
758 // ignore
759 }
760 checkpoint(true);
761 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700762 }
763
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700764 // Wait for the daemons to create the new state.
765 while (!state.exists()) {
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700766 // Check if a running daemon is dead.
767 for (int i = 0; i < mDaemons.length; ++i) {
768 String daemon = mDaemons[i];
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700769 if (mArguments[i] != null && !SystemService.isRunning(daemon)) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700770 throw new IllegalStateException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700771 }
772 }
773 checkpoint(true);
774 }
775
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700776 // Now we are connected. Read and parse the new state.
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -0800777 String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700778 if (parameters.length != 6) {
779 throw new IllegalStateException("Cannot parse the state");
780 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700781
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700782 // Set the interface and the addresses in the config.
783 mConfig.interfaze = parameters[0].trim();
784 mConfig.addresses = parameters[1].trim();
785
786 // Set the routes if they are not set in the config.
787 if (mConfig.routes == null || mConfig.routes.isEmpty()) {
788 mConfig.routes = parameters[2].trim();
789 }
790
791 // Set the DNS servers if they are not set in the config.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700792 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700793 String dnsServers = parameters[3].trim();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700794 if (!dnsServers.isEmpty()) {
795 mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
796 }
797 }
798
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700799 // Set the search domains if they are not set in the config.
800 if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) {
801 String searchDomains = parameters[4].trim();
802 if (!searchDomains.isEmpty()) {
803 mConfig.searchDomains = Arrays.asList(searchDomains.split(" "));
804 }
805 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700806
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700807 // Set the routes.
808 jniSetRoutes(mConfig.interfaze, mConfig.routes);
809
810 // Here is the last step and it must be done synchronously.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700811 synchronized (Vpn.this) {
812 // Check if the thread is interrupted while we are waiting.
813 checkpoint(false);
814
Chia-chi Yehe9107902011-07-02 01:48:50 -0700815 // Check if the interface is gone while we are waiting.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700816 if (jniCheck(mConfig.interfaze) == 0) {
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700817 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700818 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700819
820 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700821 mInterface = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700822 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
823 showNotification(mConfig, null, null);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700824
825 Log.i(TAG, "Connected!");
Jeff Sharkey899223b2012-08-04 15:24:58 -0700826 updateState(DetailedState.CONNECTED, "execute");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700827 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700828 } catch (Exception e) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700829 Log.i(TAG, "Aborting", e);
Chia-chi Yehe9107902011-07-02 01:48:50 -0700830 exit();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700831 } finally {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700832 // Kill the daemons if they fail to stop.
Jeff Sharkey899223b2012-08-04 15:24:58 -0700833 if (!initFinished) {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700834 for (String daemon : mDaemons) {
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700835 SystemService.stop(daemon);
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700836 }
837 }
838
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700839 // Do not leave an unstable state.
Jeff Sharkey899223b2012-08-04 15:24:58 -0700840 if (!initFinished || mNetworkInfo.getDetailedState() == DetailedState.CONNECTING) {
841 updateState(DetailedState.FAILED, "execute");
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700842 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700843 }
844 }
Jeff Sharkey899223b2012-08-04 15:24:58 -0700845
846 /**
847 * Monitor the daemons we started, moving to disconnected state if the
848 * underlying services fail.
849 */
850 private void monitorDaemons() {
851 if (!mNetworkInfo.isConnected()) {
852 return;
853 }
854
855 try {
856 while (true) {
857 Thread.sleep(2000);
858 for (int i = 0; i < mDaemons.length; i++) {
859 if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) {
860 return;
861 }
862 }
863 }
864 } catch (InterruptedException e) {
865 Log.d(TAG, "interrupted during monitorDaemons(); stopping services");
866 } finally {
867 for (String daemon : mDaemons) {
868 SystemService.stop(daemon);
869 }
870
871 updateState(DetailedState.DISCONNECTED, "babysit");
872 }
873 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700874 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700875}