diff --git a/Documentation/os/networking/index.rst b/Documentation/os/networking/index.rst index c8e55d2f0d006..180fec3571350 100644 --- a/Documentation/os/networking/index.rst +++ b/Documentation/os/networking/index.rst @@ -64,6 +64,7 @@ Network Support socketcan.rst pkt.rst ipfilter.rst + ipforward_mcast.rst nat.rst bridge.rst netdev.rst diff --git a/Documentation/os/networking/ipforward_mcast.rst b/Documentation/os/networking/ipforward_mcast.rst new file mode 100644 index 0000000000000..6002bd09e476f --- /dev/null +++ b/Documentation/os/networking/ipforward_mcast.rst @@ -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. diff --git a/include/nuttx/net/ioctl.h b/include/nuttx/net/ioctl.h index c3d5f14c3dc87..f921d423781d8 100644 --- a/include/nuttx/net/ioctl.h +++ b/include/nuttx/net/ioctl.h @@ -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 + * 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 ****************************************************************************/ diff --git a/include/nuttx/net/netfwd_mcast.h b/include/nuttx/net/netfwd_mcast.h new file mode 100644 index 0000000000000..1c2438ae7f554 --- /dev/null +++ b/include/nuttx/net/netfwd_mcast.h @@ -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 + +#include +#include +#include + +#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 */ diff --git a/net/devif/ipv4_input.c b/net/devif/ipv4_input.c index 0c36df762fa69..a32cb4d5132de 100644 --- a/net/devif/ipv4_input.c +++ b/net/devif/ipv4_input.c @@ -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) @@ -376,9 +376,15 @@ static int ipv4_in(FAR struct net_driver_s *dev) } #endif +#ifdef CONFIG_NET_IPFORWARD_BROADCAST + if (!forwarded) +#endif + { #ifdef CONFIG_NET_STATISTICS - g_netstats.ipv4.drop++; + g_netstats.ipv4.drop++; #endif + } + goto drop; } else if (IN_MULTICAST(NTOHL(destipaddr))) @@ -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; } } diff --git a/net/netdev/netdev_ioctl.c b/net/netdev/netdev_ioctl.c index 525b544d00017..eb27a07e8d175 100644 --- a/net/netdev/netdev_ioctl.c +++ b/net/netdev/netdev_ioctl.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #ifdef CONFIG_NET_6LOWPAN @@ -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" @@ -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 */ @@ -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); diff --git a/net/netdev/netdev_unregister.c b/net/netdev/netdev_unregister.c index 2729ef1ee6574..ce3b70640e23f 100644 --- a/net/netdev/netdev_unregister.c +++ b/net/netdev/netdev_unregister.c @@ -38,6 +38,7 @@ #include #include "mld/mld.h" +#include "netforward/netforward.h" #include "utils/utils.h" #include "netdev/netdev.h" @@ -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; diff --git a/net/netforward/CMakeLists.txt b/net/netforward/CMakeLists.txt index 34f701361f856..fd17b49f72c53 100644 --- a/net/netforward/CMakeLists.txt +++ b/net/netforward/CMakeLists.txt @@ -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() diff --git a/net/netforward/Kconfig b/net/netforward/Kconfig index ae8a552681ffc..f2772cd1b98f0 100644 --- a/net/netforward/Kconfig +++ b/net/netforward/Kconfig @@ -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 diff --git a/net/netforward/Make.defs b/net/netforward/Make.defs index f507540c515c5..06b636891062c 100644 --- a/net/netforward/Make.defs +++ b/net/netforward/Make.defs @@ -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 diff --git a/net/netforward/ipv4_forward.c b/net/netforward/ipv4_forward.c index 8af10a7684beb..75365c9eb9823 100644 --- a/net/netforward/ipv4_forward.c +++ b/net/netforward/ipv4_forward.c @@ -47,6 +47,25 @@ #if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NET_IPv4) +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_NET_IPFORWARD_BROADCAST +/* This is the argument of ipv4_forward_callback() */ + +struct ipv4_fwdarg_s +{ + FAR struct net_driver_s *dev; /* Device that received the packet */ +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + uint32_t oifmap; /* Egress interfaces, see + * NETFWD_MCAST_IFBIT() */ +#endif + bool forwarded; /* True if the packet was forwarded + * through at least one device */ +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -377,7 +396,8 @@ static int ipv4_dev_forward(FAR struct net_driver_s *dev, static int ipv4_forward_callback(FAR struct net_driver_s *fwddev, FAR void *arg) { - FAR struct net_driver_s *dev = (FAR struct net_driver_s *)arg; + FAR struct ipv4_fwdarg_s *fwdarg = (FAR struct ipv4_fwdarg_s *)arg; + FAR struct net_driver_s *dev = fwdarg->dev; FAR struct ipv4_hdr_s *ipv4; FAR struct iob_s *iob; int ret; @@ -400,6 +420,18 @@ static int ipv4_forward_callback(FAR struct net_driver_s *fwddev, if (fwddev != dev) { +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + /* Skip the devices that are not egress interfaces of the group. This + * is checked after the receiving device was excluded above, so a + * packet never goes back out the device that it came in on. + */ + + if ((fwdarg->oifmap & NETFWD_MCAST_IFBIT(fwddev->d_ifindex)) == 0) + { + return OK; + } +#endif + /* Backup the forward IP packet */ iob = netdev_iob_clone(dev, true); @@ -428,6 +460,7 @@ static int ipv4_forward_callback(FAR struct net_driver_s *fwddev, /* Restore device iob with backup iob */ netdev_iob_replace(dev, iob); + fwdarg->forwarded = true; } return OK; @@ -618,14 +651,25 @@ int ipv4_forward(FAR struct net_driver_s *dev, FAR struct ipv4_hdr_s *ipv4) * - ipv4 points to the IPv4 header with dev->d_buf. * * Returned Value: - * None + * True if the packet was forwarded through at least one device. * ****************************************************************************/ #ifdef CONFIG_NET_IPFORWARD_BROADCAST -void ipv4_forward_broadcast(FAR struct net_driver_s *dev, +bool ipv4_forward_broadcast(FAR struct net_driver_s *dev, FAR struct ipv4_hdr_s *ipv4) { + struct ipv4_fwdarg_s fwdarg; +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + in_addr_t destipaddr; +#endif + + fwdarg.dev = dev; +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + fwdarg.oifmap = NETFWD_MCAST_ALLIF; +#endif + fwdarg.forwarded = false; + /* Check if source device supports IP forwarding capability. * Broadcast/multicast forwarding is only allowed if the receiving * device has SRC_FORWARD enabled. This is consistent with the unicast @@ -636,7 +680,7 @@ void ipv4_forward_broadcast(FAR struct net_driver_s *dev, { nwarn("WARNING: IP broadcast forwarding disabled " "on source device %s\n", dev->d_ifname); - return; + return false; } /* Do not forward link-local multicast packets (224.0.0.0/24). @@ -648,19 +692,41 @@ void ipv4_forward_broadcast(FAR struct net_driver_s *dev, if ((net_ip4addr_conv32(ipv4->destipaddr) & HTONL(0xffffff00)) == HTONL(0xe0000000)) { - return; + return false; } /* Don't bother if the TTL would expire */ if (ipv4->ttl > 1) { +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + /* A multicast packet is forwarded only through the egress interfaces + * of its group. Find them once here, without the receiving device, + * and do nothing if there is none. A broadcast packet is forwarded + * through all devices. + */ + + destipaddr = net_ip4addr_conv32(ipv4->destipaddr); + if (IN_MULTICAST(NTOHL(destipaddr))) + { + fwdarg.oifmap = netfwd_mcast_lookup(AF_INET, &destipaddr) & + ~NETFWD_MCAST_IFBIT(dev->d_ifindex); + if (fwdarg.oifmap == 0) + { + return false; + } + } + +#endif + /* Forward the broadcast/multicast packet to all devices except, * of course, the device that received the packet. */ - netdev_foreach(ipv4_forward_callback, dev); + netdev_foreach(ipv4_forward_callback, &fwdarg); } + + return fwdarg.forwarded; } #endif diff --git a/net/netforward/ipv6_forward.c b/net/netforward/ipv6_forward.c index 3b9608371481c..1d0351ce23895 100644 --- a/net/netforward/ipv6_forward.c +++ b/net/netforward/ipv6_forward.c @@ -54,6 +54,25 @@ #define PACKET_FORWARDED 0 #define PACKET_NOT_FORWARDED 1 +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_NET_IPFORWARD_BROADCAST +/* This is the argument of ipv6_forward_callback() */ + +struct ipv6_fwdarg_s +{ + FAR struct net_driver_s *dev; /* Device that received the packet */ +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + uint32_t oifmap; /* Egress interfaces, see + * NETFWD_MCAST_IFBIT() */ +#endif + bool forwarded; /* True if the packet was forwarded + * through at least one device */ +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -515,7 +534,8 @@ static int ipv6_dev_forward(FAR struct net_driver_s *dev, static int ipv6_forward_callback(FAR struct net_driver_s *fwddev, FAR void *arg) { - FAR struct net_driver_s *dev = (FAR struct net_driver_s *)arg; + FAR struct ipv6_fwdarg_s *fwdarg = (FAR struct ipv6_fwdarg_s *)arg; + FAR struct net_driver_s *dev = fwdarg->dev; FAR struct ipv6_hdr_s *ipv6; FAR struct iob_s *iob; int ret; @@ -538,6 +558,18 @@ static int ipv6_forward_callback(FAR struct net_driver_s *fwddev, if (fwddev != dev) { +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + /* Skip the devices that are not egress interfaces of the group. This + * is checked after the receiving device was excluded above, so a + * packet never goes back out the device that it came in on. + */ + + if ((fwdarg->oifmap & NETFWD_MCAST_IFBIT(fwddev->d_ifindex)) == 0) + { + return OK; + } +#endif + /* Backup the forward IP packet */ iob = netdev_iob_clone(dev, true); @@ -566,6 +598,7 @@ static int ipv6_forward_callback(FAR struct net_driver_s *fwddev, /* Restore device iob with backup iob */ netdev_iob_replace(dev, iob); + fwdarg->forwarded = true; } return OK; @@ -790,14 +823,22 @@ int ipv6_forward(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6) * - ipv6 points to the IPv6 header with dev->d_buf. * * Returned Value: - * None + * True if the packet was forwarded through at least one device. * ****************************************************************************/ #ifdef CONFIG_NET_IPFORWARD_BROADCAST -void ipv6_forward_broadcast(FAR struct net_driver_s *dev, +bool ipv6_forward_broadcast(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6) { + struct ipv6_fwdarg_s fwdarg; + + fwdarg.dev = dev; +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + fwdarg.oifmap = NETFWD_MCAST_ALLIF; +#endif + fwdarg.forwarded = false; + /* Check if source device supports IP forwarding capability. * Broadcast/multicast forwarding is only allowed if the receiving * device has SRC_FORWARD enabled. This is consistent with the unicast @@ -808,7 +849,7 @@ void ipv6_forward_broadcast(FAR struct net_driver_s *dev, { nwarn("WARNING: IP broadcast forwarding disabled " "on source device %s\n", dev->d_ifname); - return; + return false; } /* Do not forward reserved, interface-local, or link-local multicast @@ -819,19 +860,40 @@ void ipv6_forward_broadcast(FAR struct net_driver_s *dev, ((ipv6->destipaddr[0] & HTONS(0xff0f)) == HTONS(0xff01)) || ((ipv6->destipaddr[0] & HTONS(0xff0f)) == HTONS(0xff02))) { - return; + return false; } /* Don't bother if the TTL would expire */ if (ipv6->ttl > 1) { +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + /* A multicast packet is forwarded only through the egress interfaces + * of its group. Find them once here, without the receiving device, + * and do nothing if there is none. Every address that gets here is + * a multicast address, there is no IPv6 broadcast. + */ + + if (net_is_addr_mcast(ipv6->destipaddr)) + { + fwdarg.oifmap = netfwd_mcast_lookup(AF_INET6, ipv6->destipaddr) & + ~NETFWD_MCAST_IFBIT(dev->d_ifindex); + if (fwdarg.oifmap == 0) + { + return false; + } + } + +#endif + /* Forward the broadcast/multicast packet to all devices except, * of course, the device that received the packet. */ - netdev_foreach(ipv6_forward_callback, dev); + netdev_foreach(ipv6_forward_callback, &fwdarg); } + + return fwdarg.forwarded; } #endif diff --git a/net/netforward/netforward.h b/net/netforward/netforward.h index d5260396b34e6..fff221ffdf61d 100644 --- a/net/netforward/netforward.h +++ b/net/netforward/netforward.h @@ -30,7 +30,10 @@ #include #include +#include #include +#include +#include #undef HAVE_FWDALLOC #ifdef CONFIG_NET_FORWARD @@ -54,6 +57,18 @@ static_assert(CONFIG_IOB_NBUFFERS > CONFIG_NET_FORWARD_NSTRUCT, "net forward may consume all the IOB and break netdev logic"); #endif +#ifdef CONFIG_NET_IPFORWARD_MULTICAST +/* Bit of an interface in an egress interface bitmap of the multicast + * forwarding table. Interface indices start at one. + */ + +# define NETFWD_MCAST_IFBIT(ifindex) ((uint32_t)1 << ((ifindex) - 1)) + +/* Egress interface bitmap that selects every interface */ + +# define NETFWD_MCAST_ALLIF UINT32_MAX +#endif + /* Allocate a new IP forwarding data callback */ #define netfwd_callback_alloc(dev) devif_callback_alloc(dev, \ @@ -126,6 +141,11 @@ void netfwd_free(FAR struct forward_s *fwd); * function will forward the broadcast packet to other networks through * other network devices. * + * If CONFIG_NET_IPFORWARD_MULTICAST is enabled, a multicast packet is + * forwarded only through the devices that its group has in the multicast + * forwarding table, and nothing is forwarded if the group has no entry. + * Broadcast packets are still forwarded through all devices. + * * Input Parameters: * dev - The device on which the packet was received and which contains * the IPv4 packet. @@ -139,12 +159,12 @@ void netfwd_free(FAR struct forward_s *fwd); * - ipv4 points to the IPv4 header with dev->d_buf. * * Returned Value: - * None + * True if the packet was forwarded through at least one device. * ****************************************************************************/ #ifdef CONFIG_NET_IPFORWARD_BROADCAST -void ipv4_forward_broadcast(FAR struct net_driver_s *dev, +bool ipv4_forward_broadcast(FAR struct net_driver_s *dev, FAR struct ipv4_hdr_s *ipv4); #endif @@ -170,12 +190,12 @@ void ipv4_forward_broadcast(FAR struct net_driver_s *dev, * - ipv6 points to the IPv6 header with dev->d_buf. * * Returned Value: - * None + * True if the packet was forwarded through at least one device. * ****************************************************************************/ #ifdef CONFIG_NET_IPFORWARD_BROADCAST -void ipv6_forward_broadcast(FAR struct net_driver_s *dev, +bool ipv6_forward_broadcast(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6); #endif @@ -387,5 +407,105 @@ void ipv4_dropstats(FAR struct ipv4_hdr_s *ipv4); # define ipv4_dropstats(ipv4) #endif +#ifdef CONFIG_NET_IPFORWARD_MULTICAST + +/**************************************************************************** + * Name: netfwd_mcast_join + * + * Description: + * Add an egress interface to the entry of a multicast group, creating the + * entry if the group does not have one. The lifetime of the entry is + * restarted. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address: a pointer to an in_addr_t for AF_INET or + * to a net_ipv6addr_t for AF_INET6 + * ifindex - The index of the egress interface, 1 to MAX_IFINDEX + * lifetime - The lifetime of the entry in ticks, zero for no expiry + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned on + * failure: -EINVAL for a bad argument, -ENOSPC if the table is full. + * + * Assumptions: + * Takes the lock of the table, never the network lock. + * + ****************************************************************************/ + +int netfwd_mcast_join(sa_family_t family, FAR const void *group, int ifindex, + clock_t lifetime); + +/**************************************************************************** + * Name: netfwd_mcast_leave + * + * Description: + * Remove an egress interface from the entry of a multicast group. The + * entry is freed when no interface is left. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address, see netfwd_mcast_join() + * ifindex - The index of the egress interface, 1 to MAX_IFINDEX + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned on + * failure: -EINVAL if ifindex is out of range, -ENOENT if the group has + * no entry or the interface is not in it. + * + * Assumptions: + * Takes the lock of the table, never the network lock. + * + ****************************************************************************/ + +int netfwd_mcast_leave(sa_family_t family, FAR const void *group, + int ifindex); + +/**************************************************************************** + * Name: netfwd_mcast_lookup + * + * Description: + * Get the egress interfaces of a multicast group. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address, see netfwd_mcast_join() + * + * Returned Value: + * The bitmap of the egress interfaces: the bit NETFWD_MCAST_IFBIT(ifindex) + * is set for each interface the group is to be forwarded to. Zero is + * returned if the group has no entry or if its entry has expired. + * + * Assumptions: + * Takes the lock of the table, never the network lock. + * + ****************************************************************************/ + +uint32_t netfwd_mcast_lookup(sa_family_t family, FAR const void *group); + +/**************************************************************************** + * Name: netfwd_mcast_ifdown + * + * Description: + * Remove a network device from every entry of the table. This is called + * when the device goes down or is unregistered. + * + * Input Parameters: + * dev - The network device + * + * Returned Value: + * None + * + * Assumptions: + * Takes the lock of the table, never the network lock. + * + ****************************************************************************/ + +struct net_driver_s; /* Forward reference */ + +void netfwd_mcast_ifdown(FAR struct net_driver_s *dev); + +#endif /* CONFIG_NET_IPFORWARD_MULTICAST */ + #endif /* CONFIG_NET_IPFORWARD */ #endif /* __NET_IPFORWARD_IPFORWARD_H */ diff --git a/net/netforward/netfwd_mcast.c b/net/netforward/netfwd_mcast.c new file mode 100644 index 0000000000000..84a412d14ee56 --- /dev/null +++ b/net/netforward/netfwd_mcast.c @@ -0,0 +1,483 @@ +/**************************************************************************** + * net/netforward/netfwd_mcast.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netforward/netforward.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The egress interfaces of an entry are kept in a uint32_t bitmap, one bit + * for each possible interface index. + */ + +static_assert(MAX_IFINDEX <= 32, "Interface bitmap is too small"); + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* One (*,G) entry of the multicast forwarding table */ + +struct netfwd_mcast_s +{ + clock_t expire; /* Tick when the entry expires, zero for never */ + union + { +#ifdef CONFIG_NET_IPv4 + in_addr_t v4; /* IPv4 group address, network order */ +#endif +#ifdef CONFIG_NET_IPv6 + net_ipv6addr_t v6; /* IPv6 group address */ +#endif + } group; /* Group address */ + uint32_t oifmap; /* Egress interfaces, see NETFWD_MCAST_IFBIT() */ + uint8_t family; /* AF_INET or AF_INET6, zero if the entry is free */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* The multicast forwarding table and its lock. The lock is a leaf lock: + * it is only taken by the public functions of this file, and nothing is + * called with it held, so it can be taken with any other network lock held. + * It is not the network lock, which the packet path does not hold. + */ + +static struct netfwd_mcast_s +g_netfwd_mcast[CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES]; +static mutex_t g_netfwd_mcast_lock = NXMUTEX_INITIALIZER; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netfwd_mcast_expired + * + * Description: + * Check if a used entry has expired. + * + * Input Parameters: + * entry - The entry to check + * now - The current time in ticks + * + * Returned Value: + * true if the entry has a lifetime and it has elapsed. + * + ****************************************************************************/ + +static bool netfwd_mcast_expired(FAR const struct netfwd_mcast_s *entry, + clock_t now) +{ + return entry->expire != 0 && entry->expire <= now; +} + +/**************************************************************************** + * Name: netfwd_mcast_match + * + * Description: + * Check if an entry is used and belongs to the group. + * + * Input Parameters: + * entry - The entry to check + * family - AF_INET or AF_INET6 + * group - The group address + * + * Returned Value: + * true if the entry belongs to the group. + * + ****************************************************************************/ + +static bool netfwd_mcast_match(FAR const struct netfwd_mcast_s *entry, + sa_family_t family, FAR const void *group) +{ + if (entry->family != family) + { + return false; + } + +#ifdef CONFIG_NET_IPv4 + if (family == AF_INET) + { + return net_ipv4addr_cmp(entry->group.v4, + *(FAR const in_addr_t *)group); + } +#endif + +#ifdef CONFIG_NET_IPv6 + if (family == AF_INET6) + { + return net_ipv6addr_cmp(entry->group.v6, (FAR const uint16_t *)group); + } +#endif + + return false; +} + +/**************************************************************************** + * Name: netfwd_mcast_find + * + * Description: + * Find the unexpired entry of a group. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address + * now - The current time in ticks + * + * Returned Value: + * The entry of the group, or NULL if the group has no entry. + * + * Assumptions: + * g_netfwd_mcast_lock is held. + * + ****************************************************************************/ + +static FAR struct netfwd_mcast_s * +netfwd_mcast_find(sa_family_t family, FAR const void *group, clock_t now) +{ + int i; + + for (i = 0; i < CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES; i++) + { + FAR struct netfwd_mcast_s *entry = &g_netfwd_mcast[i]; + + if (netfwd_mcast_match(entry, family, group) && + !netfwd_mcast_expired(entry, now)) + { + return entry; + } + } + + return NULL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netfwd_mcast_join + * + * Description: + * Add an egress interface to the entry of a multicast group, creating the + * entry if the group does not have one. The lifetime of the entry is + * restarted. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address: a pointer to an in_addr_t for AF_INET or + * to a net_ipv6addr_t for AF_INET6 + * ifindex - The index of the egress interface, 1 to MAX_IFINDEX + * lifetime - The lifetime of the entry in ticks, zero for no expiry + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned on + * failure: + * + * -EINVAL - The address family is not supported, the group is not a + * multicast address, ifindex is out of range, or the lifetime + * is negative or too long. + * -ENOSPC - The table is full. + * + * Assumptions: + * Takes the table lock, see g_netfwd_mcast_lock. + * + ****************************************************************************/ + +int netfwd_mcast_join(sa_family_t family, FAR const void *group, int ifindex, + clock_t lifetime) +{ + FAR struct netfwd_mcast_s *entry; + FAR struct netfwd_mcast_s *slot = NULL; + clock_t now; + int i; + + DEBUGASSERT(group != NULL); + + if (ifindex < 1 || ifindex > MAX_IFINDEX) + { + return -EINVAL; + } + + now = clock_systime_ticks(); + + if (lifetime < 0 || lifetime > CLOCK_MAX - now) + { + return -EINVAL; + } + + switch (family) + { +#ifdef CONFIG_NET_IPv4 + case AF_INET: + if (!IN_MULTICAST(NTOHL(*(FAR const in_addr_t *)group))) + { + return -EINVAL; + } + break; +#endif + +#ifdef CONFIG_NET_IPv6 + case AF_INET6: + if (!net_is_addr_mcast((FAR const uint16_t *)group)) + { + return -EINVAL; + } + break; +#endif + + default: + return -EINVAL; + } + + /* Look for the entry of the group. Remember the first entry that can be + * reused: either free or expired. + */ + + nxmutex_lock(&g_netfwd_mcast_lock); + + for (i = 0; i < CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES; i++) + { + entry = &g_netfwd_mcast[i]; + + if (netfwd_mcast_match(entry, family, group)) + { + if (netfwd_mcast_expired(entry, now)) + { + /* Forget the interfaces of the expired entry */ + + entry->oifmap = 0; + } + + slot = entry; + break; + } + + if (slot == NULL && + (entry->family == 0 || netfwd_mcast_expired(entry, now))) + { + slot = entry; + } + } + + if (slot == NULL) + { + nxmutex_unlock(&g_netfwd_mcast_lock); + return -ENOSPC; + } + + if (!netfwd_mcast_match(slot, family, group)) + { + /* A new entry for this group */ + + memset(slot, 0, sizeof(struct netfwd_mcast_s)); + slot->family = family; + +#ifdef CONFIG_NET_IPv4 + if (family == AF_INET) + { + slot->group.v4 = *(FAR const in_addr_t *)group; + } +#endif + +#ifdef CONFIG_NET_IPv6 + if (family == AF_INET6) + { + net_ipv6addr_copy(slot->group.v6, (FAR const uint16_t *)group); + } +#endif + } + + slot->oifmap |= NETFWD_MCAST_IFBIT(ifindex); + slot->expire = lifetime == 0 ? 0 : now + lifetime; + + nxmutex_unlock(&g_netfwd_mcast_lock); + return OK; +} + +/**************************************************************************** + * Name: netfwd_mcast_leave + * + * Description: + * Remove an egress interface from the entry of a multicast group. The + * entry is freed when no interface is left. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address, see netfwd_mcast_join() + * ifindex - The index of the egress interface, 1 to MAX_IFINDEX + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned on + * failure: + * + * -EINVAL - ifindex is out of range. + * -ENOENT - The group has no entry, or the interface is not in it. + * + * Assumptions: + * Takes the table lock, see g_netfwd_mcast_lock. + * + ****************************************************************************/ + +int netfwd_mcast_leave(sa_family_t family, FAR const void *group, + int ifindex) +{ + FAR struct netfwd_mcast_s *entry; + clock_t now; + int ret = OK; + + DEBUGASSERT(group != NULL); + + if (ifindex < 1 || ifindex > MAX_IFINDEX) + { + return -EINVAL; + } + + now = clock_systime_ticks(); + nxmutex_lock(&g_netfwd_mcast_lock); + + entry = netfwd_mcast_find(family, group, now); + if (entry == NULL || (entry->oifmap & NETFWD_MCAST_IFBIT(ifindex)) == 0) + { + ret = -ENOENT; + } + else + { + entry->oifmap &= ~NETFWD_MCAST_IFBIT(ifindex); + if (entry->oifmap == 0) + { + entry->family = 0; + } + } + + nxmutex_unlock(&g_netfwd_mcast_lock); + return ret; +} + +/**************************************************************************** + * Name: netfwd_mcast_lookup + * + * Description: + * Get the egress interfaces of a multicast group. + * + * Input Parameters: + * family - AF_INET or AF_INET6 + * group - The group address, see netfwd_mcast_join() + * + * Returned Value: + * The bitmap of the egress interfaces: the bit NETFWD_MCAST_IFBIT(ifindex) + * is set for each interface the group is to be forwarded to. Zero is + * returned if the group has no entry or if its entry has expired. + * + * Assumptions: + * Takes the table lock, see g_netfwd_mcast_lock. This is called on the + * packet path with the lock of the receiving device held. + * + ****************************************************************************/ + +uint32_t netfwd_mcast_lookup(sa_family_t family, FAR const void *group) +{ + FAR struct netfwd_mcast_s *entry; + uint32_t oifmap; + clock_t now; + + DEBUGASSERT(group != NULL); + + now = clock_systime_ticks(); + nxmutex_lock(&g_netfwd_mcast_lock); + + entry = netfwd_mcast_find(family, group, now); + oifmap = entry != NULL ? entry->oifmap : 0; + + nxmutex_unlock(&g_netfwd_mcast_lock); + return oifmap; +} + +/**************************************************************************** + * Name: netfwd_mcast_ifdown + * + * Description: + * Remove a network device from every entry of the table. This is called + * when the device goes down or is unregistered, so that its interface + * index does not stay in the table. + * + * Input Parameters: + * dev - The network device + * + * Returned Value: + * None + * + * Assumptions: + * Takes the table lock, see g_netfwd_mcast_lock. + * + ****************************************************************************/ + +void netfwd_mcast_ifdown(FAR struct net_driver_s *dev) +{ + int i; + + DEBUGASSERT(dev != NULL); + + if (dev->d_ifindex < 1 || dev->d_ifindex > MAX_IFINDEX) + { + return; + } + + nxmutex_lock(&g_netfwd_mcast_lock); + + for (i = 0; i < CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES; i++) + { + FAR struct netfwd_mcast_s *entry = &g_netfwd_mcast[i]; + + entry->oifmap &= ~NETFWD_MCAST_IFBIT(dev->d_ifindex); + if (entry->oifmap == 0) + { + entry->family = 0; + } + } + + nxmutex_unlock(&g_netfwd_mcast_lock); +} + diff --git a/net/netforward/netfwd_mcast_ioctl.c b/net/netforward/netfwd_mcast_ioctl.c new file mode 100644 index 0000000000000..7ccb1bec86985 --- /dev/null +++ b/net/netforward/netfwd_mcast_ioctl.c @@ -0,0 +1,191 @@ +/**************************************************************************** + * net/netforward/netfwd_mcast_ioctl.c + * + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "netforward/netforward.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The longest lifetime in seconds that can be converted to ticks. + * Converting a lifetime multiplies it by at most USEC_PER_SEC, and the + * result must also fit in a clock_t. + */ + +#define NETFWD_MCAST_MAXLIFE (CLOCK_MAX / USEC_PER_SEC) + +static_assert(TICK_PER_SEC <= USEC_PER_SEC, "Tick is shorter than 1us"); + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * 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) +{ + FAR struct netfwd_mcastreq_s *req = + (FAR struct netfwd_mcastreq_s *)(uintptr_t)arg; + FAR struct net_driver_s *dev; + FAR const void *group; + sa_family_t family; + clock_t lifetime = 0; + int ret; + + if (cmd != SIOCADDMCFWD && cmd != SIOCDELMCFWD) + { + return -ENOTTY; + } + + if (req == NULL) + { + return -EINVAL; + } + + /* Get the group and check that it is a multicast address */ + + family = req->imr_group.ss_family; + switch (family) + { +#ifdef CONFIG_NET_IPv4 + case AF_INET: + { + FAR struct sockaddr_in *addr = + (FAR struct sockaddr_in *)&req->imr_group; + + if (!IN_MULTICAST(NTOHL(addr->sin_addr.s_addr))) + { + return -EINVAL; + } + + group = &addr->sin_addr.s_addr; + } + break; +#endif + +#ifdef CONFIG_NET_IPv6 + case AF_INET6: + { + FAR struct sockaddr_in6 *addr = + (FAR struct sockaddr_in6 *)&req->imr_group; + + if (!net_is_addr_mcast(addr->sin6_addr.s6_addr16)) + { + return -EINVAL; + } + + group = addr->sin6_addr.s6_addr16; + } + break; +#endif + + default: + return -EAFNOSUPPORT; + } + + if (strnlen(req->imr_ifname, IFNAMSIZ) == IFNAMSIZ) + { + return -EINVAL; + } + + /* Convert the lifetime to ticks, refusing one that does not fit */ + + if (cmd == SIOCADDMCFWD) + { + if (req->imr_lifetime > NETFWD_MCAST_MAXLIFE) + { + return -EINVAL; + } + + lifetime = SEC2TICK((clock_t)req->imr_lifetime); + } + + /* Resolve the interface name and update the table with the device list + * locked, so that the device cannot be unregistered in between: that + * clears its interface index from the table, also with the list locked. + * The table has its own lock, which is taken inside the list lock. + */ + + netdev_list_lock(); + + dev = netdev_findbyname(req->imr_ifname); + if (dev == NULL) + { + ret = -ENODEV; + } + else if (cmd == SIOCADDMCFWD) + { + ret = netfwd_mcast_join(family, group, dev->d_ifindex, lifetime); + } + else + { + ret = netfwd_mcast_leave(family, group, dev->d_ifindex); + } + + netdev_list_unlock(); + return ret; +} +