Skip to content

Commit

Permalink
netlink: support dumping IPv4 multicast addresses
Browse files Browse the repository at this point in the history
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 <maze@google.com>
Cc: Lorenzo Colitti <lorenzo@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
Link: https://patch.msgid.link/20250207110836.2407224-1-yuyanghuang@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Yuyang Huang authored and Paolo Abeni committed Feb 11, 2025
1 parent 67800d2 commit eb4e17a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 18 deletions.
77 changes: 63 additions & 14 deletions net/ipv4/devinet.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <linux/notifier.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#include "igmp_internal.h"
#include <linux/slab.h>
#include <linux/hash.h>
#ifdef CONFIG_SYSCTL
Expand Down Expand Up @@ -107,15 +108,6 @@ static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
[IFA_PROTO] = { .type = NLA_U8 },
};

struct inet_fill_args {
u32 portid;
u32 seq;
int event;
unsigned int flags;
int netnsid;
int ifindex;
};

#define IN4_ADDR_HSIZE_SHIFT 8
#define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)

Expand Down Expand Up @@ -1846,9 +1838,38 @@ static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
return 0;
}

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)
static int in_dev_dump_ifmcaddr(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;
int ip_idx = 0;
int err;

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, fillargs);
if (err < 0)
goto done;

nl_dump_check_consistent(cb, nlmsg_hdr(skb));
ip_idx++;
}
err = 0;
ip_idx = 0;
done:
*s_ip_idx = ip_idx;
return err;
}

static int in_dev_dump_ifaddr(struct in_device *in_dev, struct sk_buff *skb,
struct netlink_callback *cb, int *s_ip_idx,
struct inet_fill_args *fillargs)
{
struct in_ifaddr *ifa;
int ip_idx = 0;
Expand All @@ -1874,6 +1895,21 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
return err;
}

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)
{
switch (fillargs->event) {
case RTM_NEWADDR:
return in_dev_dump_ifaddr(in_dev, skb, cb, s_ip_idx, fillargs);
case RTM_GETMULTICAST:
return in_dev_dump_ifmcaddr(in_dev, skb, cb, s_ip_idx,
fillargs);
default:
return -EINVAL;
}
}

/* Combine dev_addr_genid and dev_base_seq to detect changes.
*/
static u32 inet_base_seq(const struct net *net)
Expand All @@ -1889,13 +1925,14 @@ static u32 inet_base_seq(const struct net *net)
return res;
}

static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
static int inet_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
int event)
{
const struct nlmsghdr *nlh = cb->nlh;
struct inet_fill_args fillargs = {
.portid = NETLINK_CB(cb->skb).portid,
.seq = nlh->nlmsg_seq,
.event = RTM_NEWADDR,
.event = event,
.flags = NLM_F_MULTI,
.netnsid = -1,
};
Expand Down Expand Up @@ -1949,6 +1986,16 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
return err;
}

static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
{
return inet_dump_addr(skb, cb, RTM_NEWADDR);
}

static int inet_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
{
return inet_dump_addr(skb, cb, RTM_GETMULTICAST);
}

static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
u32 portid)
{
Expand Down Expand Up @@ -2845,6 +2892,8 @@ static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = {
{.protocol = PF_INET, .msgtype = RTM_GETNETCONF,
.doit = inet_netconf_get_devconf, .dumpit = inet_netconf_dump_devconf,
.flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
{.owner = THIS_MODULE, .protocol = PF_INET, .msgtype = RTM_GETMULTICAST,
.dumpit = inet_dump_ifmcaddr, .flags = RTNL_FLAG_DUMP_UNLOCKED},
};

void __init devinet_init(void)
Expand Down
14 changes: 10 additions & 4 deletions net/ipv4/igmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include <linux/skbuff.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#include "igmp_internal.h"
#include <linux/if_arp.h>
#include <linux/rtnetlink.h>
#include <linux/times.h>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions net/ipv4/igmp_internal.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb4e17a

Please sign in to comment.