From 3aa3cbc341c18a221ec77fe073bd3f665990f9b4 Mon Sep 17 00:00:00 2001 From: Yang Sun Date: Thu, 22 Feb 2024 02:44:33 +0800 Subject: [PATCH] [ip6] add multicast forwarding restrictions to host (#9863) On Android, the platform doesn't restrict link-local/mesh-local source addresses when forwarding multicast packets. For multicast packets sent from link-local/mesh-local address to scope larger than realm-local, set the hoplimit to 1 so the packet can be delivered to host, and will not be forwarded to infrastructure link. The feature is guarded by OPENTHREAD_CONFIG_IP6_RESTRICT_FORWARDING_LARGER_SCOPE_MCAST_WITH_LOCAL_SRC, set as 1 to enable. --- src/core/config/ip6.h | 10 ++++++++++ src/core/net/ip6.cpp | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/core/config/ip6.h b/src/core/config/ip6.h index 2e8e982c2..f16f8a3dd 100644 --- a/src/core/config/ip6.h +++ b/src/core/config/ip6.h @@ -227,6 +227,16 @@ #define OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE #endif +/** + * @def OPENTHREAD_CONFIG_IP6_RESTRICT_FORWARDING_LARGER_SCOPE_MCAST_WITH_LOCAL_SRC + * + * Define as 1 to restrict multicast forwarding to larger scope from local sources. + * + */ +#ifndef OPENTHREAD_CONFIG_IP6_RESTRICT_FORWARDING_LARGER_SCOPE_MCAST_WITH_LOCAL_SRC +#define OPENTHREAD_CONFIG_IP6_RESTRICT_FORWARDING_LARGER_SCOPE_MCAST_WITH_LOCAL_SRC 0 +#endif + /** * @} * diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 3a6472a3f..0c0bf5abd 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -1037,6 +1037,19 @@ Error Ip6::PassToHost(OwnedPtr &aMessagePtr, } #endif +#if OPENTHREAD_CONFIG_IP6_RESTRICT_FORWARDING_LARGER_SCOPE_MCAST_WITH_LOCAL_SRC + // Some platforms (e.g. Android) currently doesn't restrict link-local/mesh-local source + // addresses when forwarding multicast packets. + // For a multicast packet sent from link-local/mesh-local address to scope larger + // than realm-local, set the hop limit to 1 before sending to host, so this packet + // will not be forwarded by host. + if (aMessageInfo.GetSockAddr().IsMulticastLargerThanRealmLocal() && + (aMessageInfo.GetPeerAddr().IsLinkLocal() || (Get().IsMeshLocalAddress(aMessageInfo.GetPeerAddr())))) + { + messagePtr->Write(Header::kHopLimitFieldOffset, 1); + } +#endif + // Pass message to callback transferring its ownership. mReceiveIp6DatagramCallback.Invoke(messagePtr.Release());