Skip to content

Commit

Permalink
#44: Server config reload updates with on-link pool/range validation
Browse files Browse the repository at this point in the history
  • Loading branch information
agrabil committed Apr 4, 2022
1 parent 5172eb0 commit 9041324
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

Expand Down Expand Up @@ -167,6 +168,13 @@ public InetAddress getEndAddress()
return endAddress;
}

public boolean isV6() {
if (subnetAddress instanceof Inet6Address) {
return true;
}
return false;
}

/**
* Contains. Test if an IP address falls within a subnet.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,17 @@ private void initGlobals(DhcpServerConfig jaxbServerConfig) {

public DhcpServerConfig reload(DhcpServerConfig jaxbServerConfig) throws DhcpServerConfigException, JAXBException, IOException {

if (jaxbServerConfig != null) {
if (jaxbServerConfig != null) {
log.info("DhcpServerConfig reloading...");
validateConfigPolicies(jaxbServerConfig);
initGlobals(jaxbServerConfig);
updateDhcpLinkMap(buildDhcpLinkMap(jaxbServerConfig.getLinks()));
saveConfig(jaxbServerConfig, serverConfigFilename);
saveConfig(jaxbServerConfig);
this.jaxbServerConfig = jaxbServerConfig;
log.info("DhcpServerConfig reload complete.");
}
else {
log.error("Unable to reload null DhcpServerConfig!");
}
return this.jaxbServerConfig;
}
Expand All @@ -366,6 +371,9 @@ public void updateDhcpLinkMap(SortedMap<Subnet, DhcpLink> newLinkMap) {
// add or update any and all links in the new link map
dhcpLinkMap.putAll(newLinkMap);
}
else {
log.error("Unable to update null LinkMap!");
}
}

/**
Expand Down Expand Up @@ -574,6 +582,7 @@ private DhcpLink buildDhcpLink(Link link) throws DhcpServerConfigException {
if ((s != null) && (s.length == 2)) {
try {
Subnet subnet = new Subnet(s[0], s[1]);
validateLinkPools(subnet, link);
return new DhcpLink(subnet, link);
}
catch (NumberFormatException ex) {
Expand All @@ -596,7 +605,71 @@ private DhcpLink buildDhcpLink(Link link) throws DhcpServerConfigException {
"Link must specify an interface or address element");
}
}

public static void validateLinkPools(Subnet subnet, Link link) throws DhcpServerConfigException {
if ((subnet != null) && (link != null)) {
if (!subnet.isV6()) {
V4AddressPoolsType addrPoolsType = link.getV4AddrPools();
if (addrPoolsType != null) {
for (V4AddressPool pool : addrPoolsType.getPoolList()) {
if (!pool.isNotInLinkSubnet()) {
validateRangeInSubnet(subnet, pool.getRange());
}
}
}
}
else {
V6AddressPoolsType addrPoolsType = link.getV6NaAddrPools();
if (addrPoolsType != null) {
for (V6AddressPool pool : addrPoolsType.getPoolList()) {
if (!pool.isNotInLinkSubnet()) {
validateRangeInSubnet(subnet, pool.getRange());
}
}
}
addrPoolsType = link.getV6TaAddrPools();
if (addrPoolsType != null) {
for (V6AddressPool pool : addrPoolsType.getPoolList()) {
if (!pool.isNotInLinkSubnet()) {
validateRangeInSubnet(subnet, pool.getRange());
}
}
}
V6PrefixPoolsType prefixPoolsType = link.getV6PrefixPools();
if (prefixPoolsType != null) {
for (V6PrefixPool pool : prefixPoolsType.getPoolList()) {
if (!pool.isNotInLinkSubnet()) {
validateRangeInSubnet(subnet, pool.getRange());
}
}
}
}
}
}

public static void validateRangeInSubnet(Subnet subnet, String range) throws DhcpServerConfigException {
if ((subnet != null) && (range != null)) {
String[] addrs = range.split("-");
if (addrs.length == 2) {
try {
InetAddress start = InetAddress.getByName(addrs[0]);
if (!subnet.contains(start)) {
throw new DhcpServerConfigException("Range start address: " + start.getHostAddress() +
" is not in Link subnet: " + subnet);
}
InetAddress end = InetAddress.getByName(addrs[1]);
if (!subnet.contains(end)) {
throw new DhcpServerConfigException("Range end address: " + end.getHostAddress() +
" is not in Link subnet: " + subnet);
}
}
catch (UnknownHostException ex) {
throw new DhcpServerConfigException("Invalid range=" + range + ": " + ex);
}
}
}
}

public void putDhcpLink(SortedMap<Subnet, DhcpLink> map, DhcpLink dhcpLink) {
Subnet subnet = dhcpLink.getSubnet();
Link link = dhcpLink.getLink();
Expand Down Expand Up @@ -1266,6 +1339,19 @@ public static void validatePolicies(String level, PoliciesType policiesType) thr
}
}

/**
* Re-save the current configuration to the file used at startup
* @param config
* @throws DhcpServerConfigException
* @throws JAXBException
* @throws IOException
*/
public void saveConfig(DhcpServerConfig config)
throws DhcpServerConfigException, JAXBException, IOException
{
saveConfig(config, serverConfigFilename);
}

/**
* Save the server configuration to a file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public PoliciesType getGlobalPolicies() {
}

public PoliciesType updateGlobalPolicies(PoliciesType globalPolicies) throws DhcpServerConfigException, JAXBException, IOException {
DhcpServerConfig currentConfig = getDhcpServerConfig();
// this will validate the policies
dhcpServerConfiguration.setGlobalPolicies(globalPolicies);
// validation passed, so set the config object
currentConfig.setPolicies(globalPolicies);
// now re-save the config object
dhcpServerConfiguration.saveConfig(currentConfig);
return dhcpServerConfiguration.getGlobalPolicies();
}
}
6 changes: 6 additions & 0 deletions Jagornet-DHCP/dhcp-server/src/main/resources/dhcpserver.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
<xs:element name="configOptions" type="v4ConfigOptionsType" minOccurs="0"/>
<xs:element name="filters" type="filtersType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:complexType name="v4AddressBindingsType">
Expand All @@ -311,6 +312,7 @@
<xs:element name="policies" type="policiesType" minOccurs="0"/>
<xs:element name="configOptions" type="v4ConfigOptionsType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:complexType name="v6AddressPoolsType">
Expand All @@ -328,6 +330,7 @@
<xs:element name="addrConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
<xs:element name="filters" type="filtersType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:complexType name="v6PrefixPoolsType">
Expand All @@ -346,6 +349,7 @@
<xs:element name="prefixConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
<xs:element name="filters" type="filtersType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:complexType name="v6AddressBindingsType">
Expand All @@ -364,6 +368,7 @@
<xs:element name="iaConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
<xs:element name="addrConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:complexType name="v6PrefixBindingsType">
Expand All @@ -383,6 +388,7 @@
<xs:element name="iaConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
<xs:element name="prefixConfigOptions" type="v6ConfigOptionsType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="notInLinkSubnet" type="xs:boolean" default="false"/>
</xs:complexType>

<xs:simpleType name="range">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ links:
v6NaAddrPools:
poolList:
- range: "2001:db8:1::1:0-2001:db8:1::1:FF"
notInLinkSubnet: true

0 comments on commit 9041324

Please sign in to comment.