|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# Copyright (c) 2018-2019 Red Hat, Inc.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This file is part of nmstate
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
0535c1 |
# it under the terms of the GNU Lesser General Public License as published by
|
|
Packit Service |
0535c1 |
# the Free Software Foundation, either version 2.1 of the License, or
|
|
Packit Service |
0535c1 |
# (at your option) any later version.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This program is distributed in the hope that it will be useful,
|
|
Packit Service |
0535c1 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
0535c1 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
0535c1 |
# GNU Lesser General Public License for more details.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
0535c1 |
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
import ipaddress
|
|
Packit Service |
0535c1 |
from libnmstate.error import NmstateValueError
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
_IPV6_LINK_LOCAL_NETWORK_PREFIXES = ["fe8", "fe9", "fea", "feb"]
|
|
Packit Service |
0535c1 |
_IPV6_LINK_LOCAL_NETWORK_PREFIX_LENGTH = 10
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
KERNEL_MAIN_ROUTE_TABLE_ID = 254
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def is_ipv6_link_local_addr(ip, prefix):
|
|
Packit Service |
0535c1 |
return (
|
|
Packit Service |
0535c1 |
ip[: len(_IPV6_LINK_LOCAL_NETWORK_PREFIXES[0])]
|
|
Packit Service |
0535c1 |
in _IPV6_LINK_LOCAL_NETWORK_PREFIXES
|
|
Packit Service |
0535c1 |
and prefix >= _IPV6_LINK_LOCAL_NETWORK_PREFIX_LENGTH
|
|
Packit Service |
0535c1 |
)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def is_ipv6_address(addr):
|
|
Packit Service |
0535c1 |
return ":" in addr
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def to_ip_address_full(ip, prefix=None):
|
|
Packit Service |
0535c1 |
if prefix:
|
|
Packit Service |
0535c1 |
return f"{ip}/{prefix}"
|
|
Packit Service |
0535c1 |
else:
|
|
Packit Service |
0535c1 |
return to_ip_address_full(*ip_address_full_to_tuple(ip))
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def ip_address_full_to_tuple(addr):
|
|
Packit Service |
0535c1 |
try:
|
|
Packit Service |
0535c1 |
net = ipaddress.ip_network(addr)
|
|
Packit Service |
0535c1 |
except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as err:
|
|
Packit Service |
0535c1 |
raise NmstateValueError(f"Invalid IP address, error: {err}")
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
return f"{net.network_address}", net.prefixlen
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def canonicalize_ip_network(address):
|
|
Packit Service |
0535c1 |
try:
|
|
Packit Service |
0535c1 |
return ipaddress.ip_network(address, strict=False).with_prefixlen
|
|
Packit Service |
0535c1 |
except ValueError as e:
|
|
Packit Service |
0535c1 |
raise NmstateValueError(f"Invalid IP network address: {e}")
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def canonicalize_ip_address(address):
|
|
Packit Service |
0535c1 |
try:
|
|
Packit Service |
0535c1 |
return ipaddress.ip_address(address).compressed
|
|
Packit Service |
0535c1 |
except ValueError as e:
|
|
Packit Service |
0535c1 |
raise NmstateValueError(f"Invalid IP address: {e}")
|