Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/os/networking/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Network Support
socketcan.rst
pkt.rst
ipfilter.rst
ipforward_mcast.rst
nat.rst
bridge.rst
netdev.rst
Expand Down
95 changes: 95 additions & 0 deletions Documentation/os/networking/ipforward_mcast.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
===================================
Receiver-Aware Multicast Forwarding
===================================

By default, a multicast packet that is received on a network device is
forwarded through every other network device when
``CONFIG_NET_IPFORWARD_BROADCAST`` is selected, whether or not any receiver
is reachable through that device. ``CONFIG_NET_IPFORWARD_MULTICAST`` makes
the forwarding decision depend on the receivers: a multicast packet is
forwarded only through the network devices that are listed for its group in a
multicast forwarding table.

Behavior
========

With ``CONFIG_NET_IPFORWARD_MULTICAST``:

* A multicast packet is forwarded through the devices listed in the table
entry of its destination group, and only through them.
* A multicast packet whose group has no entry is not forwarded.
* A packet is never forwarded back out the device that it was received on.
* Broadcast packets are not affected and are still forwarded through all
devices.
* The other forwarding filters still apply: ``IFF_NOSRC_FORWARD`` and
``IFF_NODST_FORWARD``, the link-local range 224.0.0.0/24 of IPv4, the
reserved, interface-local and link-local scopes of IPv6 (ffx0, ffx1 and
ffx2), and the TTL or hop limit.
* Delivery of the packet to the local host is not changed. It still depends
on the groups that were joined with IGMP or MLD on the receiving device.

Without ``CONFIG_NET_IPFORWARD_MULTICAST``, the behavior does not change and
no additional code or data is built.

Only IPv4 and IPv6 groups, without a source, are supported: an entry is a
``(*,G)`` entry.

Forwarding Table
================

The table has ``CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES`` entries and is
statically allocated. Each entry holds a group address, a bitmap of egress
interface indices, and an optional expiry time. The size of an entry is about
32 bytes when IPv6 is enabled and about 24 bytes for IPv4 only.

An entry that has an expiry time is ignored once the time has elapsed, and its
slot is reused by the next entry that is added. When a network device goes
down or is unregistered, it is removed from every entry, and an entry without
any device left is freed.

Configuration
=============

The table is configured with two ioctl commands on a socket, defined in
``nuttx/net/ioctl.h``. The argument is a ``struct netfwd_mcastreq_s`` from
``nuttx/net/netfwd_mcast.h``:

``SIOCADDMCFWD``
Add an egress interface to the entry of a group. The entry is created if
the group does not have one. ``imr_lifetime`` is the lifetime of the entry
in seconds, zero for an entry that does not expire. Adding a device
restarts the lifetime of the entry.

``SIOCDELMCFWD``
Remove an egress interface from the entry of a group. The entry is freed
when no interface is left.

For example, to forward the group 239.1.1.1 through ``eth1``:

.. code-block:: c
struct netfwd_mcastreq_s req;
struct sockaddr_in *group = (struct sockaddr_in *)&req.imr_group;
int sd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&req, 0, sizeof(req));
group->sin_family = AF_INET;
group->sin_addr.s_addr = inet_addr("239.1.1.1");
strlcpy(req.imr_ifname, "eth1", IFNAMSIZ);
ioctl(sd, SIOCADDMCFWD, (unsigned long)&req);
``EINVAL`` is returned if the group is not a multicast address, ``ENODEV`` if
the interface does not exist, and ``ENOSPC`` if the table is full.
``SIOCDELMCFWD`` returns ``ENOENT`` if there is nothing to remove.

Locking
=======

The table has its own lock, private to ``netfwd_mcast.c``. It is taken only
inside the table functions and nothing is called while it is held, so it is
a leaf lock: the packet path takes it with the lock of the receiving device
held, and the network lock is not needed to forward a packet. The ioctls
resolve the interface name with the device list locked, and
``netdev_unregister()`` removes a device from the table with the same lock
held, so an entry cannot be added for a device that is being removed.
9 changes: 9 additions & 0 deletions include/nuttx/net/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@
#define SIOCGIFVLAN _SIOC(0x0043) /* Get VLAN interface */
#define SIOCSIFVLAN _SIOC(0x0044) /* Set VLAN interface */

