Commit 4a76711c authored by Nigel Kukard's avatar Nigel Kukard
Browse files

If the ipaddress does not overlap the gateway, add a static route to the gateway

parent 4277ab02
......@@ -15,6 +15,7 @@
"""Configure systemd networkd."""
import ipaddress
from typing import Callable, Dict, List, Optional
from idmslinux_installer.ilistate import IliState
......@@ -130,6 +131,40 @@ class ConfigSystemdNetworkd(Plugin):
if 'dns2address' in config:
network_file.write(f'DNS=%s\n' % config['dns2address'])
# If ipv4gateway is not within ipv4address's range, then add a route section
if self._outside_network4(config, 'ipv4address', 'ipv4gateway'):
network_file.write('\n[Route]\n')
network_file.write('Destination=%s\n' % config['ipv4gateway'])
# If ipv6gateway is not within ipv6address's range, then add a route section
if self._outside_network6(config, 'ipv6address', 'ipv6gateway'):
network_file.write('\n[Route]\n')
network_file.write('Destination=%s\n' % config['ipv6gateway'])
network_file.write(f'\n')
# Finally close the file
network_file.close()
def _outside_network4(self, config: Dict[str, str], network_name: str, address_name: str):
"""Check if a network does not overlaps an address."""
if (network_name not in config) or (address_name not in config):
return False
# Reduce both to networks
network = ipaddress.IPv4Network(config[network_name], strict=False)
address = ipaddress.IPv4Network(config[address_name])
return not network.overlaps(address)
def _outside_network6(self, config: Dict[str, str], network_name: str, address_name: str):
"""Check if a network does not overlaps an address."""
if (network_name not in config) or (address_name not in config):
return False
# Reduce both to networks
network = ipaddress.IPv6Network(config[network_name], strict=False)
address = ipaddress.IPv6Network(config[address_name])
return not network.overlaps(address)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment