diff --git a/interface-definitions/dhcp-relay.xml.in b/interface-definitions/dhcp-relay.xml.in index 2a2597dd5..42715c9bb 100644 --- a/interface-definitions/dhcp-relay.xml.in +++ b/interface-definitions/dhcp-relay.xml.in @@ -1,125 +1,126 @@ <?xml version="1.0"?> <!-- DHCP relay configuration --> <interfaceDefinition> <node name="service"> <children> <node name="dhcp-relay" owner="${vyos_conf_scripts_dir}/dhcp_relay.py"> <properties> <help>Host Configuration Protocol (DHCP) relay agent</help> <priority>910</priority> </properties> <children> + #include <include/generic-disable-node.xml.i> #include <include/generic-interface-multi-broadcast.xml.i> <leafNode name="listen-interface"> <properties> <help>Interface for DHCP Relay Agent to listen for requests</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces</script> </completionHelp> <valueHelp> <format>txt</format> <description>Interface name</description> </valueHelp> <constraint> #include <include/constraint/interface-name.xml.i> </constraint> <multi/> </properties> </leafNode> <leafNode name="upstream-interface"> <properties> <help>Interface for DHCP Relay Agent forward requests out</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces</script> </completionHelp> <valueHelp> <format>txt</format> <description>Interface name</description> </valueHelp> <constraint> #include <include/constraint/interface-name.xml.i> </constraint> <multi/> </properties> </leafNode> <node name="relay-options"> <properties> <help>Relay options</help> </properties> <children> <leafNode name="hop-count"> <properties> <help>Policy to discard packets that have reached specified hop-count</help> <valueHelp> <format>u32:1-255</format> <description>Hop count</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-255"/> </constraint> <constraintErrorMessage>hop-count must be a value between 1 and 255</constraintErrorMessage> </properties> <defaultValue>10</defaultValue> </leafNode> <leafNode name="max-size"> <properties> <help>Maximum packet size to send to a DHCPv4/BOOTP server</help> <valueHelp> <format>u32:64-1400</format> <description>Maximum packet size</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 64-1400"/> </constraint> <constraintErrorMessage>max-size must be a value between 64 and 1400</constraintErrorMessage> </properties> <defaultValue>576</defaultValue> </leafNode> <leafNode name="relay-agents-packets"> <properties> <help>Policy to handle incoming DHCPv4 packets which already contain relay agent options</help> <completionHelp> <list>append replace forward discard</list> </completionHelp> <valueHelp> <format>append</format> <description>append own relay options to packet</description> </valueHelp> <valueHelp> <format>replace</format> <description>replace existing agent option field</description> </valueHelp> <valueHelp> <format>forward</format> <description>forward packet unchanged</description> </valueHelp> <valueHelp> <format>discard</format> <description>discard packet (default action if giaddr not set in packet)</description> </valueHelp> <constraint> <regex>(append|replace|forward|discard)</regex> </constraint> </properties> <defaultValue>forward</defaultValue> </leafNode> </children> </node> <leafNode name="server"> <properties> <help>DHCP server address</help> <valueHelp> <format>ipv4</format> <description>DHCP server IPv4 address</description> </valueHelp> <multi/> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> </children> </node> </children> </node> </interfaceDefinition> diff --git a/interface-definitions/dhcpv6-relay.xml.in b/interface-definitions/dhcpv6-relay.xml.in index 947adef75..a80317609 100644 --- a/interface-definitions/dhcpv6-relay.xml.in +++ b/interface-definitions/dhcpv6-relay.xml.in @@ -1,81 +1,82 @@ <?xml version="1.0"?> <!-- DHCPv6 relay configuration --> <interfaceDefinition> <node name="service"> <children> <node name="dhcpv6-relay" owner="${vyos_conf_scripts_dir}/dhcpv6_relay.py"> <properties> <help>DHCPv6 Relay Agent parameters</help> <priority>900</priority> </properties> <children> + #include <include/generic-disable-node.xml.i> <tagNode name="listen-interface"> <properties> <help>Interface for DHCPv6 Relay Agent to listen for requests</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces</script> </completionHelp> </properties> <children> <leafNode name="address"> <properties> <help>IPv6 address on listen-interface listen for requests on</help> <valueHelp> <format>ipv6</format> <description>IPv6 address on listen interface</description> </valueHelp> <constraint> <validator name="ipv6-address"/> </constraint> </properties> </leafNode> </children> </tagNode> <leafNode name="max-hop-count"> <properties> <help>Maximum hop count for which requests will be processed</help> <valueHelp> <format>u32:1-255</format> <description>Hop count</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-255"/> </constraint> <constraintErrorMessage>max-hop-count must be a value between 1 and 255</constraintErrorMessage> </properties> <defaultValue>10</defaultValue> </leafNode> <tagNode name="upstream-interface"> <properties> <help>Interface for DHCPv6 Relay Agent forward requests out</help> <completionHelp> <script>${vyos_completion_dir}/list_interfaces</script> </completionHelp> </properties> <children> <leafNode name="address"> <properties> <help>IPv6 address to forward requests to</help> <valueHelp> <format>ipv6</format> <description>IPv6 address of the DHCP server</description> </valueHelp> <constraint> <validator name="ipv6-address"/> </constraint> <multi/> </properties> </leafNode> </children> </tagNode> <leafNode name="use-interface-id-option"> <properties> <help>Option to set DHCPv6 interface-ID option</help> <valueless/> </properties> </leafNode> </children> </node> </children> </node> </interfaceDefinition> diff --git a/src/conf_mode/dhcp_relay.py b/src/conf_mode/dhcp_relay.py index 7e702a446..7322cc571 100755 --- a/src/conf_mode/dhcp_relay.py +++ b/src/conf_mode/dhcp_relay.py @@ -1,108 +1,108 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2020 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os from sys import exit from vyos.base import Warning from vyos.config import Config from vyos.configdict import dict_merge from vyos.template import render from vyos.base import Warning from vyos.util import call from vyos.util import dict_search from vyos.xml import defaults from vyos import ConfigError from vyos import airbag airbag.enable() config_file = r'/run/dhcp-relay/dhcrelay.conf' def get_config(config=None): if config: conf = config else: conf = Config() base = ['service', 'dhcp-relay'] if not conf.exists(base): return None relay = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True) # We have gathered the dict representation of the CLI, but there are default # options which we need to update into the dictionary retrived. default_values = defaults(base) relay = dict_merge(default_values, relay) return relay def verify(relay): # bail out early - looks like removal from running config - if not relay: + if not relay or 'disable' in relay: return None if 'lo' in (dict_search('interface', relay) or []): raise ConfigError('DHCP relay does not support the loopback interface.') if 'server' not in relay : raise ConfigError('No DHCP relay server(s) configured.\n' \ 'At least one DHCP relay server required.') if 'interface' in relay: Warning('DHCP relay interface is DEPRECATED - please use upstream-interface and listen-interface instead!') if 'upstream_interface' in relay or 'listen_interface' in relay: raise ConfigError('<interface> configuration is not compatible with upstream/listen interface') else: Warning('<interface> is going to be deprecated.\n' \ 'Please use <listen-interface> and <upstream-interface>') if 'upstream_interface' in relay and 'listen_interface' not in relay: raise ConfigError('No listen-interface configured') if 'listen_interface' in relay and 'upstream_interface' not in relay: raise ConfigError('No upstream-interface configured') return None def generate(relay): # bail out early - looks like removal from running config - if not relay: + if not relay or 'disable' in relay: return None render(config_file, 'dhcp-relay/dhcrelay.conf.j2', relay) return None def apply(relay): # bail out early - looks like removal from running config service_name = 'isc-dhcp-relay.service' - if not relay: + if not relay or 'disable' in relay: call(f'systemctl stop {service_name}') if os.path.exists(config_file): os.unlink(config_file) return None call(f'systemctl restart {service_name}') return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1) diff --git a/src/conf_mode/dhcpv6_relay.py b/src/conf_mode/dhcpv6_relay.py index c1bd51f62..9d6597455 100755 --- a/src/conf_mode/dhcpv6_relay.py +++ b/src/conf_mode/dhcpv6_relay.py @@ -1,110 +1,111 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2020 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os from sys import exit from vyos.config import Config from vyos.configdict import dict_merge from vyos.ifconfig import Interface from vyos.template import render +from vyos.template import is_ipv6 from vyos.util import call from vyos.util import dict_search from vyos.validate import is_ipv6_link_local from vyos.xml import defaults from vyos import ConfigError from vyos import airbag airbag.enable() config_file = '/run/dhcp-relay/dhcrelay6.conf' def get_config(config=None): if config: conf = config else: conf = Config() base = ['service', 'dhcpv6-relay'] if not conf.exists(base): return None relay = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True) # We have gathered the dict representation of the CLI, but there are default # options which we need to update into the dictionary retrived. default_values = defaults(base) relay = dict_merge(default_values, relay) return relay def verify(relay): # bail out early - looks like removal from running config - if not relay: + if not relay or 'disable' in relay: return None if 'upstream_interface' not in relay: raise ConfigError('At least one upstream interface required!') for interface, config in relay['upstream_interface'].items(): if 'address' not in config: raise ConfigError('DHCPv6 server required for upstream ' \ f'interface {interface}!') if 'listen_interface' not in relay: raise ConfigError('At least one listen interface required!') # DHCPv6 relay requires at least one global unicat address assigned to the # interface for interface in relay['listen_interface']: has_global = False for addr in Interface(interface).get_addr(): - if not is_ipv6_link_local(addr): + if is_ipv6(addr) and not is_ipv6_link_local(addr): has_global = True if not has_global: raise ConfigError(f'Interface {interface} does not have global '\ 'IPv6 address assigned!') return None def generate(relay): # bail out early - looks like removal from running config - if not relay: + if not relay or 'disable' in relay: return None render(config_file, 'dhcp-relay/dhcrelay6.conf.j2', relay) return None def apply(relay): # bail out early - looks like removal from running config service_name = 'isc-dhcp-relay6.service' - if not relay: + if not relay or 'disable' in relay: # DHCPv6 relay support is removed in the commit call(f'systemctl stop {service_name}') if os.path.exists(config_file): os.unlink(config_file) return None call(f'systemctl restart {service_name}') return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1)