Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Migrate sendmsg6 v4 mapped address tests

Migrate test case from bpf/test_sock_addr.c ensuring that sendmsg
returns -ENOTSUPP when sending to an IPv4-mapped IPv6 address to
prog_tests/sock_addr.c.

Signed-off-by: Jordan Rife <jrife@google.com>
Link: https://lore.kernel.org/r/20240510190246.3247730-9-jrife@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jordan Rife and committed by
Alexei Starovoitov
54462e84 f46a1048

+42 -20
+17
tools/testing/selftests/bpf/prog_tests/sock_addr.c
··· 447 447 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_prog); 448 448 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_deny_prog); 449 449 BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_preserve_dst_prog); 450 + BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_v4mapped_prog); 450 451 BPF_SKEL_FUNCS(sendmsg_unix_prog, sendmsg_unix_prog); 451 452 BPF_SKEL_FUNCS(recvmsg4_prog, recvmsg4_prog); 452 453 BPF_SKEL_FUNCS(recvmsg6_prog, recvmsg6_prog); ··· 832 831 SERV6_REWRITE_PORT, 833 832 SRC6_REWRITE_IP, 834 833 SYSCALL_EPERM, 834 + }, 835 + { 836 + SOCK_ADDR_TEST_SENDMSG, 837 + "sendmsg6: sendmsg IPv4-mapped IPv6 (dgram)", 838 + sendmsg_v6_v4mapped_prog_load, 839 + sendmsg_v6_v4mapped_prog_destroy, 840 + BPF_CGROUP_UDP6_SENDMSG, 841 + &user_ops, 842 + AF_INET6, 843 + SOCK_DGRAM, 844 + SERV6_IP, 845 + SERV6_PORT, 846 + SERV6_REWRITE_IP, 847 + SERV6_REWRITE_PORT, 848 + SRC6_REWRITE_IP, 849 + SYSCALL_ENOTSUPP, 835 850 }, 836 851 { 837 852 SOCK_ADDR_TEST_SENDMSG,
+25
tools/testing/selftests/bpf/progs/sendmsg6_prog.c
··· 20 20 #define DST_REWRITE_IP6_2 0 21 21 #define DST_REWRITE_IP6_3 1 22 22 23 + #define DST_REWRITE_IP6_V4_MAPPED_0 0 24 + #define DST_REWRITE_IP6_V4_MAPPED_1 0 25 + #define DST_REWRITE_IP6_V4_MAPPED_2 0x0000FFFF 26 + #define DST_REWRITE_IP6_V4_MAPPED_3 0xc0a80004 // 192.168.0.4 27 + 23 28 #define DST_REWRITE_PORT6 6666 24 29 25 30 SEC("cgroup/sendmsg6") ··· 60 55 /* Unexpected destination. Reject sendmsg. */ 61 56 return 0; 62 57 } 58 + 59 + return 1; 60 + } 61 + 62 + SEC("cgroup/sendmsg6") 63 + int sendmsg_v6_v4mapped_prog(struct bpf_sock_addr *ctx) 64 + { 65 + /* Rewrite source. */ 66 + ctx->msg_src_ip6[0] = bpf_htonl(SRC_REWRITE_IP6_0); 67 + ctx->msg_src_ip6[1] = bpf_htonl(SRC_REWRITE_IP6_1); 68 + ctx->msg_src_ip6[2] = bpf_htonl(SRC_REWRITE_IP6_2); 69 + ctx->msg_src_ip6[3] = bpf_htonl(SRC_REWRITE_IP6_3); 70 + 71 + /* Rewrite destination. */ 72 + ctx->user_ip6[0] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_0); 73 + ctx->user_ip6[1] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_1); 74 + ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_2); 75 + ctx->user_ip6[3] = bpf_htonl(DST_REWRITE_IP6_V4_MAPPED_3); 76 + 77 + ctx->user_port = bpf_htons(DST_REWRITE_PORT6); 63 78 64 79 return 1; 65 80 }
-20
tools/testing/selftests/bpf/test_sock_addr.c
··· 94 94 static int connect6_prog_load(const struct sock_addr_test *test); 95 95 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test); 96 96 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test); 97 - static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test); 98 97 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test); 99 98 100 99 static struct sock_addr_test tests[] = { ··· 299 300 SUCCESS, 300 301 }, 301 302 { 302 - "sendmsg6: IPv4-mapped IPv6", 303 - sendmsg6_rw_v4mapped_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 - SYSCALL_ENOTSUPP, 314 - }, 315 - { 316 303 "sendmsg6: set dst IP = [::] (BSD'ism)", 317 304 sendmsg6_rw_wildcard_prog_load, 318 305 BPF_CGROUP_UDP6_SENDMSG, ··· 495 510 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test) 496 511 { 497 512 return sendmsg6_rw_dst_asm_prog_load(test, SERV6_REWRITE_IP); 498 - } 499 - 500 - static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test) 501 - { 502 - return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP); 503 513 } 504 514 505 515 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test)