[KA05] Export keepalive offload api for IpSec Nat-T file descriptor

Adds system api of createSocketKeepalive to take file descriptor,
so privileged apps could use it without the need of IpSecService.

Bug: 114151147
Test: atest FrameworksNetTests
Change-Id: If926c21704b6ed73a0adfcadad732b97b42bacae
This commit is contained in:
junyulai 2019-01-15 11:32:44 +08:00
commit 48a5938ee8
3 changed files with 152 additions and 28 deletions

View file

@ -15,6 +15,8 @@
*/
package android.net;
import static android.net.IpSecManager.INVALID_RESOURCE_ID;
import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
@ -61,6 +63,7 @@ import com.android.internal.util.Protocol;
import libcore.net.event.NetworkEventDispatcher;
import java.io.FileDescriptor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.InetAddress;
@ -1849,8 +1852,39 @@ public class ConnectivityManager {
@NonNull InetAddress destination,
@NonNull @CallbackExecutor Executor executor,
@NonNull Callback callback) {
return new NattSocketKeepalive(mService, network, socket, source, destination, executor,
callback);
return new NattSocketKeepalive(mService, network, socket.getFileDescriptor(),
socket.getResourceId(), source, destination, executor, callback);
}
/**
* Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called
* by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}.
*
* @param network The {@link Network} the socket is on.
* @param fd The {@link FileDescriptor} that needs to be kept alive. The provided
* {@link FileDescriptor} must be bound to a port and the keepalives will be sent from
* that port.
* @param source The source address of the {@link UdpEncapsulationSocket}.
* @param destination The destination address of the {@link UdpEncapsulationSocket}. The
* keepalive packets will always be sent to port 4500 of the given {@code destination}.
* @param executor The executor on which callback will be invoked. The provided {@link Executor}
* must run callback sequentially, otherwise the order of callbacks cannot be
* guaranteed.
* @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
* changes. Must be extended by applications that use this API.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD)
public SocketKeepalive createNattKeepalive(@NonNull Network network,
@NonNull FileDescriptor fd,
@NonNull InetAddress source,
@NonNull InetAddress destination,
@NonNull @CallbackExecutor Executor executor,
@NonNull Callback callback) {
return new NattSocketKeepalive(mService, network, fd, INVALID_RESOURCE_ID /* Unused */,
source, destination, executor, callback);
}
/**

View file

@ -181,6 +181,10 @@ interface IConnectivityManager
void startNattKeepalive(in Network network, int intervalSeconds, in Messenger messenger,
in IBinder binder, String srcAddr, int srcPort, String dstAddr);
void startNattKeepaliveWithFd(in Network network, in FileDescriptor fd, int resourceId,
int intervalSeconds, in Messenger messenger, in IBinder binder, String srcAddr,
String dstAddr);
void stopKeepalive(in Network network, int slot);
String getCaptivePortalServerUrl();

View file

@ -17,11 +17,11 @@
package android.net;
import android.annotation.NonNull;
import android.net.IpSecManager.UdpEncapsulationSocket;
import android.os.Binder;
import android.os.RemoteException;
import android.util.Log;
import java.io.FileDescriptor;
import java.net.InetAddress;
import java.util.concurrent.Executor;
@ -32,11 +32,13 @@ public final class NattSocketKeepalive extends SocketKeepalive {
@NonNull private final InetAddress mSource;
@NonNull private final InetAddress mDestination;
@NonNull private final UdpEncapsulationSocket mSocket;
@NonNull private final FileDescriptor mFd;
private final int mResourceId;
NattSocketKeepalive(@NonNull IConnectivityManager service,
@NonNull Network network,
@NonNull UdpEncapsulationSocket socket,
@NonNull FileDescriptor fd,
int resourceId,
@NonNull InetAddress source,
@NonNull InetAddress destination,
@NonNull Executor executor,
@ -44,15 +46,15 @@ public final class NattSocketKeepalive extends SocketKeepalive {
super(service, network, executor, callback);
mSource = source;
mDestination = destination;
mSocket = socket;
mFd = fd;
mResourceId = resourceId;
}
@Override
void startImpl(int intervalSec) {
try {
// TODO: Create new interface in ConnectivityService and pass fd to it.
mService.startNattKeepalive(mNetwork, intervalSec, mMessenger, new Binder(),
mSource.getHostAddress(), mSocket.getPort(), mDestination.getHostAddress());
mService.startNattKeepaliveWithFd(mNetwork, mFd, mResourceId, intervalSec, mMessenger,
new Binder(), mSource.getHostAddress(), mDestination.getHostAddress());
} catch (RemoteException e) {
Log.e(TAG, "Error starting packet keepalive: ", e);
stopLooper();