Skip to content

net/netforward: Add receiver-aware multicast forwarding (stage 1). - #22839

Draft
Brijesh-Thakkar wants to merge 7 commits into
apache:masterfrom
Brijesh-Thakkar:feat/ipfwd-mcast-stage1
Draft

Brijesh-Thakkar wants to merge 7 commits into
apache:masterfrom
Brijesh-Thakkar:feat/ipfwd-mcast-stage1

Conversation

@Brijesh-Thakkar

@Brijesh-Thakkar Brijesh-Thakkar commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

Related to #18762 (stage 1 of 4).

Related: #22803. #22831 changes the same broadcast loop in ipv4_forward.c/ipv6_forward.c; whichever merges second needs a small rebase (resolution already prepared).

Why. With CONFIG_NET_IPFORWARD_BROADCAST, a multicast packet is
forwarded through every other network device, whether or not a receiver is
reachable through it. IPv4 additionally only forwards groups that were
joined on the receiving device, and IPv6 forwards every multicast packet
above link-local scope. #18762 proposes receiver-aware forwarding. This PR
is stage 1: the forwarding table, the data path that uses it and a minimal
control interface. Learning the table from IGMP (stage 2) and MLD (stage 3)
snooping and a sim defconfig (stage 4) will follow as separate PRs.

What. A new option CONFIG_NET_IPFORWARD_MULTICAST (default n, depends
on NET_IPFORWARD_BROADCAST, selects NETDEV_IFINDEX):

  • A multicast packet is forwarded only through the egress interfaces that
    the multicast forwarding table lists for its group. A group without an
    entry is not forwarded.
  • A packet is never sent back out the interface it was received on.
  • Broadcast forwarding and local delivery are unchanged; the existing filters
    (IFF_NOSRC_FORWARD/IFF_NODST_FORWARD, 224.0.0.0/24, IPv6 scopes ff0x
    for x <= 2, TTL/hop limit) still apply.
  • With the option off, forwarding is unchanged. The one difference is in
    the IPv4 statistics, see Compatibility below.

How.

  • net/netforward/netfwd_mcast.c: a static table of
    CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES (default 8) (*,G) entries, each a
    group address, a uint32_t egress interface-index bitmap (indices 1..32,
    others are refused) and an optional expiry checked lazily on lookup.
    API: netfwd_mcast_join(), netfwd_mcast_leave(),
    netfwd_mcast_lookup(), netfwd_mcast_ifdown(). No timers, work items
    or dynamic allocation.
  • Locking: the table has its own mutex_t, private to netfwd_mcast.c
    and taken only inside those four functions, with nothing called while it
    is held (a leaf lock, like g_ipfilter_lock in net/ipfilter). The
    packet path does not take the network lock. The ioctl resolves the
    interface and updates the table with the device list locked, and
    netdev_unregister() clears the device from the table under the same
    lock, so an entry cannot outlive its device.
  • ipv4_forward_broadcast() / ipv6_forward_broadcast() look the group
    up once, remove the receiving device, return early if nothing is left and
    pass the bitmap to the per-device callbacks, which only test one bit after
    their existing receiving-device check. Both functions now have a single
    prototype that returns bool: true if the packet was forwarded through at
    least one device, with or without the option. The callbacks always get a
    small struct private to each file (ipv4_fwdarg_s, ipv6_fwdarg_s) and
    record it there.
  • ipv4_in() now also forwards a multicast packet whose group was not
    joined on the receiving device (forwarding depends on the egress devices;
    local delivery is still gated by IGMP). A forwarded packet that is not
    for this host is released without being counted as dropped, and the same
    now applies to a forwarded broadcast packet that no protocol takes.
    ipv6_in() needed no change.
  • netdev_ifdown() and netdev_unregister() remove the device from the
    table, so it is not revived on ifup and a reused interface index does not
    inherit entries.
  • SIOCADDMCFWD / SIOCDELMCFWD (struct netfwd_mcastreq_s in
    include/nuttx/net/netfwd_mcast.h) add or remove one egress interface of a
    group, with a lifetime in seconds (0 = permanent). The handler is isolated
    in net/netforward/netfwd_mcast_ioctl.c so it can later be replaced by
    MRT_*-style socket options without touching the table or data path.
  • Documentation: Documentation/os/networking/ipforward_mcast.rst.

