No files matched your search
@@ -45,6 +45,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||
|
||||
### Fixed
|
||||
- `get_route_to()` reads unreachable, blackhole, prohibit and throw routes and
|
||||
multipath routes (#57). Their destinations came back as `unreachable/32`,
|
||||
`blackhole/32` or `nexthop/32`, and a multipath route as one entry without a next
|
||||
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.
|
||||
- `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
|
||||
|
||||
+56
-12
@@ -12,6 +12,29 @@ 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"}
|
||||
|
||||
#: The route types ``ip route`` prints in front of the destination; a unicast route
|
||||
#: carries none (#57).
|
||||
_ROUTE_TYPES = frozenset(
|
||||
{
|
||||
"unicast",
|
||||
"local",
|
||||
"broadcast",
|
||||
"anycast",
|
||||
"multicast",
|
||||
"blackhole",
|
||||
"unreachable",
|
||||
"prohibit",
|
||||
"throw",
|
||||
"nat",
|
||||
"xresolve",
|
||||
}
|
||||
)
|
||||
#: Route types that send nothing out of an interface: no next hop, and no outgoing
|
||||
#: interface either, though IPv6 names ``lo`` for them.
|
||||
_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+)")
|
||||
_DEV = re.compile(r"dev\s+(\S+)")
|
||||
_PROTO = re.compile(r"proto\s+(\S+)")
|
||||
@@ -19,32 +42,53 @@ _METRIC = re.compile(r"metric\s+(\d+)")
|
||||
|
||||
|
||||
class IpRoute(NamedTuple):
|
||||
"""One line of ``ip route show``."""
|
||||
"""One route of ``ip route show``; a multipath route is one per next hop."""
|
||||
|
||||
network: str
|
||||
proto: str
|
||||
nexthop: str
|
||||
iface: str
|
||||
metric: int
|
||||
route_type: str = "unicast"
|
||||
|
||||
|
||||
def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
|
||||
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``."""
|
||||
route = ""
|
||||
hops: list[str] = []
|
||||
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),
|
||||
)
|
||||
if line.split(None, 1)[0] == _NEXTHOP:
|
||||
hops.append(line)
|
||||
continue
|
||||
if route:
|
||||
yield from _routes(route, hops, family)
|
||||
route, hops = line, []
|
||||
if route:
|
||||
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 _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]:
|
||||
"""The route *line* with its multipath next-hop lines *hops*: one per next hop."""
|
||||
words = line.split()
|
||||
route_type = words.pop(0) if words[0] in _ROUTE_TYPES and len(words) > 1 else "unicast"
|
||||
network = _DEFAULT[family] if words[0] == "default" else words[0]
|
||||
if "/" not in network:
|
||||
network += _HOST_PREFIX[family]
|
||||
proto = _first(_PROTO, line) or "kernel"
|
||||
metric = int(_first(_METRIC, line) or 0)
|
||||
if route_type in _NO_FORWARDING:
|
||||
yield IpRoute(network, proto, "", "", metric, route_type)
|
||||
return
|
||||
for hop in hops or [line]:
|
||||
yield IpRoute(network, proto, _first(_VIA, hop), _first(_DEV, hop), metric, route_type)
|
||||
|
||||
|
||||
def _first(pattern: re.Pattern[str], line: str) -> str:
|
||||
|
||||
@@ -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, parse_ip_route
|
||||
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, route_attributes
|
||||
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
|
||||
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
|
||||
|
||||
@@ -948,19 +948,19 @@ class LinuxDriver(
|
||||
"protocol_attributes": {},
|
||||
}
|
||||
|
||||
def _add(network: str, proto: str, nexthop: str, iface: str, metric: int) -> None:
|
||||
if destination and network != destination:
|
||||
def _add(route: IpRoute) -> None:
|
||||
if destination and route.network != destination:
|
||||
return
|
||||
mapped = proto_map.get(proto, proto)
|
||||
mapped = proto_map.get(route.proto, route.proto)
|
||||
if protocol and mapped != protocol.lower():
|
||||
return
|
||||
routes.setdefault(network, []).append(
|
||||
_make_entry(mapped, nexthop, iface, metric, network)
|
||||
)
|
||||
entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network)
|
||||
entry["protocol_attributes"] = route_attributes(route.route_type)
|
||||
routes.setdefault(route.network, []).append(entry)
|
||||
|
||||
for family, command in IP_ROUTE_COMMANDS.items():
|
||||
for route in parse_ip_route(self._send(command), family):
|
||||
_add(*route)
|
||||
_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.
|
||||
|
||||
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_linux._routes import IpRoute, parse_ip_route
|
||||
from napalm_linux.linux import (
|
||||
_YUM_SECURITY_COMMAND,
|
||||
LinuxDriver,
|
||||
@@ -2341,3 +2342,136 @@ class TestRouteFamilies:
|
||||
)
|
||||
|
||||
assert "2001:db8:2::7/128" in routes
|
||||
|
||||
|
||||
#: ``ip -4 route show`` / ``ip -6 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint
|
||||
#: 22.3), recorded in a network namespace set up for it: a multipath default per
|
||||
#: family, and unreachable, prohibit, throw and blackhole routes (#57). Documentation
|
||||
#: addresses only. The trailing blanks and the tabs are ip's.
|
||||
_IP4_ROUTE_TYPES = (
|
||||
"default proto static metric 100 \n"
|
||||
"\tnexthop via 192.0.2.1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 198.51.100.1 dev eth1 weight 1 \n"
|
||||
"unreachable 10.0.0.0/8 metric 1024 \n"
|
||||
"prohibit 172.16.0.0/12 \n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||
"throw 192.168.0.0/16 \n"
|
||||
"198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.10 \n"
|
||||
"blackhole 203.0.113.0/24 proto static \n"
|
||||
"203.0.113.7 via 192.0.2.1 dev eth0 \n"
|
||||
)
|
||||
_IP6_ROUTE_TYPES = (
|
||||
"2001:db8:1::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"2001:db8:2::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"blackhole 2001:db8:beef::/48 dev lo metric 1024 pref medium\n"
|
||||
"unreachable 2001:db8:dead::/48 dev lo metric 1024 pref medium\n"
|
||||
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"fe80::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"default proto static metric 1024 pref medium\n"
|
||||
"\tnexthop via 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||
)
|
||||
|
||||
|
||||
class TestRouteTypesAndMultipath:
|
||||
"""A route type in front of the destination is the type, and a multipath
|
||||
route's ``nexthop`` lines are its next hops (#57)."""
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
if "ip -4 route" in command:
|
||||
return _IP4_ROUTE_TYPES
|
||||
if "ip -6 route" in command:
|
||||
return _IP6_ROUTE_TYPES
|
||||
return ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_every_destination_is_an_address(self, routes):
|
||||
assert sorted(routes) == sorted(
|
||||
[
|
||||
"0.0.0.0/0",
|
||||
"10.0.0.0/8",
|
||||
"172.16.0.0/12",
|
||||
"192.0.2.0/24",
|
||||
"192.168.0.0/16",
|
||||
"198.51.100.0/24",
|
||||
"203.0.113.0/24",
|
||||
"203.0.113.7/32",
|
||||
"2001:db8:1::/64",
|
||||
"2001:db8:2::/64",
|
||||
"2001:db8:beef::/48",
|
||||
"2001:db8:dead::/48",
|
||||
"fe80::/64",
|
||||
"::/0",
|
||||
]
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "route_type", "metric"),
|
||||
[
|
||||
("10.0.0.0/8", "unreachable", 1024),
|
||||
("172.16.0.0/12", "prohibit", 0),
|
||||
("192.168.0.0/16", "throw", 0),
|
||||
("203.0.113.0/24", "blackhole", 0),
|
||||
("2001:db8:beef::/48", "blackhole", 1024),
|
||||
("2001:db8:dead::/48", "unreachable", 1024),
|
||||
],
|
||||
)
|
||||
def test_a_route_that_forwards_nothing_says_its_type(self, routes, network, route_type, metric):
|
||||
(entry,) = routes[network]
|
||||
|
||||
assert entry["protocol_attributes"] == {"type": route_type}
|
||||
assert entry["preference"] == metric
|
||||
# Nothing leaves through an interface; IPv6 names lo for it.
|
||||
assert (entry["next_hop"], entry["outgoing_interface"]) == ("", "")
|
||||
|
||||
def test_the_rest_of_a_typed_line_is_read_as_before(self, routes):
|
||||
assert routes["203.0.113.0/24"][0]["protocol"] == "static"
|
||||
|
||||
def test_a_unicast_route_has_no_type(self, routes):
|
||||
assert routes["192.0.2.0/24"][0]["protocol_attributes"] == {}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "hops"),
|
||||
[
|
||||
("0.0.0.0/0", [("192.0.2.1", "eth0"), ("198.51.100.1", "eth1")]),
|
||||
("::/0", [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]),
|
||||
],
|
||||
)
|
||||
def test_a_multipath_route_has_one_entry_per_next_hop(self, routes, network, hops):
|
||||
entries = routes[network]
|
||||
|
||||
assert [(e["next_hop"], e["outgoing_interface"]) for e in entries] == hops
|
||||
assert {(e["protocol"], e["preference"]) for e in entries} == {
|
||||
("static", 100 if network == "0.0.0.0/0" else 1024)
|
||||
}
|
||||
|
||||
def test_no_next_hop_line_is_a_route_of_its_own(self, routes):
|
||||
assert not any(network.startswith("nexthop") for network in routes)
|
||||
|
||||
|
||||
class TestParseIpRoute:
|
||||
"""``parse_ip_route`` line by line (#57)."""
|
||||
|
||||
def test_a_typed_route_that_forwards_keeps_its_interface(self):
|
||||
# ``ip route show table local``: a local route names the interface it is on.
|
||||
line = "local 192.0.2.10 dev eth0 proto kernel scope host src 192.0.2.10\n"
|
||||
|
||||
assert list(parse_ip_route(line, "ipv4")) == [
|
||||
IpRoute("192.0.2.10/32", "kernel", "", "eth0", 0, "local")
|
||||
]
|
||||
|
||||
def test_a_next_hop_line_without_a_route_is_dropped(self):
|
||||
out = "\tnexthop via 192.0.2.1 dev eth0 weight 1\n192.0.2.0/24 dev eth0 proto kernel\n"
|
||||
|
||||
assert [r.network for r in parse_ip_route(out, "ipv4")] == ["192.0.2.0/24"]
|
||||
|
||||
def test_a_route_without_next_hop_lines_is_one_route(self):
|
||||
out = "default via 192.0.2.1 dev eth0 proto dhcp metric 100\n"
|
||||
|
||||
assert list(parse_ip_route(out, "ipv4")) == [
|
||||
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100)
|
||||
]
|
||||
Reference in new issue
Block a user