diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-10-08 21:23:57 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2019-10-09 17:19:03 +0200 |
commit | afc82770cce851da31267829f1726f85093d9d76 (patch) | |
tree | 737581797aa07cebad9d3f1cec8eb50b29505ca4 | |
parent | 21fe962befb2ebd1625eb7a6c28cb3e9005fe37e (diff) | |
download | vyos-1x-afc82770cce851da31267829f1726f85093d9d76.tar.gz vyos-1x-afc82770cce851da31267829f1726f85093d9d76.zip |
Python/ifconfig: T1557: add generic support for DHCP client options
-rw-r--r-- | python/vyos/ifconfig.py | 52 | ||||
-rwxr-xr-x | src/conf_mode/interface-bonding.py | 15 | ||||
-rwxr-xr-x | src/conf_mode/interface-bridge.py | 33 | ||||
-rwxr-xr-x | src/conf_mode/interface-ethernet.py | 28 |
4 files changed, 110 insertions, 18 deletions
diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py index 4ac605b54..0f80f4cea 100644 --- a/python/vyos/ifconfig.py +++ b/python/vyos/ifconfig.py @@ -23,13 +23,23 @@ from netifaces import ifaddresses, AF_INET, AF_INET6 from subprocess import Popen, PIPE, STDOUT from time import sleep +dhclient_base = r'/var/lib/dhcp/dhclient_' dhcp_cfg = """ # generated by ifconfig.py option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; +timeout 60; +retry 300; + interface "{{ intf }}" { send host-name "{{ hostname }}"; - request subnet-mask, broadcast-address, routers, domain-name-servers, rfc3442-classless-static-routes, domain-name, interface-mtu; + {% if client_id -%} + send dhcp-client-identifier "{{ client_id }}"; + {% endif -%} + request subnet-mask, broadcast-address, routers, domain-name-servers, + rfc3442-classless-static-routes, domain-name, interface-mtu; + require subnet-mask; } + """ dhcpv6_cfg = """ @@ -37,10 +47,8 @@ dhcpv6_cfg = """ interface "{{ intf }}" { request routers, domain-name-servers, domain-name; } -""" - -dhclient_base = r'/var/lib/dhcp/dhclient_' +""" class Interface: def __init__(self, ifname, type=None): @@ -78,6 +86,13 @@ class Interface: self._dhcpv6_pid_file = dhclient_base + self._ifname + '.v6pid' self._dhcpv6_lease_file = dhclient_base + self._ifname + '.v6leases' + # DHCP options + self._dhcp_options = { + 'intf' : self._ifname, + 'hostname' : '', + 'client_id' : '', + } + def _debug_msg(self, msg): if os.path.isfile('/tmp/vyos.ifconfig.debug'): print('DEBUG/{:<6} {}'.format(self._ifname, msg)) @@ -449,6 +464,21 @@ class Interface: cmd = 'ip addr del "{}" dev "{}"'.format(addr, self._ifname) return self._cmd(cmd) + + def get_dhcp_options(self): + """ + Return dictionary with supported DHCP options. + Dictionary should be altered and send back via set_dhcp_options() + so those options are applied when DHCP is run. + """ + return self._dhcp_options + + def set_dhcp_options(self, options): + """ + Store new DHCP options used by next run of DHCP client. + """ + self._dhcp_options = options + # replace dhcpv4/v6 with systemd.networkd? def _set_dhcp(self): """ @@ -461,15 +491,13 @@ class Interface: >>> j = Interface('eth0') >>> j.set_dhcp() """ - dhcp = { - 'hostname': 'vyos', - 'intf': self._ifname - } - # read configured system hostname. - # maybe change to vyos hostd client ??? - with open('/etc/hostname', 'r') as f: - dhcp['hostname'] = f.read().rstrip('\n') + dhcp = self.get_dhcp_options() + if not dhcp['hostname']: + # read configured system hostname. + # maybe change to vyos hostd client ??? + with open('/etc/hostname', 'r') as f: + dhcp['hostname'] = f.read().rstrip('\n') # render DHCP configuration tmpl = jinja2.Template(dhcp_cfg) diff --git a/src/conf_mode/interface-bonding.py b/src/conf_mode/interface-bonding.py index 4d5009c73..7caf9e0e8 100755 --- a/src/conf_mode/interface-bonding.py +++ b/src/conf_mode/interface-bonding.py @@ -368,9 +368,18 @@ def apply(bond): # update interface description used e.g. within SNMP b.set_alias(bond['description']) - # - # missing DHCP/DHCPv6 options go here - # + # get DHCP config dictionary and update values + opt = b.get_dhcp_options() + + if bond['dhcp_client_id']: + opt['client_id'] = bond['dhcp_client_id'] + + if bond['dhcp_hostname']: + opt['hostname'] = bond['dhcp_hostname'] + + # store DHCP config dictionary - used later on when addresses + # are requested + b.set_dhcp_options(opt) # ignore link state changes b.set_link_detect(bond['disable_link_detect']) diff --git a/src/conf_mode/interface-bridge.py b/src/conf_mode/interface-bridge.py index 37b5c4979..b2755547c 100755 --- a/src/conf_mode/interface-bridge.py +++ b/src/conf_mode/interface-bridge.py @@ -32,6 +32,10 @@ default_config_data = { 'arp_cache_tmo': 30, 'description': '', 'deleted': False, + 'dhcp_client_id': '', + 'dhcp_hostname': '', + 'dhcpv6_prm_only': False, + 'dhcpv6_temporary': False, 'disable': False, 'disable_link_detect': 1, 'forwarding_delay': 14, @@ -81,6 +85,22 @@ def get_config(): if conf.exists('description'): bridge['description'] = conf.return_value('description') + # get DHCP client identifier + if conf.exists('dhcp-options client-id'): + bridge['dhcp_client_id'] = conf.return_value('dhcp-options client-id') + + # DHCP client host name (overrides the system host name) + if conf.exists('dhcp-options host-name'): + bridge['dhcp_hostname'] = conf.return_value('dhcp-options host-name') + + # DHCPv6 only acquire config parameters, no address + if conf.exists('dhcpv6-options parameters-only'): + bridge['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only') + + # DHCPv6 temporary IPv6 address + if conf.exists('dhcpv6-options temporary'): + bridge['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary') + # Disable this bridge interface if conf.exists('disable'): bridge['disable'] = True @@ -203,6 +223,19 @@ def apply(bridge): # update interface description used e.g. within SNMP br.set_alias(bridge['description']) + # get DHCP config dictionary and update values + opt = br.get_dhcp_options() + + if bridge['dhcp_client_id']: + opt['client_id'] = bridge['dhcp_client_id'] + + if bridge['dhcp_hostname']: + opt['hostname'] = bridge['dhcp_hostname'] + + # store DHCP config dictionary - used later on when addresses + # are requested + br.set_dhcp_options(opt) + # Change interface MAC address if bridge['mac']: br.set_mac(bridge['mac']) diff --git a/src/conf_mode/interface-ethernet.py b/src/conf_mode/interface-ethernet.py index 317da5772..5f9b4d8b0 100755 --- a/src/conf_mode/interface-ethernet.py +++ b/src/conf_mode/interface-ethernet.py @@ -66,6 +66,19 @@ def apply_vlan_config(vlan, config): if type(vlan) != type(VLANIf("lo")): raise TypeError() + # get DHCP config dictionary and update values + opt = vlan.get_dhcp_options() + + if config['dhcp_client_id']: + opt['client_id'] = config['dhcp_client_id'] + + if config['dhcp_hostname']: + opt['hostname'] = config['dhcp_hostname'] + + # store DHCP config dictionary - used later on when addresses + # are requested + vlan.set_dhcp_options(opt) + # update interface description used e.g. within SNMP vlan.set_alias(config['description']) # ignore link state changes @@ -273,9 +286,18 @@ def apply(eth): # update interface description used e.g. within SNMP e.set_alias(eth['description']) - # - # missing DHCP/DHCPv6 options go here - # + # get DHCP config dictionary and update values + opt = e.get_dhcp_options() + + if eth['dhcp_client_id']: + opt['client_id'] = eth['dhcp_client_id'] + + if eth['dhcp_hostname']: + opt['hostname'] = eth['dhcp_hostname'] + + # store DHCP config dictionary - used later on when addresses + # are requested + e.set_dhcp_options(opt) # ignore link state changes e.set_link_detect(eth['disable_link_detect']) |