Skip to content

Commit 8eaf805

Browse files
jrifeAlexei Starovoitov
authored andcommitted
selftests/bpf: Migrate wildcard destination rewrite test
Migrate test case from bpf/test_sock_addr.c ensuring that sendmsg respects when sendmsg6 hooks rewrite the destination IP with the IPv6 wildcard IP, [::]. Signed-off-by: Jordan Rife <jrife@google.com> Link: https://lore.kernel.org/r/20240510190246.3247730-10-jrife@google.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 54462e8 commit 8eaf805

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

tools/testing/selftests/bpf/prog_tests/sock_addr.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_prog);
448448
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_deny_prog);
449449
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_preserve_dst_prog);
450450
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_v4mapped_prog);
451+
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_wildcard_prog);
451452
BPF_SKEL_FUNCS(sendmsg_unix_prog, sendmsg_unix_prog);
452453
BPF_SKEL_FUNCS(recvmsg4_prog, recvmsg4_prog);
453454
BPF_SKEL_FUNCS(recvmsg6_prog, recvmsg6_prog);
@@ -849,6 +850,22 @@ static struct sock_addr_test tests[] = {
849850
SRC6_REWRITE_IP,
850851
SYSCALL_ENOTSUPP,
851852
},
853+
{
854+
SOCK_ADDR_TEST_SENDMSG,
855+
"sendmsg6: sendmsg dst IP = [::] (BSD'ism) (dgram)",
856+
sendmsg_v6_wildcard_prog_load,
857+
sendmsg_v6_wildcard_prog_destroy,
858+
BPF_CGROUP_UDP6_SENDMSG,
859+
&user_ops,
860+
AF_INET6,
861+
SOCK_DGRAM,
862+
SERV6_IP,
863+
SERV6_PORT,
864+
SERV6_REWRITE_IP,
865+
SERV6_REWRITE_PORT,
866+
SRC6_REWRITE_IP,
867+
SUCCESS,
868+
},
852869
{
853870
SOCK_ADDR_TEST_SENDMSG,
854871
"sendmsg_unix: sendmsg (dgram)",

tools/testing/selftests/bpf/progs/sendmsg6_prog.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ int sendmsg_v6_v4mapped_prog(struct bpf_sock_addr *ctx)
8484
return 1;
8585
}
8686

87+
SEC("cgroup/sendmsg6")
88+
int sendmsg_v6_wildcard_prog(struct bpf_sock_addr *ctx)
89+
{
90+
/* Rewrite source. */
91+
ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
92+
ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
93+
ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
94+
ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
95+
96+
/* Rewrite destination. */
97+
ctx->user_ip6[0] = bpf_htonl(0);
98+
ctx->user_ip6[1] = bpf_htonl(0);
99+
ctx->user_ip6[2] = bpf_htonl(0);
100+
ctx->user_ip6[3] = bpf_htonl(0);
101+
102+
ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
103+
104+
return 1;
105+
}
106+
87107
SEC("cgroup/sendmsg6")
88108
int sendmsg_v6_preserve_dst_prog(struct bpf_sock_addr *ctx)
89109
{

tools/testing/selftests/bpf/test_sock_addr.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ static int connect4_prog_load(const struct sock_addr_test *test);
9494
static int connect6_prog_load(const struct sock_addr_test *test);
9595
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
9696
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
97-
static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test);
9897

9998
static struct sock_addr_test tests[] = {
10099
/* bind */
@@ -298,20 +297,6 @@ static struct sock_addr_test tests[] = {
298297
SRC6_REWRITE_IP,
299298
SUCCESS,
300299
},
301-
{
302-
"sendmsg6: set dst IP = [::] (BSD'ism)",
303-
sendmsg6_rw_wildcard_prog_load,
304-
BPF_CGROUP_UDP6_SENDMSG,
305-
BPF_CGROUP_UDP6_SENDMSG,
306-
AF_INET6,
307-
SOCK_DGRAM,
308-
SERV6_IP,
309-
SERV6_PORT,
310-
SERV6_REWRITE_IP,
311-
SERV6_REWRITE_PORT,
312-
SRC6_REWRITE_IP,
313-
SUCCESS,
314-
},
315300
};
316301

317302
static int load_insns(const struct sock_addr_test *test,
@@ -497,11 +482,6 @@ static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
497482
return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP);
498483
}
499484

500-
static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test)
501-
{
502-
return sendmsg6_rw_dst_asm_prog_load(test, WILDCARD6_IP);
503-
}
504-
505485
static int cmp_addr(const struct sockaddr_storage *addr1,
506486
const struct sockaddr_storage *addr2, int cmp_port)
507487
{

0 commit comments

Comments
 (0)