diff --git a/data/templates/dhcp-server/dhcpd.conf.j2 b/data/templates/dhcp-server/dhcpd.conf.j2 index 639526532..d00837389 100644 --- a/data/templates/dhcp-server/dhcpd.conf.j2 +++ b/data/templates/dhcp-server/dhcpd.conf.j2 @@ -1,250 +1,251 @@ ### Autogenerated by dhcp_server.py ### # For options please consult the following website: # https://www.isc.org/wp-content/uploads/2017/08/dhcp43options.html # # log-facility local7; {% if hostfile_update is vyos_defined %} on release { set ClientName = pick-first-value(host-decl-name, option fqdn.hostname, option host-name); set ClientIp = binary-to-ascii(10, 8, ".",leased-address); execute("/usr/libexec/vyos/system/on-dhcp-event.sh", "release", "", ClientIp, "", ""); } on expiry { set ClientName = pick-first-value(host-decl-name, option fqdn.hostname, option host-name); set ClientIp = binary-to-ascii(10, 8, ".",leased-address); execute("/usr/libexec/vyos/system/on-dhcp-event.sh", "release", "", ClientIp, "", ""); } {% endif %} {{ 'use-host-decl-names on;' if host_decl_name is vyos_defined }} ddns-update-style {{ 'interim' if dynamic_dns_update is vyos_defined else 'none' }}; option rfc3442-static-route code 121 = array of integer 8; option windows-static-route code 249 = array of integer 8; option wpad-url code 252 = text; option rfc8925-ipv6-only-preferred code 108 = unsigned integer 32; # Vendor specific options - Ubiquiti Networks option space ubnt; option ubnt.unifi-controller code 1 = ip-address; class "ubnt" { match if substring (option vendor-class-identifier , 0, 4) = "ubnt"; option vendor-class-identifier "ubnt"; vendor-option-space ubnt; } {% if global_parameters is vyos_defined %} # The following {{ global_parameters | length }} line(s) have been added as # global-parameters in the CLI and have not been validated !!! {% for parameter in global_parameters %} {{ parameter }} {% endfor %} {% endif %} -{% if failover is vyos_defined %} -# DHCP failover configuration -failover peer "{{ failover.name }}" { -{% if failover.status == 'primary' %} +{% if high_availability is vyos_defined %} +# DHCP HA configuration +{% set split_value = '256' if high_availability.mode == 'active-passive' else '128' %} +failover peer "{{ high_availability.name }}" { +{% if high_availability.status == 'primary' %} primary; mclt 1800; - split 128; -{% elif failover.status == 'secondary' %} + split {{ split_value }}; +{% elif high_availability.status == 'secondary' %} secondary; {% endif %} - address {{ failover.source_address }}; + address {{ high_availability.source_address }}; port 647; - peer address {{ failover.remote }}; + peer address {{ high_availability.remote }}; peer port 647; max-response-delay 30; max-unacked-updates 10; load balance max seconds 3; } {% endif %} {% if listen_address is vyos_defined %} # DHCP server serving relay subnet, we need a connector to the real world {% for address in listen_address %} # Connected subnet statement for listen-address {{ address }} subnet {{ address | network_from_ipv4 }} netmask {{ address | netmask_from_ipv4 }} { } {% endfor %} {% endif %} # Shared network configration(s) {% if shared_network_name is vyos_defined %} {% for network, network_config in shared_network_name.items() if network_config.disable is not vyos_defined %} shared-network {{ network }} { {% if network_config.authoritative is vyos_defined %} authoritative; {% endif %} {% if network_config.name_server is vyos_defined %} option domain-name-servers {{ network_config.name_server | join(', ') }}; {% endif %} {% if network_config.domain_name is vyos_defined %} option domain-name "{{ network_config.domain_name }}"; {% endif %} {% if network_config.domain_search is vyos_defined %} option domain-search "{{ network_config.domain_search | join('", "') }}"; {% endif %} {% if network_config.ntp_server is vyos_defined %} option ntp-servers {{ network_config.ntp_server | join(', ') }}; {% endif %} {% if network_config.ping_check is vyos_defined %} ping-check true; {% endif %} {% if network_config.shared_network_parameters is vyos_defined %} # The following {{ network_config.shared_network_parameters | length }} line(s) # were added as shared-network-parameters in the CLI and have not been validated {% for parameter in network_config.shared_network_parameters %} {{ parameter }} {% endfor %} {% endif %} {% if network_config.subnet is vyos_defined %} {% for subnet, subnet_config in network_config.subnet.items() %} {% if subnet_config.description is vyos_defined %} # {{ subnet_config.description }} {% endif %} subnet {{ subnet | address_from_cidr }} netmask {{ subnet | netmask_from_cidr }} { {% if subnet_config.name_server is vyos_defined %} option domain-name-servers {{ subnet_config.name_server | join(', ') }}; {% endif %} {% if subnet_config.domain_name is vyos_defined %} option domain-name "{{ subnet_config.domain_name }}"; {% endif %} {% if subnet_config.domain_search is vyos_defined %} option domain-search "{{ subnet_config.domain_search | join('", "') }}"; {% endif %} {% if subnet_config.ntp_server is vyos_defined %} option ntp-servers {{ subnet_config.ntp_server | join(', ') }}; {% endif %} {% if subnet_config.pop_server is vyos_defined %} option pop-server {{ subnet_config.pop_server | join(', ') }}; {% endif %} {% if subnet_config.smtp_server is vyos_defined %} option smtp-server {{ subnet_config.smtp_server | join(', ') }}; {% endif %} {% if subnet_config.time_server is vyos_defined %} option time-servers {{ subnet_config.time_server | join(', ') }}; {% endif %} {% if subnet_config.wins_server is vyos_defined %} option netbios-name-servers {{ subnet_config.wins_server | join(', ') }}; {% endif %} {% if subnet_config.ipv6_only_preferred is vyos_defined %} option rfc8925-ipv6-only-preferred {{ subnet_config.ipv6_only_preferred }}; {% endif %} {% if subnet_config.static_route is vyos_defined %} {% set static_default_route = '' %} {% if subnet_config.default_router is vyos_defined %} {% set static_default_route = ', ' ~ '0.0.0.0/0' | isc_static_route(subnet_config.default_router) %} {% endif %} {% if subnet_config.static_route is vyos_defined %} {% set rfc3442_routes = [] %} {% for route, route_options in subnet_config.static_route.items() %} {% set rfc3442_routes = rfc3442_routes.append(route | isc_static_route(route_options.next_hop)) %} {% endfor %} option rfc3442-static-route {{ rfc3442_routes | join(', ') }}{{ static_default_route }}; option windows-static-route {{ rfc3442_routes | join(', ') }}; {% endif %} {% endif %} {% if subnet_config.ip_forwarding is vyos_defined %} option ip-forwarding true; {% endif %} {% if subnet_config.default_router is vyos_defined %} option routers {{ subnet_config.default_router }}; {% endif %} {% if subnet_config.server_identifier is vyos_defined %} option dhcp-server-identifier {{ subnet_config.server_identifier }}; {% endif %} {% if subnet_config.subnet_parameters is vyos_defined %} # The following {{ subnet_config.subnet_parameters | length }} line(s) were added as # subnet-parameters in the CLI and have not been validated!!! {% for parameter in subnet_config.subnet_parameters %} {{ parameter }} {% endfor %} {% endif %} {% if subnet_config.tftp_server_name is vyos_defined %} option tftp-server-name "{{ subnet_config.tftp_server_name }}"; {% endif %} {% if subnet_config.bootfile_name is vyos_defined %} option bootfile-name "{{ subnet_config.bootfile_name }}"; filename "{{ subnet_config.bootfile_name }}"; {% endif %} {% if subnet_config.bootfile_server is vyos_defined %} next-server {{ subnet_config.bootfile_server }}; {% endif %} {% if subnet_config.bootfile_size is vyos_defined %} option boot-size {{ subnet_config.bootfile_size }}; {% endif %} {% if subnet_config.time_offset is vyos_defined %} option time-offset {{ subnet_config.time_offset }}; {% endif %} {% if subnet_config.wpad_url is vyos_defined %} option wpad-url "{{ subnet_config.wpad_url }}"; {% endif %} {% if subnet_config.client_prefix_length is vyos_defined %} option subnet-mask {{ ('0.0.0.0/' ~ subnet_config.client_prefix_length) | netmask_from_cidr }}; {% endif %} {% if subnet_config.lease is vyos_defined %} default-lease-time {{ subnet_config.lease }}; max-lease-time {{ subnet_config.lease }}; {% endif %} {% if network_config.ping_check is not vyos_defined and subnet_config.ping_check is vyos_defined %} ping-check true; {% endif %} {% if subnet_config.static_mapping is vyos_defined %} {% for host, host_config in subnet_config.static_mapping.items() if host_config.disable is not vyos_defined %} host {{ host | replace('_','-') if host_decl_name is vyos_defined else network | replace('_','-') ~ '_' ~ host | replace('_','-') }} { {% if host_config.ip_address is vyos_defined %} fixed-address {{ host_config.ip_address }}; {% endif %} hardware ethernet {{ host_config.mac_address }}; {% if host_config.static_mapping_parameters is vyos_defined %} # The following {{ host_config.static_mapping_parameters | length }} line(s) were added # as static-mapping-parameters in the CLI and have not been validated {% for parameter in host_config.static_mapping_parameters %} {{ parameter }} {% endfor %} {% endif %} } {% endfor %} {% endif %} {% if subnet_config.vendor_option.ubiquiti.unifi_controller is vyos_defined %} option ubnt.unifi-controller {{ subnet_config.vendor_option.ubiquiti.unifi_controller }}; {% endif %} {% if subnet_config.range is vyos_defined %} {# pool configuration can only be used if there follows a range option #} pool { {% endif %} {% if subnet_config.enable_failover is vyos_defined %} - failover peer "{{ failover.name }}"; + failover peer "{{ high_availability.name }}"; deny dynamic bootp clients; {% endif %} {% if subnet_config.range is vyos_defined %} {% for range, range_options in subnet_config.range.items() %} range {{ range_options.start }} {{ range_options.stop }}; {% endfor %} {% endif %} {% if subnet_config.range is vyos_defined %} {# pool configuration can only be used if there follows a range option #} } {% endif %} } {% endfor %} {% endif %} on commit { set shared-networkname = "{{ network }}"; {% if hostfile_update is vyos_defined %} set ClientIp = binary-to-ascii(10, 8, ".", leased-address); set ClientMac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6)); set ClientName = pick-first-value(host-decl-name, option fqdn.hostname, option host-name, "empty_hostname"); if not (ClientName = "empty_hostname") { set ClientDomain = pick-first-value(config-option domain-name, "..YYZ!"); execute("/usr/libexec/vyos/system/on-dhcp-event.sh", "commit", ClientName, ClientIp, ClientMac, ClientDomain); } else { log(concat("Hostname is not defined for client with IP: ", ClientIP, " MAC: ", ClientMac)); } {% endif %} } } {% endfor %} {% endif %} diff --git a/interface-definitions/include/version/dhcp-server-version.xml.i b/interface-definitions/include/version/dhcp-server-version.xml.i index 7c4b5633e..cc84ea8b9 100644 --- a/interface-definitions/include/version/dhcp-server-version.xml.i +++ b/interface-definitions/include/version/dhcp-server-version.xml.i @@ -1,3 +1,3 @@ <!-- include start from include/version/dhcp-server-version.xml.i --> -<syntaxVersion component='dhcp-server' version='7'></syntaxVersion> +<syntaxVersion component='dhcp-server' version='8'></syntaxVersion> <!-- include end --> diff --git a/interface-definitions/service_dhcp-server.xml.in b/interface-definitions/service_dhcp-server.xml.in index a3c48afca..495d1a00b 100644 --- a/interface-definitions/service_dhcp-server.xml.in +++ b/interface-definitions/service_dhcp-server.xml.in @@ -1,490 +1,511 @@ <?xml version="1.0"?> <!-- DHCP server configuration --> <interfaceDefinition> <node name="service"> <children> <node name="dhcp-server" owner="${vyos_conf_scripts_dir}/service_dhcp-server.py"> <properties> <help>Dynamic Host Configuration Protocol (DHCP) for DHCP server</help> <priority>911</priority> </properties> <children> #include <include/generic-disable-node.xml.i> <leafNode name="dynamic-dns-update"> <properties> <help>Dynamically update Domain Name System (RFC4702)</help> <valueless/> </properties> </leafNode> - <node name="failover"> + <node name="high-availability"> <properties> - <help>DHCP failover configuration</help> + <help>DHCP high availability configuration</help> </properties> <children> #include <include/source-address-ipv4.xml.i> + <leafNode name="mode"> + <properties> + <help>Configure high availability mode</help> + <completionHelp> + <list>active-active active-passive</list> + </completionHelp> + <valueHelp> + <format>active-active</format> + <description>Both server attend DHCP requests</description> + </valueHelp> + <valueHelp> + <format>active-passive</format> + <description>Only primary server attends DHCP requests</description> + </valueHelp> + <constraint> + <regex>(active-active|active-passive)</regex> + </constraint> + <constraintErrorMessage>Invalid DHCP high availability mode</constraintErrorMessage> + </properties> + <defaultValue>active-active</defaultValue> + </leafNode> <leafNode name="remote"> <properties> <help>IPv4 remote address used for connectio</help> <valueHelp> <format>ipv4</format> <description>IPv4 address of failover peer</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="name"> <properties> <help>Peer name used to identify connection</help> <constraint> #include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i> </constraint> <constraintErrorMessage>Invalid failover peer name. May only contain letters, numbers and .-_</constraintErrorMessage> </properties> </leafNode> <leafNode name="status"> <properties> <help>Failover hierarchy</help> <completionHelp> <list>primary secondary</list> </completionHelp> <valueHelp> <format>primary</format> <description>Configure this server to be the primary node</description> </valueHelp> <valueHelp> <format>secondary</format> <description>Configure this server to be the secondary node</description> </valueHelp> <constraint> <regex>(primary|secondary)</regex> </constraint> <constraintErrorMessage>Invalid DHCP failover peer status</constraintErrorMessage> </properties> </leafNode> </children> </node> <leafNode name="global-parameters"> <properties> <help>Additional global parameters for DHCP server. You must use the syntax of dhcpd.conf in this text-field. Using this without proper knowledge may result in a crashed DHCP server. Check system log to look for errors.</help> <multi/> </properties> </leafNode> <leafNode name="hostfile-update"> <properties> <help>Updating /etc/hosts file (per client lease)</help> <valueless/> </properties> </leafNode> <leafNode name="host-decl-name"> <properties> <help>Use host declaration name for forward DNS name</help> <valueless/> </properties> </leafNode> #include <include/listen-address-ipv4.xml.i> <tagNode name="shared-network-name"> <properties> <help>Name of DHCP shared network</help> <constraint> #include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i> </constraint> <constraintErrorMessage>Invalid shared network name. May only contain letters, numbers and .-_</constraintErrorMessage> </properties> <children> <leafNode name="authoritative"> <properties> <help>Option to make DHCP server authoritative for this physical network</help> <valueless/> </properties> </leafNode> #include <include/dhcp/domain-name.xml.i> #include <include/dhcp/domain-search.xml.i> #include <include/dhcp/ntp-server.xml.i> #include <include/dhcp/ping-check.xml.i> #include <include/generic-description.xml.i> #include <include/generic-disable-node.xml.i> #include <include/name-server-ipv4.xml.i> <leafNode name="shared-network-parameters"> <properties> <help>Additional shared-network parameters for DHCP server. You must use the syntax of dhcpd.conf in this text-field. Using this without proper knowledge may result in a crashed DHCP server. Check system log to look for errors.</help> <multi/> </properties> </leafNode> <tagNode name="subnet"> <properties> <help>DHCP subnet for shared network</help> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <constraint> <validator name="ipv4-prefix"/> </constraint> <constraintErrorMessage>Invalid IPv4 subnet definition</constraintErrorMessage> </properties> <children> <leafNode name="bootfile-name"> <properties> <help>Bootstrap file name</help> <constraint> <regex>[[:ascii:]]{1,253}</regex> </constraint> </properties> </leafNode> <leafNode name="bootfile-server"> <properties> <help>Server from which the initial boot file is to be loaded</help> <valueHelp> <format>ipv4</format> <description>Bootfile server IPv4 address</description> </valueHelp> <valueHelp> <format>hostname</format> <description>Bootfile server FQDN</description> </valueHelp> <constraint> <validator name="ipv4-address"/> <validator name="fqdn"/> </constraint> </properties> </leafNode> <leafNode name="bootfile-size"> <properties> <help>Bootstrap file size</help> <valueHelp> <format>u32:1-16</format> <description>Bootstrap file size in 512 byte blocks</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-16"/> </constraint> </properties> </leafNode> <leafNode name="client-prefix-length"> <properties> <help>Specifies the clients subnet mask as per RFC 950. If unset, subnet declaration is used.</help> <valueHelp> <format>u32:0-32</format> <description>DHCP client prefix length must be 0 to 32</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-32"/> </constraint> <constraintErrorMessage>DHCP client prefix length must be 0 to 32</constraintErrorMessage> </properties> </leafNode> <leafNode name="default-router"> <properties> <help>IP address of default router</help> <valueHelp> <format>ipv4</format> <description>Default router IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> #include <include/dhcp/domain-name.xml.i> #include <include/dhcp/domain-search.xml.i> #include <include/generic-description.xml.i> #include <include/name-server-ipv4.xml.i> <leafNode name="enable-failover"> <properties> <help>Enable DHCP failover support for this subnet</help> <valueless/> </properties> </leafNode> <leafNode name="exclude"> <properties> <help>IP address to exclude from DHCP lease range</help> <valueHelp> <format>ipv4</format> <description>IPv4 address to exclude from lease range</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="ip-forwarding"> <properties> <help>Enable IP forwarding on client</help> <valueless/> </properties> </leafNode> <leafNode name="lease"> <properties> <help>Lease timeout in seconds</help> <valueHelp> <format>u32</format> <description>DHCP lease time in seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4294967295"/> </constraint> <constraintErrorMessage>DHCP lease time must be between 0 and 4294967295 (49 days)</constraintErrorMessage> </properties> <defaultValue>86400</defaultValue> </leafNode> #include <include/dhcp/ntp-server.xml.i> #include <include/dhcp/ping-check.xml.i> <leafNode name="pop-server"> <properties> <help>IP address of POP3 server</help> <valueHelp> <format>ipv4</format> <description>POP3 server IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="server-identifier"> <properties> <help>Address for DHCP server identifier</help> <valueHelp> <format>ipv4</format> <description>DHCP server identifier IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="smtp-server"> <properties> <help>IP address of SMTP server</help> <valueHelp> <format>ipv4</format> <description>SMTP server IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <tagNode name="range"> <properties> <help>DHCP lease range</help> <constraint> #include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i> </constraint> <constraintErrorMessage>Invalid range name, may only be alphanumeric, dot and hyphen</constraintErrorMessage> </properties> <children> <leafNode name="start"> <properties> <help>First IP address for DHCP lease range</help> <valueHelp> <format>ipv4</format> <description>IPv4 start address of pool</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="stop"> <properties> <help>Last IP address for DHCP lease range</help> <valueHelp> <format>ipv4</format> <description>IPv4 end address of pool</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> </children> </tagNode> <tagNode name="static-mapping"> <properties> <help>Name of static mapping</help> <constraint> <regex>[-_a-zA-Z0-9.]+</regex> </constraint> <constraintErrorMessage>Invalid static mapping name, may only be alphanumeric, dot and hyphen</constraintErrorMessage> </properties> <children> #include <include/generic-disable-node.xml.i> <leafNode name="ip-address"> <properties> <help>Fixed IP address of static mapping</help> <valueHelp> <format>ipv4</format> <description>IPv4 address used in static mapping</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="mac-address"> <properties> <help>Media Access Control (MAC) address</help> <valueHelp> <format>macaddr</format> <description>Hardware (MAC) address</description> </valueHelp> <constraint> <validator name="mac-address"/> </constraint> </properties> </leafNode> <leafNode name="static-mapping-parameters"> <properties> <help>Additional static-mapping parameters for DHCP server. Will be placed inside the "host" block of the mapping. You must use the syntax of dhcpd.conf in this text-field. Using this without proper knowledge may result in a crashed DHCP server. Check system log to look for errors.</help> <multi/> </properties> </leafNode> </children> </tagNode> <tagNode name="static-route"> <properties> <help>Classless static route destination subnet</help> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <constraint> <validator name="ipv4-prefix"/> </constraint> </properties> <children> <leafNode name="next-hop"> <properties> <help>IP address of router to be used to reach the destination subnet</help> <valueHelp> <format>ipv4</format> <description>IPv4 address of router</description> </valueHelp> <constraint> <validator name="ip-address"/> </constraint> </properties> </leafNode> </children> </tagNode > <leafNode name="ipv6-only-preferred"> <properties> <help>Disable IPv4 on IPv6 only hosts (RFC 8925)</help> <valueHelp> <format>u32</format> <description>Seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4294967295"/> </constraint> <constraintErrorMessage>Seconds must be between 0 and 4294967295 (49 days)</constraintErrorMessage> </properties> </leafNode> <leafNode name="subnet-parameters"> <properties> <help>Additional subnet parameters for DHCP server. You must use the syntax of dhcpd.conf in this text-field. Using this without proper knowledge may result in a crashed DHCP server. Check system log to look for errors.</help> <multi/> </properties> </leafNode> <leafNode name="tftp-server-name"> <properties> <help>TFTP server name</help> <valueHelp> <format>ipv4</format> <description>TFTP server IPv4 address</description> </valueHelp> <valueHelp> <format>hostname</format> <description>TFTP server FQDN</description> </valueHelp> <constraint> <validator name="ipv4-address"/> <validator name="fqdn"/> </constraint> </properties> </leafNode> <leafNode name="time-offset"> <properties> <help>Client subnet offset in seconds from Coordinated Universal Time (UTC)</help> <valueHelp> <format>[-]N</format> <description>Time offset (number, may be negative)</description> </valueHelp> <constraint> <regex>-?[0-9]+</regex> </constraint> <constraintErrorMessage>Invalid time offset value</constraintErrorMessage> </properties> </leafNode> <leafNode name="time-server"> <properties> <help>IP address of time server</help> <valueHelp> <format>ipv4</format> <description>Time server IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <node name="vendor-option"> <properties> <help>Vendor Specific Options</help> </properties> <children> <node name="ubiquiti"> <properties> <help>Ubiquiti specific parameters</help> </properties> <children> <leafNode name="unifi-controller"> <properties> <help>Address of UniFi controller</help> <valueHelp> <format>ipv4</format> <description>IP address of UniFi controller</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> </children> </node> </children> </node> <leafNode name="wins-server"> <properties> <help>IP address for Windows Internet Name Service (WINS) server</help> <valueHelp> <format>ipv4</format> <description>WINS server IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="wpad-url"> <properties> <help>Web Proxy Autodiscovery (WPAD) URL</help> </properties> </leafNode> </children> </tagNode> </children> </tagNode> </children> </node> </children> </node> </interfaceDefinition> diff --git a/smoketest/scripts/cli/test_service_dhcp-server.py b/smoketest/scripts/cli/test_service_dhcp-server.py index 3d1f47ece..b962cc636 100755 --- a/smoketest/scripts/cli/test_service_dhcp-server.py +++ b/smoketest/scripts/cli/test_service_dhcp-server.py @@ -1,527 +1,578 @@ #!/usr/bin/env python3 # # Copyright (C) 2020-2024 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 unittest from base_vyostest_shim import VyOSUnitTestSHIM from vyos.configsession import ConfigSessionError from vyos.utils.process import process_named_running from vyos.utils.file import read_file from vyos.template import address_from_cidr from vyos.template import inc_ip from vyos.template import dec_ip from vyos.template import netmask_from_cidr PROCESS_NAME = 'dhcpd' DHCPD_CONF = '/run/dhcp-server/dhcpd.conf' base_path = ['service', 'dhcp-server'] subnet = '192.0.2.0/25' router = inc_ip(subnet, 1) dns_1 = inc_ip(subnet, 2) dns_2 = inc_ip(subnet, 3) domain_name = 'vyos.net' class TestServiceDHCPServer(VyOSUnitTestSHIM.TestCase): @classmethod def setUpClass(cls): super(TestServiceDHCPServer, cls).setUpClass() cidr_mask = subnet.split('/')[-1] cls.cli_set(cls, ['interfaces', 'dummy', 'dum8765', 'address', f'{router}/{cidr_mask}']) @classmethod def tearDownClass(cls): cls.cli_delete(cls, ['interfaces', 'dummy', 'dum8765']) super(TestServiceDHCPServer, cls).tearDownClass() def tearDown(self): self.cli_delete(base_path) self.cli_commit() def test_dhcp_single_pool_range(self): shared_net_name = 'SMOKE-1' range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) range_1_start = inc_ip(subnet, 40) range_1_stop = inc_ip(subnet, 50) self.cli_set(base_path + ['dynamic-dns-update']) pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['name-server', dns_1]) self.cli_set(pool + ['name-server', dns_2]) self.cli_set(pool + ['domain-name', domain_name]) self.cli_set(pool + ['ping-check']) # check validate() - No DHCP address range or active static-mapping set with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) self.cli_set(pool + ['range', '1', 'start', range_1_start]) self.cli_set(pool + ['range', '1', 'stop', range_1_stop]) # commit changes self.cli_commit() config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'ddns-update-style interim;', config) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option domain-name-servers {dns_1}, {dns_2};', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'option domain-name "{domain_name}";', config) self.assertIn(f'default-lease-time 86400;', config) self.assertIn(f'max-lease-time 86400;', config) self.assertIn(f'ping-check true;', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) self.assertIn(f'range {range_1_start} {range_1_stop};', config) self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_single_pool_options(self): shared_net_name = 'SMOKE-0815' range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) smtp_server = '1.2.3.4' time_server = '4.3.2.1' tftp_server = 'tftp.vyos.io' search_domains = ['foo.vyos.net', 'bar.vyos.net'] bootfile_name = 'vyos' bootfile_server = '192.0.2.1' wpad = 'http://wpad.vyos.io/foo/bar' server_identifier = bootfile_server ipv6_only_preferred = '300' pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['name-server', dns_1]) self.cli_set(pool + ['name-server', dns_2]) self.cli_set(pool + ['domain-name', domain_name]) self.cli_set(pool + ['ip-forwarding']) self.cli_set(pool + ['smtp-server', smtp_server]) self.cli_set(pool + ['pop-server', smtp_server]) self.cli_set(pool + ['time-server', time_server]) self.cli_set(pool + ['tftp-server-name', tftp_server]) for search in search_domains: self.cli_set(pool + ['domain-search', search]) self.cli_set(pool + ['bootfile-name', bootfile_name]) self.cli_set(pool + ['bootfile-server', bootfile_server]) self.cli_set(pool + ['wpad-url', wpad]) self.cli_set(pool + ['server-identifier', server_identifier]) self.cli_set(pool + ['static-route', '10.0.0.0/24', 'next-hop', '192.0.2.1']) self.cli_set(pool + ['ipv6-only-preferred', ipv6_only_preferred]) # check validate() - No DHCP address range or active static-mapping set with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) # commit changes self.cli_commit() config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'ddns-update-style none;', config) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option domain-name-servers {dns_1}, {dns_2};', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'option domain-name "{domain_name}";', config) search = '"' + ('", "').join(search_domains) + '"' self.assertIn(f'option domain-search {search};', config) self.assertIn(f'option ip-forwarding true;', config) self.assertIn(f'option smtp-server {smtp_server};', config) self.assertIn(f'option pop-server {smtp_server};', config) self.assertIn(f'option time-servers {time_server};', config) self.assertIn(f'option wpad-url "{wpad}";', config) self.assertIn(f'option dhcp-server-identifier {server_identifier};', config) self.assertIn(f'option tftp-server-name "{tftp_server}";', config) self.assertIn(f'option bootfile-name "{bootfile_name}";', config) self.assertIn(f'filename "{bootfile_name}";', config) self.assertIn(f'next-server {bootfile_server};', config) self.assertIn(f'default-lease-time 86400;', config) self.assertIn(f'max-lease-time 86400;', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) self.assertIn(f'option rfc8925-ipv6-only-preferred {ipv6_only_preferred};', config) # weird syntax for those static routes self.assertIn(f'option rfc3442-static-route 24,10,0,0,192,0,2,1, 0,192,0,2,1;', config) self.assertIn(f'option windows-static-route 24,10,0,0,192,0,2,1;', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_single_pool_static_mapping(self): shared_net_name = 'SMOKE-2' domain_name = 'private' pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['name-server', dns_1]) self.cli_set(pool + ['name-server', dns_2]) self.cli_set(pool + ['domain-name', domain_name]) # check validate() - No DHCP address range or active static-mapping set with self.assertRaises(ConfigSessionError): self.cli_commit() client_base = 10 for client in ['client1', 'client2', 'client3']: mac = '00:50:00:00:00:{}'.format(client_base) self.cli_set(pool + ['static-mapping', client, 'mac-address', mac]) self.cli_set(pool + ['static-mapping', client, 'ip-address', inc_ip(subnet, client_base)]) client_base += 1 # cannot have mappings with duplicate IP addresses self.cli_set(pool + ['static-mapping', 'dupe1', 'mac-address', '00:50:00:00:fe:ff']) self.cli_set(pool + ['static-mapping', 'dupe1', 'ip-address', inc_ip(subnet, 10)]) with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_set(pool + ['static-mapping', 'dupe1', 'disable']) self.cli_commit() self.cli_delete(pool + ['static-mapping', 'dupe1']) # cannot have mappings with duplicate MAC addresses self.cli_set(pool + ['static-mapping', 'dupe2', 'mac-address', '00:50:00:00:00:10']) self.cli_set(pool + ['static-mapping', 'dupe2', 'ip-address', inc_ip(subnet, 120)]) with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_delete(pool + ['static-mapping', 'dupe2']) # commit changes self.cli_commit() config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'ddns-update-style none;', config) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option domain-name-servers {dns_1}, {dns_2};', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'option domain-name "{domain_name}";', config) self.assertIn(f'default-lease-time 86400;', config) self.assertIn(f'max-lease-time 86400;', config) client_base = 10 for client in ['client1', 'client2', 'client3']: mac = '00:50:00:00:00:{}'.format(client_base) ip = inc_ip(subnet, client_base) self.assertIn(f'host {shared_net_name}_{client}' + ' {', config) self.assertIn(f'fixed-address {ip};', config) self.assertIn(f'hardware ethernet {mac};', config) client_base += 1 self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_multiple_pools(self): lease_time = '14400' for network in ['0', '1', '2', '3']: shared_net_name = f'VyOS-SMOKETEST-{network}' subnet = f'192.0.{network}.0/24' router = inc_ip(subnet, 1) dns_1 = inc_ip(subnet, 2) range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) range_1_start = inc_ip(subnet, 30) range_1_stop = inc_ip(subnet, 40) pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['name-server', dns_1]) self.cli_set(pool + ['domain-name', domain_name]) self.cli_set(pool + ['lease', lease_time]) self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) self.cli_set(pool + ['range', '1', 'start', range_1_start]) self.cli_set(pool + ['range', '1', 'stop', range_1_stop]) client_base = 60 for client in ['client1', 'client2', 'client3', 'client4']: mac = '02:50:00:00:00:{}'.format(client_base) self.cli_set(pool + ['static-mapping', client, 'mac-address', mac]) self.cli_set(pool + ['static-mapping', client, 'ip-address', inc_ip(subnet, client_base)]) client_base += 1 # commit changes self.cli_commit() config = read_file(DHCPD_CONF) for network in ['0', '1', '2', '3']: shared_net_name = f'VyOS-SMOKETEST-{network}' subnet = f'192.0.{network}.0/24' router = inc_ip(subnet, 1) dns_1 = inc_ip(subnet, 2) range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) range_1_start = inc_ip(subnet, 30) range_1_stop = inc_ip(subnet, 40) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'ddns-update-style none;', config) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option domain-name-servers {dns_1};', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'option domain-name "{domain_name}";', config) self.assertIn(f'default-lease-time {lease_time};', config) self.assertIn(f'max-lease-time {lease_time};', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) self.assertIn(f'range {range_1_start} {range_1_stop};', config) self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) client_base = 60 for client in ['client1', 'client2', 'client3', 'client4']: mac = '02:50:00:00:00:{}'.format(client_base) ip = inc_ip(subnet, client_base) self.assertIn(f'host {shared_net_name}_{client}' + ' {', config) self.assertIn(f'fixed-address {ip};', config) self.assertIn(f'hardware ethernet {mac};', config) client_base += 1 # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_exclude_not_in_range(self): # T3180: verify else path when slicing DHCP ranges and exclude address # is not part of the DHCP range range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) pool = base_path + ['shared-network-name', 'EXCLUDE-TEST', 'subnet', subnet] self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['exclude', router]) self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) # commit changes self.cli_commit() # VErify config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_exclude_in_range(self): # T3180: verify else path when slicing DHCP ranges and exclude address # is not part of the DHCP range range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 100) # the DHCP exclude addresse is blanked out of the range which is done # by slicing one range into two ranges exclude_addr = inc_ip(range_0_start, 20) range_0_stop_excl = dec_ip(exclude_addr, 1) range_0_start_excl = inc_ip(exclude_addr, 1) pool = base_path + ['shared-network-name', 'EXCLUDE-TEST-2', 'subnet', subnet] self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['exclude', exclude_addr]) self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) # commit changes self.cli_commit() # Verify config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'range {range_0_start} {range_0_stop_excl};', config) self.assertIn(f'range {range_0_start_excl} {range_0_stop};', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_relay_server(self): # Listen on specific address and return DHCP leases from a non # directly connected pool self.cli_set(base_path + ['listen-address', router]) relay_subnet = '10.0.0.0/16' relay_router = inc_ip(relay_subnet, 1) range_0_start = '10.0.1.0' range_0_stop = '10.0.250.255' pool = base_path + ['shared-network-name', 'RELAY', 'subnet', relay_subnet] self.cli_set(pool + ['default-router', relay_router]) self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) # commit changes self.cli_commit() config = read_file(DHCPD_CONF) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) # Check the relay network self.assertIn(f'subnet {network} netmask {netmask}' + r' { }', config) relay_network = address_from_cidr(relay_subnet) relay_netmask = netmask_from_cidr(relay_subnet) self.assertIn(f'subnet {relay_network} netmask {relay_netmask}' + r' {', config) self.assertIn(f'option routers {relay_router};', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) def test_dhcp_invalid_raw_options(self): shared_net_name = 'SMOKE-5' range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) self.cli_set(base_path + ['global-parameters', 'this-is-crap']) # check generate() - dhcpd should not acceot this garbage config with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_delete(base_path + ['global-parameters']) # commit changes self.cli_commit() # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) - def test_dhcp_failover(self): + def test_dhcp_high_availability(self): shared_net_name = 'FAILOVER' failover_name = 'VyOS-Failover' range_0_start = inc_ip(subnet, 10) range_0_stop = inc_ip(subnet, 20) pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] # we use the first subnet IP address as default gateway self.cli_set(pool + ['default-router', router]) - - # check validate() - No DHCP address range or active static-mapping set - with self.assertRaises(ConfigSessionError): - self.cli_commit() self.cli_set(pool + ['range', '0', 'start', range_0_start]) self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) # failover failover_local = router failover_remote = inc_ip(router, 1) - self.cli_set(base_path + ['failover', 'source-address', failover_local]) - self.cli_set(base_path + ['failover', 'name', failover_name]) - self.cli_set(base_path + ['failover', 'remote', failover_remote]) - self.cli_set(base_path + ['failover', 'status', 'primary']) + self.cli_set(base_path + ['high-availability', 'source-address', failover_local]) + self.cli_set(base_path + ['high-availability', 'name', failover_name]) + self.cli_set(base_path + ['high-availability', 'remote', failover_remote]) + self.cli_set(base_path + ['high-availability', 'status', 'primary']) # check validate() - failover needs to be enabled for at least one subnet with self.assertRaises(ConfigSessionError): self.cli_commit() self.cli_set(pool + ['enable-failover']) # commit changes self.cli_commit() config = read_file(DHCPD_CONF) self.assertIn(f'failover peer "{failover_name}"' + r' {', config) self.assertIn(f'primary;', config) self.assertIn(f'mclt 1800;', config) self.assertIn(f'mclt 1800;', config) self.assertIn(f'split 128;', config) self.assertIn(f'port 647;', config) self.assertIn(f'peer port 647;', config) self.assertIn(f'max-response-delay 30;', config) self.assertIn(f'max-unacked-updates 10;', config) self.assertIn(f'load balance max seconds 3;', config) self.assertIn(f'address {failover_local};', config) self.assertIn(f'peer address {failover_remote};', config) network = address_from_cidr(subnet) netmask = netmask_from_cidr(subnet) self.assertIn(f'ddns-update-style none;', config) self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) self.assertIn(f'option routers {router};', config) self.assertIn(f'range {range_0_start} {range_0_stop};', config) self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) self.assertIn(f'failover peer "{failover_name}";', config) self.assertIn(f'deny dynamic bootp clients;', config) # Check for running process self.assertTrue(process_named_running(PROCESS_NAME)) + def test_dhcp_high_availability_mode(self): + shared_net_name = 'FAILOVER' + failover_name = 'VyOS-Failover' + + range_0_start = inc_ip(subnet, 10) + range_0_stop = inc_ip(subnet, 20) + + pool = base_path + ['shared-network-name', shared_net_name, 'subnet', subnet] + # we use the first subnet IP address as default gateway + self.cli_set(pool + ['default-router', router]) + self.cli_set(pool + ['range', '0', 'start', range_0_start]) + self.cli_set(pool + ['range', '0', 'stop', range_0_stop]) + + # failover + failover_local = router + failover_remote = inc_ip(router, 1) + + self.cli_set(base_path + ['high-availability', 'source-address', failover_local]) + self.cli_set(base_path + ['high-availability', 'name', failover_name]) + self.cli_set(base_path + ['high-availability', 'remote', failover_remote]) + self.cli_set(base_path + ['high-availability', 'status', 'primary']) + self.cli_set(base_path + ['high-availability', 'mode', 'active-passive']) + self.cli_set(pool + ['enable-failover']) + + # commit changes + self.cli_commit() + + config = read_file(DHCPD_CONF) + + self.assertIn(f'failover peer "{failover_name}"' + r' {', config) + self.assertIn(f'primary;', config) + self.assertIn(f'mclt 1800;', config) + self.assertIn(f'mclt 1800;', config) + self.assertIn(f'split 256;', config) + self.assertIn(f'port 647;', config) + self.assertIn(f'peer port 647;', config) + self.assertIn(f'max-response-delay 30;', config) + self.assertIn(f'max-unacked-updates 10;', config) + self.assertIn(f'load balance max seconds 3;', config) + self.assertIn(f'address {failover_local};', config) + self.assertIn(f'peer address {failover_remote};', config) + + network = address_from_cidr(subnet) + netmask = netmask_from_cidr(subnet) + self.assertIn(f'ddns-update-style none;', config) + self.assertIn(f'subnet {network} netmask {netmask}' + r' {', config) + self.assertIn(f'option routers {router};', config) + self.assertIn(f'range {range_0_start} {range_0_stop};', config) + self.assertIn(f'set shared-networkname = "{shared_net_name}";', config) + self.assertIn(f'failover peer "{failover_name}";', config) + self.assertIn(f'deny dynamic bootp clients;', config) + + # Check for running process + self.assertTrue(process_named_running(PROCESS_NAME)) + def test_dhcp_on_interface_with_vrf(self): self.cli_set(['interfaces', 'ethernet', 'eth1', 'address', '10.1.1.1/30']) self.cli_set(['interfaces', 'ethernet', 'eth1', 'vrf', 'SMOKE-DHCP']) self.cli_set(['protocols', 'static', 'route', '10.1.10.0/24', 'interface', 'eth1', 'vrf', 'SMOKE-DHCP']) self.cli_set(['vrf', 'name', 'SMOKE-DHCP', 'protocols', 'static', 'route', '10.1.10.0/24', 'next-hop', '10.1.1.2']) self.cli_set(['vrf', 'name', 'SMOKE-DHCP', 'table', '1000']) self.cli_set(base_path + ['shared-network-name', 'SMOKE-DHCP-NETWORK', 'subnet', '10.1.10.0/24', 'default-router', '10.1.10.1']) self.cli_set(base_path + ['shared-network-name', 'SMOKE-DHCP-NETWORK', 'subnet', '10.1.10.0/24', 'name-server', '1.1.1.1']) self.cli_set(base_path + ['shared-network-name', 'SMOKE-DHCP-NETWORK', 'subnet', '10.1.10.0/24', 'range', '1', 'start', '10.1.10.10']) self.cli_set(base_path + ['shared-network-name', 'SMOKE-DHCP-NETWORK', 'subnet', '10.1.10.0/24', 'range', '1', 'stop', '10.1.10.20']) self.cli_set(base_path + ['listen-address', '10.1.1.1']) self.cli_commit() config = read_file(DHCPD_CONF) self.assertIn('subnet 10.1.1.0 netmask 255.255.255.252', config) self.cli_delete(['interfaces', 'ethernet', 'eth1', 'vrf', 'SMOKE-DHCP']) self.cli_delete(['protocols', 'static', 'route', '10.1.10.0/24', 'interface', 'eth1', 'vrf']) self.cli_delete(['vrf', 'name', 'SMOKE-DHCP']) self.cli_commit() if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/src/conf_mode/service_dhcp-server.py b/src/conf_mode/service_dhcp-server.py index e910ecdf7..3cfd74a19 100755 --- a/src/conf_mode/service_dhcp-server.py +++ b/src/conf_mode/service_dhcp-server.py @@ -1,349 +1,353 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2024 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 ipaddress import ip_address from ipaddress import ip_network from netaddr import IPAddress from netaddr import IPRange from sys import exit from vyos.base import DeprecationWarning from vyos.config import Config from vyos.template import render from vyos.utils.dict import dict_search from vyos.utils.process import call from vyos.utils.process import run from vyos.utils.network import is_subnet_connected from vyos.utils.network import is_addr_assigned from vyos import ConfigError from vyos import airbag airbag.enable() config_file = '/run/dhcp-server/dhcpd.conf' systemd_override = r'/run/systemd/system/isc-dhcp-server.service.d/10-override.conf' def dhcp_slice_range(exclude_list, range_dict): """ This function is intended to slice a DHCP range. What does it mean? Lets assume we have a DHCP range from '192.0.2.1' to '192.0.2.100' but want to exclude address '192.0.2.74' and '192.0.2.75'. We will pass an input 'range_dict' in the format: {'start' : '192.0.2.1', 'stop' : '192.0.2.100' } and we will receive an output list of: [{'start' : '192.0.2.1' , 'stop' : '192.0.2.73' }, {'start' : '192.0.2.76', 'stop' : '192.0.2.100' }] The resulting list can then be used in turn to build the proper dhcpd configuration file. """ output = [] # exclude list must be sorted for this to work exclude_list = sorted(exclude_list) range_start = range_dict['start'] range_stop = range_dict['stop'] range_last_exclude = '' for e in exclude_list: if (ip_address(e) >= ip_address(range_start)) and \ (ip_address(e) <= ip_address(range_stop)): range_last_exclude = e for e in exclude_list: if (ip_address(e) >= ip_address(range_start)) and \ (ip_address(e) <= ip_address(range_stop)): # Build new address range ending one address before exclude address r = { 'start' : range_start, 'stop' : str(ip_address(e) -1) } # On the next run our address range will start one address after # the exclude address range_start = str(ip_address(e) + 1) # on subsequent exclude addresses we can not # append them to our output if not (ip_address(r['start']) > ip_address(r['stop'])): # Everything is fine, add range to result output.append(r) # Take care of last IP address range spanning from the last exclude # address (+1) to the end of the initial configured range if ip_address(e) == ip_address(range_last_exclude): r = { 'start': str(ip_address(e) + 1), 'stop': str(range_stop) } if not (ip_address(r['start']) > ip_address(r['stop'])): output.append(r) else: # if the excluded address was not part of the range, we simply return # the entire ranga again if not range_last_exclude: if range_dict not in output: output.append(range_dict) return output def get_config(config=None): if config: conf = config else: conf = Config() base = ['service', 'dhcp-server'] if not conf.exists(base): return None dhcp = conf.get_config_dict(base, key_mangling=('-', '_'), no_tag_node_value_mangle=True, get_first_key=True, with_recursive_defaults=True) if 'shared_network_name' in dhcp: for network, network_config in dhcp['shared_network_name'].items(): if 'subnet' in network_config: for subnet, subnet_config in network_config['subnet'].items(): # If exclude IP addresses are defined we need to slice them out of # the defined ranges if {'exclude', 'range'} <= set(subnet_config): new_range_id = 0 new_range_dict = {} for r, r_config in subnet_config['range'].items(): for slice in dhcp_slice_range(subnet_config['exclude'], r_config): new_range_dict.update({new_range_id : slice}) new_range_id +=1 dhcp['shared_network_name'][network]['subnet'][subnet].update( {'range' : new_range_dict}) + if len(dhcp['high_availability']) == 1: + ## only default value for mode is set, need to remove ha node + del dhcp['high_availability'] + return dhcp def verify(dhcp): # bail out early - looks like removal from running config if not dhcp or 'disable' in dhcp: return None # If DHCP is enabled we need one share-network if 'shared_network_name' not in dhcp: raise ConfigError('No DHCP shared networks configured.\n' \ 'At least one DHCP shared network must be configured.') # Inspect shared-network/subnet listen_ok = False subnets = [] failover_ok = False shared_networks = len(dhcp['shared_network_name']) disabled_shared_networks = 0 common_deprecation_msg = 'are subject of removal in VyOS 1.5! Please raise a feature request for proper CLI nodes!' if 'global_parameters' in dhcp: DeprecationWarning(f'Additional global parameters {common_deprecation_msg}') # A shared-network requires a subnet definition for network, network_config in dhcp['shared_network_name'].items(): if 'shared_network_parameters' in network_config: DeprecationWarning(f'Additional shared network parameters in "{network}" {common_deprecation_msg}') if 'disable' in network_config: disabled_shared_networks += 1 if 'subnet' not in network_config: raise ConfigError(f'No subnets defined for {network}. At least one\n' \ 'lease subnet must be configured.') for subnet, subnet_config in network_config['subnet'].items(): if 'subnet_parameters' in subnet_config: DeprecationWarning(f'Additional subnet parameters in "{subnet}" {common_deprecation_msg}') # All delivered static routes require a next-hop to be set if 'static_route' in subnet_config: for route, route_option in subnet_config['static_route'].items(): if 'next_hop' not in route_option: raise ConfigError(f'DHCP static-route "{route}" requires router to be defined!') # DHCP failover needs at least one subnet that uses it if 'enable_failover' in subnet_config: - if 'failover' not in dhcp: - raise ConfigError(f'Can not enable failover for "{subnet}" in "{network}".\n' \ - 'Failover is not configured globally!') + if 'high_availability' not in dhcp: + raise ConfigError(f'Can not enable high availability for "{subnet}" in "{network}".\n' \ + 'High availability is not configured globally!') failover_ok = True # Check if DHCP address range is inside configured subnet declaration if 'range' in subnet_config: networks = [] for range, range_config in subnet_config['range'].items(): if not {'start', 'stop'} <= set(range_config): raise ConfigError(f'DHCP range "{range}" start and stop address must be defined!') # Start/Stop address must be inside network for key in ['start', 'stop']: if ip_address(range_config[key]) not in ip_network(subnet): raise ConfigError(f'DHCP range "{range}" {key} address not within shared-network "{network}, {subnet}"!') # Stop address must be greater or equal to start address if ip_address(range_config['stop']) < ip_address(range_config['start']): raise ConfigError(f'DHCP range "{range}" stop address must be greater or equal\n' \ 'to the ranges start address!') for network in networks: start = range_config['start'] stop = range_config['stop'] if start in network: raise ConfigError(f'Range "{range}" start address "{start}" already part of another range!') if stop in network: raise ConfigError(f'Range "{range}" stop address "{stop}" already part of another range!') tmp = IPRange(range_config['start'], range_config['stop']) networks.append(tmp) # Exclude addresses must be in bound if 'exclude' in subnet_config: for exclude in subnet_config['exclude']: if ip_address(exclude) not in ip_network(subnet): raise ConfigError(f'Excluded IP address "{exclude}" not within shared-network "{network}, {subnet}"!') # At least one DHCP address range or static-mapping required if 'range' not in subnet_config and 'static_mapping' not in subnet_config: raise ConfigError(f'No DHCP address range or active static-mapping configured\n' \ f'within shared-network "{network}, {subnet}"!') if 'static_mapping' in subnet_config: # Static mappings require just a MAC address (will use an IP from the dynamic pool if IP is not set) used_ips = [] used_mac = [] for mapping, mapping_config in subnet_config['static_mapping'].items(): if 'ip_address' in mapping_config: if ip_address(mapping_config['ip_address']) not in ip_network(subnet): raise ConfigError(f'Configured static lease address for mapping "{mapping}" is\n' \ f'not within shared-network "{network}, {subnet}"!') if 'mac_address' not in mapping_config: raise ConfigError(f'MAC address required for static mapping "{mapping}"\n' \ f'within shared-network "{network}, {subnet}"!') if 'disable' not in mapping_config: if mapping_config['ip_address'] in used_ips: raise ConfigError(f'Configured IP address for static mapping "{mapping}" already exists on another static mapping') used_ips.append(mapping_config['ip_address']) if 'mac_address' in mapping_config and 'disable' not in mapping_config: if mapping_config['mac_address'] in used_mac: raise ConfigError(f'Configured MAC address for static mapping "{mapping}" already exists on another static mapping') used_mac.append(mapping_config['mac_address']) # There must be one subnet connected to a listen interface. # This only counts if the network itself is not disabled! if 'disable' not in network_config: if is_subnet_connected(subnet, primary=False): listen_ok = True # Subnets must be non overlapping if subnet in subnets: raise ConfigError(f'Configured subnets must be unique! Subnet "{subnet}"\n' 'defined multiple times!') subnets.append(subnet) # Check for overlapping subnets net = ip_network(subnet) for n in subnets: net2 = ip_network(n) if (net != net2): if net.overlaps(net2): raise ConfigError(f'Conflicting subnet ranges: "{net}" overlaps "{net2}"!') # Prevent 'disable' for shared-network if only one network is configured if (shared_networks - disabled_shared_networks) < 1: raise ConfigError(f'At least one shared network must be active!') - if 'failover' in dhcp: + if 'high_availability' in dhcp: if not failover_ok: raise ConfigError('DHCP failover must be enabled for at least one subnet!') for key in ['name', 'remote', 'source_address', 'status']: - if key not in dhcp['failover']: + if key not in dhcp['high_availability']: tmp = key.replace('_', '-') raise ConfigError(f'DHCP failover requires "{tmp}" to be specified!') for address in (dict_search('listen_address', dhcp) or []): if is_addr_assigned(address, include_vrf=True): listen_ok = True # no need to probe further networks, we have one that is valid continue else: raise ConfigError(f'listen-address "{address}" not configured on any interface') if not listen_ok: raise ConfigError('None of the configured subnets have an appropriate primary IP address on any\n' 'broadcast interface configured, nor was there an explicit listen-address\n' 'configured for serving DHCP relay packets!') return None def generate(dhcp): # bail out early - looks like removal from running config if not dhcp or 'disable' in dhcp: return None # Please see: https://vyos.dev/T1129 for quoting of the raw # parameters we can pass to ISC DHCPd tmp_file = '/tmp/dhcpd.conf' render(tmp_file, 'dhcp-server/dhcpd.conf.j2', dhcp, formater=lambda _: _.replace(""", '"')) # XXX: as we have the ability for a user to pass in "raw" options via VyOS # CLI (see T3544) we now ask ISC dhcpd to test the newly rendered # configuration tmp = run(f'/usr/sbin/dhcpd -4 -q -t -cf {tmp_file}') if tmp > 0: if os.path.exists(tmp_file): os.unlink(tmp_file) raise ConfigError('Configuration file errors encountered - check your options!') # Now that we know that the newly rendered configuration is "good" we can # render the "real" configuration render(config_file, 'dhcp-server/dhcpd.conf.j2', dhcp, formater=lambda _: _.replace(""", '"')) render(systemd_override, 'dhcp-server/10-override.conf.j2', dhcp) # Clean up configuration test file if os.path.exists(tmp_file): os.unlink(tmp_file) return None def apply(dhcp): call('systemctl daemon-reload') # bail out early - looks like removal from running config if not dhcp or 'disable' in dhcp: call('systemctl stop isc-dhcp-server.service') if os.path.exists(config_file): os.unlink(config_file) return None call('systemctl restart isc-dhcp-server.service') 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/migration-scripts/dhcp-server/7-to-8 b/src/migration-scripts/dhcp-server/7-to-8 new file mode 100755 index 000000000..a0dc96ad0 --- /dev/null +++ b/src/migration-scripts/dhcp-server/7-to-8 @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2024 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/>. + +# T6171: rename "service dhcp-server failover" to "service dhcp-server high-availability" + +from sys import argv +from sys import exit + +from vyos.configtree import ConfigTree + +if len(argv) < 2: + print("Must specify file name!") + exit(1) + +file_name = argv[1] + +with open(file_name, 'r') as f: + config_file = f.read() + +base = ['service', 'dhcp-server'] +config = ConfigTree(config_file) + +if not config.exists(base): + # Nothing to do + exit(0) + +if config.exists(base + ['failover']): + config.rename(base + ['failover'],'high-availability') + +try: + with open(file_name, 'w') as f: + f.write(config.to_string()) +except OSError as e: + print(f'Failed to save the modified config: {e}') + exit(1) \ No newline at end of file