diff options
author | Christian Poessinger <christian@poessinger.com> | 2022-03-26 09:40:44 +0100 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2022-03-26 09:40:44 +0100 |
commit | 2cfbb51731eb24c80bd4d697e370dde4972400a5 (patch) | |
tree | 403edf4dc042059f56ca97485a71bc89cb51de20 /python | |
parent | 772d05156aaa75c904fe340cfce024da00f187f4 (diff) | |
download | vyos-1x-2cfbb51731eb24c80bd4d697e370dde4972400a5.tar.gz vyos-1x-2cfbb51731eb24c80bd4d697e370dde4972400a5.zip |
vyos.validate: T4321: make is_addr_assigned() VRF aware
Commit 1bfe09f9 ("vyos.validate: T4321: make is_intf_addr_assigned() VRF aware")
added VRF support for an interface bound function. As an interface can only be
bound to one VRF check makes less sense.
This commit moves the VRF awareness from is_intf_addr_assigned() to
is_addr_assigned() so we check the VRF assignment even prior of calling
is_intf_addr_assigned() and fail fast.
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/validate.py | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/python/vyos/validate.py b/python/vyos/validate.py index 6dfa1139f..e005da0e4 100644 --- a/python/vyos/validate.py +++ b/python/vyos/validate.py @@ -43,14 +43,13 @@ def _are_same_ip(one, two): s_two = AF_INET if is_ipv4(two) else AF_INET6 return inet_pton(f_one, one) == inet_pton(f_one, two) -def is_intf_addr_assigned(intf, address, vrf=None) -> bool: +def is_intf_addr_assigned(intf, address) -> bool: """ Verify if the given IPv4/IPv6 address is assigned to specific interface. It can check both a single IP address (e.g. 192.0.2.1 or a assigned CIDR address 192.0.2.1/24. """ from vyos.template import is_ipv4 - from vyos.util import get_interface_config from netifaces import ifaddresses from netifaces import AF_INET @@ -68,12 +67,6 @@ def is_intf_addr_assigned(intf, address, vrf=None) -> bool: print(e) return False - # Check if interface belongs to requested VRF. If interfaces does not - # belong to requested VRF - bail out early - tmp = get_interface_config(intf) - if 'master' in tmp and tmp['master'] != vrf: - return False - # determine IP version (AF_INET or AF_INET6) depending on passed address addr_type = AF_INET if is_ipv4(address) else AF_INET6 @@ -104,14 +97,20 @@ def is_intf_addr_assigned(intf, address, vrf=None) -> bool: return False -def is_addr_assigned(addr, vrf=None) -> bool: - """ - Verify if the given IPv4/IPv6 address is assigned to any interface - """ +def is_addr_assigned(ip_address, vrf=None) -> bool: + """ Verify if the given IPv4/IPv6 address is assigned to any interfac """ from netifaces import interfaces - for intf in interfaces(): - tmp = is_intf_addr_assigned(intf, addr, vrf) - if tmp == True: + from vyos.util import get_interface_config + from vyos.util import dict_search + for interface in interfaces(): + # Check if interface belongs to the requested VRF, if this is not the + # case there is no need to proceed with this data set - continue loop + # with next element + tmp = get_interface_config(interface) + if dict_search('master', tmp) != vrf: + continue + + if is_intf_addr_assigned(interface, ip_address): return True return False |