From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
Jakub Sitnicki <jakub@cloudflare.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Willem de Bruijn <willemb@google.com>,
Neal Cardwell <ncardwell@google.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Daniel Borkmann <daniel@iogearbox.net>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Shuah Khan <shuah@kernel.org>, Sechang Lim <rhkrqnwk98@gmail.com>,
Michal Luczaj <mhal@rbox.co>,
Mattia Meleleo <mattia.meleleo@coralogix.com>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH bpf 06/11] bpf, sockmap: Fix reconnect with a psock by rejecting disconnect
Date: Sat, 21 Nov 2026 18:30:07 +0800 [thread overview]
Message-ID: <20261121103222.88209-7-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261121103222.88209-1-jiayuan.chen@linux.dev>
Commit 8cc3aef0cb19 ("tcp: Do not allow buggy transitions between ehash
and lhash2.") rejects connect() -> disconnect -> listen(), but
connect() -> disconnect -> connect() is still allowed.
A socket in a sockmap keeps its psock across tcp_disconnect(), and the
new connection runs into what the old one left there:
- recvmsg() returns the old data still queued on psock->ingress_msg.
- psock->copied_seq and psock->ingress_bytes still account for it, so
copied_seq runs ahead of rcv_nxt for the rest of the connection.
- sk_psock_backlog() does not take the socket lock for ingress, so it
can queue old skbs from psock->ingress_skb after the reconnect.
Cleaning all of this up on disconnect is hard. Do what TLS did in commit
5071a1e606b3 ("net: tls: explicitly disallow disconnect") and reject the
disconnect with -EOPNOTSUPP.
Unlike TLS, some callers have to be let through. None of them leads to
a reconnect with old data:
- A child can be added to a sockmap before accept(), e.g. by a sockops
prog. When the listener is closed, inet_child_forget() disconnects
the child and frees it right after, without looking at the return
value. Such a child has no sk_socket.
- close() with a zero linger time disconnects through sk_prot, which
is still tcp_bpf as long as somebody else holds the psock.
sock_map_close() sets SK_PSOCK_CLOSING for it.
- A listener is disconnected by shutdown(SHUT_RD) and
connect(AF_UNSPEC), and has no data.
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
include/linux/skmsg.h | 1 +
net/core/sock_map.c | 1 +
net/ipv4/tcp_bpf.c | 21 +++++++++++++++++++++
3 files changed, 23 insertions(+)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index 98f71c6d0af73..7e8256b2b4d6c 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -70,6 +70,7 @@ enum sk_psock_state_bits {
SK_PSOCK_RX_STRP_ENABLED,
SK_PSOCK_STRP_DONE_PENDING,
SK_PSOCK_TX_BUSY,
+ SK_PSOCK_CLOSING,
};
struct sk_psock_link {
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 6747b3cc1cac1..6fc1d29ac6ed5 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -1763,6 +1763,7 @@ void sock_map_close(struct sock *sk, long timeout)
sock_map_remove_links(sk, psock);
rcu_read_unlock();
sk_psock_stop(psock);
+ sk_psock_set_state(psock, SK_PSOCK_CLOSING);
release_sock(sk);
cancel_delayed_work_sync(&psock->work);
sk_psock_put(sk, psock);
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 077b65927ac8d..9715716518c49 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -710,12 +710,33 @@ static struct proto *tcpv6_prot_saved __read_mostly;
static DEFINE_SPINLOCK(tcpv6_prot_lock);
static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
+static int tcp_bpf_disconnect(struct sock *sk, int flags)
+{
+ struct sk_psock *psock;
+ bool reject;
+
+ /*
+ * A new connection must not reuse the psock. Let through a child
+ * that nobody has accepted yet, a socket in close() and a listener.
+ */
+ rcu_read_lock();
+ psock = sk_psock(sk);
+ reject = psock && sk->sk_socket && sk->sk_state != TCP_LISTEN &&
+ !sk_psock_test_state(psock, SK_PSOCK_CLOSING);
+ rcu_read_unlock();
+ if (reject)
+ return -EOPNOTSUPP;
+
+ return tcp_disconnect(sk, flags);
+}
+
static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
struct proto *base)
{
prot[TCP_BPF_BASE] = *base;
prot[TCP_BPF_BASE].destroy = sock_map_destroy;
prot[TCP_BPF_BASE].close = sock_map_close;
+ prot[TCP_BPF_BASE].disconnect = tcp_bpf_disconnect;
prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable;
prot[TCP_BPF_BASE].ioctl = tcp_bpf_ioctl;
--
2.43.0
next prev parent reply other threads:[~2026-11-21 10:35 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 10:30 [PATCH bpf 00/11] fix misc sockmap issues Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 01/11] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 02/11] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 03/11] bpf, sockmap: Fix strparser reinit after the parser link is removed Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 04/11] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 05/11] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-11-21 10:53 ` sashiko-bot
2026-11-21 10:30 ` Jiayuan Chen [this message]
2026-11-21 10:30 ` [PATCH bpf 07/11] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 08/11] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-11-21 10:56 ` sashiko-bot
2026-11-21 10:30 ` [PATCH bpf 09/11] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 10/11] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-11-21 10:30 ` [PATCH bpf 11/11] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
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=20261121103222.88209-7-jiayuan.chen@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mattia.meleleo@coralogix.com \
--cc=memxor@gmail.com \
--cc=mhal@rbox.co \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rhkrqnwk98@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.