From: Yingjie Wang <x-professor@outlook.com>
To: "netdev@vger.kernel.org" <netdev@vger.kernel.org>
Cc: David Ahern <dsahern@kernel.org>,
Ido Schimmel <idosch@nvidia.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>,
Binbin Deng <18983559317@163.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
"cgroups@vger.kernel.org" <cgroups@vger.kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH net-next v2 1/3] net: multicast: keep source filter state when adding a source fails
Date: Sat, 5 Dec 2026 08:19:09 +0000 [thread overview]
Message-ID: <20261205081907.2229988-1-x-professor@outlook.com> (raw)
In-Reply-To: <20261205081856.2229881-1-x-professor@outlook.com>
When a source is added to a membership that has no source filter yet,
ip_mc_source() and ip6_mc_source() first move the socket's reference on
the device group to the requested filter mode, then allocate the socket
source list, and only then add the source to the device. The mode
switch is not undone when the allocation fails, and errors from adding
the source to the device are ignored.
The source list comes from sock_kmalloc(), which fails once the
socket's optmem_max budget is used up. An unprivileged IPv6 socket can
use it up with about 2,300 memberships. MCAST_JOIN_SOURCE_GROUP on an
any-source membership of that socket then returns ENOBUFS but leaves
the filter in INCLUDE mode with no sources, so the socket stops
receiving the group: in a QEMU guest it received 4 of 4 datagrams
before the call and 0 of 4 after it. If adding the source to the
device fails, the call succeeds, but the device filter does not have
the source. MCAST_JOIN_SOURCE_GROUP and IP_ADD_SOURCE_MEMBERSHIP also
keep a membership they created when the source cannot be added. The
next patches charge these allocations to the memory cgroup, which makes
such failures more likely.
Allocate the source list before changing any state. Add the source to
the device and, for a membership without a filter, take the reference on
the new mode in the same ip_mc_add_src() or ip6_mc_add_src() call; both
undo their own changes on failure. Drop the reference on the old mode
only after that succeeds. Return an error from the device update except
-ESRCH, which means that the device has no entry for the group. That
happens for example when an IPv6 request with gsr_interface 0 resolves
to a different device than the membership's, and such calls have always
succeeded. Drop a membership created by the same setsockopt() call if
the source cannot be added.
Removing a source that was never added is already handled by
commit 2f14be6c9b9e ("ipv4/ipv6: preserve multicast filters after a
failed source leave").
The device side of this was found with slab fault injection while
testing the next patches.
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
With this change, the MCAST_JOIN_SOURCE_GROUP case above still fails
with ENOBUFS, but the filter stays in EXCLUDE mode and the group is
still received (4/4 before and after). Injected allocation failures for
IPv4 and IPv6 return ENOBUFS and leave the filter and the membership
unchanged. MCAST_BLOCK_SOURCE with gsr_interface 0 still succeeds when
the route for the group uses a different device than the membership's.
The IGMPv3 and MLDv2 records sent when an any-source membership becomes
INCLUDE {S} are the same as before.
net/ipv4/igmp.c | 80 +++++++++++++++++++++++-----------------
net/ipv4/ip_sockglue.c | 33 ++++++++++-------
net/ipv6/ipv6_sockglue.c | 11 +++++-
net/ipv6/mcast.c | 30 +++++++++------
4 files changed, 94 insertions(+), 60 deletions(-)
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 7c69bdaaedd5..446c49db1adf 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2498,7 +2498,7 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
struct ip_mc_socklist *pmc;
struct in_device *in_dev = NULL;
struct inet_sock *inet = inet_sk(sk);
- struct ip_sf_socklist *psl;
+ struct ip_sf_socklist *newpsl = NULL, *psl;
struct net *net = sock_net(sk);
int leavegroup = 0;
int i, j, rv;
@@ -2534,17 +2534,9 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
goto done; /* err = -EADDRNOTAVAIL */
/* if a source filter was set, must be the same mode as before */
- if (pmc->sflist) {
- if (pmc->sfmode != omode) {
- err = -EINVAL;
- goto done;
- }
- } else if (pmc->sfmode != omode) {
- /* allow mode switches for empty-set filters */
- ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
- ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
- NULL, 0);
- pmc->sfmode = omode;
+ if (pmc->sflist && pmc->sfmode != omode) {
+ err = -EINVAL;
+ goto done;
}
if (!add) {
@@ -2580,8 +2572,16 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
err = -ENOBUFS;
goto done;
}
+ rv = 1; /* > 0 for insert logic below if sl_count is 0 */
+ for (i = 0; psl && i < psl->sl_count; i++) {
+ rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
+ sizeof(__be32));
+ if (rv == 0)
+ break;
+ }
+ if (rv == 0) /* address already there is an error */
+ goto done;
if (!psl || psl->sl_count == psl->sl_max) {
- struct ip_sf_socklist *newpsl;
int count = IP_SFBLOCK;
if (psl)
@@ -2593,10 +2593,36 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
goto done;
}
newpsl->sl_max = count;
- newpsl->sl_count = count - IP_SFBLOCK;
+ newpsl->sl_count = psl ? psl->sl_count : 0;
+ for (j = 0; j < newpsl->sl_count; j++)
+ newpsl->sl_addr[j] = psl->sl_addr[j];
+ for (j = newpsl->sl_count - 1; j >= i; j--)
+ newpsl->sl_addr[j + 1] = newpsl->sl_addr[j];
+ newpsl->sl_addr[i] = mreqs->imr_sourceaddr;
+ newpsl->sl_count++;
+ }
+
+ /* update the interface list; an empty filter takes the new mode
+ * together with its first source
+ */
+ err = ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
+ &mreqs->imr_sourceaddr, pmc->sfmode == omode);
+ /* -ESRCH: no device entry for the group, update the socket only */
+ if (err && err != -ESRCH) {
+ if (newpsl)
+ sock_kfree_s(sk, newpsl,
+ struct_size(newpsl, sl_addr,
+ newpsl->sl_max));
+ goto done;
+ }
+ if (pmc->sfmode != omode) {
+ ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
+ NULL, 0);
+ pmc->sfmode = omode;
+ }
+
+ if (newpsl) {
if (psl) {
- for (i = 0; i < psl->sl_count; i++)
- newpsl->sl_addr[i] = psl->sl_addr[i];
/* decrease mem now to avoid the memleak warning */
atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
&sk->sk_omem_alloc);
@@ -2604,25 +2630,13 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
rcu_assign_pointer(pmc->sflist, newpsl);
if (psl)
kfree_rcu(psl, rcu);
- psl = newpsl;
- }
- rv = 1; /* > 0 for insert logic below if sl_count is 0 */
- for (i = 0; i < psl->sl_count; i++) {
- rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
- sizeof(__be32));
- if (rv == 0)
- break;
+ } else {
+ for (j = psl->sl_count - 1; j >= i; j--)
+ psl->sl_addr[j + 1] = psl->sl_addr[j];
+ psl->sl_addr[i] = mreqs->imr_sourceaddr;
+ psl->sl_count++;
}
- if (rv == 0) /* address already there is an error */
- goto done;
- for (j = psl->sl_count-1; j >= i; j--)
- psl->sl_addr[j+1] = psl->sl_addr[j];
- psl->sl_addr[i] = mreqs->imr_sourceaddr;
- psl->sl_count++;
err = 0;
- /* update the interface list */
- ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
- &mreqs->imr_sourceaddr, 1);
done:
if (leavegroup)
err = ip_mc_leave_group(sk, &imr);
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index 90d0b5bee68b..c5ff545612e2 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -723,7 +723,9 @@ static int do_mcast_group_source(struct sock *sk, int optname,
{
struct group_source_req greqs;
struct ip_mreq_source mreqs;
+ struct ip_mreqn joined = {};
struct sockaddr_in *psin;
+ bool drop_group = false;
int omode, add, err;
err = copy_group_source_from_sockptr(&greqs, optval, optlen);
@@ -747,23 +749,24 @@ static int do_mcast_group_source(struct sock *sk, int optname,
omode = MCAST_EXCLUDE;
add = 0;
} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
- struct ip_mreqn mreq;
-
psin = (struct sockaddr_in *)&greqs.gsr_group;
- mreq.imr_multiaddr = psin->sin_addr;
- mreq.imr_address.s_addr = 0;
- mreq.imr_ifindex = greqs.gsr_interface;
- err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
+ joined.imr_multiaddr = psin->sin_addr;
+ joined.imr_ifindex = greqs.gsr_interface;
+ err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE);
if (err && err != -EADDRINUSE)
return err;
- greqs.gsr_interface = mreq.imr_ifindex;
+ drop_group = !err;
+ greqs.gsr_interface = joined.imr_ifindex;
omode = MCAST_INCLUDE;
add = 1;
} else /* MCAST_LEAVE_SOURCE_GROUP */ {
omode = MCAST_INCLUDE;
add = 0;
}
- return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
+ err = ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
+ if (err && drop_group)
+ ip_mc_leave_group(sk, &joined);
+ return err;
}
static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
@@ -1286,7 +1289,9 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
case IP_ADD_SOURCE_MEMBERSHIP:
case IP_DROP_SOURCE_MEMBERSHIP:
{
+ struct ip_mreqn joined = {};
struct ip_mreq_source mreqs;
+ bool drop_group = false;
int omode, add;
if (optlen != sizeof(struct ip_mreq_source))
@@ -1302,14 +1307,12 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
omode = MCAST_EXCLUDE;
add = 0;
} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
- struct ip_mreqn mreq;
-
- mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
- mreq.imr_address.s_addr = mreqs.imr_interface;
- mreq.imr_ifindex = 0;
- err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
+ joined.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
+ joined.imr_address.s_addr = mreqs.imr_interface;
+ err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE);
if (err && err != -EADDRINUSE)
break;
+ drop_group = !err;
omode = MCAST_INCLUDE;
add = 1;
} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
@@ -1317,6 +1320,8 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
add = 0;
}
err = ip_mc_source(add, omode, sk, &mreqs, 0);
+ if (err && drop_group)
+ ip_mc_leave_group(sk, &joined);
break;
}
case MCAST_JOIN_GROUP:
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index 48dcd2487907..bde66915acbf 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -145,6 +145,7 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
sockptr_t optval, int optlen)
{
struct group_source_req greqs;
+ bool drop_group = false;
int omode, add;
int ret;
@@ -173,13 +174,21 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
/* prior join w/ different source is ok */
if (retv && retv != -EADDRINUSE)
return retv;
+ drop_group = !retv;
omode = MCAST_INCLUDE;
add = 1;
} else /* MCAST_LEAVE_SOURCE_GROUP */ {
omode = MCAST_INCLUDE;
add = 0;
}
- return ip6_mc_source(add, omode, sk, &greqs);
+ ret = ip6_mc_source(add, omode, sk, &greqs);
+ if (ret && drop_group) {
+ struct sockaddr_in6 *psin6;
+
+ psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
+ ipv6_sock_mc_drop(sk, greqs.gsr_interface, &psin6->sin6_addr);
+ }
+ return ret;
}
static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index 03138d1d850d..da456f006f85 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -400,16 +400,9 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
goto done; /* err = -EADDRNOTAVAIL */
/* if a source filter was set, must be the same mode as before */
- if (rcu_access_pointer(pmc->sflist)) {
- if (pmc->sfmode != omode) {
- err = -EINVAL;
- goto done;
- }
- } else if (pmc->sfmode != omode) {
- /* allow mode switches for empty-set filters */
- ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
- ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
- pmc->sfmode = omode;
+ if (psl && pmc->sfmode != omode) {
+ err = -EINVAL;
+ goto done;
}
if (!add) {
@@ -485,8 +478,21 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
}
newpsl->sl_addr[i - 1] = *source;
- /* update the interface list */
- ip6_mc_add_src(idev, group, omode, 1, source, 1);
+ /* update the interface list; an empty filter takes the new mode
+ * together with its first source
+ */
+ err = ip6_mc_add_src(idev, group, omode, 1, source,
+ pmc->sfmode == omode);
+ /* -ESRCH: no device entry for the group, update the socket only */
+ if (err && err != -ESRCH) {
+ sock_kfree_s(sk, newpsl,
+ struct_size(newpsl, sl_addr, newpsl->sl_max));
+ goto done;
+ }
+ if (pmc->sfmode != omode) {
+ ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
+ pmc->sfmode = omode;
+ }
if (psl)
atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
--
2.43.0
next prev parent reply other threads:[~2026-12-05 8:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 8:18 [PATCH net-next v2 0/3] net: multicast: account memberships and device records to memcg Yingjie Wang
2026-12-05 8:19 ` Yingjie Wang [this message]
2026-12-05 8:19 ` [PATCH net-next v2 2/3] net: multicast: account socket memberships and source lists " Yingjie Wang
2026-12-05 8:19 ` [PATCH net-next v2 3/3] net: multicast: account device records created by sockets " Yingjie Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205081907.2229988-1-x-professor@outlook.com \
--to=x-professor@outlook.com \
--cc=18983559317@163.com \
--cc=cgroups@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox