1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <sys/select.h>
14 #include <sys/socket.h>
16 #include <linux/filter.h>
19 #include <bpf/libbpf.h>
21 #include "cgroup_helpers.h"
22 #include "bpf_rlimit.h"
29 #define CG_PATH "/foo"
30 #define CONNECT4_PROG_PATH "./connect4_prog.o"
31 #define CONNECT6_PROG_PATH "./connect6_prog.o"
32 #define SENDMSG4_PROG_PATH "./sendmsg4_prog.o"
33 #define SENDMSG6_PROG_PATH "./sendmsg6_prog.o"
35 #define SERV4_IP "192.168.1.254"
36 #define SERV4_REWRITE_IP "127.0.0.1"
37 #define SRC4_IP "172.16.0.1"
38 #define SRC4_REWRITE_IP "127.0.0.4"
39 #define SERV4_PORT 4040
40 #define SERV4_REWRITE_PORT 4444
42 #define SERV6_IP "face:b00c:1234:5678::abcd"
43 #define SERV6_REWRITE_IP "::1"
44 #define SERV6_V4MAPPED_IP "::ffff:192.168.0.4"
46 #define SRC6_REWRITE_IP "::6"
47 #define WILDCARD6_IP "::"
48 #define SERV6_PORT 6060
49 #define SERV6_REWRITE_PORT 6666
51 #define INET_NTOP_BUF 40
53 struct sock_addr_test
;
55 typedef int (*load_fn
)(const struct sock_addr_test
*test
);
56 typedef int (*info_fn
)(int, struct sockaddr
*, socklen_t
*);
58 char bpf_log_buf
[BPF_LOG_BUF_SIZE
];
60 struct sock_addr_test
{
62 /* BPF prog properties */
64 enum bpf_attach_type expected_attach_type
;
65 enum bpf_attach_type attach_type
;
66 /* Socket properties */
69 /* IP:port pairs for BPF prog to override */
70 const char *requested_ip
;
71 unsigned short requested_port
;
72 const char *expected_ip
;
73 unsigned short expected_port
;
74 const char *expected_src_ip
;
75 /* Expected test result */
86 static int bind4_prog_load(const struct sock_addr_test
*test
);
87 static int bind6_prog_load(const struct sock_addr_test
*test
);
88 static int connect4_prog_load(const struct sock_addr_test
*test
);
89 static int connect6_prog_load(const struct sock_addr_test
*test
);
90 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
);
91 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
);
92 static int recvmsg_allow_prog_load(const struct sock_addr_test
*test
);
93 static int recvmsg_deny_prog_load(const struct sock_addr_test
*test
);
94 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
);
95 static int recvmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
);
96 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
);
97 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
);
98 static int recvmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
);
99 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
);
100 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
);
101 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
);
103 static struct sock_addr_test tests
[] = {
106 "bind4: load prog with wrong expected attach type",
108 BPF_CGROUP_INET6_BIND
,
109 BPF_CGROUP_INET4_BIND
,
120 "bind4: attach prog with wrong attach type",
122 BPF_CGROUP_INET4_BIND
,
123 BPF_CGROUP_INET6_BIND
,
134 "bind4: rewrite IP & TCP port in",
136 BPF_CGROUP_INET4_BIND
,
137 BPF_CGROUP_INET4_BIND
,
148 "bind4: rewrite IP & UDP port in",
150 BPF_CGROUP_INET4_BIND
,
151 BPF_CGROUP_INET4_BIND
,
162 "bind6: load prog with wrong expected attach type",
164 BPF_CGROUP_INET4_BIND
,
165 BPF_CGROUP_INET6_BIND
,
176 "bind6: attach prog with wrong attach type",
178 BPF_CGROUP_INET6_BIND
,
179 BPF_CGROUP_INET4_BIND
,
190 "bind6: rewrite IP & TCP port in",
192 BPF_CGROUP_INET6_BIND
,
193 BPF_CGROUP_INET6_BIND
,
204 "bind6: rewrite IP & UDP port in",
206 BPF_CGROUP_INET6_BIND
,
207 BPF_CGROUP_INET6_BIND
,
220 "connect4: load prog with wrong expected attach type",
222 BPF_CGROUP_INET6_CONNECT
,
223 BPF_CGROUP_INET4_CONNECT
,
234 "connect4: attach prog with wrong attach type",
236 BPF_CGROUP_INET4_CONNECT
,
237 BPF_CGROUP_INET6_CONNECT
,
248 "connect4: rewrite IP & TCP port",
250 BPF_CGROUP_INET4_CONNECT
,
251 BPF_CGROUP_INET4_CONNECT
,
262 "connect4: rewrite IP & UDP port",
264 BPF_CGROUP_INET4_CONNECT
,
265 BPF_CGROUP_INET4_CONNECT
,
276 "connect6: load prog with wrong expected attach type",
278 BPF_CGROUP_INET4_CONNECT
,
279 BPF_CGROUP_INET6_CONNECT
,
290 "connect6: attach prog with wrong attach type",
292 BPF_CGROUP_INET6_CONNECT
,
293 BPF_CGROUP_INET4_CONNECT
,
304 "connect6: rewrite IP & TCP port",
306 BPF_CGROUP_INET6_CONNECT
,
307 BPF_CGROUP_INET6_CONNECT
,
318 "connect6: rewrite IP & UDP port",
320 BPF_CGROUP_INET6_CONNECT
,
321 BPF_CGROUP_INET6_CONNECT
,
334 "sendmsg4: load prog with wrong expected attach type",
335 sendmsg4_rw_asm_prog_load
,
336 BPF_CGROUP_UDP6_SENDMSG
,
337 BPF_CGROUP_UDP4_SENDMSG
,
348 "sendmsg4: attach prog with wrong attach type",
349 sendmsg4_rw_asm_prog_load
,
350 BPF_CGROUP_UDP4_SENDMSG
,
351 BPF_CGROUP_UDP6_SENDMSG
,
362 "sendmsg4: rewrite IP & port (asm)",
363 sendmsg4_rw_asm_prog_load
,
364 BPF_CGROUP_UDP4_SENDMSG
,
365 BPF_CGROUP_UDP4_SENDMSG
,
376 "sendmsg4: rewrite IP & port (C)",
377 sendmsg4_rw_c_prog_load
,
378 BPF_CGROUP_UDP4_SENDMSG
,
379 BPF_CGROUP_UDP4_SENDMSG
,
390 "sendmsg4: deny call",
391 sendmsg_deny_prog_load
,
392 BPF_CGROUP_UDP4_SENDMSG
,
393 BPF_CGROUP_UDP4_SENDMSG
,
404 "sendmsg6: load prog with wrong expected attach type",
405 sendmsg6_rw_asm_prog_load
,
406 BPF_CGROUP_UDP4_SENDMSG
,
407 BPF_CGROUP_UDP6_SENDMSG
,
418 "sendmsg6: attach prog with wrong attach type",
419 sendmsg6_rw_asm_prog_load
,
420 BPF_CGROUP_UDP6_SENDMSG
,
421 BPF_CGROUP_UDP4_SENDMSG
,
432 "sendmsg6: rewrite IP & port (asm)",
433 sendmsg6_rw_asm_prog_load
,
434 BPF_CGROUP_UDP6_SENDMSG
,
435 BPF_CGROUP_UDP6_SENDMSG
,
446 "sendmsg6: rewrite IP & port (C)",
447 sendmsg6_rw_c_prog_load
,
448 BPF_CGROUP_UDP6_SENDMSG
,
449 BPF_CGROUP_UDP6_SENDMSG
,
460 "sendmsg6: IPv4-mapped IPv6",
461 sendmsg6_rw_v4mapped_prog_load
,
462 BPF_CGROUP_UDP6_SENDMSG
,
463 BPF_CGROUP_UDP6_SENDMSG
,
474 "sendmsg6: set dst IP = [::] (BSD'ism)",
475 sendmsg6_rw_wildcard_prog_load
,
476 BPF_CGROUP_UDP6_SENDMSG
,
477 BPF_CGROUP_UDP6_SENDMSG
,
488 "sendmsg6: preserve dst IP = [::] (BSD'ism)",
489 sendmsg_allow_prog_load
,
490 BPF_CGROUP_UDP6_SENDMSG
,
491 BPF_CGROUP_UDP6_SENDMSG
,
502 "sendmsg6: deny call",
503 sendmsg_deny_prog_load
,
504 BPF_CGROUP_UDP6_SENDMSG
,
505 BPF_CGROUP_UDP6_SENDMSG
,
518 "recvmsg4: return code ok",
519 recvmsg_allow_prog_load
,
520 BPF_CGROUP_UDP4_RECVMSG
,
521 BPF_CGROUP_UDP4_RECVMSG
,
532 "recvmsg4: return code !ok",
533 recvmsg_deny_prog_load
,
534 BPF_CGROUP_UDP4_RECVMSG
,
535 BPF_CGROUP_UDP4_RECVMSG
,
546 "recvmsg6: return code ok",
547 recvmsg_allow_prog_load
,
548 BPF_CGROUP_UDP6_RECVMSG
,
549 BPF_CGROUP_UDP6_RECVMSG
,
560 "recvmsg6: return code !ok",
561 recvmsg_deny_prog_load
,
562 BPF_CGROUP_UDP6_RECVMSG
,
563 BPF_CGROUP_UDP6_RECVMSG
,
574 "recvmsg4: rewrite IP & port (asm)",
575 recvmsg4_rw_asm_prog_load
,
576 BPF_CGROUP_UDP4_RECVMSG
,
577 BPF_CGROUP_UDP4_RECVMSG
,
588 "recvmsg6: rewrite IP & port (asm)",
589 recvmsg6_rw_asm_prog_load
,
590 BPF_CGROUP_UDP6_RECVMSG
,
591 BPF_CGROUP_UDP6_RECVMSG
,
603 static int mk_sockaddr(int domain
, const char *ip
, unsigned short port
,
604 struct sockaddr
*addr
, socklen_t addr_len
)
606 struct sockaddr_in6
*addr6
;
607 struct sockaddr_in
*addr4
;
609 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
610 log_err("Unsupported address family");
614 memset(addr
, 0, addr_len
);
616 if (domain
== AF_INET
) {
617 if (addr_len
< sizeof(struct sockaddr_in
))
619 addr4
= (struct sockaddr_in
*)addr
;
620 addr4
->sin_family
= domain
;
621 addr4
->sin_port
= htons(port
);
622 if (inet_pton(domain
, ip
, (void *)&addr4
->sin_addr
) != 1) {
623 log_err("Invalid IPv4: %s", ip
);
626 } else if (domain
== AF_INET6
) {
627 if (addr_len
< sizeof(struct sockaddr_in6
))
629 addr6
= (struct sockaddr_in6
*)addr
;
630 addr6
->sin6_family
= domain
;
631 addr6
->sin6_port
= htons(port
);
632 if (inet_pton(domain
, ip
, (void *)&addr6
->sin6_addr
) != 1) {
633 log_err("Invalid IPv6: %s", ip
);
641 static int load_insns(const struct sock_addr_test
*test
,
642 const struct bpf_insn
*insns
, size_t insns_cnt
)
644 struct bpf_load_program_attr load_attr
;
647 memset(&load_attr
, 0, sizeof(struct bpf_load_program_attr
));
648 load_attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
649 load_attr
.expected_attach_type
= test
->expected_attach_type
;
650 load_attr
.insns
= insns
;
651 load_attr
.insns_cnt
= insns_cnt
;
652 load_attr
.license
= "GPL";
654 ret
= bpf_load_program_xattr(&load_attr
, bpf_log_buf
, BPF_LOG_BUF_SIZE
);
655 if (ret
< 0 && test
->expected_result
!= LOAD_REJECT
) {
656 log_err(">>> Loading program error.\n"
657 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf
);
663 /* [1] These testing programs try to read different context fields, including
664 * narrow loads of different sizes from user_ip4 and user_ip6, and write to
665 * those allowed to be overridden.
667 * [2] BPF_LD_IMM64 & BPF_JMP_REG are used below whenever there is a need to
668 * compare a register with unsigned 32bit integer. BPF_JMP_IMM can't be used
669 * in such cases since it accepts only _signed_ 32bit integer as IMM
670 * argument. Also note that BPF_LD_IMM64 contains 2 instructions what matters
671 * to count jumps properly.
674 static int bind4_prog_load(const struct sock_addr_test
*test
)
678 uint16_t u4_addr16
[2];
681 struct sockaddr_in addr4_rw
;
683 if (inet_pton(AF_INET
, SERV4_IP
, (void *)&ip4
) != 1) {
684 log_err("Invalid IPv4: %s", SERV4_IP
);
688 if (mk_sockaddr(AF_INET
, SERV4_REWRITE_IP
, SERV4_REWRITE_PORT
,
689 (struct sockaddr
*)&addr4_rw
, sizeof(addr4_rw
)) == -1)
693 struct bpf_insn insns
[] = {
694 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
696 /* if (sk.family == AF_INET && */
697 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
698 offsetof(struct bpf_sock_addr
, family
)),
699 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 24),
701 /* (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */
702 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
703 offsetof(struct bpf_sock_addr
, type
)),
704 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 1),
706 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_STREAM
, 20),
708 /* 1st_byte_of_user_ip4 == expected && */
709 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
710 offsetof(struct bpf_sock_addr
, user_ip4
)),
711 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr8
[0], 18),
713 /* 2nd_byte_of_user_ip4 == expected && */
714 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
715 offsetof(struct bpf_sock_addr
, user_ip4
) + 1),
716 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr8
[1], 16),
718 /* 3rd_byte_of_user_ip4 == expected && */
719 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
720 offsetof(struct bpf_sock_addr
, user_ip4
) + 2),
721 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr8
[2], 14),
723 /* 4th_byte_of_user_ip4 == expected && */
724 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
725 offsetof(struct bpf_sock_addr
, user_ip4
) + 3),
726 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr8
[3], 12),
728 /* 1st_half_of_user_ip4 == expected && */
729 BPF_LDX_MEM(BPF_H
, BPF_REG_7
, BPF_REG_6
,
730 offsetof(struct bpf_sock_addr
, user_ip4
)),
731 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr16
[0], 10),
733 /* 2nd_half_of_user_ip4 == expected && */
734 BPF_LDX_MEM(BPF_H
, BPF_REG_7
, BPF_REG_6
,
735 offsetof(struct bpf_sock_addr
, user_ip4
) + 2),
736 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr16
[1], 8),
738 /* whole_user_ip4 == expected) { */
739 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
740 offsetof(struct bpf_sock_addr
, user_ip4
)),
741 BPF_LD_IMM64(BPF_REG_8
, ip4
.u4_addr32
), /* See [2]. */
742 BPF_JMP_REG(BPF_JNE
, BPF_REG_7
, BPF_REG_8
, 4),
744 /* user_ip4 = addr4_rw.sin_addr */
745 BPF_MOV32_IMM(BPF_REG_7
, addr4_rw
.sin_addr
.s_addr
),
746 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
747 offsetof(struct bpf_sock_addr
, user_ip4
)),
749 /* user_port = addr4_rw.sin_port */
750 BPF_MOV32_IMM(BPF_REG_7
, addr4_rw
.sin_port
),
751 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
752 offsetof(struct bpf_sock_addr
, user_port
)),
756 BPF_MOV64_IMM(BPF_REG_0
, 1),
760 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
763 static int bind6_prog_load(const struct sock_addr_test
*test
)
765 struct sockaddr_in6 addr6_rw
;
768 if (inet_pton(AF_INET6
, SERV6_IP
, (void *)&ip6
) != 1) {
769 log_err("Invalid IPv6: %s", SERV6_IP
);
773 if (mk_sockaddr(AF_INET6
, SERV6_REWRITE_IP
, SERV6_REWRITE_PORT
,
774 (struct sockaddr
*)&addr6_rw
, sizeof(addr6_rw
)) == -1)
778 struct bpf_insn insns
[] = {
779 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
781 /* if (sk.family == AF_INET6 && */
782 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
783 offsetof(struct bpf_sock_addr
, family
)),
784 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 18),
786 /* 5th_byte_of_user_ip6 == expected && */
787 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
788 offsetof(struct bpf_sock_addr
, user_ip6
[1])),
789 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip6
.s6_addr
[4], 16),
791 /* 3rd_half_of_user_ip6 == expected && */
792 BPF_LDX_MEM(BPF_H
, BPF_REG_7
, BPF_REG_6
,
793 offsetof(struct bpf_sock_addr
, user_ip6
[1])),
794 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip6
.s6_addr16
[2], 14),
796 /* last_word_of_user_ip6 == expected) { */
797 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
798 offsetof(struct bpf_sock_addr
, user_ip6
[3])),
799 BPF_LD_IMM64(BPF_REG_8
, ip6
.s6_addr32
[3]), /* See [2]. */
800 BPF_JMP_REG(BPF_JNE
, BPF_REG_7
, BPF_REG_8
, 10),
803 #define STORE_IPV6_WORD(N) \
804 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_addr.s6_addr32[N]), \
805 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
806 offsetof(struct bpf_sock_addr, user_ip6[N]))
808 /* user_ip6 = addr6_rw.sin6_addr */
814 /* user_port = addr6_rw.sin6_port */
815 BPF_MOV32_IMM(BPF_REG_7
, addr6_rw
.sin6_port
),
816 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
817 offsetof(struct bpf_sock_addr
, user_port
)),
822 BPF_MOV64_IMM(BPF_REG_0
, 1),
826 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
829 static int load_path(const struct sock_addr_test
*test
, const char *path
)
831 struct bpf_prog_load_attr attr
;
832 struct bpf_object
*obj
;
835 memset(&attr
, 0, sizeof(struct bpf_prog_load_attr
));
837 attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
838 attr
.expected_attach_type
= test
->expected_attach_type
;
839 attr
.prog_flags
= BPF_F_TEST_RND_HI32
;
841 if (bpf_prog_load_xattr(&attr
, &obj
, &prog_fd
)) {
842 if (test
->expected_result
!= LOAD_REJECT
)
843 log_err(">>> Loading program (%s) error.\n", path
);
850 static int connect4_prog_load(const struct sock_addr_test
*test
)
852 return load_path(test
, CONNECT4_PROG_PATH
);
855 static int connect6_prog_load(const struct sock_addr_test
*test
)
857 return load_path(test
, CONNECT6_PROG_PATH
);
860 static int xmsg_ret_only_prog_load(const struct sock_addr_test
*test
,
863 struct bpf_insn insns
[] = {
865 BPF_MOV64_IMM(BPF_REG_0
, rc
),
868 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
871 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
)
873 return xmsg_ret_only_prog_load(test
, /*rc*/ 1);
876 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
)
878 return xmsg_ret_only_prog_load(test
, /*rc*/ 0);
881 static int recvmsg_allow_prog_load(const struct sock_addr_test
*test
)
883 return xmsg_ret_only_prog_load(test
, /*rc*/ 1);
886 static int recvmsg_deny_prog_load(const struct sock_addr_test
*test
)
888 return xmsg_ret_only_prog_load(test
, /*rc*/ 0);
891 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
)
893 struct sockaddr_in dst4_rw_addr
;
894 struct in_addr src4_rw_ip
;
896 if (inet_pton(AF_INET
, SRC4_REWRITE_IP
, (void *)&src4_rw_ip
) != 1) {
897 log_err("Invalid IPv4: %s", SRC4_REWRITE_IP
);
901 if (mk_sockaddr(AF_INET
, SERV4_REWRITE_IP
, SERV4_REWRITE_PORT
,
902 (struct sockaddr
*)&dst4_rw_addr
,
903 sizeof(dst4_rw_addr
)) == -1)
906 struct bpf_insn insns
[] = {
907 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
909 /* if (sk.family == AF_INET && */
910 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
911 offsetof(struct bpf_sock_addr
, family
)),
912 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 8),
914 /* sk.type == SOCK_DGRAM) { */
915 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
916 offsetof(struct bpf_sock_addr
, type
)),
917 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 6),
919 /* msg_src_ip4 = src4_rw_ip */
920 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_ip
.s_addr
),
921 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
922 offsetof(struct bpf_sock_addr
, msg_src_ip4
)),
924 /* user_ip4 = dst4_rw_addr.sin_addr */
925 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_addr
.s_addr
),
926 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
927 offsetof(struct bpf_sock_addr
, user_ip4
)),
929 /* user_port = dst4_rw_addr.sin_port */
930 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_port
),
931 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
932 offsetof(struct bpf_sock_addr
, user_port
)),
936 BPF_MOV64_IMM(BPF_REG_0
, 1),
940 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
943 static int recvmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
)
945 struct sockaddr_in src4_rw_addr
;
947 if (mk_sockaddr(AF_INET
, SERV4_IP
, SERV4_PORT
,
948 (struct sockaddr
*)&src4_rw_addr
,
949 sizeof(src4_rw_addr
)) == -1)
952 struct bpf_insn insns
[] = {
953 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
955 /* if (sk.family == AF_INET && */
956 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
957 offsetof(struct bpf_sock_addr
, family
)),
958 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 6),
960 /* sk.type == SOCK_DGRAM) { */
961 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
962 offsetof(struct bpf_sock_addr
, type
)),
963 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 4),
965 /* user_ip4 = src4_rw_addr.sin_addr */
966 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_addr
.sin_addr
.s_addr
),
967 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
968 offsetof(struct bpf_sock_addr
, user_ip4
)),
970 /* user_port = src4_rw_addr.sin_port */
971 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_addr
.sin_port
),
972 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
973 offsetof(struct bpf_sock_addr
, user_port
)),
977 BPF_MOV64_IMM(BPF_REG_0
, 1),
981 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
984 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
)
986 return load_path(test
, SENDMSG4_PROG_PATH
);
989 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test
*test
,
990 const char *rw_dst_ip
)
992 struct sockaddr_in6 dst6_rw_addr
;
993 struct in6_addr src6_rw_ip
;
995 if (inet_pton(AF_INET6
, SRC6_REWRITE_IP
, (void *)&src6_rw_ip
) != 1) {
996 log_err("Invalid IPv6: %s", SRC6_REWRITE_IP
);
1000 if (mk_sockaddr(AF_INET6
, rw_dst_ip
, SERV6_REWRITE_PORT
,
1001 (struct sockaddr
*)&dst6_rw_addr
,
1002 sizeof(dst6_rw_addr
)) == -1)
1005 struct bpf_insn insns
[] = {
1006 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
1008 /* if (sk.family == AF_INET6) { */
1009 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
1010 offsetof(struct bpf_sock_addr
, family
)),
1011 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 18),
1013 #define STORE_IPV6_WORD_N(DST, SRC, N) \
1014 BPF_MOV32_IMM(BPF_REG_7, SRC[N]), \
1015 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
1016 offsetof(struct bpf_sock_addr, DST[N]))
1018 #define STORE_IPV6(DST, SRC) \
1019 STORE_IPV6_WORD_N(DST, SRC, 0), \
1020 STORE_IPV6_WORD_N(DST, SRC, 1), \
1021 STORE_IPV6_WORD_N(DST, SRC, 2), \
1022 STORE_IPV6_WORD_N(DST, SRC, 3)
1024 STORE_IPV6(msg_src_ip6
, src6_rw_ip
.s6_addr32
),
1025 STORE_IPV6(user_ip6
, dst6_rw_addr
.sin6_addr
.s6_addr32
),
1027 /* user_port = dst6_rw_addr.sin6_port */
1028 BPF_MOV32_IMM(BPF_REG_7
, dst6_rw_addr
.sin6_port
),
1029 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
1030 offsetof(struct bpf_sock_addr
, user_port
)),
1035 BPF_MOV64_IMM(BPF_REG_0
, 1),
1039 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
1042 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
)
1044 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_REWRITE_IP
);
1047 static int recvmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
)
1049 struct sockaddr_in6 src6_rw_addr
;
1051 if (mk_sockaddr(AF_INET6
, SERV6_IP
, SERV6_PORT
,
1052 (struct sockaddr
*)&src6_rw_addr
,
1053 sizeof(src6_rw_addr
)) == -1)
1056 struct bpf_insn insns
[] = {
1057 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
1059 /* if (sk.family == AF_INET6) { */
1060 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
1061 offsetof(struct bpf_sock_addr
, family
)),
1062 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 10),
1064 STORE_IPV6(user_ip6
, src6_rw_addr
.sin6_addr
.s6_addr32
),
1066 /* user_port = dst6_rw_addr.sin6_port */
1067 BPF_MOV32_IMM(BPF_REG_7
, src6_rw_addr
.sin6_port
),
1068 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
1069 offsetof(struct bpf_sock_addr
, user_port
)),
1073 BPF_MOV64_IMM(BPF_REG_0
, 1),
1077 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
1080 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
)
1082 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_V4MAPPED_IP
);
1085 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
)
1087 return sendmsg6_rw_dst_asm_prog_load(test
, WILDCARD6_IP
);
1090 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
)
1092 return load_path(test
, SENDMSG6_PROG_PATH
);
1095 static int cmp_addr(const struct sockaddr_storage
*addr1
,
1096 const struct sockaddr_storage
*addr2
, int cmp_port
)
1098 const struct sockaddr_in
*four1
, *four2
;
1099 const struct sockaddr_in6
*six1
, *six2
;
1101 if (addr1
->ss_family
!= addr2
->ss_family
)
1104 if (addr1
->ss_family
== AF_INET
) {
1105 four1
= (const struct sockaddr_in
*)addr1
;
1106 four2
= (const struct sockaddr_in
*)addr2
;
1107 return !((four1
->sin_port
== four2
->sin_port
|| !cmp_port
) &&
1108 four1
->sin_addr
.s_addr
== four2
->sin_addr
.s_addr
);
1109 } else if (addr1
->ss_family
== AF_INET6
) {
1110 six1
= (const struct sockaddr_in6
*)addr1
;
1111 six2
= (const struct sockaddr_in6
*)addr2
;
1112 return !((six1
->sin6_port
== six2
->sin6_port
|| !cmp_port
) &&
1113 !memcmp(&six1
->sin6_addr
, &six2
->sin6_addr
,
1114 sizeof(struct in6_addr
)));
1120 static int cmp_sock_addr(info_fn fn
, int sock1
,
1121 const struct sockaddr_storage
*addr2
, int cmp_port
)
1123 struct sockaddr_storage addr1
;
1124 socklen_t len1
= sizeof(addr1
);
1126 memset(&addr1
, 0, len1
);
1127 if (fn(sock1
, (struct sockaddr
*)&addr1
, (socklen_t
*)&len1
) != 0)
1130 return cmp_addr(&addr1
, addr2
, cmp_port
);
1133 static int cmp_local_ip(int sock1
, const struct sockaddr_storage
*addr2
)
1135 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 0);
1138 static int cmp_local_addr(int sock1
, const struct sockaddr_storage
*addr2
)
1140 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 1);
1143 static int cmp_peer_addr(int sock1
, const struct sockaddr_storage
*addr2
)
1145 return cmp_sock_addr(getpeername
, sock1
, addr2
, /*cmp_port*/ 1);
1148 static int start_server(int type
, const struct sockaddr_storage
*addr
,
1153 fd
= socket(addr
->ss_family
, type
, 0);
1155 log_err("Failed to create server socket");
1159 if (bind(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
1160 log_err("Failed to bind server socket");
1164 if (type
== SOCK_STREAM
) {
1165 if (listen(fd
, 128) == -1) {
1166 log_err("Failed to listen on server socket");
1179 static int connect_to_server(int type
, const struct sockaddr_storage
*addr
,
1185 domain
= addr
->ss_family
;
1187 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
1188 log_err("Unsupported address family");
1192 fd
= socket(domain
, type
, 0);
1194 log_err("Failed to create client socket");
1198 if (connect(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
1199 log_err("Fail to connect to server");
1211 int init_pktinfo(int domain
, struct cmsghdr
*cmsg
)
1213 struct in6_pktinfo
*pktinfo6
;
1214 struct in_pktinfo
*pktinfo4
;
1216 if (domain
== AF_INET
) {
1217 cmsg
->cmsg_level
= SOL_IP
;
1218 cmsg
->cmsg_type
= IP_PKTINFO
;
1219 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1220 pktinfo4
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
1221 memset(pktinfo4
, 0, sizeof(struct in_pktinfo
));
1222 if (inet_pton(domain
, SRC4_IP
,
1223 (void *)&pktinfo4
->ipi_spec_dst
) != 1)
1225 } else if (domain
== AF_INET6
) {
1226 cmsg
->cmsg_level
= SOL_IPV6
;
1227 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1228 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1229 pktinfo6
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
1230 memset(pktinfo6
, 0, sizeof(struct in6_pktinfo
));
1231 if (inet_pton(domain
, SRC6_IP
,
1232 (void *)&pktinfo6
->ipi6_addr
) != 1)
1241 static int sendmsg_to_server(int type
, const struct sockaddr_storage
*addr
,
1242 socklen_t addr_len
, int set_cmsg
, int flags
,
1246 char buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
1247 struct cmsghdr align
;
1250 char buf
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
1251 struct cmsghdr align
;
1259 domain
= addr
->ss_family
;
1261 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
1262 log_err("Unsupported address family");
1266 fd
= socket(domain
, type
, 0);
1268 log_err("Failed to create client socket");
1272 memset(&iov
, 0, sizeof(iov
));
1273 iov
.iov_base
= &data
;
1274 iov
.iov_len
= sizeof(data
);
1276 memset(&hdr
, 0, sizeof(hdr
));
1277 hdr
.msg_name
= (void *)addr
;
1278 hdr
.msg_namelen
= addr_len
;
1283 if (domain
== AF_INET
) {
1284 hdr
.msg_control
= &control4
;
1285 hdr
.msg_controllen
= sizeof(control4
.buf
);
1286 } else if (domain
== AF_INET6
) {
1287 hdr
.msg_control
= &control6
;
1288 hdr
.msg_controllen
= sizeof(control6
.buf
);
1290 if (init_pktinfo(domain
, CMSG_FIRSTHDR(&hdr
))) {
1291 log_err("Fail to init pktinfo");
1296 if (sendmsg(fd
, &hdr
, flags
) != sizeof(data
)) {
1297 log_err("Fail to send message to server");
1298 *syscall_err
= errno
;
1310 static int fastconnect_to_server(const struct sockaddr_storage
*addr
,
1315 return sendmsg_to_server(SOCK_STREAM
, addr
, addr_len
, /*set_cmsg*/0,
1316 MSG_FASTOPEN
, &sendmsg_err
);
1319 static int recvmsg_from_client(int sockfd
, struct sockaddr_storage
*src_addr
)
1328 FD_SET(sockfd
, &rfds
);
1333 if (select(sockfd
+ 1, &rfds
, NULL
, NULL
, &tv
) <= 0 ||
1334 !FD_ISSET(sockfd
, &rfds
))
1337 memset(&iov
, 0, sizeof(iov
));
1338 iov
.iov_base
= data
;
1339 iov
.iov_len
= sizeof(data
);
1341 memset(&hdr
, 0, sizeof(hdr
));
1342 hdr
.msg_name
= src_addr
;
1343 hdr
.msg_namelen
= sizeof(struct sockaddr_storage
);
1347 return recvmsg(sockfd
, &hdr
, 0);
1350 static int init_addrs(const struct sock_addr_test
*test
,
1351 struct sockaddr_storage
*requested_addr
,
1352 struct sockaddr_storage
*expected_addr
,
1353 struct sockaddr_storage
*expected_src_addr
)
1355 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1357 if (mk_sockaddr(test
->domain
, test
->expected_ip
, test
->expected_port
,
1358 (struct sockaddr
*)expected_addr
, addr_len
) == -1)
1361 if (mk_sockaddr(test
->domain
, test
->requested_ip
, test
->requested_port
,
1362 (struct sockaddr
*)requested_addr
, addr_len
) == -1)
1365 if (test
->expected_src_ip
&&
1366 mk_sockaddr(test
->domain
, test
->expected_src_ip
, 0,
1367 (struct sockaddr
*)expected_src_addr
, addr_len
) == -1)
1375 static int run_bind_test_case(const struct sock_addr_test
*test
)
1377 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1378 struct sockaddr_storage requested_addr
;
1379 struct sockaddr_storage expected_addr
;
1384 if (init_addrs(test
, &requested_addr
, &expected_addr
, NULL
))
1387 servfd
= start_server(test
->type
, &requested_addr
, addr_len
);
1391 if (cmp_local_addr(servfd
, &expected_addr
))
1394 /* Try to connect to server just in case */
1395 clientfd
= connect_to_server(test
->type
, &expected_addr
, addr_len
);
1408 static int run_connect_test_case(const struct sock_addr_test
*test
)
1410 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1411 struct sockaddr_storage expected_src_addr
;
1412 struct sockaddr_storage requested_addr
;
1413 struct sockaddr_storage expected_addr
;
1418 if (init_addrs(test
, &requested_addr
, &expected_addr
,
1419 &expected_src_addr
))
1422 /* Prepare server to connect to */
1423 servfd
= start_server(test
->type
, &expected_addr
, addr_len
);
1427 clientfd
= connect_to_server(test
->type
, &requested_addr
, addr_len
);
1431 /* Make sure src and dst addrs were overridden properly */
1432 if (cmp_peer_addr(clientfd
, &expected_addr
))
1435 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1438 if (test
->type
== SOCK_STREAM
) {
1439 /* Test TCP Fast Open scenario */
1440 clientfd
= fastconnect_to_server(&requested_addr
, addr_len
);
1444 /* Make sure src and dst addrs were overridden properly */
1445 if (cmp_peer_addr(clientfd
, &expected_addr
))
1448 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1461 static int run_xmsg_test_case(const struct sock_addr_test
*test
, int max_cmsg
)
1463 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1464 struct sockaddr_storage expected_addr
;
1465 struct sockaddr_storage server_addr
;
1466 struct sockaddr_storage sendmsg_addr
;
1467 struct sockaddr_storage recvmsg_addr
;
1473 if (test
->type
!= SOCK_DGRAM
)
1476 if (init_addrs(test
, &sendmsg_addr
, &server_addr
, &expected_addr
))
1479 /* Prepare server to sendmsg to */
1480 servfd
= start_server(test
->type
, &server_addr
, addr_len
);
1484 for (set_cmsg
= 0; set_cmsg
<= max_cmsg
; ++set_cmsg
) {
1488 clientfd
= sendmsg_to_server(test
->type
, &sendmsg_addr
,
1489 addr_len
, set_cmsg
, /*flags*/0,
1493 else if (clientfd
== -1)
1496 /* Try to receive message on server instead of using
1497 * getpeername(2) on client socket, to check that client's
1498 * destination address was rewritten properly, since
1499 * getpeername(2) doesn't work with unconnected datagram
1502 * Get source address from recvmsg(2) as well to make sure
1503 * source was rewritten properly: getsockname(2) can't be used
1504 * since socket is unconnected and source defined for one
1505 * specific packet may differ from the one used by default and
1506 * returned by getsockname(2).
1508 if (recvmsg_from_client(servfd
, &recvmsg_addr
) == -1)
1511 if (cmp_addr(&recvmsg_addr
, &expected_addr
, /*cmp_port*/0))
1524 static int run_test_case(int cgfd
, const struct sock_addr_test
*test
)
1529 printf("Test case: %s .. ", test
->descr
);
1531 progfd
= test
->loadfn(test
);
1532 if (test
->expected_result
== LOAD_REJECT
&& progfd
< 0)
1534 else if (test
->expected_result
== LOAD_REJECT
|| progfd
< 0)
1537 err
= bpf_prog_attach(progfd
, cgfd
, test
->attach_type
,
1538 BPF_F_ALLOW_OVERRIDE
);
1539 if (test
->expected_result
== ATTACH_REJECT
&& err
) {
1540 err
= 0; /* error was expected, reset it */
1542 } else if (test
->expected_result
== ATTACH_REJECT
|| err
) {
1544 } else if (test
->expected_result
== ATTACH_OKAY
) {
1549 switch (test
->attach_type
) {
1550 case BPF_CGROUP_INET4_BIND
:
1551 case BPF_CGROUP_INET6_BIND
:
1552 err
= run_bind_test_case(test
);
1554 case BPF_CGROUP_INET4_CONNECT
:
1555 case BPF_CGROUP_INET6_CONNECT
:
1556 err
= run_connect_test_case(test
);
1558 case BPF_CGROUP_UDP4_SENDMSG
:
1559 case BPF_CGROUP_UDP6_SENDMSG
:
1560 err
= run_xmsg_test_case(test
, 1);
1562 case BPF_CGROUP_UDP4_RECVMSG
:
1563 case BPF_CGROUP_UDP6_RECVMSG
:
1564 err
= run_xmsg_test_case(test
, 0);
1570 if (test
->expected_result
== SYSCALL_EPERM
&& err
== EPERM
) {
1571 err
= 0; /* error was expected, reset it */
1575 if (test
->expected_result
== SYSCALL_ENOTSUPP
&& err
== ENOTSUPP
) {
1576 err
= 0; /* error was expected, reset it */
1580 if (err
|| test
->expected_result
!= SUCCESS
)
1587 /* Detaching w/o checking return code: best effort attempt. */
1589 bpf_prog_detach(cgfd
, test
->attach_type
);
1591 printf("[%s]\n", err
? "FAIL" : "PASS");
1595 static int run_tests(int cgfd
)
1601 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
1602 if (run_test_case(cgfd
, &tests
[i
]))
1607 printf("Summary: %d PASSED, %d FAILED\n", passes
, fails
);
1608 return fails
? -1 : 0;
1611 int main(int argc
, char **argv
)
1618 "%s has to be run via %s.sh. Skip direct run.\n",
1623 if (setup_cgroup_environment())
1626 cgfd
= create_and_get_cgroup(CG_PATH
);
1630 if (join_cgroup(CG_PATH
))
1633 if (run_tests(cgfd
))
1641 cleanup_cgroup_environment();