Skip to contentSkip to conversation

netlink: support dumping IPv4 multicast addresses

PATCH Superseded Yuyang Huang 2 participants 6 messages Feb 4, 2025 – Feb 6, 2025

Subsystems (5)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/2 netlink: support dumping IPv4 multicast addresses

Extended RTM_GETMULTICAST to support dumping joined IPv4 multicast addresses, in addition to the existing IPv6 functionality. This allows userspace applications to retrieve both IPv4 and IPv6 multicast addresses through similar netlink command and then monitor future changes by registering to RTNLGRP_IPV4_MCADDR and RTNLGRP_IPV6_MCADDR.

Cc Maciej Żenczykowski <[email protected]>
Cc Lorenzo Colitti <[email protected]>

---

Changelog since v6:
- Move inet_fill_args and inet_fill_ifmcaddr from igmp.h to igmp_internal.h to avoid exposing these two definitions kernel wide.

Changelog since v5:
- Move the test case to a separate patch.
- Refactor the code to remove the dependency on enum addr_type_t type.
- Return error if fillargs->event is not set properly.
- Return -EINVAL in an unreachable code path.

Changelog since v4:
- Fixes a bug where fillargs->event was not initialized in the code path for dumping ifaddr.
- Fixes a bug where reply messages contain the wrong sequence number.
- Minor style fixes.
- Adds a ynl selftest.

Changelog since v3:
- Refactor in_dev_dump_addr() to break down the logic into two separate functions to simplify the logic.

Changelog since v2:
- Fix checkpatch.pl warnings.
- Remove one redundant EXPORT_SYMBOL().

Changelog since v1:
- Minor style fixes.
- Use for_each_pmc_rcu() instead of for_each_pmc_rtnl().

 include/linux/igmp_internal.h | 17 ++++++++
 net/ipv4/devinet.c            | 77 ++++++++++++++++++++++++++++-------
 net/ipv4/igmp.c               | 14 +++++--
 3 files changed, 90 insertions(+), 18 deletions(-)
 create mode 100644 include/linux/igmp_internal.h
include/linux/igmp_internal.h new file +17 -0
new file mode 100644
index 000000000000..0a1bcc8ec8e1
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _LINUX_IGMP_INTERNAL_H
+#define _LINUX_IGMP_INTERNAL_H
+
+struct inet_fill_args {
+	u32 portid;
+	u32 seq;
+	int event;
+	unsigned int flags;
+	int netnsid;
+	int ifindex;
+};
+
+int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
+		       const struct ip_mc_list *im,
+		       struct inet_fill_args *args);
+#endif
net/ipv4/devinet.c +63 -14

124 lines not on this page · raw message

net/ipv4/igmp.c +10 -4
index 3da126cea884..ac3c45bdbdf0 100644
@@ -81,6 +81,7 @@
 #include <linux/skbuff.h>
 #include <linux/inetdevice.h>
 #include <linux/igmp.h>
+#include <linux/igmp_internal.h>
 #include <linux/if_arp.h>
 #include <linux/rtnetlink.h>
 #include <linux/times.h>
@@ -1432,14 +1433,16 @@ static void ip_mc_hash_remove(struct in_device *in_dev,
 	*mc_hash = im->next_hash;
 }
 
-static int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
-			      const struct ip_mc_list *im, int event)
+int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
+		       const struct ip_mc_list *im,
+		       struct inet_fill_args *args)
 {
 	struct ifa_cacheinfo ci;
 	struct ifaddrmsg *ifm;
 	struct nlmsghdr *nlh;
 
-	nlh = nlmsg_put(skb, 0, 0, event, sizeof(struct ifaddrmsg), 0);
+	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
+			sizeof(struct ifaddrmsg), args->flags);
 	if (!nlh)
 		return -EMSGSIZE;
 