/* 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.

* forwarding entry of a multicast
* group */
#define SIOCDELMCFWD _SIOC(0x004A) /* Remove an egress interface from
* the forwarding entry of a
* multicast group */

/****************************************************************************
* Public Type Definitions
****************************************************************************/
Expand Down
96 changes: 96 additions & 0 deletions include/nuttx/net/netfwd_mcast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/****************************************************************************
* include/nuttx/net/netfwd_mcast.h
*
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

#ifndef __INCLUDE_NUTTX_NET_NETFWD_MCAST_H
#define __INCLUDE_NUTTX_NET_NETFWD_MCAST_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <sys/socket.h>
#include <stdint.h>
#include <net/if.h>

#ifdef CONFIG_NET_IPFORWARD_MULTICAST

/****************************************************************************
* Public Types
****************************************************************************/

/* The argument of the SIOCADDMCFWD and SIOCDELMCFWD ioctl commands. The
* forwarding entry of a multicast group lists the egress interfaces that
* packets for the group are forwarded through.
*/

struct netfwd_mcastreq_s
{
struct sockaddr_storage imr_group; /* Group, AF_INET or AF_INET6 */
char imr_ifname[IFNAMSIZ]; /* Name of the egress interface */
uint32_t imr_lifetime; /* Lifetime of the entry in
* seconds, zero for no expiry.
* Only used to add */
};

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

#ifdef __cplusplus
extern "C"
{
#endif

/****************************************************************************
* Name: netfwd_mcast_ioctl
*
* Description:
* Handle the SIOCADDMCFWD and SIOCDELMCFWD ioctl commands.
*
* Input Parameters:
* cmd - The ioctl command
* arg - A pointer to a struct netfwd_mcastreq_s
*
* Returned Value:
* OK on success, -ENOTTY if cmd is not a multicast forwarding command,
* or another negated errno value on failure:
*
* -EINVAL - The group is not a multicast address, the lifetime is
* too long, or the interface name is not terminated.
* -EAFNOSUPPORT - The address family of the group is not supported.
* -ENODEV - There is no interface with that name.
* -ENOSPC - The forwarding table is full.
* -ENOENT - SIOCDELMCFWD found no such entry.
*
****************************************************************************/

int netfwd_mcast_ioctl(int cmd, unsigned long arg);

#ifdef __cplusplus
}
#endif

#endif /* CONFIG_NET_IPFORWARD_MULTICAST */
#endif /* __INCLUDE_NUTTX_NET_NETFWD_MCAST_H */
24 changes: 21 additions & 3 deletions net/devif/ipv4_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ static int ipv4_in(FAR struct net_driver_s *dev)
net_ipv4addr_broadcast(destipaddr, dev->d_netmask)))
{
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
ipv4_forward_broadcast(dev, ipv4);
bool forwarded = ipv4_forward_broadcast(dev, ipv4);
#endif

if (isfrag)
Expand All @@ -376,9 +376,15 @@ 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.

if (!forwarded)
#endif
{
#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv4.drop++;
g_netstats.ipv4.drop++;
#endif
}

goto drop;
}
else if (IN_MULTICAST(NTOHL(destipaddr)))
Expand All @@ -393,9 +399,21 @@ static int ipv4_in(FAR struct net_driver_s *dev)
else
#endif
{
#ifdef CONFIG_NET_IPFORWARD_MULTICAST
/* The group was not joined on this device, but the packet may
* still have egress interfaces in the forwarding table.
*/

bool forwarded = ipv4_forward_broadcast(dev, ipv4);

if (!forwarded)
#endif
{
#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv4.drop++;
g_netstats.ipv4.drop++;
#endif
}

goto drop;
}
}
Expand Down
17 changes: 17 additions & 0 deletions net/netdev/netdev_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <nuttx/net/netdev.h>
#include <nuttx/net/radiodev.h>
#include <nuttx/net/bridge.h>
#include <nuttx/net/netfwd_mcast.h>
#include <nuttx/net/vlan.h>

