]> git.dujemihanovic.xyz Git - linux.git/commitdiff
selftests/bpf: Migrate wildcard destination rewrite test
authorJordan Rife <jrife@google.com>
Fri, 10 May 2024 19:02:26 +0000 (14:02 -0500)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 13 May 2024 00:10:41 +0000 (17:10 -0700)
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>
tools/testing/selftests/bpf/prog_tests/sock_addr.c
tools/testing/selftests/bpf/progs/sendmsg6_prog.c
tools/testing/selftests/bpf/test_sock_addr.c

index f096203171b1750b5b29a49e5351ca8a9658dfd4..e3c450d11b9ef467d8cd55bbcb78d9b6a834825b 100644 (file)
@@ -448,6 +448,7 @@ BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_prog);
 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_deny_prog);
 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_preserve_dst_prog);
 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_v4mapped_prog);
+BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_wildcard_prog);
 BPF_SKEL_FUNCS(sendmsg_unix_prog, sendmsg_unix_prog);
 BPF_SKEL_FUNCS(recvmsg4_prog, recvmsg4_prog);
 BPF_SKEL_FUNCS(recvmsg6_prog, recvmsg6_prog);
@@ -849,6 +850,22 @@ static struct sock_addr_test tests[] = {
                SRC6_REWRITE_IP,
                SYSCALL_ENOTSUPP,
        },
+       {
+               SOCK_ADDR_TEST_SENDMSG,
+               "sendmsg6: sendmsg dst IP = [::] (BSD'ism) (dgram)",
+               sendmsg_v6_wildcard_prog_load,
+               sendmsg_v6_wildcard_prog_destroy,
+               BPF_CGROUP_UDP6_SENDMSG,
+               &user_ops,
+               AF_INET6,
+               SOCK_DGRAM,
+               SERV6_IP,
+               SERV6_PORT,
+               SERV6_REWRITE_IP,
+               SERV6_REWRITE_PORT,
+               SRC6_REWRITE_IP,
+               SUCCESS,
+       },
        {
                SOCK_ADDR_TEST_SENDMSG,
                "sendmsg_unix: sendmsg (dgram)",
index 7611d9e17dd169557f2b00da034540d1edeebbeb..36a7f960799f00f95bc28f2c42186bc118496792 100644 (file)
@@ -84,6 +84,26 @@ int sendmsg_v6_v4mapped_prog(struct bpf_sock_addr *ctx)
        return 1;
 }
 
+SEC("cgroup/sendmsg6")
+int sendmsg_v6_wildcard_prog(struct bpf_sock_addr *ctx)
+{
+       /* Rewrite source. */
+       ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0);
+       ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1);
+       ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2);
+       ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3);
+
+       /* Rewrite destination. */
+       ctx->user_ip6[0] = bpf_htonl(0);
+       ctx->user_ip6[1] = bpf_htonl(0);
+       ctx->user_ip6[2] = bpf_htonl(0);
+       ctx->user_ip6[3] = bpf_htonl(0);
+
+       ctx->user_port = bpf_htons(DST_REWRITE_PORT6);
+
+       return 1;
+}
+
 SEC("cgroup/sendmsg6")
 int sendmsg_v6_preserve_dst_prog(struct bpf_sock_addr *ctx)
 {
index 4ead113753f817f0cac36d61716fd8a4d0a6524f..85fb2a793be501e4905c06bdb520e086ae3f6832 100644 (file)
@@ -94,7 +94,6 @@ static int connect4_prog_load(const struct sock_addr_test *test);
 static int connect6_prog_load(const struct sock_addr_test *test);
 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
-static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test);
 
 static struct sock_addr_test tests[] = {
        /* bind */
@@ -298,20 +297,6 @@ static struct sock_addr_test tests[] = {
                SRC6_REWRITE_IP,
                SUCCESS,
        },
-       {
-               "sendmsg6: set dst IP = [::] (BSD'ism)",
-               sendmsg6_rw_wildcard_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,
-       },
 };
 
 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)
        return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP);
 }
 
-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 cmp_addr(const struct sockaddr_storage *addr1,
                    const struct sockaddr_storage *addr2, int cmp_port)
 {