From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=fail (p=none dis=none) header.from=benschubert.me Received: from mail-106116.protonmail.ch (mail-106116.protonmail.ch [79.135.106.116]) by passt.top (Postfix) with ESMTPS id 754755A026E for ; Tue, 01 Dec 2026 15:08:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benschubert.me; s=protonmail3; t=1796134084; x=1796393284; bh=OPcp8YnHkJqywQxzPqTovdarcquH3ZzPkGyIUnzgbKU=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=S5r2QZP6Mq14ks+MCehkKEYnzozITIPDUcM2VArKULmRUYS7JZx3rI3skWwyZtQWw CXd/lYKnUG9c+pLSmwMQCq8DQLjOG4qnSKWviHGb2FM3a0IB9eV9TcELYt9bX+pzHL pEaWFORwg95i0eFneFLEEfw/WO8YImtPUsjpPGklctmPfL+g9S+iJUUcDyMyo7Gep5 X//4amespZm37/MNCnZ+gJjTZ3r9Y23m1OcVsJuYDiQAf/PHq/BCo1XiOhA87o+BZy L7o1kOrZQYZltORvHxEpCQX0V9IBuSNhCLizJfvBUMopyMek9kHjtLao0Q9wt60Emk b3h76YaZw2Akg== Date: Tue, 01 Dec 2026 14:07:58 +0000 To: "passt-dev@passt.top" From: Benjamin Schubert Subject: [PATCH] netlink: Fix gateway selection for multipath default routes Message-ID: Feedback-ID: 12078535:user:proton X-Pm-Message-ID: b029c57e60c8305d69d25594bd11c02bebe2e0b6 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-MailFrom: contact@benschubert.me X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: USSAEJZDDWSG32YXMA7RBN72IKOXDXLX X-Message-ID-Hash: USSAEJZDDWSG32YXMA7RBN72IKOXDXLX X-Mailman-Approved-At: Tue, 01 Dec 2026 15:20:39 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Benjamin Schubert The documentation of `--gateway` in `passt.1` states that for a multipath default route, the first nexthop router returned by the kernel with the highest weight is selected. `nl_route_get_def_multipath()` however allowed a subsequent route with the same weight to override the nexthop, so it was the last gateway with a nexthop that would actually be chosen. This aligns the code with the documentation, by fixing the code to only select a new gateway if it has strictly a higher weight. Also update the selected weight only after finding a gateway. Since a device-only enxthop is not a gateway cnadidate, it must not prevent a subsequent gateway from being selected. Add 7 tests for various ipv4 and ipv6 scenarios with multipath gateways. Note that ipv6 with no default gateway is not accepted by the kernel, and thus ipv6 has one less test. All 7 tests fail before this change. Link: https://bugs.passt.top/show_bug.cgi?id=3D230 Signed-off-by: Benjamin Schubert --- netlink.c | 7 +- test/pasta_options/multipath_gateway | 97 ++++++++++++++++++++++++++++ test/run | 1 + 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 test/pasta_options/multipath_gateway diff --git a/netlink.c b/netlink.c index 8b16ebc..37c55cc 100644 --- a/netlink.c +++ b/netlink.c @@ -360,11 +360,11 @@ unsigned int nl_get_ext_if(int s, sa_family_t af) =20 =09return 0; } =20 /** - * nl_route_get_def_multipath() - Get lowest-weight route from nexthop lis= t + * nl_route_get_def_multipath() - Get first highest-weight route from next= hop list * @rta:=09Routing netlink attribute with type RTA_MULTIPATH * @gw:=09=09Default gateway to fill * * Return: true if a gateway was found, false otherwise */ @@ -378,22 +378,21 @@ static bool nl_route_get_def_multipath(struct rtattr = *rta, void *gw) =09for (rtnh =3D (struct rtnexthop *)RTA_DATA(rta); =09 RTNH_OK(rtnh, nh_len); rtnh =3D RTNH_NEXT_AND_DEC(rtnh, nh_len)) { =09=09size_t len =3D rtnh->rtnh_len - sizeof(*rtnh); =09=09struct rtattr *rta_inner; =20 -=09=09if (rtnh->rtnh_hops < hops) +=09=09if (rtnh->rtnh_hops <=3D hops) =09=09=09continue; =20 -=09=09hops =3D rtnh->rtnh_hops; - =09=09for (rta_inner =3D RTNH_DATA(rtnh); RTA_OK(rta_inner, len); =09=09 rta_inner =3D RTA_NEXT(rta_inner, len)) { =20 =09=09=09if (rta_inner->rta_type !=3D RTA_GATEWAY) =09=09=09=09continue; =20 =09=09=09memcpy(gw, RTA_DATA(rta_inner), RTA_PAYLOAD(rta_inner)); +=09=09=09hops =3D rtnh->rtnh_hops; =09=09=09found =3D true; =09=09} =09} =20 =09return found; diff --git a/test/pasta_options/multipath_gateway b/test/pasta_options/mult= ipath_gateway new file mode 100644 index 0000000..f7bd45d --- /dev/null +++ b/test/pasta_options/multipath_gateway @@ -0,0 +1,97 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# PASST - Plug A Simple Socket Transport +# 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 + +htools=09ip unshare sed grep + +set=09OUT __STATEDIR__/multipath-gateway.out +set=09ERR __STATEDIR__/multipath-gateway.err + +def=09ipv4 +set=09FAMILY 4 +set=09PREFIX 192.0.2. +set=09ADDRESS 192.0.2.10/24 +set=09LINKLOCAL true +set=09ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0; __L= INKLOCAL__ +endef + +def=09ipv6 +set=09FAMILY 6 +set=09PREFIX 2001:db8:1:: +set=09ADDRESS 2001:db8:1::10/64 +set=09LINKLOCAL ip -6 addr add fe80::10/64 dev dummy0 nodad +set=09ADDRESS_SETUP ip -__FAMILY__ address add __ADDRESS__ dev dummy0 noda= d; __LINKLOCAL__ +endef + +# Keep each invocation in its own user/network namespace +# Reuse unshare's user namespace instead of creating another with pasta +def=09check_gateway +set=09LOG __STATEDIR__/ipv__FAMILY__-__CASE__.log +set=09LINK_SETUP ip link set lo up; ip link add dummy0 type dummy; ip link= set dummy0 up +set=09ROUTE_SETUP ip -__FAMILY__ route add default __NEXTHOPS__ +set=09ROUTE_SHOW ip -__FAMILY__ route show default +set=09NETWORK_SHOW ip -__FAMILY__ address show dev dummy0; ip -__FAMILY__ = route show table all +set=09SETUP __LINK_SETUP__; __ADDRESS_SETUP__; __NETWORK_SHOW__; __ROUTE_S= ETUP__ +set=09PASTA ./pasta --netns-only --ipv__FAMILY__-only --debug --interface = dummy0 +hout=09STATUS unshare --map-root-user --net -- sh -ec '__SETUP__; __ROUTE_= SHOW__; exec __PASTA__ -- true' >'__LOG__' 2>&1; echo \$? +host=09cat '__LOG__' +check=09[ '__STATUS__' =3D '0' ] +hout=09ACTUAL sed --quiet 's/.*router: *//p' '__LOG__' +info=09Gateway: expected __EXPECTED__, actual __ACTUAL__ +check=09[ '__ACTUAL__' =3D '__EXPECTED__' ] +endef + +test=09IPv4: first gateway wins an equal-weight tie +set=09CASE equal +ipv4 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __P= REFIX__2 dev dummy0 weight 1 +set=09EXPECTED __PREFIX__1 +check_gateway + +test=09IPv6: first gateway wins an equal-weight tie +set=09CASE equal +ipv6 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __P= REFIX__2 dev dummy0 weight 1 +set=09EXPECTED __PREFIX__1 +check_gateway + +test=09IPv4: first gateway with the highest weight wins +set=09CASE highest +ipv4 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __P= REFIX__2 dev dummy0 weight 3 nexthop via __PREFIX__3 dev dummy0 weight 3 +set=09EXPECTED __PREFIX__2 +check_gateway + +test=09IPv6: first gateway with the highest weight wins +set=09CASE highest +ipv6 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 1 nexthop via __P= REFIX__2 dev dummy0 weight 3 nexthop via __PREFIX__3 dev dummy0 weight 3 +set=09EXPECTED __PREFIX__2 +check_gateway + +test=09IPv4: lower-weight gateway does not replace the selected gateway +set=09CASE lower +ipv4 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 3 nexthop via __P= REFIX__2 dev dummy0 weight 1 +set=09EXPECTED __PREFIX__1 +check_gateway + +test=09IPv6: lower-weight gateway does not replace the selected gateway +set=09CASE lower +ipv6 +set=09NEXTHOPS nexthop via __PREFIX__1 dev dummy0 weight 3 nexthop via __P= REFIX__2 dev dummy0 weight 1 +set=09EXPECTED __PREFIX__1 +check_gateway + +test=09IPv4: gateway-less nexthop does not mask an equal-weight gateway +set=09CASE direct +ipv4 +set=09NEXTHOPS nexthop dev dummy0 weight 1 nexthop via __PREFIX__2 dev dum= my0 weight 1 +set=09EXPECTED __PREFIX__2 +check_gateway diff --git a/test/run b/test/run index 751ff50..fe9f177 100755 --- a/test/run +++ b/test/run @@ -86,10 +86,11 @@ run() { =09setup pasta_options =09test pasta_options/log_to_file =09test pasta_options/netns_only =09test pasta_options/no_pidns =09test pasta_options/dns_host_follow +=09test pasta_options/multipath_gateway =09teardown pasta_options =20 =09setup build =09test pasta_podman/bats =09teardown build --=20 2.56.1