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@xxxxxxxxxxx> --- 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