From a30dac7c24549f9ee32a484b74af049607ffd42b Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Wed, 29 Aug 2018 22:40:10 +0200 Subject: vyos package: add IP address validators * is_addr_assigned(addr) - Test if address is assigned to ANY interface on the system * is_ipv4(addr) - Test if it is an IPv4 address, both network and host * is_ipv6(addr) - Test if it is an IPv6 address, both network and host --- python/vyos/validate.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 python/vyos/validate.py (limited to 'python/vyos/validate.py') diff --git a/python/vyos/validate.py b/python/vyos/validate.py new file mode 100644 index 000000000..549812371 --- /dev/null +++ b/python/vyos/validate.py @@ -0,0 +1,63 @@ +# Copyright 2018 VyOS maintainers and contributors +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see . + +import netifaces +import ipaddress + +def is_ipv4(addr): + """ + Check addr if it is an IPv4 address/network. + + Return True/False + """ + if ipaddress.ip_network(addr).version == 4: + return True + else: + return False + +def is_ipv6(addr): + """ + Check addr if it is an IPv6 address/network. + + Return True/False + """ + if ipaddress.ip_network(addr).version == 6: + return True + else: + return False + +def is_addr_assigned(addr): + """ + Verify if the given IPv4/IPv6 address is assigned to any interface on this system + + Return True/False + """ + + # determine IP version (AF_INET or AF_INET6) depending on passed address + addr_type = netifaces.AF_INET + if is_ipv6(addr): + addr_type = netifaces.AF_INET6 + + for interface in netifaces.interfaces(): + # check if the requested address type is configured at all + if addr_type in netifaces.ifaddresses(interface).keys(): + # Check every IP address on this interface for a match + for ip in netifaces.ifaddresses(interface)[addr_type]: + # Check if it matches to the address requested + if ip['addr'] == addr: + return True + + return False + -- cgit v1.2.3 From b8baf2191062b35ce78f87fdd7e9b922f57d7d70 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 30 Aug 2018 22:03:28 +0200 Subject: vyos: package: extend validator by is_subnet_connected() Verify given IPv4/IPv6 subnet is connected to any interface on this system. Required by e.g. DHCP server that we have for IPv4 and IPv6. --- python/vyos/validate.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'python/vyos/validate.py') diff --git a/python/vyos/validate.py b/python/vyos/validate.py index 549812371..b681edc5e 100644 --- a/python/vyos/validate.py +++ b/python/vyos/validate.py @@ -40,7 +40,8 @@ def is_ipv6(addr): def is_addr_assigned(addr): """ - Verify if the given IPv4/IPv6 address is assigned to any interface on this system + Verify if the given IPv4/IPv6 address is assigned to any interface on this + system. Return True/False """ @@ -61,3 +62,38 @@ def is_addr_assigned(addr): return False +def is_subnet_connected(subnet, primary=False): + """ + Verify is the given IPv4/IPv6 subnet is connected to any interface on this + system. + + primary check if the subnet is reachable via the primary IP address of this + interface. E.g. ISC DHCP can only listen on primary addresses. + + Return True/False + """ + + # determine IP version (AF_INET or AF_INET6) depending on passed address + addr_type = netifaces.AF_INET + if is_ipv6(subnet): + addr_type = netifaces.AF_INET6 + + for interface in netifaces.interfaces(): + # check if the requested address type is configured at all + if addr_type not in netifaces.ifaddresses(interface).keys(): + return False + + # An interface can have multiple addresses, but some software components + # only support the primary address :( + if primary: + ip = netifaces.ifaddresses(interface)[addr_type][0]['addr'] + if ipaddress.ip_address(ip) in ipaddress.ip_network(subnet): + return True + else: + # Check every assigned IP address if it is connected to the subnet + # in question + for ip in netifaces.ifaddresses(interface)[addr_type]: + if ipaddress.ip_address(ip['addr']) in ipaddress.ip_network(subnet): + return True + + return False -- cgit v1.2.3