#ifdef CONFIG_NET_6LOWPAN
Expand Down Expand Up @@ -86,6 +87,7 @@
#include "devif/devif.h"
#include "igmp/igmp.h"
#include "icmpv6/icmpv6.h"
#include "netforward/netforward.h"
#include "route/route.h"
#include "netlink/netlink.h"
#include "utils/utils.h"
Expand Down Expand Up @@ -2083,6 +2085,15 @@ int psock_vioctl(FAR struct socket *psock, int cmd, va_list ap)
}
#endif

#ifdef CONFIG_NET_IPFORWARD_MULTICAST
/* Check for a multicast forwarding table command */

if (ret == -ENOTTY)
{
ret = netfwd_mcast_ioctl(cmd, arg);
}
#endif

#ifdef HAVE_IEEE802154_IOCTL
/* Check for a IEEE802.15.4 network device IOCTL command */

Expand Down Expand Up @@ -2224,6 +2235,12 @@ int netdev_ifdown(FAR struct net_driver_s *dev)

dev->d_flags &= ~(IFF_UP | IFF_RUNNING);

#ifdef CONFIG_NET_IPFORWARD_MULTICAST
/* Stop forwarding multicast packets through this device */

netfwd_mcast_ifdown(dev);
#endif

/* Update the driver status */

netlink_device_notify(dev);
Expand Down
11 changes: 11 additions & 0 deletions net/netdev/netdev_unregister.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <nuttx/net/netdev.h>

#include "mld/mld.h"
#include "netforward/netforward.h"
#include "utils/utils.h"
#include "netdev/netdev.h"

Expand Down Expand Up @@ -148,6 +149,16 @@ int netdev_unregister(FAR struct net_driver_s *dev)
curr->flink = NULL;
}

#ifdef CONFIG_NET_IPFORWARD_MULTICAST
/* Remove the device from the multicast forwarding table before its
* interface index can be assigned to another device. This is done
* with the list lock held, like the SIOCADDMCFWD ioctl, so that the
* ioctl cannot add the index again after it was removed.
*/

netfwd_mcast_ifdown(dev);
#endif

#ifdef CONFIG_NETDEV_IFINDEX
free_ifindex(dev->d_ifindex);
g_netdev_by_index[dev->d_ifindex - 1] = NULL;
Expand Down
4 changes: 4 additions & 0 deletions net/netforward/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ if(CONFIG_NET_FORWARD)
list(APPEND SRCS netfwd_dropstats.c)
endif()

if(CONFIG_NET_IPFORWARD_MULTICAST)
list(APPEND SRCS netfwd_mcast.c netfwd_mcast_ioctl.c)
endif()

target_sources(net PRIVATE ${SRCS})
endif()
31 changes: 31 additions & 0 deletions net/netforward/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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_MULTICAST
bool "Receiver-aware multicast forwarding"
default n
depends on NET_IPFORWARD_BROADCAST
select NETDEV_IFINDEX
---help---
If selected, multicast packets are forwarded only through the
network devices that are listed for the destination group in a
multicast forwarding table, which identifies the devices by their
interface index. A multicast packet whose group has
no entry in the table is not forwarded. Broadcast packets are not
affected and are still forwarded through all devices.

Local delivery of multicast packets is not changed by this option:
it is still controlled by the groups that were joined on the
receiving device with IGMP or MLD.

If this option is not selected, multicast packets are forwarded
through all network devices as before, and no additional code or
data is built.

config NET_IPFORWARD_MULTICAST_NENTRIES
int "Number of multicast forwarding entries"
default 8
range 1 255
depends on NET_IPFORWARD_MULTICAST
---help---
The maximum number of multicast groups that can have an entry in
the multicast forwarding table. The table is statically
allocated, each entry takes about 32 bytes.

config NET_FORWARD_NCALLBACKS
int "Temporary cached forward callbacks max number"
default 0
Expand Down
4 changes: 4 additions & 0 deletions net/netforward/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ifeq ($(CONFIG_NET_STATISTICS),y)
NET_CSRCS += netfwd_dropstats.c
endif

ifeq ($(CONFIG_NET_IPFORWARD_MULTICAST),y)
NET_CSRCS += netfwd_mcast.c netfwd_mcast_ioctl.c
endif

# Include IP forwarding build support

DEPPATH += --dep-path netforward
Expand Down
Loading
Loading