@@ -1468,6 +1471,9 @@ static int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
 static void inet_ifmcaddr_notify(struct net_device *dev,
 				 const struct ip_mc_list *im, int event)
 {
+	struct inet_fill_args fillargs = {
+		.event = event,
+	};
 	struct net *net = dev_net(dev);
 	struct sk_buff *skb;
 	int err = -ENOMEM;
@@ -1479,7 +1485,7 @@ static void inet_ifmcaddr_notify(struct net_device *dev,
 	if (!skb)
 		goto error;
 
-	err = inet_fill_ifmcaddr(skb, dev, im, event);
+	err = inet_fill_ifmcaddr(skb, dev, im, &fillargs);
 	if (err < 0) {
 		WARN_ON_ONCE(err == -EMSGSIZE);
 		nlmsg_free(skb);
Signature · 1 line
2.48.1.362.g079036d154-goog
#1 lore raw

PATCH v7 2/2 selftests/net: Add selftest for IPv4 RTM_GETMULTICAST support

This change introduces a new selftest case to verify the functionality of dumping IPv4 multicast addresses using the RTM_GETMULTICAST netlink message. The test utilizes the ynl library to interact with the netlink interface and validate that the kernel correctly reports the joined IPv4 multicast addresses.

To run the test, execute the following command:

$ vng -v --user root --cpus 16 -- \
    make -C tools/testing/selftests TARGETS=net \
    TEST_PROGS=rtnetlink.py TEST_GEN_PROGS="" run_tests
Cc Maciej Żenczykowski <[email protected]>
Cc Lorenzo Colitti <[email protected]>

---

Changelog since v6:
- Move getmaddrs definition to rt_addr.yaml.

 Documentation/netlink/specs/rt_addr.yaml  | 23 +++++++++++++++++
 tools/testing/selftests/net/Makefile      |  1 +
 tools/testing/selftests/net/lib/py/ynl.py |  4 +--
 tools/testing/selftests/net/rtnetlink.py  | 30 +++++++++++++++++++++++
 4 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/net/rtnetlink.py
Documentation/netlink/specs/rt_addr.yaml +23 -0
index cbee1cedb177..5dd5469044c7 100644
@@ -168,6 +168,29 @@ operations:
         reply:
           value: 20
           attributes: *ifaddr-all
+    -
+      name: getmaddrs
+      doc: Get / dump IPv4/IPv6 multicast addresses.
+      attribute-set: addr-attrs
+      fixed-header: ifaddrmsg
+      do:
+        request:
+          value: 58
+          attributes:
+            - ifa-family
+            - ifa-index
+        reply:
+          value: 58
+          attributes: &mcaddr-attrs
+            - ifa-multicast
+            - ifa-cacheinfo
+      dump:
+        request:
+          value: 58
+            - ifa-family
+        reply:
+          value: 58
+          attributes: *mcaddr-attrs
 
 mcast-groups:
   list:
tools/testing/selftests/net/Makefile +1 -0
index 73ee88d6b043..e2f03211f9b3 100644
@@ -36,6 +36,7 @@ TEST_PROGS += cmsg_so_priority.sh
 TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh
 TEST_PROGS += netns-name.sh
 TEST_PROGS += nl_netdev.py
+TEST_PROGS += rtnetlink.py
 TEST_PROGS += srv6_end_dt46_l3vpn_test.sh
 TEST_PROGS += srv6_end_dt4_l3vpn_test.sh
 TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
tools/testing/selftests/net/lib/py/ynl.py +2 -2
index ad1e36baee2a..7b1e29467e46 100644
@@ -38,8 +38,8 @@ class EthtoolFamily(YnlFamily):
 
 
 class RtnlFamily(YnlFamily):
-    def __init__(self, recv_size=0):
-        super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
+    def __init__(self, recv_size=0, spec='rt_link.yaml'):
+        super().__init__((SPEC_PATH / Path(spec)).as_posix(),
                          schema='', recv_size=recv_size)
 
 
tools/testing/selftests/net/rtnetlink.py new file +30 -0
new file mode 100755
index 000000000000..b12e12827219
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+from lib.py import ksft_exit, ksft_run, ksft_ge, RtnlFamily
+import socket
+
+IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01'
+
+def dump_mcaddr_check(rtnl: RtnlFamily) -> None:
+    """
+    Verify that at least one interface has the IPv4 all-hosts multicast address.
+    At least the loopback interface should have this address.
+    """
+
+    addresses = rtnl.getmaddrs({"ifa-family": socket.AF_INET}, dump=True)
+
+    all_host_multicasts = [
+        addr for addr in addresses if addr['ifa-multicast'] == IPV4_ALL_HOSTS_MULTICAST
+    ]
+
+    ksft_ge(len(all_host_multicasts), 1,
+            "No interface found with the IPv4 all-hosts multicast address")
+
+def main() -> None:
+    rtnl = RtnlFamily(spec='rt_addr.yaml')
+    ksft_run([dump_mcaddr_check], args=(rtnl, ))
+    ksft_exit()
+
+if __name__ == "__main__":
+    main()
Signature · 1 line
2.48.1.362.g079036d154-goog
maintainer
#2 lore raw

Re: [PATCH net-next, v7 2/2] selftests/net: Add selftest for IPv4 RTM_GETMULTICAST support

tools/testing/selftests/net/lib/py/ynl.py class EthtoolFamily(YnlFamily): · line 43

Quoted 13 lines · Yuyang Huang
tools/testing/selftests/net/lib/py/ynl.py +2 -2
index ad1e36baee2a..7b1e29467e46 100644
@@ -38,8 +38,8 @@ class EthtoolFamily(YnlFamily):
 
 
 class RtnlFamily(YnlFamily):
-    def __init__(self, recv_size=0):
-        super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
+    def __init__(self, recv_size=0, spec='rt_link.yaml'):
+        super().__init__((SPEC_PATH / Path(spec)).as_posix(),
                          schema='', recv_size=recv_size)

The preferred way of handling this case is to define a new class, still derived from YnlFamily, setting the correct path in the constructor.

/P

#3 lore raw

Re: [PATCH net-next, v7 2/2] selftests/net: Add selftest for IPv4 RTM_GETMULTICAST support

Quoted 2 lines

The preferred way of handling this case is to define a new class, still derived from YnlFamily, setting the correct path in the constructor.

Thanks for the advice. Will fix it in the next patch version.

Thanks,
Yuyang

Quoted 21 lines · Paolo Abeni

tools/testing/selftests/net/lib/py/ynl.py class EthtoolFamily(YnlFamily): · line 43

Quoted 13 lines · Yuyang Huang
tools/testing/selftests/net/lib/py/ynl.py +2 -2
index ad1e36baee2a..7b1e29467e46 100644
@@ -38,8 +38,8 @@ class EthtoolFamily(YnlFamily):
 
 
 class RtnlFamily(YnlFamily):
-    def __init__(self, recv_size=0):
-        super().__init__((SPEC_PATH / Path('rt_link.yaml')).as_posix(),
+    def __init__(self, recv_size=0, spec='rt_link.yaml'):
+        super().__init__((SPEC_PATH / Path(spec)).as_posix(),
                          schema='', recv_size=recv_size)

The preferred way of handling this case is to define a new class, still derived from YnlFamily, setting the correct path in the constructor.

/P

maintainer
#4 lore raw
In reply to Yuyang Huang

include/linux/igmp_internal.h

Quoted 5 lines · Yuyang Huang
include/linux/igmp_internal.h new file +0 -0
new file mode 100644
index 000000000000..0a1bcc8ec8e1

I did not undertand you intended to place the new header under the 'include' directory. I still have a preference for a really private header that would under 'net/ipv4/' (IMHO the possible future divergence of inet_fill_args and inet6_fill_args is not very relevant) but it's not a deal breaker.

/P

#5 lore raw
Quoted 5 lines

I did not undertand you intended to place the new header under the 'include' directory. I still have a preference for a really private header that would under 'net/ipv4/' (IMHO the possible future divergence of inet_fill_args and inet6_fill_args is not very relevant) but it's not a deal breaker.

Thanks for the advice, I will move the header file under 'net/ipv4' in the next version of this patch.

Thanks,

Yuyang

Quoted 16 lines · Paolo Abeni

include/linux/igmp_internal.h

Quoted 5 lines · Yuyang Huang
include/linux/igmp_internal.h new file +0 -0
new file mode 100644
index 000000000000..0a1bcc8ec8e1

I did not undertand you intended to place the new header under the 'include' directory. I still have a preference for a really private header that would under 'net/ipv4/' (IMHO the possible future divergence of inet_fill_args and inet6_fill_args is not very relevant) but it's not a deal breaker.

/P