* [RFC next-net 1/3] net: accept legacy nexthop attributes when deleting by nexthop id
[not found] <20261120200635.95798-1-stephen@networkplumber.org>
@ 2026-11-20 20:01 ` Stephen Hemminger
2026-11-20 20:01 ` [RFC next-net 2/3] ipv4: accept the reported route type when deleting a blackhole nexthop route Stephen Hemminger
2026-11-20 20:01 ` [RFC next-net 3/3] selftests: fib_nexthops: test route flush with nexthop objects Stephen Hemminger
2 siblings, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2026-11-20 20:01 UTC (permalink / raw)
To: netdev
Cc: dsahern, Stephen Hemminger, Yuen Su, Ido Schimmel,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, linux-kernel
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@gmail.com>
Link: https://lore.kernel.org/netdev/CAC9WPdwmM4=64DH5nY85srnLhRBS7PkjR9vrsALpob-GutbmZw@mail.gmail.com/
Fixes: 493ced1ac47c ("ipv4: Allow routes to use nexthop objects")
Fixes: 5b98324ebe29 ("ipv6: Allow routes to use nexthop objects")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
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
^ permalink raw reply [flat|nested] 3+ messages in thread* [RFC next-net 2/3] ipv4: accept the reported route type when deleting a blackhole nexthop route
[not found] <20261120200635.95798-1-stephen@networkplumber.org>
2026-11-20 20:01 ` [RFC next-net 1/3] net: accept legacy nexthop attributes when deleting by nexthop id Stephen Hemminger
@ 2026-11-20 20:01 ` Stephen Hemminger
2026-11-20 20:01 ` [RFC next-net 3/3] selftests: fib_nexthops: test route flush with nexthop objects Stephen Hemminger
2 siblings, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2026-11-20 20:01 UTC (permalink / raw)
To: netdev
Cc: dsahern, Stephen Hemminger, Yuen Su, Ido Schimmel,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, linux-kernel
fib_dump_info() rewrites rtm_type to RTN_BLACKHOLE for a blackhole nexthop
object, before the nexthop_compat_mode bail-out:
if (fi->nh) {
if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
goto nla_put_failure;
if (nexthop_is_blackhole(fi->nh))
rtm->rtm_type = RTN_BLACKHOLE;
So rtm_type describes the nexthop object, not the alias, which keeps the
type it was created with. fib_table_delete() matches the stored type, so
the kernel won't delete a route using the type it just reported:
# ip nexthop add id 301 blackhole
# ip route add 192.0.2.0/24 nhid 301
# ip route show 192.0.2.0/24
blackhole 192.0.2.0/24 nhid 301 dev lo
# ip route del blackhole 192.0.2.0/24 nhid 301
RTNETLINK answers: No such process
# ip route del unicast 192.0.2.0/24 nhid 301
#
"ip route flush" echoes the dumped message back, rtmsg header included, so
it carries the rewritten type and fails with ESRCH. Because the rewrite
happens before the compat-mode check, this breaks flush even with
nexthop_compat_mode off, where there are no legacy attributes at all.
Accept RTN_BLACKHOLE on delete when the alias uses a blackhole nexthop
object, so a route can be removed using the type that was advertised.
"ip route flush type blackhole" works too: the type filter is applied to
the dump in userspace, so the kernel only ever sees the type it reported.
Dropping the rewrite in fib_dump_info() instead would change what every
existing dump reader sees.
IPv6 is unaffected, ip6_route_del() doesn't compare rtm_type.
Reported-by: Yuen Su <zypulsecore@gmail.com>
Link: https://lore.kernel.org/netdev/CAC9WPdwmM4=64DH5nY85srnLhRBS7PkjR9vrsALpob-GutbmZw@mail.gmail.com/
Fixes: 4c7e8084fd46 ("ipv4: Plumb support for nexthop object in a fib_info")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
net/ipv4/fib_lookup.h | 1 +
net/ipv4/fib_semantics.c | 14 ++++++++++++++
net/ipv4/fib_trie.c | 2 +-
3 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/net/ipv4/fib_lookup.h b/net/ipv4/fib_lookup.h
index 0b72796dd1ad..5109cc197de2 100644
--- a/net/ipv4/fib_lookup.h
+++ b/net/ipv4/fib_lookup.h
@@ -40,6 +40,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
struct netlink_ext_ack *extack);
int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
struct netlink_ext_ack *extack);
+bool fib_type_match(u8 fc_type, const struct fib_alias *fa);
bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi);
int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
const struct fib_rt_info *fri, unsigned int flags);
diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index db030bc26b6d..a089761e2104 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -1061,6 +1061,20 @@ int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
return 0;
}
+/* fib_dump_info() reports a blackhole nexthop object as RTN_BLACKHOLE
+ * whatever type the alias has, so accept that type back on delete.
+ */
+bool fib_type_match(u8 fc_type, const struct fib_alias *fa)
+{
+ const struct fib_info *fi = fa->fa_info;
+
+ if (!fc_type || fa->fa_type == fc_type)
+ return true;
+
+ return fc_type == RTN_BLACKHOLE && fi->nh &&
+ nexthop_is_blackhole(fi->nh);
+}
+
bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
{
struct nlattr *nla;
diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 523a5a4228d0..c67df5aa1b96 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -1726,7 +1726,7 @@ int fib_table_delete(struct net *net, struct fib_table *tb,
(fa->fa_dscp != dscp))
break;
- if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
+ if (fib_type_match(cfg->fc_type, fa) &&
(cfg->fc_scope == RT_SCOPE_NOWHERE ||
fa->fa_info->fib_scope == cfg->fc_scope) &&
(!cfg->fc_prefsrc ||
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread* [RFC next-net 3/3] selftests: fib_nexthops: test route flush with nexthop objects
[not found] <20261120200635.95798-1-stephen@networkplumber.org>
2026-11-20 20:01 ` [RFC next-net 1/3] net: accept legacy nexthop attributes when deleting by nexthop id Stephen Hemminger
2026-11-20 20:01 ` [RFC next-net 2/3] ipv4: accept the reported route type when deleting a blackhole nexthop route Stephen Hemminger
@ 2026-11-20 20:01 ` Stephen Hemminger
2 siblings, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2026-11-20 20:01 UTC (permalink / raw)
To: netdev
Cc: dsahern, Stephen Hemminger, Ido Schimmel, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Shuah Khan, linux-kselftest, linux-kernel
fib_nexthops.sh exercises "ip nexthop flush" but never "ip route flush"
against routes using nexthop objects, which is how the dump and delete
paths drifted apart.
Add ipv4_route_flush and ipv6_route_flush: a single nexthop object, a
group, a blackhole object and, for IPv4, a route via an IPv6 nexthop
object, since each expands differently in the dump. Flush a batch mixing
nexthop object routes with an ordinary one, and check the nexthop objects
outlive the routes.
Also check what must keep failing: a delete naming the wrong id, and an
add combining an id with a nexthop spec.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
tools/testing/selftests/net/fib_nexthops.sh | 126 ++++++++++++++++++++
1 file changed, 126 insertions(+)
diff --git a/tools/testing/selftests/net/fib_nexthops.sh b/tools/testing/selftests/net/fib_nexthops.sh
index adbec51917eb..501ed0fc05a8 100755
--- a/tools/testing/selftests/net/fib_nexthops.sh
+++ b/tools/testing/selftests/net/fib_nexthops.sh
@@ -29,6 +29,7 @@ IPV4_TESTS="
ipv4_large_grp
ipv4_large_res_grp
ipv4_compat_mode
+ ipv4_route_flush
ipv4_fdb_grp_fcnal
ipv4_fdb_port_fcnal
ipv4_mpath_select
@@ -44,6 +45,7 @@ IPV6_TESTS="
ipv6_large_grp
ipv6_large_res_grp
ipv6_compat_mode
+ ipv6_route_flush
ipv6_fdb_grp_fcnal
ipv6_fdb_port_fcnal
ipv6_mpath_select
@@ -2148,6 +2150,130 @@ ipv6_compat_mode()
sysctl_nexthop_compat_mode_set 1 "IPv6"
}
+ipv4_route_flush()
+{
+ echo
+ echo "IPv4 route flush with nexthop objects"
+ echo "-------------------------------------"
+
+ # "ip route flush" dumps the routes and sends each dumped message
+ # back as RTM_DELROUTE. With nexthop_compat_mode on, a dump of a
+ # route using a nexthop object carries RTA_NH_ID plus the legacy
+ # expansion of that nexthop, so the delete has to accept both.
+
+ run_cmd "$IP nexthop add id 601 via 172.16.1.2 dev veth1"
+ run_cmd "$IP nexthop add id 602 via 172.16.1.3 dev veth1"
+ run_cmd "$IP nexthop add id 610 group 601/602"
+ run_cmd "$IP nexthop add id 603 blackhole"
+ run_cmd "$IP -6 nexthop add id 604 via 2001:db8:91::2 dev veth1"
+
+ # single nexthop object: dump has RTA_NH_ID, RTA_GATEWAY, RTA_OIF
+ run_cmd "$IP ro add 172.16.101.0/24 nhid 601"
+ run_cmd "$IP ro flush 172.16.101.0/24"
+ log_test $? 0 "Flush route with single nexthop object"
+ check_route "172.16.101.0/24" ""
+ log_test $? 0 "Flushed single nexthop object route is gone"
+
+ # nexthop group: dump has RTA_NH_ID plus RTA_MULTIPATH
+ run_cmd "$IP ro add 172.16.102.0/24 nhid 610"
+ run_cmd "$IP ro flush 172.16.102.0/24"
+ log_test $? 0 "Flush route with nexthop group"
+ check_route "172.16.102.0/24" ""
+ log_test $? 0 "Flushed nexthop group route is gone"
+
+ # blackhole nexthop object: the dump reports the route as
+ # RTN_BLACKHOLE even though the FIB alias is unicast
+ run_cmd "$IP ro add 172.16.103.0/24 nhid 603"
+ run_cmd "$IP ro flush 172.16.103.0/24"
+ log_test $? 0 "Flush route with blackhole nexthop object"
+ check_route "172.16.103.0/24" ""
+ log_test $? 0 "Flushed blackhole nexthop object route is gone"
+
+ # IPv4 route via an IPv6 nexthop object expands to RTA_VIA
+ run_cmd "$IP ro add 172.16.104.0/24 nhid 604"
+ run_cmd "$IP ro flush 172.16.104.0/24"
+ log_test $? 0 "Flush route with cross-family nexthop object"
+ check_route "172.16.104.0/24" ""
+ log_test $? 0 "Flushed cross-family route is gone"
+
+ # flushing a mix in one batch must not disturb ordinary routes
+ run_cmd "$IP ro add 172.16.105.0/24 nhid 601"
+ run_cmd "$IP ro add 172.16.106.0/24 nhid 610"
+ run_cmd "$IP ro add 172.16.107.0/24 via 172.16.1.2 dev veth1"
+ run_cmd "$IP ro flush dev veth1 proto boot"
+ log_test $? 0 "Flush mixed batch of nexthop object and ordinary routes"
+
+ # the nexthop objects themselves must survive a route flush
+ run_cmd "$IP nexthop get id 601"
+ log_test $? 0 "Nexthop object survives route flush"
+ run_cmd "$IP nexthop get id 610"
+ log_test $? 0 "Nexthop group survives route flush"
+
+ # deleting by nexthop id must still ignore a conflicting selector
+ # rather than silently deleting the wrong route
+ run_cmd "$IP ro add 172.16.108.0/24 nhid 601"
+ run_cmd "$IP ro del 172.16.108.0/24 nhid 602"
+ log_test $? 2 "Delete by wrong nexthop id fails"
+ check_route "172.16.108.0/24" "172.16.108.0/24 nhid 601 via 172.16.1.2 dev veth1"
+ log_test $? 0 "Route with other nexthop id is retained"
+ run_cmd "$IP ro del 172.16.108.0/24 nhid 601"
+ log_test $? 0 "Delete by correct nexthop id succeeds"
+
+ # adding a route must still reject nhid together with a nexthop spec
+ run_cmd "$IP ro add 172.16.109.0/24 nhid 601 via 172.16.1.2 dev veth1"
+ log_test $? 2 "Add rejects nexthop id with nexthop specification"
+
+ $IP nexthop flush >/dev/null 2>&1
+ $IP ro flush dev veth1 proto boot >/dev/null 2>&1
+}
+
+ipv6_route_flush()
+{
+ echo
+ echo "IPv6 route flush with nexthop objects"
+ echo "-------------------------------------"
+
+ run_cmd "$IP -6 nexthop add id 611 via 2001:db8:91::2 dev veth1"
+ run_cmd "$IP -6 nexthop add id 612 via 2001:db8:91::3 dev veth1"
+ run_cmd "$IP nexthop add id 620 group 611/612"
+ run_cmd "$IP -6 nexthop add id 613 blackhole"
+
+ run_cmd "$IP -6 ro add 2001:db8:101::/64 nhid 611"
+ run_cmd "$IP -6 ro flush 2001:db8:101::/64"
+ log_test $? 0 "Flush IPv6 route with single nexthop object"
+ check_route6 "2001:db8:101::/64" ""
+ log_test $? 0 "Flushed IPv6 single nexthop object route is gone"
+
+ run_cmd "$IP -6 ro add 2001:db8:102::/64 nhid 620"
+ run_cmd "$IP -6 ro flush 2001:db8:102::/64"
+ log_test $? 0 "Flush IPv6 route with nexthop group"
+ check_route6 "2001:db8:102::/64" ""
+ log_test $? 0 "Flushed IPv6 nexthop group route is gone"
+
+ run_cmd "$IP -6 ro add 2001:db8:103::/64 nhid 613"
+ run_cmd "$IP -6 ro flush 2001:db8:103::/64"
+ log_test $? 0 "Flush IPv6 route with blackhole nexthop object"
+ check_route6 "2001:db8:103::/64" ""
+ log_test $? 0 "Flushed IPv6 blackhole nexthop object route is gone"
+
+ run_cmd "$IP -6 nexthop get id 611"
+ log_test $? 0 "IPv6 nexthop object survives route flush"
+ run_cmd "$IP nexthop get id 620"
+ log_test $? 0 "IPv6 nexthop group survives route flush"
+
+ run_cmd "$IP -6 ro add 2001:db8:104::/64 nhid 611"
+ run_cmd "$IP -6 ro del 2001:db8:104::/64 nhid 612"
+ log_test $? 2 "Delete IPv6 route by wrong nexthop id fails"
+ run_cmd "$IP -6 ro del 2001:db8:104::/64 nhid 611"
+ log_test $? 0 "Delete IPv6 route by correct nexthop id succeeds"
+
+ run_cmd "$IP -6 ro add 2001:db8:105::/64 nhid 611 via 2001:db8:91::2 dev veth1"
+ log_test $? 2 "Add rejects IPv6 nexthop id with nexthop specification"
+
+ $IP nexthop flush >/dev/null 2>&1
+ $IP -6 ro flush dev veth1 proto boot >/dev/null 2>&1
+}
+
ipv4_compat_mode()
{
local rc
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread