[RFC next-net 1/3] net: accept legacy nexthop attributes when deleting by nexthop id
From: Stephen Hemminger
Date: Fri Nov 20 2026 - 15:06:56 EST
A route dump expands a nexthop object into the legacy attributes as well
when nexthop_compat_mode is on, which is the default. Both delete paths
then reject the result as "Nexthop specification and nexthop id are
mutually exclusive", so the kernel refuses its own dump output.
That breaks "ip route flush", which dumps routes and sends them back as
RTM_DELROUTE:
# ip nexthop add id 101 via 192.0.2.1 dev dummy0
# ip route add 203.0.113.0/24 nhid 101
# ip route show 203.0.113.0/24
203.0.113.0/24 nhid 101 via 192.0.2.1 dev dummy0
# ip route flush to 203.0.113.0/24
Failed to send flush request: Invalid argument
Setting nexthop_compat_mode to 0 drops the legacy attributes and the same
flush works, so the round trip is all that's wrong here. Any client that
deletes a route by echoing a dump hits this, so fix it once in the kernel.
Ignore the legacy attributes on RTM_DELROUTE when RTA_NH_ID is present.
The id is already authoritative in the matching code: fib_nh_match()
decides on cfg->fc_nh_id alone, and ip6_route_del() compares rt->nh->id
and skips the device and gateway tests for nexthop-backed routes.
RTM_NEWROUTE still rejects the combination.
Clear the parsed fields next to the existing exclusivity check rather
than filtering inside the attribute loop. The lwtunnel validation done
there cannot reject a dumped route, because an encap route holds a
reference on its encap module for as long as it exists. IPv6 drops the
attributes in rtm_to_fib6_config() the same way.
A delete passing both an id and a nexthop spec now ignores the spec. The
request can't tell a hand-written gateway from a dump-expanded one, and
the combination is -EINVAL today, so nothing depends on it.
Reported-by: Yuen Su <zypulsecore@xxxxxxxxx>
Link: https://lore.kernel.org/netdev/CAC9WPdwmM4=64DH5nY85srnLhRBS7PkjR9vrsALpob-GutbmZw@xxxxxxxxxxxxxx/
Fixes: 493ced1ac47c ("ipv4: Allow routes to use nexthop objects")
Fixes: 5b98324ebe29 ("ipv6: Allow routes to use nexthop objects")
Signed-off-by: Stephen Hemminger <stephen@xxxxxxxxxxxxxxxxxx>
---
net/ipv4/fib_frontend.c | 17 +++++++++++++++--
net/ipv6/route.c | 14 ++++++++++++--
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 8c123b111b3d..1d5a42dd35b5 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -869,8 +869,21 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
}
if (cfg->fc_nh_id) {
- if (cfg->fc_oif || cfg->fc_gw_family ||
- cfg->fc_encap || cfg->fc_mp) {
+ if (nlh->nlmsg_type == RTM_DELROUTE) {
+ /* A dump of a route using a nexthop object expands
+ * that nexthop into the legacy attributes too. Ignore
+ * those here so the dump can be fed back as-is; the id
+ * identifies the nexthop on its own.
+ */
+ cfg->fc_oif = 0;
+ cfg->fc_gw_family = 0;
+ memset(&cfg->fc_gw6, 0, sizeof(cfg->fc_gw6));
+ cfg->fc_encap = NULL;
+ cfg->fc_encap_type = LWTUNNEL_ENCAP_NONE;
+ cfg->fc_mp = NULL;
+ cfg->fc_mp_len = 0;
+ } else if (cfg->fc_oif || cfg->fc_gw_family ||
+ cfg->fc_encap || cfg->fc_mp) {
NL_SET_ERR_MSG(extack,
"Nexthop specification and nexthop id are mutually exclusive");
err = -EINVAL;
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 4a4c5ace63c5..7467ef5d5f1d 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -5258,8 +5258,18 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
if (tb[RTA_NH_ID]) {
- if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
- tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
+ /* A dump expands the nexthop object into the legacy
+ * attributes too. Ignore those on delete so the dump can be
+ * fed back as-is; the id identifies the nexthop on its own.
+ */
+ if (!newroute) {
+ tb[RTA_GATEWAY] = NULL;
+ tb[RTA_OIF] = NULL;
+ tb[RTA_MULTIPATH] = NULL;
+ tb[RTA_ENCAP] = NULL;
+ tb[RTA_ENCAP_TYPE] = NULL;
+ } else if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
+ tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
NL_SET_ERR_MSG(extack,
"Nexthop specification and nexthop id are mutually exclusive");
goto errout;
--
2.53.0