Merge branch 'selftests/bpf: Add sockaddr tests for kernel networking'
Jordan Rife says: ==================== This patch series adds test coverage for BPF sockaddr hooks and their interactions with kernel socket functions (i.e. kernel_bind(), kernel_connect(), kernel_sendmsg(), sock_sendmsg(), kernel_getpeername(), and kernel_getsockname()) while also rounding out IPv4 and IPv6 sockaddr hook coverage in prog_tests/sock_addr.c. As with v1 of this patch series, we add regression coverage for the issues addressed by these patches, - commit 0bdf399342c5("net: Avoid address overwrite in kernel_connect") - commit 86a7e0b69bd5("net: prevent rewrite of msg_name in sock_sendmsg()") - commit c889a99a21bf("net: prevent address rewrite in kernel_bind()") - commit 01b2885d9415("net: Save and restore msg_namelen in sock_sendmsg") but broaden the focus a bit. In order to extend prog_tests/sock_addr.c to test these kernel functions, we add a set of new kfuncs that wrap individual socket operations to bpf_testmod and invoke them through set of corresponding SYSCALL programs (progs/sock_addr_kern.c). Each test case can be configured to use a different set of "sock_ops" depending on whether it is testing kernel calls (kernel_bind(), kernel_connect(), etc.) or system calls (bind(), connect(), etc.). ======= Patches ======= * Patch 1 fixes the sock_addr bind test program to work for big endian architectures such as s390x. * Patch 2 introduces the new kfuncs to bpf_testmod. * Patch 3 introduces the BPF program which allows us to invoke these kfuncs invividually from the test program. * Patch 4 lays the groundwork for IPv4 and IPv6 sockaddr hook coverage by migrating much of the environment setup logic from bpf/test_sock_addr.sh into prog_tests/sock_addr.c and moves test cases to cover bind4/6, connect4/6, sendmsg4/6 and recvmsg4/6 hooks. * Patch 5 makes the set of socket operations for each test case configurable, laying the groundwork for Patch 6. * Patch 6 introduces two sets of sock_ops that invoke the kernel equivalents of connect(), bind(), etc. and uses these to add coverage for the kernel socket functions. ======= Changes ======= v2->v3 ------ * Renamed bind helpers. Dropped "_ntoh" suffix. * Added guards to kfuncs to make sure addrlen and msglen do not exceed the buffer capacity. * Added KF_SLEEPABLE flag to kfuncs. * Added a mutex (sock_lock) to kfuncs to serialize access to sock. * Added NULL check for sock to each kfunc. * Use the "sock_addr" networking namespace for all network interface setup and testing. * Use "nodad" when calling "ip -6 addr add" during interface setup to avoid delays and remove ping loop. * Removed test cases from test_sock_addr.c to make it clear what remains to be migrated. * Removed unused parameter (expect_change) from sock_addr_op(). Link: https://lore.kernel.org/bpf/20240412165230.2009746-1-jrife@google.com/T/#u v1->v2 ------ * Dropped test_progs/sock_addr_kern.c and the sock_addr_kern test module in favor of simply expanding bpf_testmod and test_progs/sock_addr.c. * Migrated environment setup logic from bpf/test_sock_addr.sh into prog_tests/sock_addr.c rather than invoking the script from the test program. * Added kfuncs to bpf_testmod as well as the sock_addr_kern BPF program to enable us to invoke kernel socket functions from test_progs/sock_addr.c. * Added test coverage for kernel socket functions to test_progs/sock_addr.c. Link: https://lore.kernel.org/bpf/20240329191907.1808635-1-jrife@google.com/T/#u ==================== Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
This commit is contained in:
commit
f8c423d1ca
@ -10,18 +10,30 @@
|
||||
#include <linux/percpu-defs.h>
|
||||
#include <linux/sysfs.h>
|
||||
#include <linux/tracepoint.h>
|
||||
#include <linux/net.h>
|
||||
#include <linux/socket.h>
|
||||
#include <linux/nsproxy.h>
|
||||
#include <linux/inet.h>
|
||||
#include <linux/in.h>
|
||||
#include <linux/in6.h>
|
||||
#include <linux/un.h>
|
||||
#include <net/sock.h>
|
||||
#include "bpf_testmod.h"
|
||||
#include "bpf_testmod_kfunc.h"
|
||||
|
||||
#define CREATE_TRACE_POINTS
|
||||
#include "bpf_testmod-events.h"
|
||||
|
||||
#define CONNECT_TIMEOUT_SEC 1
|
||||
|
||||
typedef int (*func_proto_typedef)(long);
|
||||
typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
|
||||
typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
|
||||
|
||||
DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
|
||||
long bpf_testmod_test_struct_arg_result;
|
||||
static DEFINE_MUTEX(sock_lock);
|
||||
static struct socket *sock;
|
||||
|
||||
struct bpf_testmod_struct_arg_1 {
|
||||
int a;
|
||||
@ -498,6 +510,237 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)
|
||||
{
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_init_sock(struct init_sock_args *args)
|
||||
{
|
||||
int proto;
|
||||
int err;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (sock) {
|
||||
pr_err("%s called without releasing old sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
switch (args->af) {
|
||||
case AF_INET:
|
||||
case AF_INET6:
|
||||
proto = args->type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;
|
||||
break;
|
||||
case AF_UNIX:
|
||||
proto = PF_UNIX;
|
||||
break;
|
||||
default:
|
||||
pr_err("invalid address family %d\n", args->af);
|
||||
err = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = sock_create_kern(current->nsproxy->net_ns, args->af, args->type,
|
||||
proto, &sock);
|
||||
|
||||
if (!err)
|
||||
/* Set timeout for call to kernel_connect() to prevent it from hanging,
|
||||
* and consider the connection attempt failed if it returns
|
||||
* -EINPROGRESS.
|
||||
*/
|
||||
sock->sk->sk_sndtimeo = CONNECT_TIMEOUT_SEC * HZ;
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc void bpf_kfunc_close_sock(void)
|
||||
{
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (sock) {
|
||||
sock_release(sock);
|
||||
sock = NULL;
|
||||
}
|
||||
|
||||
mutex_unlock(&sock_lock);
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_connect(struct addr_args *args)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (args->addrlen > sizeof(args->addr))
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_connect(sock, (struct sockaddr *)&args->addr,
|
||||
args->addrlen, 0);
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_bind(struct addr_args *args)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (args->addrlen > sizeof(args->addr))
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_bind(sock, (struct sockaddr *)&args->addr, args->addrlen);
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_listen(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_listen(sock, 128);
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_sendmsg(struct sendmsg_args *args)
|
||||
{
|
||||
struct msghdr msg = {
|
||||
.msg_name = &args->addr.addr,
|
||||
.msg_namelen = args->addr.addrlen,
|
||||
};
|
||||
struct kvec iov;
|
||||
int err;
|
||||
|
||||
if (args->addr.addrlen > sizeof(args->addr.addr) ||
|
||||
args->msglen > sizeof(args->msg))
|
||||
return -EINVAL;
|
||||
|
||||
iov.iov_base = args->msg;
|
||||
iov.iov_len = args->msglen;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_sendmsg(sock, &msg, &iov, 1, args->msglen);
|
||||
args->addr.addrlen = msg.msg_namelen;
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args)
|
||||
{
|
||||
struct msghdr msg = {
|
||||
.msg_name = &args->addr.addr,
|
||||
.msg_namelen = args->addr.addrlen,
|
||||
};
|
||||
struct kvec iov;
|
||||
int err;
|
||||
|
||||
if (args->addr.addrlen > sizeof(args->addr.addr) ||
|
||||
args->msglen > sizeof(args->msg))
|
||||
return -EINVAL;
|
||||
|
||||
iov.iov_base = args->msg;
|
||||
iov.iov_len = args->msglen;
|
||||
|
||||
iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, args->msglen);
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = sock_sendmsg(sock, &msg);
|
||||
args->addr.addrlen = msg.msg_namelen;
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_getsockname(struct addr_args *args)
|
||||
{
|
||||
int err;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_getsockname(sock, (struct sockaddr *)&args->addr);
|
||||
if (err < 0)
|
||||
goto out;
|
||||
|
||||
args->addrlen = err;
|
||||
err = 0;
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_kfunc_call_kernel_getpeername(struct addr_args *args)
|
||||
{
|
||||
int err;
|
||||
|
||||
mutex_lock(&sock_lock);
|
||||
|
||||
if (!sock) {
|
||||
pr_err("%s called without initializing sock", __func__);
|
||||
err = -EPERM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
err = kernel_getpeername(sock, (struct sockaddr *)&args->addr);
|
||||
if (err < 0)
|
||||
goto out;
|
||||
|
||||
args->addrlen = err;
|
||||
err = 0;
|
||||
out:
|
||||
mutex_unlock(&sock_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
|
||||
BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
|
||||
@ -525,6 +768,15 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_init_sock, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_close_sock, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_connect, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_bind, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_listen, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_sendmsg, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_sock_sendmsg, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getsockname, KF_SLEEPABLE)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getpeername, KF_SLEEPABLE)
|
||||
BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
|
||||
|
||||
static int bpf_testmod_ops_init(struct btf *btf)
|
||||
@ -655,6 +907,8 @@ static int bpf_testmod_init(void)
|
||||
return ret;
|
||||
if (bpf_fentry_test1(0) < 0)
|
||||
return -EINVAL;
|
||||
sock = NULL;
|
||||
mutex_init(&sock_lock);
|
||||
return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
|
||||
}
|
||||
|
||||
@ -668,6 +922,7 @@ static void bpf_testmod_exit(void)
|
||||
while (refcount_read(&prog_test_struct.cnt) > 1)
|
||||
msleep(20);
|
||||
|
||||
bpf_kfunc_close_sock();
|
||||
sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
|
||||
}
|
||||
|
||||
|
@ -64,6 +64,22 @@ struct prog_test_fail3 {
|
||||
char arr2[];
|
||||
};
|
||||
|
||||
struct init_sock_args {
|
||||
int af;
|
||||
int type;
|
||||
};
|
||||
|
||||
struct addr_args {
|
||||
char addr[sizeof(struct __kernel_sockaddr_storage)];
|
||||
int addrlen;
|
||||
};
|
||||
|
||||
struct sendmsg_args {
|
||||
struct addr_args addr;
|
||||
char msg[10];
|
||||
int msglen;
|
||||
};
|
||||
|
||||
struct prog_test_ref_kfunc *
|
||||
bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) __ksym;
|
||||
void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
|
||||
@ -107,4 +123,15 @@ void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p);
|
||||
void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len);
|
||||
|
||||
void bpf_kfunc_common_test(void) __ksym;
|
||||
|
||||
int bpf_kfunc_init_sock(struct init_sock_args *args) __ksym;
|
||||
void bpf_kfunc_close_sock(void) __ksym;
|
||||
int bpf_kfunc_call_kernel_connect(struct addr_args *args) __ksym;
|
||||
int bpf_kfunc_call_kernel_bind(struct addr_args *args) __ksym;
|
||||
int bpf_kfunc_call_kernel_listen(void) __ksym;
|
||||
int bpf_kfunc_call_kernel_sendmsg(struct sendmsg_args *args) __ksym;
|
||||
int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args) __ksym;
|
||||
int bpf_kfunc_call_kernel_getsockname(struct addr_args *args) __ksym;
|
||||
int bpf_kfunc_call_kernel_getpeername(struct addr_args *args) __ksym;
|
||||
|
||||
#endif /* _BPF_TESTMOD_KFUNC_H */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -12,6 +12,8 @@
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_endian.h>
|
||||
|
||||
#include "bind_prog.h"
|
||||
|
||||
#define SERV4_IP 0xc0a801feU /* 192.168.1.254 */
|
||||
#define SERV4_PORT 4040
|
||||
#define SERV4_REWRITE_IP 0x7f000001U /* 127.0.0.1 */
|
||||
@ -118,23 +120,23 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)
|
||||
|
||||
// u8 narrow loads:
|
||||
user_ip4 = 0;
|
||||
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[0] << 0;
|
||||
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[1] << 8;
|
||||
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[2] << 16;
|
||||
user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[3] << 24;
|
||||
user_ip4 |= load_byte(ctx->user_ip4, 0, sizeof(user_ip4));
|
||||
user_ip4 |= load_byte(ctx->user_ip4, 1, sizeof(user_ip4));
|
||||
user_ip4 |= load_byte(ctx->user_ip4, 2, sizeof(user_ip4));
|
||||
user_ip4 |= load_byte(ctx->user_ip4, 3, sizeof(user_ip4));
|
||||
if (ctx->user_ip4 != user_ip4)
|
||||
return 0;
|
||||
|
||||
user_port = 0;
|
||||
user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
|
||||
user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
|
||||
user_port |= load_byte(ctx->user_port, 0, sizeof(user_port));
|
||||
user_port |= load_byte(ctx->user_port, 1, sizeof(user_port));
|
||||
if (ctx->user_port != user_port)
|
||||
return 0;
|
||||
|
||||
// u16 narrow loads:
|
||||
user_ip4 = 0;
|
||||
user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[0] << 0;
|
||||
user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[1] << 16;
|
||||
user_ip4 |= load_word(ctx->user_ip4, 0, sizeof(user_ip4));
|
||||
user_ip4 |= load_word(ctx->user_ip4, 1, sizeof(user_ip4));
|
||||
if (ctx->user_ip4 != user_ip4)
|
||||
return 0;
|
||||
|
||||
|
@ -12,6 +12,8 @@
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_endian.h>
|
||||
|
||||
#include "bind_prog.h"
|
||||
|
||||
#define SERV6_IP_0 0xfaceb00c /* face:b00c:1234:5678::abcd */
|
||||
#define SERV6_IP_1 0x12345678
|
||||
#define SERV6_IP_2 0x00000000
|
||||
@ -129,25 +131,25 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)
|
||||
// u8 narrow loads:
|
||||
for (i = 0; i < 4; i++) {
|
||||
user_ip6 = 0;
|
||||
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0;
|
||||
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8;
|
||||
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16;
|
||||
user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24;
|
||||
user_ip6 |= load_byte(ctx->user_ip6[i], 0, sizeof(user_ip6));
|
||||
user_ip6 |= load_byte(ctx->user_ip6[i], 1, sizeof(user_ip6));
|
||||
user_ip6 |= load_byte(ctx->user_ip6[i], 2, sizeof(user_ip6));
|
||||
user_ip6 |= load_byte(ctx->user_ip6[i], 3, sizeof(user_ip6));
|
||||
if (ctx->user_ip6[i] != user_ip6)
|
||||
return 0;
|
||||
}
|
||||
|
||||
user_port = 0;
|
||||
user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0;
|
||||
user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8;
|
||||
user_port |= load_byte(ctx->user_port, 0, sizeof(user_port));
|
||||
user_port |= load_byte(ctx->user_port, 1, sizeof(user_port));
|
||||
if (ctx->user_port != user_port)
|
||||
return 0;
|
||||
|
||||
// u16 narrow loads:
|
||||
for (i = 0; i < 4; i++) {
|
||||
user_ip6 = 0;
|
||||
user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
|
||||
user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16;
|
||||
user_ip6 |= load_word(ctx->user_ip6[i], 0, sizeof(user_ip6));
|
||||
user_ip6 |= load_word(ctx->user_ip6[i], 1, sizeof(user_ip6));
|
||||
if (ctx->user_ip6[i] != user_ip6)
|
||||
return 0;
|
||||
}
|
||||
|
19
tools/testing/selftests/bpf/progs/bind_prog.h
Normal file
19
tools/testing/selftests/bpf/progs/bind_prog.h
Normal file
@ -0,0 +1,19 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef __BIND_PROG_H__
|
||||
#define __BIND_PROG_H__
|
||||
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define load_byte(src, b, s) \
|
||||
(((volatile __u8 *)&(src))[b] << 8 * b)
|
||||
#define load_word(src, w, s) \
|
||||
(((volatile __u16 *)&(src))[w] << 16 * w)
|
||||
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define load_byte(src, b, s) \
|
||||
(((volatile __u8 *)&(src))[(b) + (sizeof(src) - (s))] << 8 * ((s) - (b) - 1))
|
||||
#define load_word(src, w, s) \
|
||||
(((volatile __u16 *)&(src))[w] << 16 * (((s) / 2) - (w) - 1))
|
||||
#else
|
||||
# error "Fix your compiler's __BYTE_ORDER__?!"
|
||||
#endif
|
||||
|
||||
#endif
|
65
tools/testing/selftests/bpf/progs/sock_addr_kern.c
Normal file
65
tools/testing/selftests/bpf/progs/sock_addr_kern.c
Normal file
@ -0,0 +1,65 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2024 Google LLC */
|
||||
#include <vmlinux.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "../bpf_testmod/bpf_testmod_kfunc.h"
|
||||
|
||||
SEC("syscall")
|
||||
int init_sock(struct init_sock_args *args)
|
||||
{
|
||||
bpf_kfunc_init_sock(args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int close_sock(void *ctx)
|
||||
{
|
||||
bpf_kfunc_close_sock();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_connect(struct addr_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_connect(args);
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_bind(struct addr_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_bind(args);
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_listen(struct addr_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_listen();
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_sendmsg(struct sendmsg_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_sendmsg(args);
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int sock_sendmsg(struct sendmsg_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_sock_sendmsg(args);
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_getsockname(struct addr_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_getsockname(args);
|
||||
}
|
||||
|
||||
SEC("syscall")
|
||||
int kernel_getpeername(struct addr_args *args)
|
||||
{
|
||||
return bpf_kfunc_call_kernel_getpeername(args);
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
@ -97,11 +97,7 @@ static int sendmsg_deny_prog_load(const struct sock_addr_test *test);
|
||||
static int recvmsg_allow_prog_load(const struct sock_addr_test *test);
|
||||
static int recvmsg_deny_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
|
||||
static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
|
||||
static int recvmsg6_rw_c_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test);
|
||||
static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test);
|
||||
|
||||
@ -135,34 +131,6 @@ static struct sock_addr_test tests[] = {
|
||||
NULL,
|
||||
ATTACH_REJECT,
|
||||
},
|
||||
{
|
||||
"bind4: rewrite IP & TCP port in",
|
||||
bind4_prog_load,
|
||||
BPF_CGROUP_INET4_BIND,
|
||||
BPF_CGROUP_INET4_BIND,
|
||||
AF_INET,
|
||||
SOCK_STREAM,
|
||||
SERV4_IP,
|
||||
SERV4_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
NULL,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"bind4: rewrite IP & UDP port in",
|
||||
bind4_prog_load,
|
||||
BPF_CGROUP_INET4_BIND,
|
||||
BPF_CGROUP_INET4_BIND,
|
||||
AF_INET,
|
||||
SOCK_DGRAM,
|
||||
SERV4_IP,
|
||||
SERV4_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
NULL,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"bind6: load prog with wrong expected attach type",
|
||||
bind6_prog_load,
|
||||
@ -191,34 +159,6 @@ static struct sock_addr_test tests[] = {
|
||||
NULL,
|
||||
ATTACH_REJECT,
|
||||
},
|
||||
{
|
||||
"bind6: rewrite IP & TCP port in",
|
||||
bind6_prog_load,
|
||||
BPF_CGROUP_INET6_BIND,
|
||||
BPF_CGROUP_INET6_BIND,
|
||||
AF_INET6,
|
||||
SOCK_STREAM,
|
||||
SERV6_IP,
|
||||
SERV6_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
NULL,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"bind6: rewrite IP & UDP port in",
|
||||
bind6_prog_load,
|
||||
BPF_CGROUP_INET6_BIND,
|
||||
BPF_CGROUP_INET6_BIND,
|
||||
AF_INET6,
|
||||
SOCK_DGRAM,
|
||||
SERV6_IP,
|
||||
SERV6_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
NULL,
|
||||
SUCCESS,
|
||||
},
|
||||
|
||||
/* connect */
|
||||
{
|
||||
@ -249,34 +189,6 @@ static struct sock_addr_test tests[] = {
|
||||
NULL,
|
||||
ATTACH_REJECT,
|
||||
},
|
||||
{
|
||||
"connect4: rewrite IP & TCP port",
|
||||
connect4_prog_load,
|
||||
BPF_CGROUP_INET4_CONNECT,
|
||||
BPF_CGROUP_INET4_CONNECT,
|
||||
AF_INET,
|
||||
SOCK_STREAM,
|
||||
SERV4_IP,
|
||||
SERV4_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
SRC4_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"connect4: rewrite IP & UDP port",
|
||||
connect4_prog_load,
|
||||
BPF_CGROUP_INET4_CONNECT,
|
||||
BPF_CGROUP_INET4_CONNECT,
|
||||
AF_INET,
|
||||
SOCK_DGRAM,
|
||||
SERV4_IP,
|
||||
SERV4_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
SRC4_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"connect6: load prog with wrong expected attach type",
|
||||
connect6_prog_load,
|
||||
@ -305,34 +217,6 @@ static struct sock_addr_test tests[] = {
|
||||
NULL,
|
||||
ATTACH_REJECT,
|
||||
},
|
||||
{
|
||||
"connect6: rewrite IP & TCP port",
|
||||
connect6_prog_load,
|
||||
BPF_CGROUP_INET6_CONNECT,
|
||||
BPF_CGROUP_INET6_CONNECT,
|
||||
AF_INET6,
|
||||
SOCK_STREAM,
|
||||
SERV6_IP,
|
||||
SERV6_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
SRC6_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"connect6: rewrite IP & UDP port",
|
||||
connect6_prog_load,
|
||||
BPF_CGROUP_INET6_CONNECT,
|
||||
BPF_CGROUP_INET6_CONNECT,
|
||||
AF_INET6,
|
||||
SOCK_DGRAM,
|
||||
SERV6_IP,
|
||||
SERV6_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
SRC6_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
|
||||
/* sendmsg */
|
||||
{
|
||||
@ -377,20 +261,6 @@ static struct sock_addr_test tests[] = {
|
||||
SRC4_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"sendmsg4: rewrite IP & port (C)",
|
||||
sendmsg4_rw_c_prog_load,
|
||||
BPF_CGROUP_UDP4_SENDMSG,
|
||||
BPF_CGROUP_UDP4_SENDMSG,
|
||||
AF_INET,
|
||||
SOCK_DGRAM,
|
||||
SERV4_IP,
|
||||
SERV4_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
SRC4_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"sendmsg4: deny call",
|
||||
sendmsg_deny_prog_load,
|
||||
@ -447,20 +317,6 @@ static struct sock_addr_test tests[] = {
|
||||
SRC6_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"sendmsg6: rewrite IP & port (C)",
|
||||
sendmsg6_rw_c_prog_load,
|
||||
BPF_CGROUP_UDP6_SENDMSG,
|
||||
BPF_CGROUP_UDP6_SENDMSG,
|
||||
AF_INET6,
|
||||
SOCK_DGRAM,
|
||||
SERV6_IP,
|
||||
SERV6_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
SRC6_REWRITE_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"sendmsg6: IPv4-mapped IPv6",
|
||||
sendmsg6_rw_v4mapped_prog_load,
|
||||
@ -575,34 +431,6 @@ static struct sock_addr_test tests[] = {
|
||||
NULL,
|
||||
LOAD_REJECT,
|
||||
},
|
||||
{
|
||||
"recvmsg4: rewrite IP & port (C)",
|
||||
recvmsg4_rw_c_prog_load,
|
||||
BPF_CGROUP_UDP4_RECVMSG,
|
||||
BPF_CGROUP_UDP4_RECVMSG,
|
||||
AF_INET,
|
||||
SOCK_DGRAM,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
SERV4_REWRITE_IP,
|
||||
SERV4_REWRITE_PORT,
|
||||
SERV4_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
{
|
||||
"recvmsg6: rewrite IP & port (C)",
|
||||
recvmsg6_rw_c_prog_load,
|
||||
BPF_CGROUP_UDP6_RECVMSG,
|
||||
BPF_CGROUP_UDP6_RECVMSG,
|
||||
AF_INET6,
|
||||
SOCK_DGRAM,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
SERV6_REWRITE_IP,
|
||||
SERV6_REWRITE_PORT,
|
||||
SERV6_IP,
|
||||
SUCCESS,
|
||||
},
|
||||
};
|
||||
|
||||
static int load_insns(const struct sock_addr_test *test,
|
||||
@ -761,16 +589,6 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
|
||||
return load_insns(test, insns, ARRAY_SIZE(insns));
|
||||
}
|
||||
|
||||
static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
|
||||
{
|
||||
return load_path(test, RECVMSG4_PROG_PATH);
|
||||
}
|
||||
|
||||
static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test)
|
||||
{
|
||||
return load_path(test, SENDMSG4_PROG_PATH);
|
||||
}
|
||||
|
||||
static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
|
||||
const char *rw_dst_ip)
|
||||
{
|
||||
@ -829,11 +647,6 @@ static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
|
||||
return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP);
|
||||
}
|
||||
|
||||
static int recvmsg6_rw_c_prog_load(const struct sock_addr_test *test)
|
||||
{
|
||||
return load_path(test, RECVMSG6_PROG_PATH);
|
||||
}
|
||||
|
||||
static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test)
|
||||
{
|
||||
return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP);
|
||||
@ -844,11 +657,6 @@ static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test)
|
||||
return sendmsg6_rw_dst_asm_prog_load(test, WILDCARD6_IP);
|
||||
}
|
||||
|
||||
static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test)
|
||||
{
|
||||
return load_path(test, SENDMSG6_PROG_PATH);
|
||||
}
|
||||
|
||||
static int cmp_addr(const struct sockaddr_storage *addr1,
|
||||
const struct sockaddr_storage *addr2, int cmp_port)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user