* [PATCH] netlink: Only consider gateway from the selected template interface
@ 2026-12-03 15:12 Benjamin Schubert
2026-12-03 19:37 ` Jon Maloy
0 siblings, 1 reply; 2+ messages in thread
From: Benjamin Schubert @ 2026-12-03 15:12 UTC (permalink / raw)
To: passt-dev
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] netlink: Only consider gateway from the selected template interface
2026-12-03 15:12 [PATCH] netlink: Only consider gateway from the selected template interface Benjamin Schubert
@ 2026-12-03 19:37 ` Jon Maloy
0 siblings, 0 replies; 2+ messages in thread
From: Jon Maloy @ 2026-12-03 19:37 UTC (permalink / raw)
To: Benjamin Schubert, passt-dev
On 2026-12-03 10:12, Benjamin Schubert wrote:
> From: Benjamin Schubert <bschubert15@bloomberg.net>
>
[...]
> 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.
The change looks correct to me. A couple of comments:
- passt.1 still says that, for a multipath default route, "the gateway
is the first nexthop router returned by the kernel which has the
highest weight in the set of paths". With this change, it's the
highest-weight one on the template interface, so I think the man
page should be updated accordingly.
- Alternatively, nl_get_ext_if() could pick the template interface
from the highest-weight nexthop instead of the first one, which
would keep the documented behaviour. I'm fine with either, maybe
Stefano has a preference.
>
> 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)
This line exceeds 80 columns.
> {
> int nh_len = RTA_PAYLOAD(rta);
> struct rtnexthop *rtnh;
[...]
> +def ipv4_other_interface
> +ipv4
> +set OTHER_PREFIX 192.51.100.
I guess you meant 198.51.100. (TEST-NET-2, RFC 5737): 192.51.100.0/24
is regular address space.
> +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
> +
[...]
> +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
Nit: a Fixes: tag for 6c7623d0 ("netlink: Add support to fetch default
gateway from multipath routes") would be nice, as that's where we
started picking the gateway from any nexthop, including ones on
interfaces other than the template interface.
///jon
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 19:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 15:12 [PATCH] netlink: Only consider gateway from the selected template interface Benjamin Schubert
2026-12-03 19:37 ` Jon Maloy
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).