Netdev List
 help / color / mirror / Atom feed
* [PATCH bpf-next 1/2] bpf: Add bpf_ksock_bind() kfunc
@ 2026-10-22 18:16 Mahe Tardy
  2026-10-22 18:16 ` [PATCH bpf-next 2/2] selftests/bpf: Test " Mahe Tardy
  2026-10-22 18:45 ` [PATCH bpf-next 1/2] bpf: Add " bot+bpf-ci
  0 siblings, 2 replies; 6+ messages in thread
From: Mahe Tardy @ 2026-10-22 18:16 UTC (permalink / raw)
  To: bpf
  Cc: andrii, ast, daniel, davem, willemb, eddyz87, edumazet,
	john.fastabend, kuba, horms, liamwisehart, martin.lau, pabeni,
	song, netdev, sdf.kernel, yonghong.song, jolsa, kuniyu, memxor,
	emil, almagui, Mahe Tardy

Part of the effort to make ksock have all basic socket operations. This
is for example useful when replying to a peer which expects a message
from a particular source port.

Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
---
 net/core/bpf_ksock.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/net/core/bpf_ksock.c b/net/core/bpf_ksock.c
index 37168f43c16c..c7b487d90cd0 100644
--- a/net/core/bpf_ksock.c
+++ b/net/core/bpf_ksock.c
@@ -178,6 +178,33 @@ bpf_ksock_create(const struct bpf_ksock_create_opts *opts, u32 opts__sz,
 	return NULL;
 }

+/**
+ * bpf_ksock_bind() - Bind a BPF kernel socket to a local address.
+ * @ks:		The BPF kernel socket context.
+ * @addr:	Pointer to an IPv4 or IPv6 socket address.
+ * @addr__sz:	Size of the address union.
+ *
+ * Binds the socket to the specified local address and port. A port of zero
+ * selects an ephemeral port.
+ *
+ * This function may sleep, so it can only be used in sleepable BPF programs
+ * (SYSCALL).
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+__bpf_kfunc int bpf_ksock_bind(struct bpf_ksock *ks,
+			       const union bpf_ksock_addr *addr, u32 addr__sz)
+{
+	struct sockaddr_storage sa;
+	int addrlen;
+
+	addrlen = bpf_ksock_addr_to_sockaddr(addr, addr__sz, &sa);
+	if (addrlen < 0)
+		return addrlen;
+
+	return __sys_bind_socket(ks->sock, &sa, addrlen);
+}
+
 /**
  * bpf_ksock_connect() - Connect a BPF kernel socket to a remote address.
  * @ks:		The BPF kernel socket context.
@@ -285,6 +312,7 @@ __bpf_kfunc_end_defs();
 BTF_KFUNCS_START(ksock_init_kfunc_btf_ids)
 BTF_ID_FLAGS(func, bpf_ksock_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_ksock_connect, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_ksock_bind, KF_SLEEPABLE)
 BTF_KFUNCS_END(ksock_init_kfunc_btf_ids)

 static const struct btf_kfunc_id_set ksock_init_kfunc_set = {
--
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-10-23 17:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-22 18:16 [PATCH bpf-next 1/2] bpf: Add bpf_ksock_bind() kfunc Mahe Tardy
2026-10-22 18:16 ` [PATCH bpf-next 2/2] selftests/bpf: Test " Mahe Tardy
2026-10-22 18:45 ` [PATCH bpf-next 1/2] bpf: Add " bot+bpf-ci
2026-10-23  8:42   ` Mahe Tardy
2026-10-23 16:05     ` Alan Maguire
2026-10-23 17:10       ` Mahe Tardy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox