[<prev] [next>] [day] [month] [year] [list]
Message-ID:
<AM6PR03MB584831E68265B20D7DA57D4199A42@AM6PR03MB5848.eurprd03.prod.outlook.com>
Date: Wed, 10 Jul 2024 19:40:53 +0100
From: Juntong Deng <juntong.deng@...look.com>
To: ast@...nel.org,
daniel@...earbox.net,
john.fastabend@...il.com,
martin.lau@...ux.dev,
eddyz87@...il.com,
song@...nel.org,
yonghong.song@...ux.dev,
kpsingh@...nel.org,
sdf@...ichev.me,
haoluo@...gle.com,
jolsa@...nel.org,
andrii@...nel.org,
avagin@...il.com,
snorcht@...il.com
Cc: bpf@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [RFC PATCH 09/16] bpf/crib: Add CRIB kfuncs for getting socket source/destination addresses
This patch adds CRIB kfuncs for getting socket source/destination
addresses, which are wrappers for inet_getname() and inet6_getname().
Signed-off-by: Juntong Deng <juntong.deng@...look.com>
---
kernel/bpf/crib/bpf_checkpoint.c | 50 ++++++++++++++++++++++++++++++++
kernel/bpf/crib/bpf_crib.c | 5 ++++
2 files changed, 55 insertions(+)
diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c
index 28ad26986053..4d48f08324ef 100644
--- a/kernel/bpf/crib/bpf_checkpoint.c
+++ b/kernel/bpf/crib/bpf_checkpoint.c
@@ -8,6 +8,8 @@
#include <linux/bpf_crib.h>
#include <linux/fdtable.h>
+#include <net/inet_common.h>
+#include <net/ipv6.h>
extern void bpf_file_release(struct file *file);
@@ -98,4 +100,52 @@ __bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it)
bpf_file_release(kit->file);
}
+/**
+ * bpf_inet_src_addr_from_socket() - Wrap inet_getname to get the source
+ * IPv4 address and source port of the specified socket
+ *
+ * @sock: specified socket
+ * @addr: buffer
+ */
+__bpf_kfunc int bpf_inet_src_addr_from_socket(struct socket *sock, struct sockaddr_in *addr)
+{
+ return inet_getname(sock, (struct sockaddr *)addr, 0);
+}
+
+/**
+ * bpf_inet_dst_addr_from_socket() - Wrap inet_getname to get the destination
+ * IPv4 address and destination port of the specified socket
+ *
+ * @sock: specified socket
+ * @addr: buffer
+ */
+__bpf_kfunc int bpf_inet_dst_addr_from_socket(struct socket *sock, struct sockaddr_in *addr)
+{
+ return inet_getname(sock, (struct sockaddr *)addr, 1);
+}
+
+/**
+ * bpf_inet6_src_addr_from_socket() - Wrap inet6_getname to get the source
+ * IPv6 address and source port of the specified socket
+ *
+ * @sock: specified socket
+ * @addr: buffer
+ */
+__bpf_kfunc int bpf_inet6_src_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr)
+{
+ return inet6_getname(sock, (struct sockaddr *)addr, 0);
+}
+
+/**
+ * bpf_inet6_dst_addr_from_socket() - Wrap inet6_getname to get the destination
+ * IPv6 address and destination port of the specified socket
+ *
+ * @sock: specified socket
+ * @addr: buffer
+ */
+__bpf_kfunc int bpf_inet6_dst_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr)
+{
+ return inet6_getname(sock, (struct sockaddr *)addr, 1);
+}
+
__bpf_kfunc_end_defs();
diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c
index da545f55b4eb..e33fa37f8f72 100644
--- a/kernel/bpf/crib/bpf_crib.c
+++ b/kernel/bpf/crib/bpf_crib.c
@@ -234,6 +234,11 @@ BTF_ID_FLAGS(func, bpf_receive_queue_from_sock, KF_OBTAIN)
BTF_ID_FLAGS(func, bpf_write_queue_from_sock, KF_OBTAIN)
BTF_ID_FLAGS(func, bpf_reader_queue_from_udp_sock, KF_OBTAIN)
+BTF_ID_FLAGS(func, bpf_inet_src_addr_from_socket, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_inet_dst_addr_from_socket, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_inet6_src_addr_from_socket, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_inet6_dst_addr_from_socket, KF_TRUSTED_ARGS)
+
BTF_KFUNCS_END(bpf_crib_kfuncs)
static int bpf_prog_run_crib(struct bpf_prog *prog,
--
2.39.2
Powered by blists - more mailing lists