Skip to contentSkip to conversation

netlink: support dumping IPv4 multicast addresses

PATCH RESEND Superseded Yuyang Huang 2 participants 2 messages Jan 5, 2025 – Jan 8, 2025

Subsystems (3)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v3 1/1 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 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.h |  2 ++
 net/ipv4/devinet.c   | 63 +++++++++++++++++++++++++++++++++++++-------
 net/ipv4/igmp.c      |  8 +++---
 3 files changed, 59 insertions(+), 14 deletions(-)
include/linux/igmp.h +2 -0
index 073b30a9b850..757c0aeea1ac 100644
@@ -142,4 +142,6 @@ extern void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
 extern void ip_mc_inc_group(struct in_device *in_dev, __be32 addr);
 int ip_mc_check_igmp(struct sk_buff *skb);
 
+int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,
+		       const struct ip_mc_list *im, int event, int flags);
 #endif
net/ipv4/devinet.c +53 -10

107 lines not on this page · raw message

net/ipv4/igmp.c +4 -4
index 8a370ef37d3f..32230f381e14 100644
@@ -1432,14 +1432,14 @@ 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, int event, int flags)
 {
 	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, 0, 0, event, sizeof(struct ifaddrmsg), flags);
 	if (!nlh)
 		return -EMSGSIZE;
 
@@ -1477,7 +1477,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, event, 0);
 	if (err < 0) {
 		WARN_ON_ONCE(err == -EMSGSIZE);
 		nlmsg_free(skb);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
maintainer
#1 lore raw
Quoted 21 lines · Yuyang Huang
net/ipv4/devinet.c +11 -2
@@ -1850,21 +1851,46 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
 			    struct netlink_callback *cb, int *s_ip_idx,
 			    struct inet_fill_args *fillargs)
 {
+	struct ip_mc_list *im;
 	struct in_ifaddr *ifa;
 	int ip_idx = 0;
 	int err;
 
-	in_dev_for_each_ifa_rcu(ifa, in_dev) {
-		if (ip_idx < *s_ip_idx) {
+	switch (fillargs->type) {
+	case UNICAST_ADDR:
+		fillargs->event = RTM_NEWADDR;
+		in_dev_for_each_ifa_rcu(ifa, in_dev) {
+			if (ip_idx < *s_ip_idx) {
+				ip_idx++;
+				continue;
+			}
+			err = inet_fill_ifaddr(skb, ifa, fillargs);
+			if (err < 0)

net/ipv4/devinet.c static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, · line 1874

Quoted 10 lines
net/ipv4/devinet.c +4 -4
+				goto done;
+
+			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
 			ip_idx++;
-			continue;
 		}
-		err = inet_fill_ifaddr(skb, ifa, fillargs);
-		if (err < 0)
-			goto done;
+		break;

Almost all of the logic is under the switch cases, you are not really saving a lot here. I think it would be simpler just to have simpler in_dev_dump_addr for each type.

Quoted 24 lines
net/ipv4/devinet.c +18 -2
+	case MULTICAST_ADDR:
+		for (im = rcu_dereference(in_dev->mc_list);
+		     im;
+		     im = rcu_dereference(im->next_rcu)) {
+			if (ip_idx < *s_ip_idx) {
+				ip_idx++;
+				continue;
+			}
+			err = inet_fill_ifmcaddr(skb, in_dev->dev, im,
+						 RTM_GETMULTICAST, NLM_F_MULTI);
+			if (err < 0)
+				goto done;
 
-		nl_dump_check_consistent(cb, nlmsg_hdr(skb));
-		ip_idx++;
+			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
+			ip_idx++;
+		}
+		break;
+	default:
+		break;
 	}
 	err = 0;
 	ip_idx = 0;