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

Commit fc99f19f authored by markchien's avatar markchien
Browse files

Support specific client address configuration

If specific config "clientAddr" is set, the DHCP server will only offer
"clientAddr". This is for peer-to-peer use case. If there are multiple
clients for which "clientAddr" is already in use, dhcp server will not
offer other addresses to clients.

Bug: 141256482
Test: manual
      atest NetworkStackNextTests

Change-Id: I96bc24a9c30bfc48dff38c3c4456085694fd381c
parent ca4e33ed
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25,4 +25,5 @@ parcelable DhcpServingParamsParcel {
  long dhcpLeaseTimeSecs;
  int linkMtu;
  boolean metered;
  int clientAddr;
}
+1 −0
Original line number Diff line number Diff line
@@ -26,5 +26,6 @@ parcelable DhcpServingParamsParcel {
    long dhcpLeaseTimeSecs;
    int linkMtu;
    boolean metered;
    int clientAddr;
}
+12 −4
Original line number Diff line number Diff line
@@ -80,6 +80,8 @@ class DhcpLeaseRepository {
    private int mSubnetMask;
    private int mNumAddresses;
    private long mLeaseTimeMs;
    @Nullable
    private Inet4Address mClientAddr;

    /**
     * Next timestamp when committed or declined leases should be checked for expired ones. This
@@ -128,21 +130,24 @@ class DhcpLeaseRepository {
    private final LinkedHashMap<Inet4Address, Long> mDeclinedAddrs = new LinkedHashMap<>();

    DhcpLeaseRepository(@NonNull IpPrefix prefix, @NonNull Set<Inet4Address> reservedAddrs,
            long leaseTimeMs, @NonNull SharedLog log, @NonNull Clock clock) {
        updateParams(prefix, reservedAddrs, leaseTimeMs);
            long leaseTimeMs, @Nullable Inet4Address clientAddr, @NonNull SharedLog log,
            @NonNull Clock clock) {
        mLog = log;
        mClock = clock;
        mClientAddr = clientAddr;
        updateParams(prefix, reservedAddrs, leaseTimeMs, clientAddr);
    }

    public void updateParams(@NonNull IpPrefix prefix, @NonNull Set<Inet4Address> reservedAddrs,
            long leaseTimeMs) {
            long leaseTimeMs, @Nullable Inet4Address clientAddr) {
        mPrefix = prefix;
        mReservedAddrs = Collections.unmodifiableSet(new HashSet<>(reservedAddrs));
        mPrefixLength = prefix.getPrefixLength();
        mSubnetMask = prefixLengthToV4NetmaskIntHTH(mPrefixLength);
        mSubnetAddr = inet4AddressToIntHTH((Inet4Address) prefix.getAddress()) & mSubnetMask;
        mNumAddresses = 1 << (IPV4_ADDR_BITS - prefix.getPrefixLength());
        mNumAddresses = clientAddr != null ? 1 : 1 << (IPV4_ADDR_BITS - prefix.getPrefixLength());
        mLeaseTimeMs = leaseTimeMs;
        mClientAddr = clientAddr;

        cleanMap(mDeclinedAddrs);
        if (cleanMap(mCommittedLeases)) {
@@ -514,6 +519,9 @@ class DhcpLeaseRepository {
     * address (with the ordering in {@link #getAddrIndex(int)}) is returned.
     */
    private int getValidAddress(int addr) {
        // Only mClientAddr is valid if static client address is enforced.
        if (mClientAddr != null) return inet4AddressToIntHTH(mClientAddr);

        final int lastByteMask = 0xff;
        int addrIndex = getAddrIndex(addr); // 0-based index of the address in the subnet

+4 −3
Original line number Diff line number Diff line
@@ -205,8 +205,8 @@ public class DhcpServer extends IDhcpServer.Stub {
                @NonNull SharedLog log, @NonNull Clock clock) {
            return new DhcpLeaseRepository(
                    DhcpServingParams.makeIpPrefix(servingParams.serverAddr),
                    servingParams.excludedAddrs,
                    servingParams.dhcpLeaseTimeSecs * 1000, log.forSubComponent(REPO_TAG), clock);
                    servingParams.excludedAddrs, servingParams.dhcpLeaseTimeSecs * 1000,
                    servingParams.clientAddr, log.forSubComponent(REPO_TAG), clock);
        }

        @Override
@@ -351,7 +351,8 @@ public class DhcpServer extends IDhcpServer.Stub {
                    mLeaseRepo.updateParams(
                            DhcpServingParams.makeIpPrefix(mServingParams.serverAddr),
                            params.excludedAddrs,
                            params.dhcpLeaseTimeSecs);
                            params.dhcpLeaseTimeSecs,
                            params.clientAddr);

                    cb = pair.second;
                    break;
+26 −2
Original line number Diff line number Diff line
@@ -84,6 +84,12 @@ public class DhcpServingParams {
     */
    public final boolean metered;

    /**
     * Client inet address. This will be the only address offered by DhcpServer if set.
     */
    @Nullable
    public final Inet4Address clientAddr;

    /**
     * Checked exception thrown when some parameters used to build {@link DhcpServingParams} are
     * missing or invalid.
@@ -97,7 +103,7 @@ public class DhcpServingParams {
    private DhcpServingParams(@NonNull LinkAddress serverAddr,
            @NonNull Set<Inet4Address> defaultRouters,
            @NonNull Set<Inet4Address> dnsServers, @NonNull Set<Inet4Address> excludedAddrs,
            long dhcpLeaseTimeSecs, int linkMtu, boolean metered) {
            long dhcpLeaseTimeSecs, int linkMtu, boolean metered, Inet4Address clientAddr) {
        this.serverAddr = serverAddr;
        this.defaultRouters = defaultRouters;
        this.dnsServers = dnsServers;
@@ -105,6 +111,7 @@ public class DhcpServingParams {
        this.dhcpLeaseTimeSecs = dhcpLeaseTimeSecs;
        this.linkMtu = linkMtu;
        this.metered = metered;
        this.clientAddr = clientAddr;
    }

    /**
@@ -119,6 +126,11 @@ public class DhcpServingParams {
        final LinkAddress serverAddr = new LinkAddress(
                intToInet4AddressHTH(parcel.serverAddr),
                parcel.serverAddrPrefixLength);
        Inet4Address clientAddr = null;
        if (parcel.clientAddr != 0) {
            clientAddr = intToInet4AddressHTH(parcel.clientAddr);
        }

        return new Builder()
                .setServerAddr(serverAddr)
                .setDefaultRouters(toInet4AddressSet(parcel.defaultRouters))
@@ -127,6 +139,7 @@ public class DhcpServingParams {
                .setDhcpLeaseTimeSecs(parcel.dhcpLeaseTimeSecs)
                .setLinkMtu(parcel.linkMtu)
                .setMetered(parcel.metered)
                .setClientAddr(clientAddr)
                .build();
    }

@@ -181,6 +194,7 @@ public class DhcpServingParams {
        private long mDhcpLeaseTimeSecs;
        private int mLinkMtu = MTU_UNSET;
        private boolean mMetered;
        private Inet4Address mClientAddr;

        /**
         * Set the server address and served prefix for the DHCP server.
@@ -304,6 +318,16 @@ public class DhcpServingParams {
            return this;
        }

        /**
         * Set the client address.
         *
         * <p>If not set, the default value is null.
         */
        public Builder setClientAddr(@Nullable Inet4Address clientAddr) {
            this.mClientAddr = clientAddr;
            return this;
        }

        /**
         * Create a new {@link DhcpServingParams} instance based on parameters set in the builder.
         *
@@ -358,7 +382,7 @@ public class DhcpServingParams {
                    Collections.unmodifiableSet(new HashSet<>(mDefaultRouters)),
                    Collections.unmodifiableSet(new HashSet<>(mDnsServers)),
                    Collections.unmodifiableSet(excl),
                    mDhcpLeaseTimeSecs, mLinkMtu, mMetered);
                    mDhcpLeaseTimeSecs, mLinkMtu, mMetered, mClientAddr);
        }
    }

Loading