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
This commit is contained in:
Christian Manivong committed 2026-11-02 08:48:39 +01:00
1 parent a1e8ec78c3
commit 9089429c2d
5 files changed
+62 -12

No files matched your search

+3 -1
View File
@@ -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 hop. A typed route now has its real prefix and its type in
`protocol_attributes` (`{"type": "blackhole"}`), with no next hop and no outgoing `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, 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 - `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 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 fixes there (#56). The security-advisory query did not parse under `sudo -n`, so no
+11 -3
View File
@@ -51,6 +51,11 @@ class IpRoute(NamedTuple):
metric: int metric: int
route_type: str = "unicast" 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]: def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``.""" """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) yield from _routes(route, hops, family)
def route_attributes(route_type: str) -> dict[str, str]: def with_length(prefix: str) -> str:
"""NAPALM's ``protocol_attributes`` for a route of *route_type*: its type, unless unicast.""" """*prefix* with a prefix length: a host route without one is ``/32`` or ``/128``
return {} if route_type == "unicast" else {"type": route_type} 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]: def _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]:
+3 -5
View File
@@ -75,7 +75,7 @@ from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
from napalm_linux._hostname import HostnameMixin from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin from napalm_linux._local_accounts import LinuxLocalAccountsMixin
from napalm_linux._ping import parse_ping, ping_command 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._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._sudo_history import LinuxSudoHistoryMixin from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -955,7 +955,7 @@ class LinuxDriver(
if protocol and mapped != protocol.lower(): if protocol and mapped != protocol.lower():
return return
entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network) 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) routes.setdefault(route.network, []).append(entry)
for family, command in IP_ROUTE_COMMANDS.items(): for family, command in IP_ROUTE_COMMANDS.items():
@@ -989,12 +989,10 @@ class LinuxDriver(
if not vm: if not vm:
continue continue
code = vm.group(1) code = vm.group(1)
prefix = vm.group(2) prefix = with_length(vm.group(2))
metric = int(vm.group(4)) metric = int(vm.group(4))
nexthop = vm.group(5) or "" nexthop = vm.group(5) or ""
iface = (vm.group(6) or "").rstrip(",") iface = (vm.group(6) or "").rstrip(",")
if "/" not in prefix:
prefix += "/32"
mapped_proto = _frr_code.get(code, code.lower()) mapped_proto = _frr_code.get(code, code.lower())
if prefix in routes: if prefix in routes:
for entry in routes[prefix]: for entry in routes[prefix]:
+3 -3
View File
@@ -4,7 +4,7 @@
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24, "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24,
"napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12, "napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12,
"napalm_linux/linux.py::LinuxDriver.get_networks": 13, "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_snmp_config": 13,
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28, "napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28,
"napalm_linux/linux.py::LinuxDriver.search_packages": 26 "napalm_linux/linux.py::LinuxDriver.search_packages": 26
@@ -12,7 +12,7 @@
"function_lines": { "function_lines": {
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106, "napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106,
"napalm_linux/linux.py::LinuxDriver.get_networks": 52, "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.get_vpn_tunnels": 152,
"napalm_linux/linux.py::LinuxDriver.search_packages": 106, "napalm_linux/linux.py::LinuxDriver.search_packages": 106,
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51 "napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
@@ -21,7 +21,7 @@
"napalm_linux.linux" "napalm_linux.linux"
], ],
"python_file_lines": { "python_file_lines": {
"napalm_linux/linux.py": 2140 "napalm_linux/linux.py": 2138
}, },
"untyped_public": [] "untyped_public": []
} }
+42
View File
@@ -2487,3 +2487,45 @@ class TestParseIpRoute:
assert list(parse_ip_route(out, "ipv4")) == [ assert list(parse_ip_route(out, "ipv4")) == [
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100) 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