Test program: apache/nuttx-apps examples/ipfwdmcast (apache/nuttx-apps#4287).
The merged test program still uses the old option name NET_IPFORWARD_MCAST;
the results below were made with it renamed locally, and a follow-up
nuttx-apps PR will make the same change.

Impact

  • New feature, disabled by default. Nothing changes unless
    CONFIG_NET_IPFORWARD_MULTICAST=y; with it, multicast is no longer flooded but
    forwarded per the table (which starts empty).

  • New Kconfig options NET_IPFORWARD_MULTICAST, NET_IPFORWARD_MULTICAST_NENTRIES;
    new ioctls SIOCADDMCFWD (0x0049) and SIOCDELMCFWD (0x004A), after the
    last CAN ioctl; new header include/nuttx/net/netfwd_mcast.h. No existing
    API changes.

  • Build: Make and CMake updated for the two new files.

  • Size, imxrt1050-evk:netnsh with IPv4, IPv6, IGMP, MLD and
    forwarding, all built in the same directory (text / data / bss):

    Build text data bss
    master (97d6bc7) 198852 2160 33536
    this PR, option off 198924 2160 33536
    this PR, option on (8 entries) 200012 2176 33800
    delta, option off +72 0 0
    delta, option on +1160 +16 +264
  • Documentation: added.

  • Security: the ioctls configure forwarding like the existing route ioctls.

  • Compatibility: with the option off, forwarding is unchanged, but there
    is one deliberate change in the IPv4 statistics. A broadcast packet that
    was forwarded through at least one device, and that no protocol takes
    (not UDP, or no UDP broadcast support), is no longer counted in
    g_netstats.ipv4.drop, the same as a forwarded multicast packet with the
    option on. Before this PR it was counted as dropped even though it was
    forwarded. IPv6 statistics are unchanged. The +72 bytes of text with
    the option off are the bool results and this check.

Testing

Host: Ubuntu 26.04 LTS, Linux 7.0.0-44-generic x86_64, GCC 15.2.0,
arm-none-eabi-gcc 14.2.1, CMake 4.2.3 / Ninja 1.13.2. Branch based on
master 97d6bc7.

Builds (no warnings except the board's own #warnings on imxrt):

  • Every commit of the branch built on its own, sim, with the option off and
    on (sim:nsh + NET, IPv4, IPv6, UDP, TCP, IGMP, MLD, ICMPv6,
    NET_BROADCAST, NET_STATISTICS, NET_IPFORWARD,
    NET_IPFORWARD_BROADCAST): 14/14 builds, 0 warnings.
  • Same configuration with the option on, CMake/Ninja: OK, 0 warnings.
  • imxrt1050-evk:netnsh with the same network options, option off and on
    (sizes above).
  • checkpatch.sh -g: clean for every commit.

Runtime, sim, three TUN devices (test app examples/ipfwdmcast, no host
setup needed; packets are injected on tun0, copies sent out of tun0/tun1/tun2
are counted).

Option off (existing behavior):

nsh> ipfwdmcast
--- CONFIG_NET_IPFORWARD_MULTICAST is not set ---
PASS IPv4: group not joined on tun0, not forwarded            0 0 0 (expect 0 0 0)
PASS IPv4: group joined on tun0, flooded                      0 1 1 (expect 0 1 1)
PASS IPv6: flooded                                            0 1 1 (expect 0 1 1)
RESULT: 3 passed, 0 failed

Option on (50 checks, trimmed):

nsh> ipfwdmcast
--- IPv4, CONFIG_NET_IPFORWARD_MULTICAST=y ---
PASS IPv4: no entry, not forwarded                            0 0 0 (expect 0 0 0)
PASS IPv4: entry {tun1}, tun1 only                            0 1 0 (expect 0 1 0)
PASS IPv4: entry {tun1,tun2}, both                            0 1 1 (expect 0 1 1)
PASS IPv4: entry with tun0, never back out                    0 1 1 (expect 0 1 1)
PASS IPv4: tun0 and tun2 removed, tun1 only                   0 1 0 (expect 0 1 0)
PASS IPv4: other group, not forwarded                         0 0 0 (expect 0 0 0)
PASS IPv4: TTL or hop limit 1, not forwarded                  0 (expect 0)
PASS IPv4: link-local group, not forwarded                    0 0 0 (expect 0 0 0)
...
--- table, ioctl and local membership ---
PASS IPv4: joined on tun0 and entry, forwarded                0 1 0 (expect 0 1 0)
PASS IPv4: joined on tun0, no entry, not forwarded            0 0 0 (expect 0 0 0)
PASS IPv4: not joined, entry, forwarded                       0 1 0 (expect 0 1 0)
PASS AF_UNIX group, EAFNOSUPPORT                              -97 (expect -97)
PASS fill the table                                           0 (expect 0)
PASS one more group, ENOSPC                                   -28 (expect -28)
PASS an existing entry still takes an interface               0 (expect 0)
PASS empty entries are freed                                  0 (expect 0)
--- table changed while packets are forwarded ---
PASS stress: tun1 gets every packet, tun0 none                0 120 63 (expect 0 120 0..120)
PASS stress: group without an entry, not forwarded            0 (expect 0)
PASS stress: every ioctl succeeded                            0 in 148 loops (expect 0)
PASS stress: afterwards tun1 only                             0 1 0 (expect 0 1 0)
PASS stress: churned groups left no entry                     0 (expect 0)
--- unregister ---
PASS IPv4: entry {tun2} before tun2 is removed                0 0 1 (expect 0 0 1)
  old tun2 had index 3, tun2 got index 3 after 30 devices
PASS IPv4: new device with the old index has no entry         0 0 0 (expect 0 0 0)
RESULT: 50 passed, 0 failed

The "table changed while packets are forwarded" checks inject 120 IPv4 and
IPv6 packets (plus 30 for a group without an entry) while another thread
keeps adding and removing tun2 and other groups with the ioctls; tun1 stays
listed throughout and must get every packet. The same suite also passes
50/50 on sim:smp (4 CPUs), where the ioctls and the packet path really run
in parallel (tun1 120/120, tun2 66/120, 145 rounds of ioctls, no errors),
with DEBUG_ASSERTIONS enabled in all runs.

The test also catches regressions of the cleanup: with the two
netfwd_mcast_ifdown() calls removed, exactly the related checks fail:

FAIL IPv4: tun1 up again, still removed                       0 1 1 (expect 0 0 1)
FAIL IPv6: tun1 up again, still removed                       0 1 1 (expect 0 0 1)
FAIL IPv4: new device with the old index has no entry         0 0 1 (expect 0 0 0)
RESULT: 47 passed, 3 failed

Runtime, sim, three TAP devices with host listeners (option off: flood;
option on: no entry = nothing, entry = listed interfaces only, expiry and
ifdown stop forwarding):

Host: as above, iproute2 6.19.0, Python 3.14.4. The sim binaries (built
from this branch, option off and on, SIM_NETDEV_NUMBER=3, same network
options as above plus NET_SOCKOPTS, NET_TUN and EXAMPLES_IPFWDMCAST)
run as root in network namespaces; tap1 and tap2 are
moved to their own namespaces with one listener each, and 3 IPv4 and 3 IPv6
packets per case are sent from tap0. Counts are (IPv4, IPv6) packets
received behind tap1 and tap2.

PASS  OFF: IPv4 not joined on eth0: none; IPv6 flooded tap1(v4,v6)=(0, 3) tap2(v4,v6)=(0, 3) expect (0, 3) (0, 3)
PASS  OFF: IPv4 joined on eth0: flooded to both      tap1(v4,v6)=(3, 3) tap2(v4,v6)=(3, 3) expect (3, 3) (3, 3)
RESULT OFF: 2 passed, 0 failed
PASS  ON: no entry: nothing forwarded                tap1(v4,v6)=(0, 0) tap2(v4,v6)=(0, 0) expect (0, 0) (0, 0)
PASS  ON: entry {eth1}: tap1 only                    tap1(v4,v6)=(3, 3) tap2(v4,v6)=(0, 0) expect (3, 3) (0, 0)
PASS  ON: entry {eth1,eth2}: both                    tap1(v4,v6)=(3, 3) tap2(v4,v6)=(3, 3) expect (3, 3) (3, 3)
PASS  ON: eth2 removed: tap1 only                    tap1(v4,v6)=(3, 3) tap2(v4,v6)=(0, 0) expect (3, 3) (0, 0)
PASS  ON: 4 s lifetime, before expiry: tap1          tap1(v4,v6)=(3, 3) tap2(v4,v6)=(0, 0) expect (3, 3) (0, 0)
PASS  ON: after expiry: nothing                      tap1(v4,v6)=(0, 0) tap2(v4,v6)=(0, 0) expect (0, 0) (0, 0)
PASS  ON: entry {eth1,eth2} again: both              tap1(v4,v6)=(3, 3) tap2(v4,v6)=(3, 3) expect (3, 3) (3, 3)
PASS  ON: eth1 down: tap2 only                       tap1(v4,v6)=(0, 0) tap2(v4,v6)=(3, 3) expect (0, 0) (3, 3)
PASS  ON: eth1 up again, entry was cleared: tap2 only tap1(v4,v6)=(0, 0) tap2(v4,v6)=(3, 3) expect (0, 0) (3, 3)
RESULT ON: 9 passed, 0 failed
OVERALL: PASS ( off=PASS on=PASS )

Multicast packets are currently forwarded through every network device,
no matter whether a receiver is reachable through it.  Add the options
that will select receiver-aware multicast forwarding:

* NET_IPFORWARD_MULTICAST enables the multicast forwarding table.  It depends
  on NET_IPFORWARD_BROADCAST because it filters the devices that this
  option forwards multicast packets to.  It is disabled by default, so
  the current behavior does not change.
* NET_IPFORWARD_MULTICAST_NENTRIES sets the size of the statically allocated
  table.

The options do not select any code yet, the table and its use in the
forwarding path follow in the next commits.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Comment thread net/netforward/netfwd_mcast.c Resolved
Comment thread net/netforward/netfwd_mcast.c Resolved
Comment thread net/netforward/netforward.h Resolved
Comment thread net/netforward/netforward.h Resolved
Comment thread net/netforward/ipv4_forward.c Resolved
*
****************************************************************************/

int netfwd_mcast_ioctl(int cmd, unsigned long arg)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not keep in netdev_ioctl as the static function like others

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depends on the interface question below. with socket options the handler moves to ipv4_setsockopt.c/ipv6_setsockopt.c and this file goes away, if we keep the ioctl i'll make it a static function in netdev_ioctl.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, either way is fine.

Comment thread include/nuttx/net/ioctl.h

/* Multicast forwarding table ***********************************************/

#define SIOCADDMCFWD _SIOC(0x0049) /* Add an egress interface to the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use Linux/POSIX compliant interface?

#define IP_ADD_MEMBERSHIP     (__SO_PROTOCOL + 4)  /* Join a multicast group */
#define IP_DROP_MEMBERSHIP    (__SO_PROTOCOL + 5)  /* Leave a multicast group */
#define IP_UNBLOCK_SOURCE     (__SO_PROTOCOL + 6)  /* Unblock previously
                                                    * blocked multicast source */
#define IP_BLOCK_SOURCE       (__SO_PROTOCOL + 7)  /* Stop receiving multicast
                                                    * data from source */

#define IP_ADD_SOURCE_MEMBERSHIP (__SO_PROTOCOL + 8) /* Join a multicast group;
                                                      * allow receive only from
                                                      * source */

#define IP_DROP_SOURCE_MEMBERSHIP (__SO_PROTOCOL + 9) /* Leave a source-specific
                                                       * group.  Stop receiving
                                                       * data from a given
                                                       * multicast group that come
                                                       * from a given source */

#define IP_MSFILTER           (__SO_PROTOCOL + 10) /* Access advanced, full-
                                                    * state filtering API */
#define IP_MULTICAST_ALL      (__SO_PROTOCOL + 11) /* Modify the delivery policy
                                                    * of multicast messages bound
                                                    * to INADDR_ANY */

/* SOL_IPV6 protocol-level socket options. */

#define IPV6_JOIN_GROUP       (__SO_PROTOCOL + 1)  /* Join a multicast group */
#define IPV6_LEAVE_GROUP      (__SO_PROTOCOL + 2)  /* Quit a multicast group */
#define IPV6_MULTICAST_HOPS   (__SO_PROTOCOL + 3)  /* Multicast hop limit */
#define IPV6_MULTICAST_IF     (__SO_PROTOCOL + 4)  /* Interface to use for
                                                    * outgoing multicast packets */
#define IPV6_MULTICAST_LOOP   (__SO_PROTOCOL + 5)  /* Multicast packets are
                                                    * delivered back to the local
                                                    * application */

/* Protocol-independent multicast options (RFC 3678) for the level
 * IPPROTO_IP or IPPROTO_IPV6, with the values of Linux.
 */

#define MCAST_JOIN_GROUP         (__SO_PROTOCOL + 31) /* group_req */
#define MCAST_BLOCK_SOURCE       (__SO_PROTOCOL + 32) /* group_source_req */
#define MCAST_UNBLOCK_SOURCE     (__SO_PROTOCOL + 33) /* group_source_req */
#define MCAST_LEAVE_GROUP        (__SO_PROTOCOL + 34) /* group_req */
#define MCAST_JOIN_SOURCE_GROUP  (__SO_PROTOCOL + 35) /* group_source_req */
#define MCAST_LEAVE_SOURCE_GROUP (__SO_PROTOCOL + 36) /* group_source_req */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the linux interface for this is the multicast routing api (linux/mroute.h, mroute6.h): MRT_INIT on a raw igmp socket, then MRT_ADD_VIF / MRT_ADD_MFC, i.e. the kernel mfc that smcroute/mrouted/pimd work with. i'd implement a minimal subset: vifs by ifindex, (,G) entries via MRT_ADD_MFC_PROXY with a zero origin, mapped onto the current table, and drop SIOCADDMCFWD/SIOCDELMCFWD. ipv4 needs a raw igmp socket first, since nuttx only has raw icmp/icmpv6 ip sockets today, so that would be a separate prep pr.
or did you mean socket options with struct group_req like MCAST_JOIN_GROUP? that's smaller, but it would be a nuttx-only option.
note that linux (
,G) entries also check the interface the packet arrived on, today's table accepts any incoming interface, so the mrt option would add that check.
which one do you prefer? apps#4287 is already merged, so the test app would get a follow-up pr either way.

@xiaoxiang781216 xiaoxiang781216 Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's better to align with linux userspace contract, we can implement the minimal feature what we want now and improve the functionality step by step.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, will do. plan: a small prep pr adding raw IPPROTO_IGMP sockets (needed for MRT_INIT), then this pr switches from SIOCADDMCFWD/SIOCDELMCFWD to a minimal MRT_/MRT6_ subset, then a follow-up apps pr for the test app (also for the kconfig rename). i'll convert this pr to draft until then.

Add the table that will tell the forwarding path which network devices a
multicast packet must be forwarded through.  Each entry is a (*,G) group
with a bitmap of egress interface indices and an optional expiry time.

* The table is a static array of CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES
  entries, searched linearly.  There is no dynamic allocation.
* netfwd_mcast_join() and netfwd_mcast_leave() add and remove one egress
  interface of a group.  Rejecting interface indices outside of
  1..MAX_IFINDEX keeps every index inside the 32 bit bitmap.
* netfwd_mcast_lookup() returns the egress bitmap of a group, or zero if
  the group has no entry or the entry has expired.
* Expiry is checked on lookup and expired slots are reused by the next
  join, so no timer or work item is needed.
* netfwd_mcast_ifdown() clears a device from every entry.

The table has its own mutex, private to netfwd_mcast.c and taken only by
the functions above.  It is a leaf lock: nothing is called while it is
held.  The packet path, which holds the lock of the receiving device but
not the network lock, can therefore look up the table safely while the
table is changed from another context.  The table is built only when
NET_IPFORWARD_MULTICAST is selected and nothing uses it yet.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
@Brijesh-Thakkar
Brijesh-Thakkar force-pushed the feat/ipfwd-mcast-stage1 branch from 4ab48f0 to b0e3e4e Compare December 6, 2026 08:06
Comment thread net/devif/ipv4_input.c
@@ -376,23 +376,57 @@ static int ipv4_in(FAR struct net_driver_s *dev)
}
#endif

#ifdef CONFIG_NET_IPFORWARD_BROADCAST

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

·#ifdef CONFIG_NET_IPFORWARD_BROADCAST
  if (!forwarded)
#endif
#ifdef CONFIG_NET_STATISTICS
      g_netstats.ipv4.drop++;
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. i put braces around the counter though: with NET_STATISTICS off (the default) the bare if (!forwarded) would guard the goto drop below it, and a forwarded packet would fall through to local input.

Comment thread net/devif/ipv4_input.c Outdated
* which only decide if the packet is delivered locally.
*/

forwarded = ipv4_forward_broadcast(dev, ipv4);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move after line 418 and revert the change at line 411-412

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread net/devif/ipv4_input.c Outdated
* it is not a dropped packet.
*/

if (forwarded)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

·#ifdef CONFIG_NET_IPFORWARD_BROADCAST
  if (!forwarded)
#endif
#ifdef CONFIG_NET_STATISTICS
      g_netstats.ipv4.drop++;
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, same braces. the guard here is NET_IPFORWARD_MULTICAST since forwarded only exists with it.

Comment thread net/netforward/Kconfig Outdated
@@ -70,6 +70,37 @@ config NET_IPFORWARD_BROADCAST
If selected, broadcast packets received on one network device will
be forwarded though other network devices.

config NET_IPFORWARD_MCAST

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config NET_IPFORWARD_MCAST
config NET_IPFORWARD_MULTICAST

align with NET_IPFORWARD_BROADCAST

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread net/netforward/Kconfig Outdated
through all network devices as before, and no additional code or
data is built.

config NET_IPFORWARD_MCAST_NENTRIES

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config NET_IPFORWARD_MCAST_NENTRIES
config NET_IPFORWARD_MUTLICAST_NENTRIES

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, used NET_IPFORWARD_MULTICAST_NENTRIES (assuming MUTLICAST was a typo)

With NET_IPFORWARD_MULTICAST, an IPv4 multicast packet is forwarded only
through the network devices that the multicast forwarding table lists for
its group.  A group without an entry is not forwarded.

* ipv4_forward_broadcast() looks the group up once, removes the receiving
  device from the result and returns before walking the devices when no
  egress interface is left.  The bitmap is passed to the per device
  callback in a new struct ipv4_fwdarg_s, private to ipv4_forward.c.  The
  callback only tests the bit of its device, after its existing check that
  excludes the receiving device, so a packet can never go back out the
  interface it came in on.
* ipv4_forward_broadcast() now always returns true if the packet was
  forwarded through at least one device, with or without
  NET_IPFORWARD_MULTICAST.  The callback records it in struct ipv4_fwdarg_s,
  which it now always receives instead of the device.
* Broadcast packets are not multicast and are still forwarded through all
  devices.  The other filters are unchanged: source device flags, link
  local 224.0.0.0/24, and TTL.
* ipv4_in() now also forwards a multicast packet whose group was not
  joined on the receiving device, because the forwarding decision depends
  on the egress devices, not on the joined groups.  Local delivery is still
  gated by the joined groups.  A packet that was forwarded but is not for
  this host is released without counting a drop.
* ipv4_in() does the same for a broadcast packet: if it was forwarded and
  no protocol takes it, it is released without counting a drop.

Without NET_IPFORWARD_MULTICAST, the only change in behavior is that last one:
a forwarded broadcast packet that is not UDP (or with no UDP broadcast
support) is no longer counted in the IPv4 drop statistics.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
With NET_IPFORWARD_MULTICAST, an IPv6 multicast packet is forwarded only
through the network devices that the multicast forwarding table lists for
its group.  A group without an entry is not forwarded.

ipv6_forward_broadcast() looks the group up once, removes the receiving
device from the result and returns before walking the devices when no
egress interface is left.  The bitmap is passed to the per device callback
in a struct ipv6_fwdarg_s, private to ipv6_forward.c, like it is for IPv4.
The callback only tests the bit of its device, after its existing check
that excludes the receiving device, so a packet can never go back out the
interface it came in on.

The other filters are unchanged: source device flags, the reserved,
interface-local and link-local scopes, and the hop limit.

Like ipv4_forward_broadcast(), ipv6_forward_broadcast() now always returns
true if the packet was forwarded through at least one device, with or
without NET_IPFORWARD_MULTICAST.

ipv6_in() does not need a change and does not use the result.  It has no
check of the groups that were joined on the receiving device before it
forwards, and it does not count a packet as dropped after it was forwarded.

Without NET_IPFORWARD_MULTICAST, the behavior is not changed.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
…register.

The multicast forwarding table identifies the egress devices by their
interface index.  Remove a device from every entry of the table when it
goes down, so a packet is not forwarded through a device that is not
running and the entry is not revived when the device comes back up.  Also
remove it when the device is unregistered, because its interface index
can then be assigned to another device, which must not inherit the entries
of the old one.

netdev_unregister() clears the table with the device list lock held,
before the interface index is freed.  The table lock is a leaf lock, so it
can be taken there.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
Add SIOCADDMCFWD and SIOCDELMCFWD to add and remove an egress interface of
a multicast group, so that the forwarding table can be configured from
user space.  The argument is a struct netfwd_mcastreq_s with the group (IPv4
or IPv6), the name of the interface and, to add, the lifetime of the entry
in seconds, where zero means no expiry.

* A group that is not a multicast address is refused with -EINVAL, an
  unknown interface with -ENODEV and a full table with -ENOSPC.
* The lifetime is converted to ticks with SEC2TICK() on a clock_t.  A
  lifetime for which the conversion could overflow is refused.
* The interface name is resolved and the table updated with the device
  list locked, so the device cannot be unregistered in between and leave
  its interface index in the table.

The command handling is in its own file and is chained into the socket
ioctl handler like the bridge commands.  It is the only user space
interface of the table, so it can be replaced, for example by MRT_*
socket options, without changing the table or the forwarding path.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
Document CONFIG_NET_IPFORWARD_MULTICAST: how multicast packets are forwarded
through the egress interfaces of the multicast forwarding table, what is
unchanged, the size and lifetime of the table entries, and the
SIOCADDMCFWD and SIOCDELMCFWD ioctl commands that configure the table.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
@Brijesh-Thakkar
Brijesh-Thakkar force-pushed the feat/ipfwd-mcast-stage1 branch from b0e3e4e to fb28e04 Compare December 6, 2026 16:21
@Brijesh-Thakkar
Brijesh-Thakkar marked this pull request as draft December 6, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MULTICAST FORWARDING] Multicast forwarding lacks receiver-aware decision making

3 participants