fix(routes): give the IPv6 default route ::/0
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / lowest (pull_request) Successful in 37s
driver / test (3.13) (pull_request) Successful in 1m14s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m20s
CI / driver (pull_request) Successful in 2m59s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / lowest (pull_request) Successful in 37s
driver / test (3.13) (pull_request) Successful in 1m14s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m20s
CI / driver (pull_request) Successful in 2m59s
driver / publish (pull_request) Skipped
get_route_to read "ip -4 route show && ip -6 route show" as one output and mapped "default" to 0.0.0.0/0; the "default6" branch meant for IPv6 never matches, because ip -6 also prints "default". A dual-stack host showed two 0.0.0.0/0 routes, one of them family ipv6. Each family is now read with its own command, so "default" becomes ::/0 for IPv6 and a host route without a prefix length a /128 there instead of a /32. The line parse moves to napalm_linux/_routes.py. Closes #6
This commit is contained in:
1 parent
69f1701ea6
commit
897892b923
5 files changed
+108
-41
No files matched your search
@@ -71,6 +71,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
`seq=` replies and replies from a name (`bytes from host (address)`) now appear in
|
||||
`results`, BusyBox's `round-trip` line fills the RTT fields, and an iputils summary
|
||||
that counts errors is read as well.
|
||||
- `get_route_to()` reports a dual-stack host's IPv6 default route as `::/0` (#6). It
|
||||
came back as a second `0.0.0.0/0` with `family: ipv6`, because `ip -6 route show`
|
||||
calls its default route `default` as well. An IPv6 host route is now a `/128`
|
||||
instead of a `/32`.
|
||||
|
||||
## [0.15.1] - 2026-10-23
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Routes from ``ip route show``, one address family at a time."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Iterator
|
||||
from typing import NamedTuple
|
||||
|
||||
#: The command per family. ``ip -6 route show`` names its default route ``default``
|
||||
#: as well, so only the command a line came from says which family it is (#6).
|
||||
IP_ROUTE_COMMANDS = {"ipv4": "ip -4 route show", "ipv6": "ip -6 route show"}
|
||||
_DEFAULT = {"ipv4": "0.0.0.0/0", "ipv6": "::/0"}
|
||||
_HOST_PREFIX = {"ipv4": "/32", "ipv6": "/128"}
|
||||
|
||||
_VIA = re.compile(r"via\s+(\S+)")
|
||||
_DEV = re.compile(r"dev\s+(\S+)")
|
||||
_PROTO = re.compile(r"proto\s+(\S+)")
|
||||
_METRIC = re.compile(r"metric\s+(\d+)")
|
||||
|
||||
|
||||
class IpRoute(NamedTuple):
|
||||
"""One line of ``ip route show``."""
|
||||
|
||||
network: str
|
||||
proto: str
|
||||
nexthop: str
|
||||
iface: str
|
||||
metric: int
|
||||
|
||||
|
||||
def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
|
||||
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``."""
|
||||
for line in out.splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
dest = line.split(None, 1)[0]
|
||||
network = _DEFAULT[family] if dest == "default" else dest
|
||||
if "/" not in network:
|
||||
network += _HOST_PREFIX[family]
|
||||
yield IpRoute(
|
||||
network,
|
||||
_first(_PROTO, line) or "kernel",
|
||||
_first(_VIA, line),
|
||||
_first(_DEV, line),
|
||||
int(_first(_METRIC, line) or 0),
|
||||
)
|
||||
|
||||
|
||||
def _first(pattern: re.Pattern[str], line: str) -> str:
|
||||
match = pattern.search(line)
|
||||
return match.group(1) if match else ""
|
||||
+4
-38
@@ -75,6 +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
|
||||
from napalm_linux._routes import IP_ROUTE_COMMANDS, parse_ip_route
|
||||
from napalm_linux._shell import expect_for, prepare_shell
|
||||
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
|
||||
|
||||
@@ -957,44 +958,9 @@ class LinuxDriver(
|
||||
_make_entry(mapped, nexthop, iface, metric, network)
|
||||
)
|
||||
|
||||
out = self._send("ip -4 route show && ip -6 route show")
|
||||
for line in out.splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
dest_m = re.match(r"^(\S+)", line)
|
||||
if not dest_m:
|
||||
continue
|
||||
raw_dest = dest_m.group(1)
|
||||
network = (
|
||||
"0.0.0.0/0"
|
||||
if raw_dest == "default"
|
||||
else ("::/0" if raw_dest == "default6" else raw_dest)
|
||||
)
|
||||
if "/" not in network:
|
||||
network += "/32"
|
||||
|
||||
nexthop = ""
|
||||
nh_m = re.search(r"via\s+(\S+)", line)
|
||||
if nh_m:
|
||||
nexthop = nh_m.group(1)
|
||||
|
||||
iface = ""
|
||||
dev_m = re.search(r"dev\s+(\S+)", line)
|
||||
if dev_m:
|
||||
iface = dev_m.group(1)
|
||||
|
||||
proto = "kernel"
|
||||
proto_m = re.search(r"proto\s+(\S+)", line)
|
||||
if proto_m:
|
||||
proto = proto_m.group(1)
|
||||
|
||||
metric = 0
|
||||
metric_m = re.search(r"metric\s+(\d+)", line)
|
||||
if metric_m:
|
||||
metric = int(metric_m.group(1))
|
||||
|
||||
_add(network, proto, nexthop, iface, metric)
|
||||
for family, command in IP_ROUTE_COMMANDS.items():
|
||||
for route in parse_ip_route(self._send(command), family):
|
||||
_add(*route)
|
||||
|
||||
# FRR/Zebra enrichment via vtysh — properly attributes OSPF/BGP/RIP protocols.
|
||||
# FRR installs routes into the kernel as "proto zebra"; vtysh gives the real source.
|
||||
|
||||
@@ -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": 23,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 14,
|
||||
"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": 138,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 103,
|
||||
"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": 2176
|
||||
"napalm_linux/linux.py": 2142
|
||||
},
|
||||
"untyped_public": []
|
||||
}
|
||||
@@ -2240,3 +2240,48 @@ class TestGetProcesses:
|
||||
_mock_send(driver, " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6\n")
|
||||
|
||||
assert driver.get_processes() == []
|
||||
|
||||
|
||||
class TestRouteFamilies:
|
||||
"""``ip -6 route show`` calls its default route ``default`` too (#6)."""
|
||||
|
||||
_V4 = (
|
||||
"default via 192.0.2.1 dev eth0 proto dhcp src 192.0.2.10 metric 100\n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 metric 100\n"
|
||||
)
|
||||
_V6 = (
|
||||
"2001:db8:1::/64 dev eth0 proto ra metric 100 expires 86391sec pref medium\n"
|
||||
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"default via fe80::1 dev eth0 proto ra metric 100 expires 1791sec pref medium\n"
|
||||
)
|
||||
|
||||
def _routes(self, driver, v6=_V6):
|
||||
def send(command, **_kw):
|
||||
out = ""
|
||||
if "ip -4 route" in command:
|
||||
out += self._V4
|
||||
if "ip -6 route" in command:
|
||||
out += v6
|
||||
return out
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_the_ipv6_default_route_is_its_own(self, driver):
|
||||
routes = self._routes(driver)
|
||||
|
||||
assert [r["family"] for r in routes["::/0"]] == ["ipv6"]
|
||||
assert routes["::/0"][0]["next_hop"] == "fe80::1"
|
||||
|
||||
def test_the_ipv4_default_route_stays_ipv4(self, driver):
|
||||
routes = self._routes(driver)
|
||||
|
||||
assert [r["family"] for r in routes["0.0.0.0/0"]] == ["ipv4"]
|
||||
assert routes["0.0.0.0/0"][0]["next_hop"] == "192.0.2.1"
|
||||
|
||||
def test_an_ipv6_host_route_is_a_128(self, driver):
|
||||
routes = self._routes(
|
||||
driver, v6="2001:db8:2::7 via fe80::1 dev eth0 proto static metric 1024\n"
|
||||
)
|
||||
|
||||
assert "2001:db8:2::7/128" in routes
|
||||
Reference in new issue
Block a user