From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, david@gibson.dropbear.id.au,
jmaloy@redhat.com, passt-dev@passt.top
Subject: [PATCH v2] netlink: Set default route as on-link
Date: Mon, 23 Nov 2026 23:13:50 -0500 [thread overview]
Message-ID: <20261124041350.3347277-1-jmaloy@redhat.com> (raw)
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 <jmaloy@redhat.com>
---
v2: - Set RTNH_F_ONLINK for IPv6 as well, as -g with a gateway outside
the prefix given with -a fails in the same way (Stefano)
- Reworded commit message: the issue isn't specific to local mode
or to 169.254.2.2, but affects any case where we set the default
route instead of copying routes (Stefano)
- Describe this as a usability improvement, and drop the Fixes:
tag, as the previous behaviour was intended (Stefano)
- Updated code comment accordingly
---
netlink.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/netlink.c b/netlink.c
index 225b666a..a0f8783b 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);
}
--
2.56.1
reply other threads:[~2026-11-24 4:14 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124041350.3347277-1-jmaloy@redhat.com \
--to=jmaloy@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).