• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.ServiceConnection;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ResolveInfo;
28 import android.content.res.Resources;
29 import android.graphics.Bitmap;
30 import android.graphics.Canvas;
31 import android.graphics.drawable.Drawable;
32 import android.net.INetworkManagementEventObserver;
33 import android.net.LocalSocket;
34 import android.net.LocalSocketAddress;
35 import android.os.Binder;
36 import android.os.IBinder;
37 import android.os.Parcel;
38 import android.os.ParcelFileDescriptor;
39 import android.os.Process;
40 import android.os.SystemClock;
41 import android.os.SystemProperties;
42 import android.util.Log;
43 
44 import com.android.internal.R;
45 import com.android.internal.net.LegacyVpnInfo;
46 import com.android.internal.net.VpnConfig;
47 import com.android.server.ConnectivityService.VpnCallback;
48 
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.InputStream;
52 import java.io.OutputStream;
53 import java.nio.charset.Charsets;
54 import java.util.Arrays;
55 
56 /**
57  * @hide
58  */
59 public class Vpn extends INetworkManagementEventObserver.Stub {
60 
61     private final static String TAG = "Vpn";
62 
63     private final static String BIND_VPN_SERVICE =
64             android.Manifest.permission.BIND_VPN_SERVICE;
65 
66     private final Context mContext;
67     private final VpnCallback mCallback;
68 
69     private String mPackage = VpnConfig.LEGACY_VPN;
70     private String mInterface;
71     private Connection mConnection;
72     private LegacyVpnRunner mLegacyVpnRunner;
73 
Vpn(Context context, VpnCallback callback)74     public Vpn(Context context, VpnCallback callback) {
75         mContext = context;
76         mCallback = callback;
77     }
78 
79     /**
80      * Prepare for a VPN application. This method is designed to solve
81      * race conditions. It first compares the current prepared package
82      * with {@code oldPackage}. If they are the same, the prepared
83      * package is revoked and replaced with {@code newPackage}. If
84      * {@code oldPackage} is {@code null}, the comparison is omitted.
85      * If {@code newPackage} is the same package or {@code null}, the
86      * revocation is omitted. This method returns {@code true} if the
87      * operation is succeeded.
88      *
89      * Legacy VPN is handled specially since it is not a real package.
90      * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and
91      * it can be revoked by itself.
92      *
93      * @param oldPackage The package name of the old VPN application.
94      * @param newPackage The package name of the new VPN application.
95      * @return true if the operation is succeeded.
96      */
prepare(String oldPackage, String newPackage)97     public synchronized boolean prepare(String oldPackage, String newPackage) {
98         // Return false if the package does not match.
99         if (oldPackage != null && !oldPackage.equals(mPackage)) {
100             return false;
101         }
102 
103         // Return true if we do not need to revoke.
104         if (newPackage == null ||
105                 (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
106             return true;
107         }
108 
109         // Only system user can revoke a package.
110         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
111             throw new SecurityException("Unauthorized Caller");
112         }
113 
114         // Reset the interface and hide the notification.
115         if (mInterface != null) {
116             jniReset(mInterface);
117             mCallback.restore();
118             hideNotification();
119             mInterface = null;
120         }
121 
122         // Revoke the connection or stop LegacyVpnRunner.
123         if (mConnection != null) {
124             try {
125                 mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION,
126                         Parcel.obtain(), null, IBinder.FLAG_ONEWAY);
127             } catch (Exception e) {
128                 // ignore
129             }
130             mContext.unbindService(mConnection);
131             mConnection = null;
132         } else if (mLegacyVpnRunner != null) {
133             mLegacyVpnRunner.exit();
134             mLegacyVpnRunner = null;
135         }
136 
137         Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
138         mPackage = newPackage;
139         return true;
140     }
141 
142     /**
143      * Protect a socket from routing changes by binding it to the given
144      * interface. The socket is NOT closed by this method.
145      *
146      * @param socket The socket to be bound.
147      * @param name The name of the interface.
148      */
protect(ParcelFileDescriptor socket, String interfaze)149     public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception {
150         PackageManager pm = mContext.getPackageManager();
151         ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
152         if (Binder.getCallingUid() != app.uid) {
153             throw new SecurityException("Unauthorized Caller");
154         }
155         jniProtect(socket.getFd(), interfaze);
156     }
157 
158     /**
159      * Establish a VPN network and return the file descriptor of the VPN
160      * interface. This methods returns {@code null} if the application is
161      * revoked or not prepared.
162      *
163      * @param config The parameters to configure the network.
164      * @return The file descriptor of the VPN interface.
165      */
establish(VpnConfig config)166     public synchronized ParcelFileDescriptor establish(VpnConfig config) {
167         // Check if the caller is already prepared.
168         PackageManager pm = mContext.getPackageManager();
169         ApplicationInfo app = null;
170         try {
171             app = pm.getApplicationInfo(mPackage, 0);
172         } catch (Exception e) {
173             return null;
174         }
175         if (Binder.getCallingUid() != app.uid) {
176             return null;
177         }
178 
179         // Check if the service is properly declared.
180         Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
181         intent.setClassName(mPackage, config.user);
182         ResolveInfo info = pm.resolveService(intent, 0);
183         if (info == null) {
184             throw new SecurityException("Cannot find " + config.user);
185         }
186         if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
187             throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
188         }
189 
190         // Load the label.
191         String label = app.loadLabel(pm).toString();
192 
193         // Load the icon and convert it into a bitmap.
194         Drawable icon = app.loadIcon(pm);
195         Bitmap bitmap = null;
196         if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
197             int width = mContext.getResources().getDimensionPixelSize(
198                     android.R.dimen.notification_large_icon_width);
199             int height = mContext.getResources().getDimensionPixelSize(
200                     android.R.dimen.notification_large_icon_height);
201             icon.setBounds(0, 0, width, height);
202             bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
203             Canvas c = new Canvas(bitmap);
204             icon.draw(c);
205             c.setBitmap(null);
206         }
207 
208         // Configure the interface. Abort if any of these steps fails.
209         ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
210         try {
211             String interfaze = jniGetName(tun.getFd());
212             if (jniSetAddresses(interfaze, config.addresses) < 1) {
213                 throw new IllegalArgumentException("At least one address must be specified");
214             }
215             if (config.routes != null) {
216                 jniSetRoutes(interfaze, config.routes);
217             }
218             Connection connection = new Connection();
219             if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
220                 throw new IllegalStateException("Cannot bind " + config.user);
221             }
222             if (mConnection != null) {
223                 mContext.unbindService(mConnection);
224             }
225             if (mInterface != null && !mInterface.equals(interfaze)) {
226                 jniReset(mInterface);
227             }
228             mConnection = connection;
229             mInterface = interfaze;
230         } catch (RuntimeException e) {
231             try {
232                 tun.close();
233             } catch (Exception ex) {
234                 // ignore
235             }
236             throw e;
237         }
238         Log.i(TAG, "Established by " + config.user + " on " + mInterface);
239 
240         // Fill more values.
241         config.user = mPackage;
242         config.interfaze = mInterface;
243 
244         // Override DNS servers and show the notification.
245         long identity = Binder.clearCallingIdentity();
246         mCallback.override(config.dnsServers, config.searchDomains);
247         showNotification(config, label, bitmap);
248         Binder.restoreCallingIdentity(identity);
249         return tun;
250     }
251 
252     // INetworkManagementEventObserver.Stub
253     @Override
interfaceAdded(String interfaze)254     public void interfaceAdded(String interfaze) {
255     }
256 
257     // INetworkManagementEventObserver.Stub
258     @Override
interfaceStatusChanged(String interfaze, boolean up)259     public synchronized void interfaceStatusChanged(String interfaze, boolean up) {
260         if (!up && mLegacyVpnRunner != null) {
261             mLegacyVpnRunner.check(interfaze);
262         }
263     }
264 
265     // INetworkManagementEventObserver.Stub
266     @Override
interfaceLinkStateChanged(String interfaze, boolean up)267     public void interfaceLinkStateChanged(String interfaze, boolean up) {
268     }
269 
270     // INetworkManagementEventObserver.Stub
271     @Override
interfaceRemoved(String interfaze)272     public synchronized void interfaceRemoved(String interfaze) {
273         if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) {
274             long identity = Binder.clearCallingIdentity();
275             mCallback.restore();
276             hideNotification();
277             Binder.restoreCallingIdentity(identity);
278             mInterface = null;
279             if (mConnection != null) {
280                 mContext.unbindService(mConnection);
281                 mConnection = null;
282             } else if (mLegacyVpnRunner != null) {
283                 mLegacyVpnRunner.exit();
284                 mLegacyVpnRunner = null;
285             }
286         }
287     }
288 
289     // INetworkManagementEventObserver.Stub
290     @Override
limitReached(String limit, String interfaze)291     public void limitReached(String limit, String interfaze) {
292     }
293 
294     private class Connection implements ServiceConnection {
295         private IBinder mService;
296 
297         @Override
onServiceConnected(ComponentName name, IBinder service)298         public void onServiceConnected(ComponentName name, IBinder service) {
299             mService = service;
300         }
301 
302         @Override
onServiceDisconnected(ComponentName name)303         public void onServiceDisconnected(ComponentName name) {
304             mService = null;
305         }
306     }
307 
showNotification(VpnConfig config, String label, Bitmap icon)308     private void showNotification(VpnConfig config, String label, Bitmap icon) {
309         NotificationManager nm = (NotificationManager)
310                 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
311 
312         if (nm != null) {
313             String title = (label == null) ? mContext.getString(R.string.vpn_title) :
314                     mContext.getString(R.string.vpn_title_long, label);
315             String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
316                     mContext.getString(R.string.vpn_text_long, config.session);
317             config.startTime = SystemClock.elapsedRealtime();
318 
319             Notification notification = new Notification.Builder(mContext)
320                     .setSmallIcon(R.drawable.vpn_connected)
321                     .setLargeIcon(icon)
322                     .setContentTitle(title)
323                     .setContentText(text)
324                     .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config))
325                     .setDefaults(Notification.DEFAULT_ALL)
326                     .setOngoing(true)
327                     .getNotification();
328             nm.notify(R.drawable.vpn_connected, notification);
329         }
330     }
331 
hideNotification()332     private void hideNotification() {
333         NotificationManager nm = (NotificationManager)
334                 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
335 
336         if (nm != null) {
337             nm.cancel(R.drawable.vpn_connected);
338         }
339     }
340 
jniCreate(int mtu)341     private native int jniCreate(int mtu);
jniGetName(int tun)342     private native String jniGetName(int tun);
jniSetAddresses(String interfaze, String addresses)343     private native int jniSetAddresses(String interfaze, String addresses);
jniSetRoutes(String interfaze, String routes)344     private native int jniSetRoutes(String interfaze, String routes);
jniReset(String interfaze)345     private native void jniReset(String interfaze);
jniCheck(String interfaze)346     private native int jniCheck(String interfaze);
jniProtect(int socket, String interfaze)347     private native void jniProtect(int socket, String interfaze);
348 
349     /**
350      * Start legacy VPN. This method stops the daemons and restart them
351      * if arguments are not null. Heavy things are offloaded to another
352      * thread, so callers will not be blocked for a long time.
353      *
354      * @param config The parameters to configure the network.
355      * @param raoocn The arguments to be passed to racoon.
356      * @param mtpd The arguments to be passed to mtpd.
357      */
startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd)358     public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
359         // Prepare for the new request. This also checks the caller.
360         prepare(null, VpnConfig.LEGACY_VPN);
361 
362         // Start a new LegacyVpnRunner and we are done!
363         mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
364         mLegacyVpnRunner.start();
365     }
366 
367     /**
368      * Return the information of the current ongoing legacy VPN.
369      */
getLegacyVpnInfo()370     public synchronized LegacyVpnInfo getLegacyVpnInfo() {
371         // Only system user can call this method.
372         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
373             throw new SecurityException("Unauthorized Caller");
374         }
375         return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
376     }
377 
378     /**
379      * Bringing up a VPN connection takes time, and that is all this thread
380      * does. Here we have plenty of time. The only thing we need to take
381      * care of is responding to interruptions as soon as possible. Otherwise
382      * requests will be piled up. This can be done in a Handler as a state
383      * machine, but it is much easier to read in the current form.
384      */
385     private class LegacyVpnRunner extends Thread {
386         private static final String TAG = "LegacyVpnRunner";
387 
388         private final VpnConfig mConfig;
389         private final String[] mDaemons;
390         private final String[][] mArguments;
391         private final LocalSocket[] mSockets;
392         private final String mOuterInterface;
393         private final LegacyVpnInfo mInfo;
394 
395         private long mTimer = -1;
396 
LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd)397         public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
398             super(TAG);
399             mConfig = config;
400             mDaemons = new String[] {"racoon", "mtpd"};
401             mArguments = new String[][] {racoon, mtpd};
402             mSockets = new LocalSocket[mDaemons.length];
403             mInfo = new LegacyVpnInfo();
404 
405             // This is the interface which VPN is running on.
406             mOuterInterface = mConfig.interfaze;
407 
408             // Legacy VPN is not a real package, so we use it to carry the key.
409             mInfo.key = mConfig.user;
410             mConfig.user = VpnConfig.LEGACY_VPN;
411         }
412 
check(String interfaze)413         public void check(String interfaze) {
414             if (interfaze.equals(mOuterInterface)) {
415                 Log.i(TAG, "Legacy VPN is going down with " + interfaze);
416                 exit();
417             }
418         }
419 
exit()420         public void exit() {
421             // We assume that everything is reset after stopping the daemons.
422             interrupt();
423             for (LocalSocket socket : mSockets) {
424                 try {
425                     socket.close();
426                 } catch (Exception e) {
427                     // ignore
428                 }
429             }
430         }
431 
getInfo()432         public LegacyVpnInfo getInfo() {
433             // Update the info when VPN is disconnected.
434             if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) {
435                 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
436                 mInfo.intent = null;
437             }
438             return mInfo;
439         }
440 
441         @Override
run()442         public void run() {
443             // Wait for the previous thread since it has been interrupted.
444             Log.v(TAG, "Waiting");
445             synchronized (TAG) {
446                 Log.v(TAG, "Executing");
447                 execute();
448             }
449         }
450 
checkpoint(boolean yield)451         private void checkpoint(boolean yield) throws InterruptedException {
452             long now = SystemClock.elapsedRealtime();
453             if (mTimer == -1) {
454                 mTimer = now;
455                 Thread.sleep(1);
456             } else if (now - mTimer <= 60000) {
457                 Thread.sleep(yield ? 200 : 1);
458             } else {
459                 mInfo.state = LegacyVpnInfo.STATE_TIMEOUT;
460                 throw new IllegalStateException("Time is up");
461             }
462         }
463 
execute()464         private void execute() {
465             // Catch all exceptions so we can clean up few things.
466             try {
467                 // Initialize the timer.
468                 checkpoint(false);
469                 mInfo.state = LegacyVpnInfo.STATE_INITIALIZING;
470 
471                 // Wait for the daemons to stop.
472                 for (String daemon : mDaemons) {
473                     String key = "init.svc." + daemon;
474                     while (!"stopped".equals(SystemProperties.get(key, "stopped"))) {
475                         checkpoint(true);
476                     }
477                 }
478 
479                 // Clear the previous state.
480                 File state = new File("/data/misc/vpn/state");
481                 state.delete();
482                 if (state.exists()) {
483                     throw new IllegalStateException("Cannot delete the state");
484                 }
485 
486                 // Check if we need to restart any of the daemons.
487                 boolean restart = false;
488                 for (String[] arguments : mArguments) {
489                     restart = restart || (arguments != null);
490                 }
491                 if (!restart) {
492                     mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
493                     return;
494                 }
495                 mInfo.state = LegacyVpnInfo.STATE_CONNECTING;
496 
497                 // Start the daemon with arguments.
498                 for (int i = 0; i < mDaemons.length; ++i) {
499                     String[] arguments = mArguments[i];
500                     if (arguments == null) {
501                         continue;
502                     }
503 
504                     // Start the daemon.
505                     String daemon = mDaemons[i];
506                     SystemProperties.set("ctl.start", daemon);
507 
508                     // Wait for the daemon to start.
509                     String key = "init.svc." + daemon;
510                     while (!"running".equals(SystemProperties.get(key))) {
511                         checkpoint(true);
512                     }
513 
514                     // Create the control socket.
515                     mSockets[i] = new LocalSocket();
516                     LocalSocketAddress address = new LocalSocketAddress(
517                             daemon, LocalSocketAddress.Namespace.RESERVED);
518 
519                     // Wait for the socket to connect.
520                     while (true) {
521                         try {
522                             mSockets[i].connect(address);
523                             break;
524                         } catch (Exception e) {
525                             // ignore
526                         }
527                         checkpoint(true);
528                     }
529                     mSockets[i].setSoTimeout(500);
530 
531                     // Send over the arguments.
532                     OutputStream out = mSockets[i].getOutputStream();
533                     for (String argument : arguments) {
534                         byte[] bytes = argument.getBytes(Charsets.UTF_8);
535                         if (bytes.length >= 0xFFFF) {
536                             throw new IllegalArgumentException("Argument is too large");
537                         }
538                         out.write(bytes.length >> 8);
539                         out.write(bytes.length);
540                         out.write(bytes);
541                         checkpoint(false);
542                     }
543                     out.write(0xFF);
544                     out.write(0xFF);
545                     out.flush();
546 
547                     // Wait for End-of-File.
548                     InputStream in = mSockets[i].getInputStream();
549                     while (true) {
550                         try {
551                             if (in.read() == -1) {
552                                 break;
553                             }
554                         } catch (Exception e) {
555                             // ignore
556                         }
557                         checkpoint(true);
558                     }
559                 }
560 
561                 // Wait for the daemons to create the new state.
562                 while (!state.exists()) {
563                     // Check if a running daemon is dead.
564                     for (int i = 0; i < mDaemons.length; ++i) {
565                         String daemon = mDaemons[i];
566                         if (mArguments[i] != null && !"running".equals(
567                                 SystemProperties.get("init.svc." + daemon))) {
568                             throw new IllegalStateException(daemon + " is dead");
569                         }
570                     }
571                     checkpoint(true);
572                 }
573 
574                 // Now we are connected. Read and parse the new state.
575                 byte[] buffer = new byte[(int) state.length()];
576                 if (new FileInputStream(state).read(buffer) != buffer.length) {
577                     throw new IllegalStateException("Cannot read the state");
578                 }
579                 String[] parameters = new String(buffer, Charsets.UTF_8).split("\n", -1);
580                 if (parameters.length != 6) {
581                     throw new IllegalStateException("Cannot parse the state");
582                 }
583 
584                 // Set the interface and the addresses in the config.
585                 mConfig.interfaze = parameters[0].trim();
586                 mConfig.addresses = parameters[1].trim();
587 
588                 // Set the routes if they are not set in the config.
589                 if (mConfig.routes == null || mConfig.routes.isEmpty()) {
590                     mConfig.routes = parameters[2].trim();
591                 }
592 
593                 // Set the DNS servers if they are not set in the config.
594                 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
595                     String dnsServers = parameters[3].trim();
596                     if (!dnsServers.isEmpty()) {
597                         mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
598                     }
599                 }
600 
601                 // Set the search domains if they are not set in the config.
602                 if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) {
603                     String searchDomains = parameters[4].trim();
604                     if (!searchDomains.isEmpty()) {
605                         mConfig.searchDomains = Arrays.asList(searchDomains.split(" "));
606                     }
607                 }
608 
609                 // Set the routes.
610                 jniSetRoutes(mConfig.interfaze, mConfig.routes);
611 
612                 // Here is the last step and it must be done synchronously.
613                 synchronized (Vpn.this) {
614                     // Check if the thread is interrupted while we are waiting.
615                     checkpoint(false);
616 
617                     // Check if the interface is gone while we are waiting.
618                     if (jniCheck(mConfig.interfaze) == 0) {
619                         throw new IllegalStateException(mConfig.interfaze + " is gone");
620                     }
621 
622                     // Now INetworkManagementEventObserver is watching our back.
623                     mInterface = mConfig.interfaze;
624                     mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
625                     showNotification(mConfig, null, null);
626 
627                     Log.i(TAG, "Connected!");
628                     mInfo.state = LegacyVpnInfo.STATE_CONNECTED;
629                     mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null);
630                 }
631             } catch (Exception e) {
632                 Log.i(TAG, "Aborting", e);
633                 exit();
634             } finally {
635                 // Kill the daemons if they fail to stop.
636                 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING) {
637                     for (String daemon : mDaemons) {
638                         SystemProperties.set("ctl.stop", daemon);
639                     }
640                 }
641 
642                 // Do not leave an unstable state.
643                 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING ||
644                         mInfo.state == LegacyVpnInfo.STATE_CONNECTING) {
645                     mInfo.state = LegacyVpnInfo.STATE_FAILED;
646                 }
647             }
648         }
649     }
650 }
651