diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 2f181d8d9..bc6899e45 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -1,417 +1,448 @@ # Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io> # # 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 <http://www.gnu.org/licenses/>. def _are_same_ip(one, two): from socket import AF_INET from socket import AF_INET6 from socket import inet_pton from vyos.template import is_ipv4 # compare the binary representation of the IP f_one = AF_INET if is_ipv4(one) else AF_INET6 s_two = AF_INET if is_ipv4(two) else AF_INET6 return inet_pton(f_one, one) == inet_pton(f_one, two) def get_protocol_by_name(protocol_name): """Get protocol number by protocol name % get_protocol_by_name('tcp') % 6 """ import socket try: protocol_number = socket.getprotobyname(protocol_name) return protocol_number except socket.error: return protocol_name def interface_exists(interface) -> bool: import os return os.path.exists(f'/sys/class/net/{interface}') def interface_exists_in_netns(interface_name, netns): from vyos.utils.process import rc_cmd rc, out = rc_cmd(f'ip netns exec {netns} ip link show dev {interface_name}') if rc == 0: return True return False def get_vrf_members(vrf: str) -> list: """ Get list of interface VRF members :param vrf: str :return: list """ import json from vyos.utils.process import cmd if not interface_exists(vrf): raise ValueError(f'VRF "{vrf}" does not exist!') output = cmd(f'ip --json --brief link show master {vrf}') answer = json.loads(output) interfaces = [] for data in answer: if 'ifname' in data: interfaces.append(data.get('ifname')) return interfaces def get_interface_vrf(interface): """ Returns VRF of given interface """ from vyos.utils.dict import dict_search from vyos.utils.network import get_interface_config tmp = get_interface_config(interface) if dict_search('linkinfo.info_slave_kind', tmp) == 'vrf': return tmp['master'] return 'default' def get_interface_config(interface): """ Returns the used encapsulation protocol for given interface. If interface does not exist, None is returned. """ import os if not os.path.exists(f'/sys/class/net/{interface}'): return None from json import loads from vyos.utils.process import cmd tmp = loads(cmd(f'ip --detail --json link show dev {interface}'))[0] return tmp def get_interface_address(interface): """ Returns the used encapsulation protocol for given interface. If interface does not exist, None is returned. """ import os if not os.path.exists(f'/sys/class/net/{interface}'): return None from json import loads from vyos.utils.process import cmd tmp = loads(cmd(f'ip --detail --json addr show dev {interface}'))[0] return tmp def get_interface_namespace(iface): """ Returns wich netns the interface belongs to """ from json import loads from vyos.utils.process import cmd # Check if netns exist tmp = loads(cmd(f'ip --json netns ls')) if len(tmp) == 0: return None for ns in tmp: netns = f'{ns["name"]}' # Search interface in each netns data = loads(cmd(f'ip netns exec {netns} ip --json link show')) for tmp in data: if iface == tmp["ifname"]: return netns +def is_ipv6_tentative(iface: str, ipv6_address: str) -> bool: + """Check if IPv6 address is in tentative state. + + This function checks if an IPv6 address on a specific network interface is + in the tentative state. IPv6 tentative addresses are not fully configured + and are undergoing Duplicate Address Detection (DAD) to ensure they are + unique on the network. + + Args: + iface (str): The name of the network interface. + ipv6_address (str): The IPv6 address to check. + + Returns: + bool: True if the IPv6 address is tentative, False otherwise. + """ + import json + from vyos.utils.process import rc_cmd + + rc, out = rc_cmd(f'ip -6 --json address show dev {iface} scope global') + if rc: + return False + + data = json.loads(out) + for addr_info in data[0]['addr_info']: + if ( + addr_info.get('local') == ipv6_address and + addr_info.get('tentative', False) + ): + return True + return False + def is_wwan_connected(interface): """ Determine if a given WWAN interface, e.g. wwan0 is connected to the carrier network or not """ import json from vyos.utils.process import cmd if not interface.startswith('wwan'): raise ValueError(f'Specified interface "{interface}" is not a WWAN interface') # ModemManager is required for connection(s) - if service is not running, # there won't be any connection at all! if not is_systemd_service_active('ModemManager.service'): return False modem = interface.lstrip('wwan') tmp = cmd(f'mmcli --modem {modem} --output-json') tmp = json.loads(tmp) # return True/False if interface is in connected state return dict_search('modem.generic.state', tmp) == 'connected' def get_bridge_fdb(interface): """ Returns the forwarding database entries for a given interface """ import os if not os.path.exists(f'/sys/class/net/{interface}'): return None from json import loads from vyos.utils.process import cmd tmp = loads(cmd(f'bridge -j fdb show dev {interface}')) return tmp def get_all_vrfs(): """ Return a dictionary of all system wide known VRF instances """ from json import loads from vyos.utils.process import cmd tmp = loads(cmd('ip --json vrf list')) # Result is of type [{"name":"red","table":1000},{"name":"blue","table":2000}] # so we will re-arrange it to a more nicer representation: # {'red': {'table': 1000}, 'blue': {'table': 2000}} data = {} for entry in tmp: name = entry.pop('name') data[name] = entry return data def mac2eui64(mac, prefix=None): """ Convert a MAC address to a EUI64 address or, with prefix provided, a full IPv6 address. Thankfully copied from https://gist.github.com/wido/f5e32576bb57b5cc6f934e177a37a0d3 """ import re from ipaddress import ip_network # http://tools.ietf.org/html/rfc4291#section-2.5.1 eui64 = re.sub(r'[.:-]', '', mac).lower() eui64 = eui64[0:6] + 'fffe' + eui64[6:] eui64 = hex(int(eui64[0:2], 16) ^ 2)[2:].zfill(2) + eui64[2:] if prefix is None: return ':'.join(re.findall(r'.{4}', eui64)) else: try: net = ip_network(prefix, strict=False) euil = int('0x{0}'.format(eui64), 16) return str(net[euil]) except: # pylint: disable=bare-except return def check_port_availability(ipaddress, port, protocol): """ Check if port is available and not used by any service Return False if a port is busy or IP address does not exists Should be used carefully for services that can start listening dynamically, because IP address may be dynamic too """ from socketserver import TCPServer, UDPServer from ipaddress import ip_address # verify arguments try: ipaddress = ip_address(ipaddress).compressed except: raise ValueError(f'The {ipaddress} is not a valid IPv4 or IPv6 address') if port not in range(1, 65536): raise ValueError(f'The port number {port} is not in the 1-65535 range') if protocol not in ['tcp', 'udp']: raise ValueError(f'The protocol {protocol} is not supported. Only tcp and udp are allowed') # check port availability try: if protocol == 'tcp': server = TCPServer((ipaddress, port), None, bind_and_activate=True) if protocol == 'udp': server = UDPServer((ipaddress, port), None, bind_and_activate=True) server.server_close() except Exception as e: # errno.h: #define EADDRINUSE 98 /* Address already in use */ if e.errno == 98: return False return True def is_listen_port_bind_service(port: int, service: str) -> bool: """Check if listen port bound to expected program name :param port: Bind port :param service: Program name :return: bool Example: % is_listen_port_bind_service(443, 'nginx') True % is_listen_port_bind_service(443, 'ocserv-main') False """ from psutil import net_connections as connections from psutil import Process as process for connection in connections(): addr = connection.laddr pid = connection.pid pid_name = process(pid).name() pid_port = addr.port if service == pid_name and port == pid_port: return True return False def is_ipv6_link_local(addr): """ Check if addrsss is an IPv6 link-local address. Returns True/False """ from ipaddress import ip_interface from vyos.template import is_ipv6 addr = addr.split('%')[0] if is_ipv6(addr): if ip_interface(addr).is_link_local: return True return False def is_addr_assigned(ip_address, vrf=None) -> bool: """ Verify if the given IPv4/IPv6 address is assigned to any interface """ from netifaces import interfaces from vyos.utils.network import get_interface_config from vyos.utils.dict 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 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 netifaces import ifaddresses from netifaces import AF_INET from netifaces import AF_INET6 # check if the requested address type is configured at all # { # 17: [{'addr': '08:00:27:d9:5b:04', 'broadcast': 'ff:ff:ff:ff:ff:ff'}], # 2: [{'addr': '10.0.2.15', 'netmask': '255.255.255.0', 'broadcast': '10.0.2.255'}], # 10: [{'addr': 'fe80::a00:27ff:fed9:5b04%eth0', 'netmask': 'ffff:ffff:ffff:ffff::'}] # } try: addresses = ifaddresses(intf) except ValueError as e: print(e) 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 # Check every IP address on this interface for a match netmask = None if '/' in address: address, netmask = address.split('/') for ip in addresses.get(addr_type, []): # ip can have the interface name in the 'addr' field, we need to remove it # {'addr': 'fe80::a00:27ff:fec5:f821%eth2', 'netmask': 'ffff:ffff:ffff:ffff::'} ip_addr = ip['addr'].split('%')[0] if not _are_same_ip(address, ip_addr): continue # we do not have a netmask to compare against, they are the same if not netmask: return True prefixlen = '' if is_ipv4(ip_addr): prefixlen = sum([bin(int(_)).count('1') for _ in ip['netmask'].split('.')]) else: prefixlen = sum([bin(int(_,16)).count('1') for _ in ip['netmask'].split('/')[0].split(':') if _]) if str(prefixlen) == netmask: return True return False def is_loopback_addr(addr): """ Check if supplied IPv4/IPv6 address is a loopback address """ from ipaddress import ip_address return ip_address(addr).is_loopback def is_wireguard_key_pair(private_key: str, public_key:str) -> bool: """ Checks if public/private keys are keypair :param private_key: Wireguard private key :type private_key: str :param public_key: Wireguard public key :type public_key: str :return: If public/private keys are keypair returns True else False :rtype: bool """ from vyos.utils.process import cmd gen_public_key = cmd('wg pubkey', input=private_key) if gen_public_key == public_key: return True else: 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, or in other words has a broadcast address configured. ISC DHCP for instance will complain if it should listen on non broadcast interfaces. Return True/False """ from ipaddress import ip_address from ipaddress import ip_network from netifaces import ifaddresses from netifaces import interfaces from netifaces import AF_INET from netifaces import AF_INET6 from vyos.template import is_ipv6 # determine IP version (AF_INET or AF_INET6) depending on passed address addr_type = AF_INET if is_ipv6(subnet): addr_type = AF_INET6 for interface in interfaces(): # check if the requested address type is configured at all if addr_type not in ifaddresses(interface).keys(): continue # An interface can have multiple addresses, but some software components # only support the primary address :( if primary: ip = ifaddresses(interface)[addr_type][0]['addr'] if ip_address(ip) in ip_network(subnet): return True else: # Check every assigned IP address if it is connected to the subnet # in question for ip in ifaddresses(interface)[addr_type]: # remove interface extension (e.g. %eth0) that gets thrown on the end of _some_ addrs addr = ip['addr'].split('%')[0] if ip_address(addr) in ip_network(subnet): return True return False def is_afi_configured(interface, afi): """ Check if given address family is configured, or in other words - an IP address is assigned to the interface. """ from netifaces import ifaddresses from netifaces import AF_INET from netifaces import AF_INET6 if afi not in [AF_INET, AF_INET6]: raise ValueError('Address family must be in [AF_INET, AF_INET6]') try: addresses = ifaddresses(interface) except ValueError as e: print(e) return False return afi in addresses diff --git a/src/conf_mode/high-availability.py b/src/conf_mode/high-availability.py index 626a3757e..0121df11c 100755 --- a/src/conf_mode/high-availability.py +++ b/src/conf_mode/high-availability.py @@ -1,185 +1,201 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2023 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 time + from sys import exit from ipaddress import ip_interface from ipaddress import IPv4Interface from ipaddress import IPv6Interface from vyos.base import Warning from vyos.config import Config from vyos.ifconfig.vrrp import VRRP from vyos.template import render from vyos.template import is_ipv4 from vyos.template import is_ipv6 +from vyos.utils.network import is_ipv6_tentative from vyos.utils.process import call from vyos import ConfigError from vyos import airbag airbag.enable() + def get_config(config=None): if config: conf = config else: conf = Config() base = ['high-availability'] if not conf.exists(base): return None ha = conf.get_config_dict(base, key_mangling=('-', '_'), no_tag_node_value_mangle=True, get_first_key=True, with_defaults=True) ## Get the sync group used for conntrack-sync conntrack_path = ['service', 'conntrack-sync', 'failover-mechanism', 'vrrp', 'sync-group'] if conf.exists(conntrack_path): ha['conntrack_sync_group'] = conf.return_value(conntrack_path) return ha def verify(ha): if not ha or 'disable' in ha: return None used_vrid_if = [] if 'vrrp' in ha and 'group' in ha['vrrp']: for group, group_config in ha['vrrp']['group'].items(): # Check required fields if 'vrid' not in group_config: raise ConfigError(f'VRID is required but not set in VRRP group "{group}"') if 'interface' not in group_config: raise ConfigError(f'Interface is required but not set in VRRP group "{group}"') if 'address' not in group_config: raise ConfigError(f'Virtual IP address is required but not set in VRRP group "{group}"') if 'authentication' in group_config: if not {'password', 'type'} <= set(group_config['authentication']): raise ConfigError(f'Authentication requires both type and passwortd to be set in VRRP group "{group}"') if 'health_check' in group_config: health_check_types = ["script", "ping"] from vyos.utils.dict import check_mutually_exclusive_options try: check_mutually_exclusive_options(group_config["health_check"], health_check_types, required=True) except ValueError: Warning(f'Health check configuration for VRRP group "{group}" will remain unused ' \ f'until it has one of the following options: {health_check_types}') # XXX: health check has default options so we need to remove it # to avoid generating useless config statements in keepalived.conf del group_config["health_check"] # Keepalived doesn't allow mixing IPv4 and IPv6 in one group, so we mirror that restriction # We also need to make sure VRID is not used twice on the same interface with the # same address family. interface = group_config['interface'] vrid = group_config['vrid'] # XXX: filter on map object is destructive, so we force it to list. # Additionally, filter objects always evaluate to True, empty or not, # so we force them to lists as well. vaddrs = list(map(lambda i: ip_interface(i), group_config['address'])) vaddrs4 = list(filter(lambda x: isinstance(x, IPv4Interface), vaddrs)) vaddrs6 = list(filter(lambda x: isinstance(x, IPv6Interface), vaddrs)) if vaddrs4 and vaddrs6: raise ConfigError(f'VRRP group "{group}" mixes IPv4 and IPv6 virtual addresses, this is not allowed.\n' \ 'Create individual groups for IPv4 and IPv6!') if vaddrs4: tmp = {'interface': interface, 'vrid': vrid, 'ipver': 'IPv4'} if tmp in used_vrid_if: raise ConfigError(f'VRID "{vrid}" can only be used once on interface "{interface} with address family IPv4"!') used_vrid_if.append(tmp) if 'hello_source_address' in group_config: if is_ipv6(group_config['hello_source_address']): raise ConfigError(f'VRRP group "{group}" uses IPv4 but hello-source-address is IPv6!') if 'peer_address' in group_config: if is_ipv6(group_config['peer_address']): raise ConfigError(f'VRRP group "{group}" uses IPv4 but peer-address is IPv6!') if vaddrs6: tmp = {'interface': interface, 'vrid': vrid, 'ipver': 'IPv6'} if tmp in used_vrid_if: raise ConfigError(f'VRID "{vrid}" can only be used once on interface "{interface} with address family IPv6"!') used_vrid_if.append(tmp) if 'hello_source_address' in group_config: if is_ipv4(group_config['hello_source_address']): raise ConfigError(f'VRRP group "{group}" uses IPv6 but hello-source-address is IPv4!') if 'peer_address' in group_config: if is_ipv4(group_config['peer_address']): raise ConfigError(f'VRRP group "{group}" uses IPv6 but peer-address is IPv4!') # Check sync groups if 'vrrp' in ha and 'sync_group' in ha['vrrp']: for sync_group, sync_config in ha['vrrp']['sync_group'].items(): if 'member' in sync_config: for member in sync_config['member']: if member not in ha['vrrp']['group']: raise ConfigError(f'VRRP sync-group "{sync_group}" refers to VRRP group "{member}", '\ 'but it does not exist!') # Virtual-server if 'virtual_server' in ha: for vs, vs_config in ha['virtual_server'].items(): if 'address' not in vs_config and 'fwmark' not in vs_config: raise ConfigError('Either address or fwmark is required ' f'but not set for virtual-server "{vs}"') if 'port' not in vs_config and 'fwmark' not in vs_config: raise ConfigError(f'Port or fwmark is required but not set for virtual-server "{vs}"') if 'port' in vs_config and 'fwmark' in vs_config: raise ConfigError(f'Cannot set both port and fwmark for virtual-server "{vs}"') if 'real_server' not in vs_config: raise ConfigError(f'Real-server ip is required but not set for virtual-server "{vs}"') # Real-server for rs, rs_config in vs_config['real_server'].items(): if 'port' not in rs_config: raise ConfigError(f'Port is required but not set for virtual-server "{vs}" real-server "{rs}"') def generate(ha): if not ha or 'disable' in ha: return None render(VRRP.location['config'], 'high-availability/keepalived.conf.j2', ha) return None def apply(ha): service_name = 'keepalived.service' if not ha or 'disable' in ha: call(f'systemctl stop {service_name}') return None + # Check if IPv6 address is tentative T5533 + for group, group_config in ha['vrrp']['group'].items(): + if 'hello_source_address' in group_config: + if is_ipv6(group_config['hello_source_address']): + ipv6_address = group_config['hello_source_address'] + interface = group_config['interface'] + checks = 20 + interval = 0.1 + for _ in range(checks): + if is_ipv6_tentative(interface, ipv6_address): + time.sleep(interval) + call(f'systemctl reload-or-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)