From 2b9de43e51ac789dce5872d5cfdba05376dad00c Mon Sep 17 00:00:00 2001 From: Jon Maloy Date: Mon, 23 Nov 2026 23:13:50 -0500 Subject: netlink: Set default route as on-link Commit da54641f140e ("conf, pasta: With --config-net, copy all routes by default") made it possible to use a default gateway that's not on the same subnet as any address assigned to the namespace interface, as copying all routes from the template interface also copies the device route for the gateway. If pasta sets the default route itself instead, that is, with -g or --no-copy-routes, or in local mode, there's no such device route, and the kernel refuses a gateway that's not covered by any assigned prefix: $ pasta -Ix -- sh -c 'ip link set dev x up; \ ip addr add 10.0.0.2/24 dev x; \ ip route add default via 10.0.0.1 dev x; \ pasta -4 --config-net -a 192.0.2.1 -g 10.0.0.1 -- ip route' Couldn't set IPv4 route(s) in guest: Network is unreachable $ pasta -6 -Ix -- sh -c 'ip link set dev x up; \ ip addr add 2001:db8:1::2/64 dev x nodad; \ ip -6 route add default via 2001:db8:1::1 dev x; \ pasta -6 --config-net -a 2001:db8:2::1 -g 2001:db8:1::1 -- \ ip -6 route' Couldn't set IPv6 route(s) in guest: No route to host The same happens in local mode for IPv4, where the default gateway is 169.254.2.2, with -a and any address outside 169.254.0.0/16. In local mode for IPv6, the default gateway is the link-local address fe80::1, which is always covered by the fe80::/64 device route. We reply to ARP requests and neighbour solicitations for any address other than the guest's own, so the gateway is in fact always reachable on the link. Flag the default route with RTNH_F_ONLINK, so that the kernel doesn't require the gateway to be within an assigned prefix. This doesn't hide anything from the user, and makes these configurations usable. With this change: default via 10.0.0.1 dev x onlink 192.0.2.0/24 dev x proto kernel scope link src 192.0.2.1 and: default via 2001:db8:1::1 dev x metric 1024 onlink pref medium Note that RTNH_F_ONLINK is supported for IPv6 routes since Linux 4.16. Signed-off-by: Jon Maloy Signed-off-by: Stefano Brivio --- netlink.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/netlink.c b/netlink.c index 225b666..a0f8783 100644 --- a/netlink.c +++ b/netlink.c @@ -522,6 +522,14 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw) req.set.r4.rta_gw.rta_len = rta_len; } + /* The gateway might not be covered by any assigned prefix, e.g. with + * -g, or in local mode with a user-provided IPv4 address, as we don't + * copy the device route for it in these cases. We answer ARP requests + * and neighbour solicitations for any address other than the guest's, + * so it's reachable anyway: treat it as on-link + */ + req.rtm.rtm_flags = RTNH_F_ONLINK; + return nl_do(s, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len); } -- cgit v1.2.3