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 3/4] bpf: support bpf_sk_assign in netkit tx path
Date: Tue, 24 Nov 2026 17:12:43 +0200 [thread overview]
Message-ID: <20261124151244.1423343-4-razor@blackwall.org> (raw)
In-Reply-To: <20261124151244.1423343-1-razor@blackwall.org>
Let netkit programs assign a socket in the receiving network namespace,
for example a transparent proxy listener on the host for traffic coming
from a pod.
A netkit program runs with skb->dev already set to the peer and on
NETKIT_PASS the skb goes straight to __netif_rx() on that peer. The
assigned skb therefore doesn't travel the tx path of the sending side, so
transmit code doesn't need to understand the new ownership.
General tc egress assignment stays unsupported.
netkit records the skb it is running programs for in the BPF net
context and the helper only takes the netkit path for that skb.
A clone made with bpf_clone_redirect(), test runs, cls_bpf and act_bpf on
egress still get -EOPNOTSUPP.
The backlog queue outlives the RCU section so the skb takes a reference
with sock_pfree_ref even for SOCK_RCU_FREE sockets such as tcp
listeners.
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
drivers/net/netkit.c | 10 ++++++++++
include/linux/filter.h | 4 ++++
include/net/netkit.h | 15 +++++++++++++++
include/uapi/linux/bpf.h | 9 +++++++--
net/core/filter.c | 13 ++++++++++---
tools/include/uapi/linux/bpf.h | 9 +++++++--
6 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c
index 75a1a561e5aa..d32d98cad6f6 100644
--- a/drivers/net/netkit.c
+++ b/drivers/net/netkit.c
@@ -53,15 +53,25 @@ static __always_inline int
netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
enum netkit_action ret)
{
+ struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
const struct bpf_mprog_fp *fp;
const struct bpf_prog *prog;
+ struct sk_buff *prev = NULL;
+ if (bpf_net_ctx->ri.kern_flags & BPF_RI_F_NETKIT_SK_ASSIGN)
+ prev = bpf_net_ctx->sk_assign_skb;
+ bpf_net_ctx->sk_assign_skb = skb;
+ bpf_net_ctx->ri.kern_flags |= BPF_RI_F_NETKIT_SK_ASSIGN;
bpf_mprog_foreach_prog(entry, fp, prog) {
bpf_compute_data_pointers(skb);
ret = bpf_prog_run(prog, skb);
if (ret != NETKIT_NEXT)
break;
}
+ if (prev)
+ bpf_net_ctx->sk_assign_skb = prev;
+ else
+ bpf_net_ctx->ri.kern_flags &= ~BPF_RI_F_NETKIT_SK_ASSIGN;
return ret;
}
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 61448be53f8d..ddc4e6912e0c 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -981,6 +981,7 @@ struct bpf_nh_params {
#define BPF_RI_F_CPU_MAP_INIT BIT(2)
#define BPF_RI_F_DEV_MAP_INIT BIT(3)
#define BPF_RI_F_XSK_MAP_INIT BIT(4)
+#define BPF_RI_F_NETKIT_SK_ASSIGN BIT(5)
struct bpf_redirect_info {
u64 tgt_index;
@@ -998,6 +999,9 @@ struct bpf_net_context {
struct list_head cpu_map_flush_list;
struct list_head dev_map_flush_list;
struct list_head xskmap_map_flush_list;
+#ifdef CONFIG_NETKIT
+ struct sk_buff *sk_assign_skb;
+#endif
};
static inline struct bpf_net_context *bpf_net_ctx_set(struct bpf_net_context *bpf_net_ctx)
diff --git a/include/net/netkit.h b/include/net/netkit.h
index 9ec0163739f4..70992f145a63 100644
--- a/include/net/netkit.h
+++ b/include/net/netkit.h
@@ -4,6 +4,7 @@
#define __NET_NETKIT_H
#include <linux/bpf.h>
+#include <linux/filter.h>
#ifdef CONFIG_NETKIT
int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
@@ -11,6 +12,15 @@ int netkit_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
int netkit_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog);
int netkit_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
INDIRECT_CALLABLE_DECLARE(struct net_device *netkit_peer_dev(struct net_device *dev));
+
+static inline bool netkit_runs_skb(const struct sk_buff *skb)
+{
+ struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
+
+ return bpf_net_ctx &&
+ (bpf_net_ctx->ri.kern_flags & BPF_RI_F_NETKIT_SK_ASSIGN) &&
+ bpf_net_ctx->sk_assign_skb == skb;
+}
#else
static inline int netkit_prog_attach(const union bpf_attr *attr,
struct bpf_prog *prog)
@@ -40,5 +50,10 @@ static inline struct net_device *netkit_peer_dev(struct net_device *dev)
{
return NULL;
}
+
+static inline bool netkit_runs_skb(const struct sk_buff *skb)
+{
+ return false;
+}
#endif /* CONFIG_NETKIT */
#endif /* __NET_NETKIT_H */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de0919db72..25f89f3cca1e 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4611,7 +4611,10 @@ union bpf_attr {
* **bpf_clone_redirect**\ () or other methods outside of BPF may
* interfere with successful delivery to the socket.
*
- * This operation is only valid from TC ingress path.
+ * This operation is valid from TC ingress and from netkit
+ * programs. In netkit programs, only full TCP and UDP sockets
+ * are accepted, and the *skb* then holds a reference on *sk*
+ * until the receive path consumes it or the *skb* is freed.
*
* The *flags* argument must be zero.
* Return
@@ -4624,7 +4627,9 @@ union bpf_attr {
* **-ENETUNREACH** if the socket is unreachable (wrong netns).
*
* **-EOPNOTSUPP** if the operation is not supported, for example
- * a call from outside of TC ingress.
+ * a call from outside of TC ingress or netkit programs, or, in a
+ * netkit program, a socket other than a full TCP or UDP socket or
+ * an *skb* that already carries a socket it does not own.
*
* long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
* Description
diff --git a/net/core/filter.c b/net/core/filter.c
index 3206b40887c5..a8e7b46bd648 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -8132,21 +8132,28 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
{
+ bool from_netkit = netkit_runs_skb(skb);
+
if (!sk || flags != 0)
return -EINVAL;
- if (!skb_at_tc_ingress(skb))
+ if (from_netkit) {
+ if (!sk_fullsock(sk) || (!sk_is_tcp(sk) && !sk_is_udp(sk)) ||
+ (skb->sk && !skb->destructor))
+ return -EOPNOTSUPP;
+ } else if (!skb_at_tc_ingress(skb)) {
return -EOPNOTSUPP;
+ }
if (unlikely(dev_net(skb->dev) != sock_net(sk)))
return -ENETUNREACH;
if (sk_unhashed(sk))
return -EOPNOTSUPP;
- if (sk_is_refcounted(sk) &&
+ if ((from_netkit || sk_is_refcounted(sk)) &&
unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
return -ENOENT;
skb_orphan(skb);
skb->sk = sk;
- skb->destructor = sock_pfree;
+ skb->destructor = from_netkit ? sock_pfree_ref : sock_pfree;
return 0;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 62de0919db72..25f89f3cca1e 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -4611,7 +4611,10 @@ union bpf_attr {
* **bpf_clone_redirect**\ () or other methods outside of BPF may
* interfere with successful delivery to the socket.
*
- * This operation is only valid from TC ingress path.
+ * This operation is valid from TC ingress and from netkit
+ * programs. In netkit programs, only full TCP and UDP sockets
+ * are accepted, and the *skb* then holds a reference on *sk*
+ * until the receive path consumes it or the *skb* is freed.
*
* The *flags* argument must be zero.
* Return
@@ -4624,7 +4627,9 @@ union bpf_attr {
* **-ENETUNREACH** if the socket is unreachable (wrong netns).
*
* **-EOPNOTSUPP** if the operation is not supported, for example
- * a call from outside of TC ingress.
+ * a call from outside of TC ingress or netkit programs, or, in a
+ * netkit program, a socket other than a full TCP or UDP socket or
+ * an *skb* that already carries a socket it does not own.
*
* long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
* Description
--
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 ` [PATCH RFC bpf-next 2/4] bpf: preserve owned socket assignments on ingress redirects Nikolay Aleksandrov
2026-11-24 15:12 ` Nikolay Aleksandrov [this message]
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-4-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