Skip to main content

replace_route_via_dev

Function replace_route_via_dev 

Source
pub async fn replace_route_via_dev(
    dest: IpAddr,
    prefix_len: u8,
    dev_name: &str,
    src: Option<IpAddr>,
) -> Result<(), NetlinkError>
Expand description

Add or replace a route to dest/prefix_len that forwards via the interface named dev_name. Optional src sets the preferred source address.

Replaces the shell-outs: ip route replace <dest>/<prefix_len> dev <dev_name> [src <src>] ip -6 route replace <dest>/<prefix_len> dev <dev_name> [src <src>]

Uses NLM_F_REPLACE | NLM_F_CREATE semantics (via rtnetlink’s .replace() on the route add builder) so stale routes left behind by a previous daemon run don’t cause EEXIST.

The route is installed with link scope (direct-via-dev, no gateway) which is the correct form for a per-container /32 or /128 pointing at a host-side veth endpoint.

dest and src (if provided) must have matching address families — passing a v4 dest with a v6 src returns NetlinkError::Netlink without touching the kernel.

§Errors

Returns NetlinkError::NotFound if dev_name does not exist in the current netns. Returns NetlinkError::Netlink on address family mismatch or any RTNETLINK failure.