am e88d8031: Merge change 4739 into donut

Merge commit 'e88d803183b4999965fd3cda856420ad69ee0a4a'

* commit 'e88d803183b4999965fd3cda856420ad69ee0a4a':
  Add PPTP, L2TP/IPSec preshared key and other fixes.
This commit is contained in:
Android (Google) Code Review
2009-06-25 12:27:21 -07:00
committed by The Android Open Source Project
19 changed files with 301 additions and 65 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,8 +25,7 @@ import java.io.IOException;
* The service that manages the L2TP-over-IPSec VPN connection.
*/
class L2tpIpsecService extends VpnService<L2tpIpsecProfile> {
private static final String IPSEC_SERVICE = "racoon";
private static final String L2TP_SERVICE = "mtpd";
private static final String IPSEC_DAEMON = "racoon";
@Override
protected void connect(String serverIp, String username, String password)
@@ -34,7 +33,7 @@ class L2tpIpsecService extends VpnService<L2tpIpsecProfile> {
String hostIp = getHostIp();
// IPSEC
AndroidServiceProxy ipsecService = startService(IPSEC_SERVICE);
AndroidServiceProxy ipsecService = startService(IPSEC_DAEMON);
ipsecService.sendCommand(
String.format("SETKEY %s %s", hostIp, serverIp));
ipsecService.sendCommand(String.format("SET_CERTS %s %s %s %s",
@@ -42,11 +41,11 @@ class L2tpIpsecService extends VpnService<L2tpIpsecProfile> {
getUserkeyPath()));
// L2TP
AndroidServiceProxy l2tpService = startService(L2TP_SERVICE);
l2tpService.sendCommand2("l2tp", serverIp, "1701", "",
"file", getPppOptionFilePath(),
"name", username,
"password", password);
L2tpIpsecProfile p = getProfile();
MtpdHelper.sendCommand(this, L2tpService.L2TP_DAEMON, serverIp,
L2tpService.L2TP_PORT,
(p.isSecretEnabled() ? p.getSecretString() : null),
username, password);
}
private String getCaCertPath() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,19 +24,15 @@ import java.io.IOException;
* The service that manages the L2TP VPN connection.
*/
class L2tpService extends VpnService<L2tpProfile> {
private static final String L2TP_SERVICE = "mtpd";
static final String L2TP_DAEMON = "l2tp";
static final String L2TP_PORT = "1701";
@Override
protected void connect(String serverIp, String username, String password)
throws IOException {
String hostIp = getHostIp();
// L2TP
AndroidServiceProxy l2tpService = startService(L2TP_SERVICE);
l2tpService.sendCommand2("l2tp", serverIp, "1701", "",
"file", getPppOptionFilePath(),
"name", username,
"password", password);
L2tpProfile p = getProfile();
MtpdHelper.sendCommand(this, L2TP_DAEMON, serverIp, L2TP_PORT,
(p.isSecretEnabled() ? p.getSecretString() : null),
username, password);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.vpn;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* A helper class for sending commands to the MTP daemon (mtpd).
*/
class MtpdHelper {
private static final String MTPD_SERVICE = "mtpd";
private static final String VPN_LINKNAME = "vpn";
private static final String PPP_ARGS_SEPARATOR = "";
static void sendCommand(VpnService<?> vpnService, String protocol,
String serverIp, String port, String secret, String username,
String password) throws IOException {
ArrayList<String> args = new ArrayList<String>();
args.addAll(Arrays.asList(protocol, serverIp, port));
if (secret != null) args.add(secret);
args.add(PPP_ARGS_SEPARATOR);
addPppArguments(vpnService, args, serverIp, username, password);
AndroidServiceProxy mtpd = vpnService.startService(MTPD_SERVICE);
mtpd.sendCommand2(args.toArray(new String[args.size()]));
}
private static void addPppArguments(VpnService<?> vpnService,
ArrayList<String> args, String serverIp, String username,
String password) throws IOException {
args.addAll(Arrays.asList(
"linkname", VPN_LINKNAME,
"name", username,
"password", password,
"ipparam", serverIp + "@" + vpnService.getGatewayIp(),
"refuse-eap", "nodefaultroute", "usepeerdns",
"idle", "1800",
"mtu", "1400",
"mru", "1400"));
}
private MtpdHelper() {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.vpn;
import android.net.vpn.PptpProfile;
import java.io.IOException;
/**
* The service that manages the PPTP VPN connection.
*/
class PptpService extends VpnService<PptpProfile> {
static final String PPTP_DAEMON = "pptp";
static final String PPTP_PORT = "1723";
@Override
protected void connect(String serverIp, String username, String password)
throws IOException {
MtpdHelper.sendCommand(this, PPTP_DAEMON, serverIp, PPTP_PORT, null,
username, password);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.net.NetworkUtils;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnProfile;
import android.net.vpn.VpnState;
@@ -31,8 +32,10 @@ import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
@@ -153,16 +156,25 @@ abstract class VpnService<E extends VpnProfile> {
}
/**
* Returns the IP of the specified host name.
* Returns the IP address of the specified host name.
*/
protected String getIp(String hostName) throws IOException {
InetAddress iaddr = InetAddress.getByName(hostName);
byte[] aa = iaddr.getAddress();
StringBuilder sb = new StringBuilder().append(byteToInt(aa[0]));
for (int i = 1; i < aa.length; i++) {
sb.append(".").append(byteToInt(aa[i]));
return InetAddress.getByName(hostName).getHostAddress();
}
/**
* Returns the IP address of the default gateway.
*/
protected String getGatewayIp() throws IOException {
Enumeration<NetworkInterface> ifces =
NetworkInterface.getNetworkInterfaces();
for (; ifces.hasMoreElements(); ) {
NetworkInterface ni = ifces.nextElement();
int gateway = NetworkUtils.getDefaultRoute(ni.getName());
if (gateway == 0) continue;
return toInetAddress(gateway).getHostAddress();
}
return sb.toString();
throw new IOException("Default gateway is not available");
}
/**
@@ -170,7 +182,7 @@ abstract class VpnService<E extends VpnProfile> {
* connection is established.
*/
protected String getConnectMonitorFile() {
return "/etc/ppp/ip-up";
return "/etc/ppp/ip-up-vpn";
}
/**
@@ -461,12 +473,18 @@ abstract class VpnService<E extends VpnProfile> {
}
private String reallyGetHostIp() throws IOException {
Socket s = new Socket();
s.connect(new InetSocketAddress("www.google.com", 80), DNS_TIMEOUT);
String ipAddress = s.getLocalAddress().getHostAddress();
Log.d(TAG, "Host IP: " + ipAddress);
s.close();
return ipAddress;
Enumeration<NetworkInterface> ifces =
NetworkInterface.getNetworkInterfaces();
for (; ifces.hasMoreElements(); ) {
NetworkInterface ni = ifces.nextElement();
int gateway = NetworkUtils.getDefaultRoute(ni.getName());
if (gateway == 0) continue;
Enumeration<InetAddress> addrs = ni.getInetAddresses();
for (; addrs.hasMoreElements(); ) {
return addrs.nextElement().getHostAddress();
}
}
throw new IOException("Host IP is not available");
}
private String getProfileSubpath(String subpath) throws IOException {
@@ -496,8 +514,13 @@ abstract class VpnService<E extends VpnProfile> {
return ((message == null) || (message.length() == 0));
}
private static int byteToInt(byte b) {
return ((int) b) & 0x0FF;
private InetAddress toInetAddress(int addr) throws IOException {
byte[] aa = new byte[4];
for (int i= 0; i < aa.length; i++) {
aa[i] = (byte) (addr & 0x0FF);
addr >>= 8;
}
return InetAddress.getByAddress(aa);
}
private class ServiceHelper implements ProcessProxy.Callback {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,15 +19,14 @@ package android.net.vpn;
import android.os.Parcel;
/**
* The profile for L2TP-over-IPSec type of VPN.
* The profile for certificate-based L2TP-over-IPSec type of VPN.
* {@hide}
*/
public class L2tpIpsecProfile extends VpnProfile {
public class L2tpIpsecProfile extends L2tpProfile {
private static final long serialVersionUID = 1L;
private String mUserCertificate;
private String mCaCertificate;
private String mUserkey;
@Override
public VpnType getType() {
@@ -50,20 +49,11 @@ public class L2tpIpsecProfile extends VpnProfile {
return mUserCertificate;
}
public void setUserkey(String name) {
mUserkey = name;
}
public String getUserkey() {
return mUserkey;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mCaCertificate = in.readString();
mUserCertificate = in.readString();
mUserkey = in.readString();
}
@Override
@@ -71,6 +61,5 @@ public class L2tpIpsecProfile extends VpnProfile {
super.writeToParcel(parcel, flags);
parcel.writeString(mCaCertificate);
parcel.writeString(mUserCertificate);
parcel.writeString(mUserkey);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for pre-shared-key-based L2TP-over-IPSec type of VPN.
* {@hide}
*/
public class L2tpIpsecPskProfile extends L2tpProfile {
private static final long serialVersionUID = 1L;
private String mPresharedKey;
@Override
public VpnType getType() {
return VpnType.L2TP_IPSEC_PSK;
}
public void setPresharedKey(String key) {
mPresharedKey = key;
}
public String getPresharedKey() {
return mPresharedKey;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mPresharedKey = in.readString();
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeString(mPresharedKey);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for L2TP type of VPN.
* {@hide}
@@ -23,8 +25,44 @@ package android.net.vpn;
public class L2tpProfile extends VpnProfile {
private static final long serialVersionUID = 1L;
private boolean mSecret;
private String mSecretString;
@Override
public VpnType getType() {
return VpnType.L2TP;
}
/**
* Enables/disables the secret for authenticating tunnel connection.
*/
public void setSecretEnabled(boolean enabled) {
mSecret = enabled;
}
public boolean isSecretEnabled() {
return mSecret;
}
public void setSecretString(String secret) {
mSecretString = secret;
}
public String getSecretString() {
return mSecretString;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mSecret = in.readInt() > 0;
mSecretString = in.readString();
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeInt(mSecret ? 1 : 0);
parcel.writeString(mSecretString);
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
/**
* The profile for PPTP type of VPN.
* {@hide}
*/
public class PptpProfile extends VpnProfile {
private static final long serialVersionUID = 1L;
@Override
public VpnType getType() {
return VpnType.PPTP;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007, The Android Open Source Project
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,14 +21,21 @@ package android.net.vpn;
* {@hide}
*/
public enum VpnType {
L2TP_IPSEC("L2TP/IPSec", L2tpIpsecProfile.class),
L2TP("L2TP", L2tpProfile.class);
PPTP("PPTP", "", PptpProfile.class),
L2TP("L2TP", "", L2tpProfile.class),
L2TP_IPSEC_PSK("L2TP/IPSec PSK", "Pre-shared key based L2TP/IPSec VPN",
L2tpIpsecPskProfile.class),
L2TP_IPSEC("L2TP/IPSec CRT", "Certificate based L2TP/IPSec VPN",
L2tpIpsecProfile.class);
private String mDisplayName;
private String mDescription;
private Class<? extends VpnProfile> mClass;
VpnType(String displayName, Class<? extends VpnProfile> klass) {
VpnType(String displayName, String description,
Class<? extends VpnProfile> klass) {
mDisplayName = displayName;
mDescription = description;
mClass = klass;
}
@@ -36,6 +43,10 @@ public enum VpnType {
return mDisplayName;
}
public String getDescription() {
return mDescription;
}
public Class<? extends VpnProfile> getProfileClass() {
return mClass;
}