From 9089429c2dab4d5433440b8b090b3f6891999333 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:48:39 +0100 Subject: [PATCH] fix(routes): give an FRR host route without a length /128 for IPv6 The FRR enrichment in get_route_to completed every prefix without a length with /32. An IPv6 host route became a different, much larger /32 prefix, so it missed the kernel's /128 entry for the same host (stored that way since #6) and added a bogus one instead. with_length() in _routes.py completes a prefix by its family, /32 or /128. The protocol attributes move onto IpRoute, which keeps the import in linux.py on one line; get_route_to shrinks and the baseline records it. Closes #64 --- CHANGELOG.md | 4 +++- napalm_linux/_routes.py | 14 ++++++++++--- napalm_linux/linux.py | 8 +++---- tests/quality_baseline.json | 6 +++--- tests/test_linux.py | 42 +++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52a54c5..91e98d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 hop. A typed route now has its real prefix and its type in `protocol_attributes` (`{"type": "blackhole"}`), with no next hop and no outgoing interface, since it sends nothing out. A multipath route has one entry per next hop, - each with its gateway and interface. + 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_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 86ebd4c..02b028d 100644 --- a/napalm_linux/_routes.py +++ b/napalm_linux/_routes.py @@ -51,6 +51,11 @@ class IpRoute(NamedTuple): metric: int route_type: str = "unicast" + @property + def attributes(self) -> dict[str, str]: + """NAPALM's ``protocol_attributes`` for this route: its type, unless unicast.""" + return {} if self.route_type == "unicast" else {"type": self.route_type} + def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]: """The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``.""" @@ -70,9 +75,12 @@ def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]: yield from _routes(route, hops, family) -def route_attributes(route_type: str) -> dict[str, str]: - """NAPALM's ``protocol_attributes`` for a route of *route_type*: its type, unless unicast.""" - return {} if route_type == "unicast" else {"type": route_type} +def with_length(prefix: str) -> str: + """*prefix* with a prefix length: a host route without one is ``/32`` or ``/128`` + by the address's family (#64).""" + if "/" in prefix: + return prefix + return prefix + _HOST_PREFIX["ipv6" if ":" in prefix else "ipv4"] def _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]: diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index cc0e10e..84a08ca 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -75,7 +75,7 @@ from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query from napalm_linux._hostname import HostnameMixin from napalm_linux._local_accounts import LinuxLocalAccountsMixin from napalm_linux._ping import parse_ping, ping_command -from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, route_attributes +from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password from napalm_linux._sudo_history import LinuxSudoHistoryMixin @@ -955,7 +955,7 @@ class LinuxDriver( if protocol and mapped != protocol.lower(): return entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network) - entry["protocol_attributes"] = route_attributes(route.route_type) + entry["protocol_attributes"] = route.attributes routes.setdefault(route.network, []).append(entry) for family, command in IP_ROUTE_COMMANDS.items(): @@ -989,12 +989,10 @@ class LinuxDriver( if not vm: continue code = vm.group(1) - prefix = vm.group(2) + prefix = with_length(vm.group(2)) metric = int(vm.group(4)) nexthop = vm.group(5) or "" iface = (vm.group(6) or "").rstrip(",") - if "/" not in prefix: - prefix += "/32" mapped_proto = _frr_code.get(code, code.lower()) if prefix in routes: for entry in routes[prefix]: diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 5360f07..f19a0ef 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -4,7 +4,7 @@ "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24, "napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12, "napalm_linux/linux.py::LinuxDriver.get_networks": 13, - "napalm_linux/linux.py::LinuxDriver.get_route_to": 14, + "napalm_linux/linux.py::LinuxDriver.get_route_to": 13, "napalm_linux/linux.py::LinuxDriver.get_snmp_config": 13, "napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28, "napalm_linux/linux.py::LinuxDriver.search_packages": 26 @@ -12,7 +12,7 @@ "function_lines": { "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106, "napalm_linux/linux.py::LinuxDriver.get_networks": 52, - "napalm_linux/linux.py::LinuxDriver.get_route_to": 103, + "napalm_linux/linux.py::LinuxDriver.get_route_to": 101, "napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152, "napalm_linux/linux.py::LinuxDriver.search_packages": 106, "napalm_linux/linux.py::LinuxDriver.uninstall_package": 51 @@ -21,7 +21,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2140 + "napalm_linux/linux.py": 2138 }, "untyped_public": [] } diff --git a/tests/test_linux.py b/tests/test_linux.py index 0813af4..ddd9518 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -2487,3 +2487,45 @@ class TestParseIpRoute: assert list(parse_ip_route(out, "ipv4")) == [ IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100) ] + + +class TestFrrHostRoutes: + """The FRR enrichment completes a prefix without a length by its family (#64).""" + + # FRR installs its routes as "proto zebra"; vtysh names the real source. + _IP6 = "2001:db8:2::7 via fe80::1 dev eth0 proto zebra metric 20 pref medium\n" + _VTYSH = ( + "O>* 2001:db8:2::7 [110/20] via fe80::1, eth0, weight 1, 00:01:02\n" + "O>* 2001:db8:3::9 [110/30] via fe80::1, eth0, weight 1, 00:01:02\n" + "O>* 10.10.0.7 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n" + "O>* 10.10.1.0/24 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n" + ) + + @pytest.fixture() + def routes(self, driver): + def send(command, **_kw): + if command.startswith("command -v vtysh"): + return "/usr/bin/vtysh" + if command.startswith("vtysh"): + return self._VTYSH + if "ip -6 route" in command: + return self._IP6 + return "" + + with patch.object(driver, "_send", side_effect=send): + return driver.get_route_to() + + def test_an_ipv6_host_route_is_a_128(self, routes): + assert "2001:db8:3::9/128" in routes + assert not any( + network.endswith("::7/32") or network.endswith("::9/32") for network in routes + ) + + def test_it_meets_the_kernel_route_of_the_same_host(self, routes): + assert [e["protocol"] for e in routes["2001:db8:2::7/128"]] == ["ospf"] + + def test_an_ipv4_host_route_stays_a_32(self, routes): + assert "10.10.0.7/32" in routes + + def test_a_prefix_with_a_length_keeps_it(self, routes): + assert "10.10.1.0/24" in routes