diff --git a/CHANGELOG.md b/CHANGELOG.md index 91e98d7..71e2cb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 each with its gateway and interface. A host route that FRR reports without a prefix length is a `/128` for IPv6 instead of a `/32` (#64), so it meets the kernel's route for the same host and gets FRR's protocol. +- `get_route_to()` reads the next hop of an IPv4 route through an IPv6 gateway (RFC + 8950, as BGP unnumbered sets up), which `ip route` prints as `via inet6 fe80::1` + (#65). The next hop came back as `inet6`; it is now the address, for multipath + routes too. Such a route stays `family: ipv4`: the family is the destination's, + never the next hop's. - `get_available_updates()` works on dnf and yum hosts whose login user has passwordless sudo (NOPASSWD), as netOrk's own account does, and marks the security fixes there (#56). The security-advisory query did not parse under `sudo -n`, so no diff --git a/napalm_linux/_routes.py b/napalm_linux/_routes.py index 02b028d..db609ee 100644 --- a/napalm_linux/_routes.py +++ b/napalm_linux/_routes.py @@ -35,7 +35,9 @@ _NO_FORWARDING = frozenset({"blackhole", "unreachable", "prohibit", "throw"}) #: The line a multipath route's next hops start with, one each, after the route. _NEXTHOP = "nexthop" -_VIA = re.compile(r"via\s+(\S+)") +#: ``via 192.0.2.1``, or with the gateway's family in front: ``via inet6 fe80::1`` for an +#: IPv4 route with an IPv6 next hop (RFC 8950, #65). +_VIA = re.compile(r"via\s+(?:inet6?\s+)?(\S+)") _DEV = re.compile(r"dev\s+(\S+)") _PROTO = re.compile(r"proto\s+(\S+)") _METRIC = re.compile(r"metric\s+(\d+)") diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 84a08ca..ffd2449 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -933,7 +933,7 @@ class LinuxDriver( def _make_entry( proto: str, nexthop: str, iface: str, metric: int, network: str ) -> dict[str, Any]: - family = "ipv6" if (":" in network or (nexthop and ":" in nexthop)) else "ipv4" + family = "ipv6" if ":" in network else "ipv4" # not the next hop's (#65) return { "protocol": proto, "family": family, diff --git a/tests/test_linux.py b/tests/test_linux.py index ddd9518..97a3811 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -2529,3 +2529,46 @@ class TestFrrHostRoutes: def test_a_prefix_with_a_length_keeps_it(self, routes): assert "10.10.1.0/24" in routes + + +#: ``ip -4 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint 22.3), recorded in a +#: network namespace set up for it: IPv4 routes with IPv6 next hops (RFC 8950), single +#: and multipath, as BGP unnumbered installs them (#65). Documentation addresses only. +_IP4_VIA_INET6 = ( + "default via inet6 2001:db8:1::1 dev eth0 \n" + "10.20.0.0/16 proto bgp metric 20 \n" + "\tnexthop via inet6 2001:db8:1::1 dev eth0 weight 1 \n" + "\tnexthop via inet6 2001:db8:2::1 dev eth1 weight 1 \n" + "10.40.0.0/16 via inet6 2001:db8:1::1 dev eth0 proto bgp metric 20 \n" + "192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n" +) + + +class TestIpv6NextHops: + """``via inet6
``: the next hop is the address (#65).""" + + @pytest.fixture() + def routes(self, driver): + def send(command, **_kw): + return _IP4_VIA_INET6 if "ip -4 route" in command else "" + + with patch.object(driver, "_send", side_effect=send): + return driver.get_route_to() + + @pytest.mark.parametrize("network", ["0.0.0.0/0", "10.40.0.0/16"]) + def test_the_next_hop_is_the_address(self, routes, network): + assert [e["next_hop"] for e in routes[network]] == ["2001:db8:1::1"] + + def test_a_multipath_route_has_each_address(self, routes): + hops = [(e["next_hop"], e["outgoing_interface"]) for e in routes["10.20.0.0/16"]] + + assert hops == [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")] + + def test_the_route_stays_ipv4(self, routes): + """The family is the destination's; an IPv6 next hop does not change it.""" + assert {e["family"] for entries in routes.values() for e in entries} == {"ipv4"} + + def test_a_family_word_for_ipv4_is_skipped_too(self): + out = "10.50.0.0/16 via inet 192.0.2.1 dev eth0\n" + + assert [r.nexthop for r in parse_ip_route(out, "ipv4")] == ["192.0.2.1"]