diff options
author | Harald <hjensas@redhat.com> | 2022-02-08 15:49:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 08:49:00 -0600 |
commit | b97a30f0a05c1dea918c46ca9c05c869d15fe2d5 (patch) | |
tree | 95e739f709fd130a8e35885875d048617836f757 /cloudinit/net/sysconfig.py | |
parent | 339c3b0977363afcf160c564cbf446c4093525fb (diff) | |
download | vyos-cloud-init-b97a30f0a05c1dea918c46ca9c05c869d15fe2d5.tar.gz vyos-cloud-init-b97a30f0a05c1dea918c46ca9c05c869d15fe2d5.zip |
Fix IPv6 netmask format for sysconfig (#1215)
This change converts the IPv6 netmask from the network_data.json[1]
format to the CIDR style, <IPv6_addr>/<prefix>.
Using an IPv6 address like ffff:ffff:ffff:ffff:: does not work with
NetworkManager, nor networkscripts.
NetworkManager will ignore the route, logging:
ifcfg-rh: ignoring invalid route at \
"::/:: via fd00:fd00:fd00:2::fffe dev $DEV" \
(/etc/sysconfig/network-scripts/route6-$DEV:3): \
Argument for "::/::" is not ADDR/PREFIX format
Similarly if using networkscripts, ip route fail with error:
Error: inet6 prefix is expected rather than \
"fd00:fd00:fd00::/ffff:ffff:ffff:ffff::".
Also a bit of refactoring ...
cloudinit.net.sysconfig.Route.to_string:
* Move a couple of lines around to reduce repeated code.
* if "ADDRESS" not in key -> continute, so that the
code block following it can be de-indented.
cloudinit.net.network_state:
* Refactors the ipv4_mask_to_net_prefix, ipv6_mask_to_net_prefix
removes mask_to_net_prefix methods. Utilize ipaddress library to
do some of the heavy lifting.
LP: #1959148
Diffstat (limited to 'cloudinit/net/sysconfig.py')
-rw-r--r-- | cloudinit/net/sysconfig.py | 111 |
1 files changed, 52 insertions, 59 deletions
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py index 997907bb..ba85c4f6 100644 --- a/cloudinit/net/sysconfig.py +++ b/cloudinit/net/sysconfig.py @@ -10,6 +10,7 @@ from configobj import ConfigObj from cloudinit import log as logging from cloudinit import subp, util from cloudinit.distros.parsers import networkmanager_conf, resolv_conf +from cloudinit.net import network_state from . import renderer from .network_state import ( @@ -190,69 +191,61 @@ class Route(ConfigMap): # (because Route can contain a mix of IPv4 and IPv6) reindex = -1 for key in sorted(self._conf.keys()): - if "ADDRESS" in key: - index = key.replace("ADDRESS", "") - address_value = str(self._conf[key]) - # only accept combinations: - # if proto ipv6 only display ipv6 routes - # if proto ipv4 only display ipv4 routes - # do not add ipv6 routes if proto is ipv4 - # do not add ipv4 routes if proto is ipv6 - # (this array will contain a mix of ipv4 and ipv6) - if proto == "ipv4" and not self.is_ipv6_route(address_value): - netmask_value = str(self._conf["NETMASK" + index]) - gateway_value = str(self._conf["GATEWAY" + index]) - # increase IPv4 index - reindex = reindex + 1 - buf.write( - "%s=%s\n" - % ( - "ADDRESS" + str(reindex), - _quote_value(address_value), - ) - ) - buf.write( - "%s=%s\n" - % ( - "GATEWAY" + str(reindex), - _quote_value(gateway_value), - ) - ) + if "ADDRESS" not in key: + continue + + index = key.replace("ADDRESS", "") + address_value = str(self._conf[key]) + netmask_value = str(self._conf["NETMASK" + index]) + gateway_value = str(self._conf["GATEWAY" + index]) + + # only accept combinations: + # if proto ipv6 only display ipv6 routes + # if proto ipv4 only display ipv4 routes + # do not add ipv6 routes if proto is ipv4 + # do not add ipv4 routes if proto is ipv6 + # (this array will contain a mix of ipv4 and ipv6) + if proto == "ipv4" and not self.is_ipv6_route(address_value): + # increase IPv4 index + reindex = reindex + 1 + buf.write( + "%s=%s\n" + % ("ADDRESS" + str(reindex), _quote_value(address_value)) + ) + buf.write( + "%s=%s\n" + % ("GATEWAY" + str(reindex), _quote_value(gateway_value)) + ) + buf.write( + "%s=%s\n" + % ("NETMASK" + str(reindex), _quote_value(netmask_value)) + ) + metric_key = "METRIC" + index + if metric_key in self._conf: + metric_value = str(self._conf["METRIC" + index]) buf.write( "%s=%s\n" - % ( - "NETMASK" + str(reindex), - _quote_value(netmask_value), - ) - ) - metric_key = "METRIC" + index - if metric_key in self._conf: - metric_value = str(self._conf["METRIC" + index]) - buf.write( - "%s=%s\n" - % ( - "METRIC" + str(reindex), - _quote_value(metric_value), - ) - ) - elif proto == "ipv6" and self.is_ipv6_route(address_value): - netmask_value = str(self._conf["NETMASK" + index]) - gateway_value = str(self._conf["GATEWAY" + index]) - metric_value = ( - "metric " + str(self._conf["METRIC" + index]) - if "METRIC" + index in self._conf - else "" + % ("METRIC" + str(reindex), _quote_value(metric_value)) ) - buf.write( - "%s/%s via %s %s dev %s\n" - % ( - address_value, - netmask_value, - gateway_value, - metric_value, - self._route_name, - ) + elif proto == "ipv6" and self.is_ipv6_route(address_value): + prefix_value = network_state.ipv6_mask_to_net_prefix( + netmask_value + ) + metric_value = ( + "metric " + str(self._conf["METRIC" + index]) + if "METRIC" + index in self._conf + else "" + ) + buf.write( + "%s/%s via %s %s dev %s\n" + % ( + address_value, + prefix_value, + gateway_value, + metric_value, + self._route_name, ) + ) return buf.getvalue() |