From: Nikolay Aleksandrov <razor@blackwall.org>
To: bpf@vger.kernel.org
Cc: Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
netdev@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Stanislav Fomichev <sdf.kernel@gmail.com>,
Nikolay Aleksandrov <razor@blackwall.org>
Subject: [PATCH RFC bpf-next 2/4] bpf: preserve owned socket assignments on ingress redirects
Date: Tue, 24 Nov 2026 17:12:42 +0200 [thread overview]
Message-ID: <20261124151244.1423343-3-razor@blackwall.org> (raw)
In-Reply-To: <20261124151244.1423343-1-razor@blackwall.org>
Ingress assignments can borrow an RCU-protected listener, directly or
through a syncookie request. Redirects can queue the skb beyond that
protection. Peer redirects also cross into another network namespace.
Keep an assignment on a redirect from TC ingress to ingress in the same
namespace when the skb already owns a full socket reference. Acquire a
reference for a borrowed full socket before queuing it and use
sock_pfree_ref to release that reference. If the reference cannot be
acquired or the socket is a request socket, orphan the assignment.
Orphan assignments on peer and egress redirects.
Fixes: 7ae215d23c12 ("bpf: Don't refcount LISTEN sockets in sk_assign()")
Fixes: 9aa1206e8f48 ("bpf: Add redirect_peer helper")
Fixes: e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
net/core/filter.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/net/core/filter.c b/net/core/filter.c
index 53c042b267ab..3206b40887c5 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2546,6 +2546,41 @@ static struct net_device *skb_get_peer_dev(struct net_device *dev)
return NULL;
}
+#if IS_ENABLED(CONFIG_INET)
+static void skb_redirect_prefetched_sk(struct sk_buff *skb,
+ const struct net_device *dev, u32 flags)
+{
+ struct sock *sk;
+
+ if (!skb_sk_is_prefetched(skb))
+ return;
+
+ sk = skb->sk;
+ /*
+ * tc ingress redirects to ingress in the same namespace keep a
+ * prefetched socket if the skb owns a reference or can acquire one.
+ * Other redirects and request sockets lose the assignment.
+ */
+ if (!skb_at_tc_ingress(skb) || flags != BPF_F_INGRESS ||
+ !net_eq(sock_net(sk), dev_net(dev)) || !sk_fullsock(sk))
+ goto orphan;
+ if (skb->destructor == sock_pfree_ref || sk_is_refcounted(sk))
+ return;
+ if (refcount_inc_not_zero_acquire(&sk->sk_refcnt)) {
+ skb->destructor = sock_pfree_ref;
+ return;
+ }
+
+orphan:
+ skb_orphan(skb);
+}
+#else
+static inline void skb_redirect_prefetched_sk(struct sk_buff *skb,
+ const struct net_device *dev, u32 flags)
+{
+}
+#endif
+
int skb_do_redirect(struct sk_buff *skb)
{
struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
@@ -2558,6 +2593,8 @@ int skb_do_redirect(struct sk_buff *skb)
ri->flags = 0;
if (unlikely(!dev))
goto out_drop;
+
+ skb_redirect_prefetched_sk(skb, dev, flags);
if (flags & BPF_F_PEER) {
dev = skb_get_peer_dev(dev);
if (unlikely(!dev ||
--
2.47.3
next prev parent reply other threads:[~2026-11-24 15:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
2026-11-24 15:12 ` Nikolay Aleksandrov [this message]
2026-11-24 15:12 ` [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 4/4] selftests/bpf: test netkit socket assignment to a transparent proxy Nikolay Aleksandrov
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=20261124151244.1423343-3-razor@blackwall.org \
--to=razor@blackwall.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=edumazet@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf.kernel@gmail.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