/* * Copyright (C) 2018 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.dhcp; import static com.android.net.module.util.Inet4AddressUtils.getPrefixMaskAsInet4Address; import static com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTH; import static com.android.net.module.util.NetworkStackConstants.INFINITE_LEASE; import static com.android.net.module.util.NetworkStackConstants.IPV4_MAX_MTU; import static com.android.net.module.util.NetworkStackConstants.IPV4_MIN_MTU; import static java.lang.Integer.toUnsignedLong; import android.net.IpPrefix; import android.net.LinkAddress; import android.util.ArraySet; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.android.net.module.util.Inet4AddressUtils; import java.net.Inet4Address; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * Parameters used by the DhcpServer to serve requests. * *
Instances are immutable. Use {@link DhcpServingParams.Builder} to instantiate.
* @hide
*/
public class DhcpServingParams {
public static final int MTU_UNSET = 0;
public static final int MIN_PREFIX_LENGTH = 16;
public static final int MAX_PREFIX_LENGTH = 30;
/** Server inet address and prefix to serve */
@NonNull
public final LinkAddress serverAddr;
/**
* Default routers to be advertised to DHCP clients. May be empty.
* This set is provided by {@link DhcpServingParams.Builder} and is immutable.
*/
@NonNull
public final Set For example, if the served prefix is 192.168.42.0/24, this will return 255.255.255.0.
*/
@NonNull
public Inet4Address getPrefixMaskAsAddress() {
return getPrefixMaskAsInet4Address(serverAddr.getPrefixLength());
}
/**
* Get the server broadcast address.
*
* For example, if the server {@link LinkAddress} is 192.168.42.1/24, this will return
* 192.168.42.255.
*/
@NonNull
public Inet4Address getBroadcastAddress() {
return Inet4AddressUtils.getBroadcastAddress(
getServerInet4Addr(), serverAddr.getPrefixLength());
}
/**
* Utility class to create new instances of {@link DhcpServingParams} while checking validity
* of the parameters.
*/
public static class Builder {
private LinkAddress mServerAddr;
private Set This parameter is required.
*/
public Builder setServerAddr(@NonNull LinkAddress serverAddr) {
this.mServerAddr = serverAddr;
return this;
}
/**
* Set the default routers to be advertised to DHCP clients.
*
* Each router must be inside the served prefix. This may be an empty set, but it must
* always be set explicitly before building the {@link DhcpServingParams}.
*/
public Builder setDefaultRouters(@NonNull Set Each router must be inside the served prefix. This may be an empty list of routers,
* but it must always be set explicitly before building the {@link DhcpServingParams}.
*/
public Builder setDefaultRouters(@NonNull Inet4Address... defaultRouters) {
return setDefaultRouters(makeArraySet(defaultRouters));
}
/**
* Convenience method to build the parameters with no default router.
*
* Equivalent to calling {@link #setDefaultRouters(Inet4Address...)} with no address.
*/
public Builder withNoDefaultRouter() {
return setDefaultRouters();
}
/**
* Set the DNS servers to be advertised to DHCP clients.
*
* This may be an empty set, but it must always be set explicitly before building the
* {@link DhcpServingParams}.
*/
public Builder setDnsServers(@NonNull Set This may be an empty list of servers, but it must always be set explicitly before
* building the {@link DhcpServingParams}.
*/
public Builder setDnsServers(@NonNull Inet4Address... dnsServers) {
return setDnsServers(makeArraySet(dnsServers));
}
/**
* Convenience method to build the parameters with no DNS server.
*
* Equivalent to calling {@link #setDnsServers(Inet4Address...)} with no address.
*/
public Builder withNoDnsServer() {
return setDnsServers();
}
/**
* Set excluded addresses that the DHCP server is not allowed to assign to clients.
*
* This parameter is optional. DNS servers and default routers are always excluded
* and do not need to be set here.
*/
public Builder setExcludedAddrs(@NonNull Set This parameter is optional. DNS servers and default routers are always excluded
* and do not need to be set here.
*/
public Builder setExcludedAddrs(@NonNull Inet4Address... excludedAddrs) {
return setExcludedAddrs(makeArraySet(excludedAddrs));
}
/**
* Set the lease time for leases assigned by the DHCP server.
*
* This parameter is required.
*/
public Builder setDhcpLeaseTimeSecs(long dhcpLeaseTimeSecs) {
this.mDhcpLeaseTimeSecs = dhcpLeaseTimeSecs;
return this;
}
/**
* Set the link MTU to be advertised to DHCP clients.
*
* If set to {@link #MTU_UNSET}, no MTU will be advertised to clients. This parameter
* is optional and defaults to {@link #MTU_UNSET}.
*/
public Builder setLinkMtu(int linkMtu) {
this.mLinkMtu = linkMtu;
return this;
}
/**
* Set whether the DHCP server should send the ANDROID_METERED vendor-specific option.
*
* If not set, the default value is false.
*/
public Builder setMetered(boolean metered) {
this.mMetered = metered;
return this;
}
/**
* Set the client address.
*
* If not set, the default value is null.
*/
public Builder setSingleClientAddr(@Nullable Inet4Address clientAddr) {
this.mClientAddr = clientAddr;
return this;
}
/**
* Set whether the DHCP server should request a new prefix from IpServer when receiving
* DHCPDECLINE message in certain particular link.
*
* If not set, the default value is false.
*/
public Builder setChangePrefixOnDecline(boolean changePrefixOnDecline) {
this.mChangePrefixOnDecline = changePrefixOnDecline;
return this;
}
/**
* Create a new {@link DhcpServingParams} instance based on parameters set in the builder.
*
* This method has no side-effects. If it does not throw, a valid
* {@link DhcpServingParams} is returned.
* @return The constructed parameters.
* @throws InvalidParameterException At least one parameter is missing or invalid.
*/
@NonNull
public DhcpServingParams build() throws InvalidParameterException {
if (mServerAddr == null) {
throw new InvalidParameterException("Missing serverAddr");
}
if (mDefaultRouters == null) {
throw new InvalidParameterException("Missing defaultRouters");
}
if (mDnsServers == null) {
// Empty set is OK, but enforce explicitly setting it
throw new InvalidParameterException("Missing dnsServers");
}
if (mDhcpLeaseTimeSecs <= 0 || mDhcpLeaseTimeSecs > toUnsignedLong(INFINITE_LEASE)) {
throw new InvalidParameterException("Invalid lease time: " + mDhcpLeaseTimeSecs);
}
if (mLinkMtu != MTU_UNSET && (mLinkMtu < IPV4_MIN_MTU || mLinkMtu > IPV4_MAX_MTU)) {
throw new InvalidParameterException("Invalid link MTU: " + mLinkMtu);
}
if (!mServerAddr.isIpv4()) {
throw new InvalidParameterException("serverAddr must be IPv4");
}
if (mServerAddr.getPrefixLength() < MIN_PREFIX_LENGTH
|| mServerAddr.getPrefixLength() > MAX_PREFIX_LENGTH) {
throw new InvalidParameterException("Prefix length is not in supported range");
}
final IpPrefix prefix = makeIpPrefix(mServerAddr);
for (Inet4Address addr : mDefaultRouters) {
if (!prefix.contains(addr)) {
throw new InvalidParameterException(String.format(
"Default router %s is not in server prefix %s", addr, mServerAddr));
}
}
final Set