From: Benjamin Schubert <contact@benschubert.me>
To: "passt-dev@passt.top" <passt-dev@passt.top>
Subject: [PATCH] netlink: Only consider gateway from the selected template interface
Date: Thu, 03 Dec 2026 15:12:25 +0000 [thread overview]
Message-ID: <2UuogkMNAhFH_0bGAADpm7VgzMi123uydmdNZpZZ6tWdhkxB8y5S4Z4h4Ela1Vyb0QymFQg2Pe03CsvEWjLtdvbeFhbCuBJthILFuMs-JYo=@benschubert.me> (raw)
From: Benjamin Schubert <bschubert15@bloomberg.net>
Previously, netlink could select a gateway from a different interface
if it had higher weights than the one from the selected interface.
This can lead to a namespace having an unreachable default gateway.
This considers only nexthops from the current interface instead of all
interfaces when looking at the template interface, ensuring that the ip
and the gateway are always aligned.
Link: https://bugs.passt.top/show_bug.cgi?id=232
Signed-off-by: Benjamin Schubert <bschubert15@bloomberg.net>
---
netlink.c | 8 +++--
test/pasta_options/multipath_gateway | 46 ++++++++++++++++++++++++++--
2 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/netlink.c b/netlink.c
index 37c55cc..f7bc7ab 100644
--- a/netlink.c
+++ b/netlink.c
@@ -362,26 +362,30 @@ unsigned int nl_get_ext_if(int s, sa_family_t af)
}
/**
* nl_route_get_def_multipath() - Get first highest-weight route from nexthop list
* @rta: Routing netlink attribute with type RTA_MULTIPATH
+ * @ifi: Interface index
* @gw: Default gateway to fill
*
* Return: true if a gateway was found, false otherwise
*/
-static bool nl_route_get_def_multipath(struct rtattr *rta, void *gw)
+static bool nl_route_get_def_multipath(struct rtattr *rta, unsigned int ifi, void *gw)
{
int nh_len = RTA_PAYLOAD(rta);
struct rtnexthop *rtnh;
bool found = false;
int hops = -1;
for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
RTNH_OK(rtnh, nh_len); rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
size_t len = rtnh->rtnh_len - sizeof(*rtnh);
struct rtattr *rta_inner;
+ if (rtnh->rtnh_ifindex != (int)ifi)
+ continue;
+
if (rtnh->rtnh_hops <= hops)
continue;
for (rta_inner = RTNH_DATA(rtnh); RTA_OK(rta_inner, len);
rta_inner = RTA_NEXT(rta_inner, len)) {
@@ -440,11 +444,11 @@ int nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw)
continue;
for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
rta = RTA_NEXT(rta, na)) {
if (rta->rta_type == RTA_MULTIPATH)
- found = nl_route_get_def_multipath(rta, gw);
+ found = nl_route_get_def_multipath(rta, ifi, gw);
if (rta->rta_type != RTA_GATEWAY)
continue;
memcpy(gw, RTA_DATA(rta), RTA_PAYLOAD(rta));
diff --git a/test/pasta_options/multipath_gateway b/test/pasta_options/multipath_gateway
index f7bd45d..f954646 100644
--- a/test/pasta_options/multipath_gateway
+++ b/test/pasta_options/multipath_gateway
@@ -4,42 +4,56 @@
# for qemu/UNIX domain socket mode
#
# PASTA - Pack A Subtle Tap Abstraction
# for network namespace/tap device mode
#
-# test/pasta_options/multipath_gateway - Check gateway weights and ties
+# test/pasta_options/multipath_gateway - Check gateway selection
htools ip unshare sed grep
set OUT __STATEDIR__/multipath-gateway.out
set ERR __STATEDIR__/multipath-gateway.err
def ipv4
set FAMILY 4
set PREFIX 192.0.2.
set ADDRESS 192.0.2.10/24
set LINKLOCAL true
set ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0; __LINKLOCAL__
+set OTHER_INTERFACE_SETUP true
endef
def ipv6
set FAMILY 6
set PREFIX 2001:db8:1::
set ADDRESS 2001:db8:1::10/64
set LINKLOCAL ip -6 addr add fe80::10/64 dev dummy0 nodad
set ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0 nodad; __LINKLOCAL__
+set OTHER_INTERFACE_SETUP true
+endef
+
+def ipv4_other_interface
+ipv4
+set OTHER_PREFIX 192.51.100.
+set OTHER_INTERFACE_SETUP ip link add dummy1 type dummy; ip link set dummy1 up; ip -4 address add __OTHER_PREFIX__10/24 dev dummy1
+endef
+
+def ipv6_other_interface
+ipv6
+set OTHER_PREFIX 2001:db8:2::
+set OTHER_INTERFACE_SETUP ip link add dummy1 type dummy; ip link set dummy1 up; ip -6 address add __OTHER_PREFIX__10/64 dev dummy1 nodad
endef
# Keep each invocation in its own user/network namespace
# Reuse unshare's user namespace instead of creating another with pasta
def check_gateway
set LOG __STATEDIR__/ipv__FAMILY__-__CASE__.log
set LINK_SETUP ip link set lo up; ip link add dummy0 type dummy; ip link set dummy0 up
set ROUTE_SETUP ip -__FAMILY__ route add default __NEXTHOPS__
set ROUTE_SHOW ip -__FAMILY__ route show default
set NETWORK_SHOW ip -__FAMILY__ address show dev dummy0; ip -__FAMILY__ route show table all
-set SETUP __LINK_SETUP__; __ADDRESS_SETUP__; __NETWORK_SHOW__; __ROUTE_SETUP__
+set SETUP __LINK_SETUP__; __ADDRESS_SETUP__; __OTHER_INTERFACE_SETUP__; __NETWORK_SHOW__; __ROUTE_SETUP__
set PASTA ./pasta --netns-only --ipv__FAMILY__-only --debug --interface dummy0
hout STATUS unshare --map-root-user --net -- sh -ec '__SETUP__; __ROUTE_SHOW__; exec __PASTA__ -- true' >'__LOG__' 2>&1; echo \$?
host cat '__LOG__'
check [ '__STATUS__' = '0' ]
hout ACTUAL sed --quiet 's/.*router: *//p' '__LOG__'
@@ -93,5 +107,33 @@ test IPv4: gateway-less nexthop does not mask an equal-weight gateway
set CASE direct
ipv4
set NEXTHOPS nexthop dev dummy0 weight 1 nexthop via __PREFIX__2 dev dummy0 weight 1
set EXPECTED __PREFIX__2
check_gateway
+
+test IPv4: higher-weight gateway on another interface is ignored
+set CASE other-after
+ipv4_other_interface
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __OTHER_PREFIX__1 dev dummy1 weight 3
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv6: higher-weight gateway on another interface is ignored
+set CASE other-after
+ipv6_other_interface
+set NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __OTHER_PREFIX__1 dev dummy1 weight 3
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv4: selected interface can be the second multipath nexthop
+set CASE other-before
+ipv4_other_interface
+set NEXTHOPS nexthop via __OTHER_PREFIX__1 dev dummy1 weight 3 nexthop via __PREFIX__1 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
+
+test IPv6: selected interface can be the second multipath nexthop
+set CASE other-before
+ipv6_other_interface
+set NEXTHOPS nexthop via __OTHER_PREFIX__1 dev dummy1 weight 3 nexthop via __PREFIX__1 dev dummy0 weight 1
+set EXPECTED __PREFIX__1
+check_gateway
--
2.56.1
next reply other threads:[~2026-12-03 15:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 15:12 Benjamin Schubert [this message]
2026-12-03 19:37 ` Jon Maloy
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='2UuogkMNAhFH_0bGAADpm7VgzMi123uydmdNZpZZ6tWdhkxB8y5S4Z4h4Ela1Vyb0QymFQg2Pe03CsvEWjLtdvbeFhbCuBJthILFuMs-JYo=@benschubert.me' \
--to=contact@benschubert.me \
--cc=passt-dev@passt.top \
/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).