Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 215b877f authored by junyulai's avatar junyulai
Browse files

[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
parent 48eac1d4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3060,6 +3060,7 @@ package android.net {
  }
  public class ConnectivityManager {
    method @RequiresPermission("android.permission.PACKET_KEEPALIVE_OFFLOAD") public android.net.SocketKeepalive createNattKeepalive(@NonNull android.net.Network, @NonNull java.io.FileDescriptor, @NonNull java.net.InetAddress, @NonNull java.net.InetAddress, @NonNull java.util.concurrent.Executor, @NonNull android.net.SocketKeepalive.Callback);
    method public boolean getAvoidBadWifi();
    method @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS) public String getCaptivePortalServerUrl();
    method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public boolean isTetheringSupported();
+36 −2
Original line number Diff line number Diff line
@@ -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);
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -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();
+9 −7
Original line number Diff line number Diff line
@@ -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();
+12 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ import android.net.INetworkStatsService;
import android.net.LinkProperties;
import android.net.LinkProperties.CompareResult;
import android.net.MatchAllNetworkSpecifier;
import android.net.NattSocketKeepalive;
import android.net.Network;
import android.net.NetworkAgent;
import android.net.NetworkCapabilities;
@@ -6184,6 +6185,17 @@ public class ConnectivityService extends IConnectivityManager.Stub
                srcAddr, srcPort, dstAddr, ConnectivityManager.PacketKeepalive.NATT_PORT);
    }

    @Override
    public void startNattKeepaliveWithFd(Network network, FileDescriptor fd, int resourceId,
            int intervalSeconds, Messenger messenger, IBinder binder, String srcAddr,
            String dstAddr) {
        enforceKeepalivePermission();
        mKeepaliveTracker.startNattKeepalive(
                getNetworkAgentInfoForNetwork(network), fd, resourceId,
                intervalSeconds, messenger, binder,
                srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT);
    }

    @Override
    public void stopKeepalive(Network network, int slot) {
        mHandler.sendMessage(mHandler.obtainMessage(
Loading