[PATCH net-next v4] ipv6: check mc_forwarding before calling ip6mr_get_route()
From: Wenbo Sun
Date: Thu Nov 19 2026 - 22:23:54 EST
When CONFIG_IPV6_MROUTE is enabled, rt6_fill_node() currently calls
ip6mr_get_route() whenever an input interface (RTA_IIF) is specified and
the matched route prefix (rt6_dst->addr) is multicast.
However, ip6mr_get_route() should not be invoked in three cases where
the kernel does not perform multicast forwarding lookup:
1. Multicast forwarding is disabled in the network namespace
(mc_forwarding is 0);
2. The queried destination address (dest) has interface-local
(IPV6_ADDR_LOOPBACK) or link-local (IPV6_ADDR_LINKLOCAL) scope, which
ip6_mc_input() never forwards;
3. The query requests a raw FIB entry via RTM_F_FIB_MATCH (where
inet6_rtm_getroute() passes dest as NULL), for which IPv4 uses
fib_dump_info() rather than ipmr_get_route().
In those cases, calling ip6mr_get_route() falls into the cache-miss path
(where skb->dev is not set by inet6_rtm_getroute()) and fails, causing
rt6_fill_node() to jump to nla_put_failure and return -EMSGSIZE instead
of emitting RTA_IIF and the matched route attributes. Note that
supporting MFC cache lookups when mc_forwarding is enabled for routable
multicast groups would also require passing the queried (S,G) and input
device into ip6mr_get_route(), which is left out of scope for this
change.
Align rt6_fill_node() with IPv4 rt_fill_info() and ip6_mc_input() by
requiring that dest is non-NULL, the matched route prefix
(rt6_dst->addr) is multicast, dest is neither interface-local nor
link-local multicast, and mc_forwarding is enabled before calling
ip6mr_get_route().
This alignment was identified with the assistance of an LLM-based code
review comparing IPv4 and IPv6 multicast route get handling, confirmed
in a User-Mode Linux (UML) environment, and verified in UML that
routable multicast queries with mc_forwarding disabled, link-local
(ff02::1) and interface-local (ff01::1) queries with mc_forwarding
enabled, RTM_F_FIB_MATCH queries, and queries matching a non-multicast
route (::/0) all succeed and report RTA_IIF.
Built with W=1 on UML, x86_64 allmodconfig, and x86_64 allyesconfig with
zero new warnings or errors.
Signed-off-by: Wenbo Sun <sunwenbo@xxxxxxxxxxxxxxxxxxxx>
---
Changes in v4:
- Converge the series back into a focused single patch and drop patch
2/2 ("ipv6: preserve ip6mr_get_route() error in rt6_fill_node()") so
we do not alter the -EMSGSIZE errno to a misleading -ENODEV while
ip6mr_get_route() still lacks skb->dev and (S,G) parameters.
- Check `dest != NULL` (so RTM_F_FIB_MATCH queries bypass
ip6mr_get_route() and return the matched FIB6 entry with RTA_IIF) and
keep `ipv6_addr_is_multicast(&rt6_dst->addr)` (avoiding regression on
non-multicast matched routes such as ::/0), while testing
`ipv6_addr_type(dest)` against `IPV6_ADDR_LOOPBACK |
IPV6_ADDR_LINKLOCAL`, per Sashiko review.
- Add UML tests for RTM_F_FIB_MATCH and non-multicast matched routes
(::/0) with MRT6_INIT enabled.
- Link to v3: https://lore.kernel.org/netdev/20261116014744.18223-1-sunwenbo@xxxxxxxxxxxxxxxxxxxx/
Changes in v3:
- Also exclude interface-local (IPV6_ADDR_LOOPBACK) and link-local
(IPV6_ADDR_LINKLOCAL) multicast and drop Hangbin Liu's Reviewed-by tag
due to the code change.
- Link to v2: https://lore.kernel.org/netdev/20261112094630.197244-1-sunwenbo@xxxxxxxxxxxxxxxxxxxx/
Changes in v2:
- Reword commit message for clearer tone.
- Link to v1: https://lore.kernel.org/netdev/20261111094528.425826-1-sunwenbo@xxxxxxxxxxxxxxxxxxxx/
net/ipv6/route.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 1b2dcc066e19..419161269701 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -5868,7 +5868,10 @@ static int rt6_fill_node(struct net *net, struct sk_buff *skb,
#endif
if (iif) {
#ifdef CONFIG_IPV6_MROUTE
- if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
+ if (dest && ipv6_addr_is_multicast(&rt6_dst->addr) &&
+ !(ipv6_addr_type(dest) &
+ (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL)) &&
+ atomic_read(&net->ipv6.devconf_all->mc_forwarding)) {
int err = ip6mr_get_route(net, skb, rtm, portid);
if (err == 0)
--
2.33.0