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"
34 #define BIND4_PROG_PATH "./bind4_prog.o"
35 #define BIND6_PROG_PATH "./bind6_prog.o"
37 #define SERV4_IP "192.168.1.254"
38 #define SERV4_REWRITE_IP "127.0.0.1"
39 #define SRC4_IP "172.16.0.1"
40 #define SRC4_REWRITE_IP "127.0.0.4"
41 #define SERV4_PORT 4040
42 #define SERV4_REWRITE_PORT 4444
44 #define SERV6_IP "face:b00c:1234:5678::abcd"
45 #define SERV6_REWRITE_IP "::1"
46 #define SERV6_V4MAPPED_IP "::ffff:192.168.0.4"
48 #define SRC6_REWRITE_IP "::6"
49 #define WILDCARD6_IP "::"
50 #define SERV6_PORT 6060
51 #define SERV6_REWRITE_PORT 6666
53 #define INET_NTOP_BUF 40
55 struct sock_addr_test
;
57 typedef int (*load_fn
)(const struct sock_addr_test
*test
);
58 typedef int (*info_fn
)(int, struct sockaddr
*, socklen_t
*);
60 char bpf_log_buf
[BPF_LOG_BUF_SIZE
];
62 struct sock_addr_test
{
64 /* BPF prog properties */
66 enum bpf_attach_type expected_attach_type
;
67 enum bpf_attach_type attach_type
;
68 /* Socket properties */
71 /* IP:port pairs for BPF prog to override */
72 const char *requested_ip
;
73 unsigned short requested_port
;
74 const char *expected_ip
;
75 unsigned short expected_port
;
76 const char *expected_src_ip
;
77 /* Expected test result */
88 static int bind4_prog_load(const struct sock_addr_test
*test
);
89 static int bind6_prog_load(const struct sock_addr_test
*test
);
90 static int connect4_prog_load(const struct sock_addr_test
*test
);
91 static int connect6_prog_load(const struct sock_addr_test
*test
);
92 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
);
93 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
);
94 static int recvmsg_allow_prog_load(const struct sock_addr_test
*test
);
95 static int recvmsg_deny_prog_load(const struct sock_addr_test
*test
);
96 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
);
97 static int recvmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
);
98 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
);
99 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
);
100 static int recvmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
);
101 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
);
102 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
);
103 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
);
105 static struct sock_addr_test tests
[] = {
108 "bind4: load prog with wrong expected attach type",
110 BPF_CGROUP_INET6_BIND
,
111 BPF_CGROUP_INET4_BIND
,
122 "bind4: attach prog with wrong attach type",
124 BPF_CGROUP_INET4_BIND
,
125 BPF_CGROUP_INET6_BIND
,
136 "bind4: rewrite IP & TCP port in",
138 BPF_CGROUP_INET4_BIND
,
139 BPF_CGROUP_INET4_BIND
,
150 "bind4: rewrite IP & UDP port in",
152 BPF_CGROUP_INET4_BIND
,
153 BPF_CGROUP_INET4_BIND
,
164 "bind6: load prog with wrong expected attach type",
166 BPF_CGROUP_INET4_BIND
,
167 BPF_CGROUP_INET6_BIND
,
178 "bind6: attach prog with wrong attach type",
180 BPF_CGROUP_INET6_BIND
,
181 BPF_CGROUP_INET4_BIND
,
192 "bind6: rewrite IP & TCP port in",
194 BPF_CGROUP_INET6_BIND
,
195 BPF_CGROUP_INET6_BIND
,
206 "bind6: rewrite IP & UDP port in",
208 BPF_CGROUP_INET6_BIND
,
209 BPF_CGROUP_INET6_BIND
,
222 "connect4: load prog with wrong expected attach type",
224 BPF_CGROUP_INET6_CONNECT
,
225 BPF_CGROUP_INET4_CONNECT
,
236 "connect4: attach prog with wrong attach type",
238 BPF_CGROUP_INET4_CONNECT
,
239 BPF_CGROUP_INET6_CONNECT
,
250 "connect4: rewrite IP & TCP port",
252 BPF_CGROUP_INET4_CONNECT
,
253 BPF_CGROUP_INET4_CONNECT
,
264 "connect4: rewrite IP & UDP port",
266 BPF_CGROUP_INET4_CONNECT
,
267 BPF_CGROUP_INET4_CONNECT
,
278 "connect6: load prog with wrong expected attach type",
280 BPF_CGROUP_INET4_CONNECT
,
281 BPF_CGROUP_INET6_CONNECT
,
292 "connect6: attach prog with wrong attach type",
294 BPF_CGROUP_INET6_CONNECT
,
295 BPF_CGROUP_INET4_CONNECT
,
306 "connect6: rewrite IP & TCP port",
308 BPF_CGROUP_INET6_CONNECT
,
309 BPF_CGROUP_INET6_CONNECT
,
320 "connect6: rewrite IP & UDP port",
322 BPF_CGROUP_INET6_CONNECT
,
323 BPF_CGROUP_INET6_CONNECT
,
336 "sendmsg4: load prog with wrong expected attach type",
337 sendmsg4_rw_asm_prog_load
,
338 BPF_CGROUP_UDP6_SENDMSG
,
339 BPF_CGROUP_UDP4_SENDMSG
,
350 "sendmsg4: attach prog with wrong attach type",
351 sendmsg4_rw_asm_prog_load
,
352 BPF_CGROUP_UDP4_SENDMSG
,
353 BPF_CGROUP_UDP6_SENDMSG
,
364 "sendmsg4: rewrite IP & port (asm)",
365 sendmsg4_rw_asm_prog_load
,
366 BPF_CGROUP_UDP4_SENDMSG
,
367 BPF_CGROUP_UDP4_SENDMSG
,
378 "sendmsg4: rewrite IP & port (C)",
379 sendmsg4_rw_c_prog_load
,
380 BPF_CGROUP_UDP4_SENDMSG
,
381 BPF_CGROUP_UDP4_SENDMSG
,
392 "sendmsg4: deny call",
393 sendmsg_deny_prog_load
,
394 BPF_CGROUP_UDP4_SENDMSG
,
395 BPF_CGROUP_UDP4_SENDMSG
,
406 "sendmsg6: load prog with wrong expected attach type",
407 sendmsg6_rw_asm_prog_load
,
408 BPF_CGROUP_UDP4_SENDMSG
,
409 BPF_CGROUP_UDP6_SENDMSG
,
420 "sendmsg6: attach prog with wrong attach type",
421 sendmsg6_rw_asm_prog_load
,
422 BPF_CGROUP_UDP6_SENDMSG
,
423 BPF_CGROUP_UDP4_SENDMSG
,
434 "sendmsg6: rewrite IP & port (asm)",
435 sendmsg6_rw_asm_prog_load
,
436 BPF_CGROUP_UDP6_SENDMSG
,
437 BPF_CGROUP_UDP6_SENDMSG
,
448 "sendmsg6: rewrite IP & port (C)",
449 sendmsg6_rw_c_prog_load
,
450 BPF_CGROUP_UDP6_SENDMSG
,
451 BPF_CGROUP_UDP6_SENDMSG
,
462 "sendmsg6: IPv4-mapped IPv6",
463 sendmsg6_rw_v4mapped_prog_load
,
464 BPF_CGROUP_UDP6_SENDMSG
,
465 BPF_CGROUP_UDP6_SENDMSG
,
476 "sendmsg6: set dst IP = [::] (BSD'ism)",
477 sendmsg6_rw_wildcard_prog_load
,
478 BPF_CGROUP_UDP6_SENDMSG
,
479 BPF_CGROUP_UDP6_SENDMSG
,
490 "sendmsg6: preserve dst IP = [::] (BSD'ism)",
491 sendmsg_allow_prog_load
,
492 BPF_CGROUP_UDP6_SENDMSG
,
493 BPF_CGROUP_UDP6_SENDMSG
,
504 "sendmsg6: deny call",
505 sendmsg_deny_prog_load
,
506 BPF_CGROUP_UDP6_SENDMSG
,
507 BPF_CGROUP_UDP6_SENDMSG
,
520 "recvmsg4: return code ok",
521 recvmsg_allow_prog_load
,
522 BPF_CGROUP_UDP4_RECVMSG
,
523 BPF_CGROUP_UDP4_RECVMSG
,
534 "recvmsg4: return code !ok",
535 recvmsg_deny_prog_load
,
536 BPF_CGROUP_UDP4_RECVMSG
,
537 BPF_CGROUP_UDP4_RECVMSG
,
548 "recvmsg6: return code ok",
549 recvmsg_allow_prog_load
,
550 BPF_CGROUP_UDP6_RECVMSG
,
551 BPF_CGROUP_UDP6_RECVMSG
,
562 "recvmsg6: return code !ok",
563 recvmsg_deny_prog_load
,
564 BPF_CGROUP_UDP6_RECVMSG
,
565 BPF_CGROUP_UDP6_RECVMSG
,
576 "recvmsg4: rewrite IP & port (asm)",
577 recvmsg4_rw_asm_prog_load
,
578 BPF_CGROUP_UDP4_RECVMSG
,
579 BPF_CGROUP_UDP4_RECVMSG
,
590 "recvmsg6: rewrite IP & port (asm)",
591 recvmsg6_rw_asm_prog_load
,
592 BPF_CGROUP_UDP6_RECVMSG
,
593 BPF_CGROUP_UDP6_RECVMSG
,
605 static int mk_sockaddr(int domain
, const char *ip
, unsigned short port
,
606 struct sockaddr
*addr
, socklen_t addr_len
)
608 struct sockaddr_in6
*addr6
;
609 struct sockaddr_in
*addr4
;
611 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
612 log_err("Unsupported address family");
616 memset(addr
, 0, addr_len
);
618 if (domain
== AF_INET
) {
619 if (addr_len
< sizeof(struct sockaddr_in
))
621 addr4
= (struct sockaddr_in
*)addr
;
622 addr4
->sin_family
= domain
;
623 addr4
->sin_port
= htons(port
);
624 if (inet_pton(domain
, ip
, (void *)&addr4
->sin_addr
) != 1) {
625 log_err("Invalid IPv4: %s", ip
);
628 } else if (domain
== AF_INET6
) {
629 if (addr_len
< sizeof(struct sockaddr_in6
))
631 addr6
= (struct sockaddr_in6
*)addr
;
632 addr6
->sin6_family
= domain
;
633 addr6
->sin6_port
= htons(port
);
634 if (inet_pton(domain
, ip
, (void *)&addr6
->sin6_addr
) != 1) {
635 log_err("Invalid IPv6: %s", ip
);
643 static int load_insns(const struct sock_addr_test
*test
,
644 const struct bpf_insn
*insns
, size_t insns_cnt
)
646 struct bpf_load_program_attr load_attr
;
649 memset(&load_attr
, 0, sizeof(struct bpf_load_program_attr
));
650 load_attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
651 load_attr
.expected_attach_type
= test
->expected_attach_type
;
652 load_attr
.insns
= insns
;
653 load_attr
.insns_cnt
= insns_cnt
;
654 load_attr
.license
= "GPL";
656 ret
= bpf_load_program_xattr(&load_attr
, bpf_log_buf
, BPF_LOG_BUF_SIZE
);
657 if (ret
< 0 && test
->expected_result
!= LOAD_REJECT
) {
658 log_err(">>> Loading program error.\n"
659 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf
);
665 static int load_path(const struct sock_addr_test
*test
, const char *path
)
667 struct bpf_prog_load_attr attr
;
668 struct bpf_object
*obj
;
671 memset(&attr
, 0, sizeof(struct bpf_prog_load_attr
));
673 attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
674 attr
.expected_attach_type
= test
->expected_attach_type
;
675 attr
.prog_flags
= BPF_F_TEST_RND_HI32
;
677 if (bpf_prog_load_xattr(&attr
, &obj
, &prog_fd
)) {
678 if (test
->expected_result
!= LOAD_REJECT
)
679 log_err(">>> Loading program (%s) error.\n", path
);
686 static int bind4_prog_load(const struct sock_addr_test
*test
)
688 return load_path(test
, BIND4_PROG_PATH
);
691 static int bind6_prog_load(const struct sock_addr_test
*test
)
693 return load_path(test
, BIND6_PROG_PATH
);
696 static int connect4_prog_load(const struct sock_addr_test
*test
)
698 return load_path(test
, CONNECT4_PROG_PATH
);
701 static int connect6_prog_load(const struct sock_addr_test
*test
)
703 return load_path(test
, CONNECT6_PROG_PATH
);
706 static int xmsg_ret_only_prog_load(const struct sock_addr_test
*test
,
709 struct bpf_insn insns
[] = {
711 BPF_MOV64_IMM(BPF_REG_0
, rc
),
714 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
717 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
)
719 return xmsg_ret_only_prog_load(test
, /*rc*/ 1);
722 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
)
724 return xmsg_ret_only_prog_load(test
, /*rc*/ 0);
727 static int recvmsg_allow_prog_load(const struct sock_addr_test
*test
)
729 return xmsg_ret_only_prog_load(test
, /*rc*/ 1);
732 static int recvmsg_deny_prog_load(const struct sock_addr_test
*test
)
734 return xmsg_ret_only_prog_load(test
, /*rc*/ 0);
737 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
)
739 struct sockaddr_in dst4_rw_addr
;
740 struct in_addr src4_rw_ip
;
742 if (inet_pton(AF_INET
, SRC4_REWRITE_IP
, (void *)&src4_rw_ip
) != 1) {
743 log_err("Invalid IPv4: %s", SRC4_REWRITE_IP
);
747 if (mk_sockaddr(AF_INET
, SERV4_REWRITE_IP
, SERV4_REWRITE_PORT
,
748 (struct sockaddr
*)&dst4_rw_addr
,
749 sizeof(dst4_rw_addr
)) == -1)
752 struct bpf_insn insns
[] = {
753 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
755 /* if (sk.family == AF_INET && */
756 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
757 offsetof(struct bpf_sock_addr
, family
)),
758 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 8),
760 /* sk.type == SOCK_DGRAM) { */
761 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
762 offsetof(struct bpf_sock_addr
, type
)),
763 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 6),
765 /* msg_src_ip4 = src4_rw_ip */
766 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_ip
.s_addr
),
767 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
768 offsetof(struct bpf_sock_addr
, msg_src_ip4
)),
770 /* user_ip4 = dst4_rw_addr.sin_addr */
771 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_addr
.s_addr
),
772 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
773 offsetof(struct bpf_sock_addr
, user_ip4
)),
775 /* user_port = dst4_rw_addr.sin_port */
776 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_port
),
777 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
778 offsetof(struct bpf_sock_addr
, user_port
)),
782 BPF_MOV64_IMM(BPF_REG_0
, 1),
786 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
789 static int recvmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
)
791 struct sockaddr_in src4_rw_addr
;
793 if (mk_sockaddr(AF_INET
, SERV4_IP
, SERV4_PORT
,
794 (struct sockaddr
*)&src4_rw_addr
,
795 sizeof(src4_rw_addr
)) == -1)
798 struct bpf_insn insns
[] = {
799 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
801 /* if (sk.family == AF_INET && */
802 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
803 offsetof(struct bpf_sock_addr
, family
)),
804 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 6),
806 /* sk.type == SOCK_DGRAM) { */
807 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
808 offsetof(struct bpf_sock_addr
, type
)),
809 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 4),
811 /* user_ip4 = src4_rw_addr.sin_addr */
812 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_addr
.sin_addr
.s_addr
),
813 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
814 offsetof(struct bpf_sock_addr
, user_ip4
)),
816 /* user_port = src4_rw_addr.sin_port */
817 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_addr
.sin_port
),
818 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
819 offsetof(struct bpf_sock_addr
, user_port
)),
823 BPF_MOV64_IMM(BPF_REG_0
, 1),
827 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
830 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
)
832 return load_path(test
, SENDMSG4_PROG_PATH
);
835 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test
*test
,
836 const char *rw_dst_ip
)
838 struct sockaddr_in6 dst6_rw_addr
;
839 struct in6_addr src6_rw_ip
;
841 if (inet_pton(AF_INET6
, SRC6_REWRITE_IP
, (void *)&src6_rw_ip
) != 1) {
842 log_err("Invalid IPv6: %s", SRC6_REWRITE_IP
);
846 if (mk_sockaddr(AF_INET6
, rw_dst_ip
, SERV6_REWRITE_PORT
,
847 (struct sockaddr
*)&dst6_rw_addr
,
848 sizeof(dst6_rw_addr
)) == -1)
851 struct bpf_insn insns
[] = {
852 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
854 /* if (sk.family == AF_INET6) { */
855 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
856 offsetof(struct bpf_sock_addr
, family
)),
857 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 18),
859 #define STORE_IPV6_WORD_N(DST, SRC, N) \
860 BPF_MOV32_IMM(BPF_REG_7, SRC[N]), \
861 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
862 offsetof(struct bpf_sock_addr, DST[N]))
864 #define STORE_IPV6(DST, SRC) \
865 STORE_IPV6_WORD_N(DST, SRC, 0), \
866 STORE_IPV6_WORD_N(DST, SRC, 1), \
867 STORE_IPV6_WORD_N(DST, SRC, 2), \
868 STORE_IPV6_WORD_N(DST, SRC, 3)
870 STORE_IPV6(msg_src_ip6
, src6_rw_ip
.s6_addr32
),
871 STORE_IPV6(user_ip6
, dst6_rw_addr
.sin6_addr
.s6_addr32
),
873 /* user_port = dst6_rw_addr.sin6_port */
874 BPF_MOV32_IMM(BPF_REG_7
, dst6_rw_addr
.sin6_port
),
875 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
876 offsetof(struct bpf_sock_addr
, user_port
)),
881 BPF_MOV64_IMM(BPF_REG_0
, 1),
885 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
888 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
)
890 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_REWRITE_IP
);
893 static int recvmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
)
895 struct sockaddr_in6 src6_rw_addr
;
897 if (mk_sockaddr(AF_INET6
, SERV6_IP
, SERV6_PORT
,
898 (struct sockaddr
*)&src6_rw_addr
,
899 sizeof(src6_rw_addr
)) == -1)
902 struct bpf_insn insns
[] = {
903 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
905 /* if (sk.family == AF_INET6) { */
906 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
907 offsetof(struct bpf_sock_addr
, family
)),
908 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 10),
910 STORE_IPV6(user_ip6
, src6_rw_addr
.sin6_addr
.s6_addr32
),
912 /* user_port = dst6_rw_addr.sin6_port */
913 BPF_MOV32_IMM(BPF_REG_7
, src6_rw_addr
.sin6_port
),
914 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
915 offsetof(struct bpf_sock_addr
, user_port
)),
919 BPF_MOV64_IMM(BPF_REG_0
, 1),
923 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
926 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
)
928 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_V4MAPPED_IP
);
931 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
)
933 return sendmsg6_rw_dst_asm_prog_load(test
, WILDCARD6_IP
);
936 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
)
938 return load_path(test
, SENDMSG6_PROG_PATH
);
941 static int cmp_addr(const struct sockaddr_storage
*addr1
,
942 const struct sockaddr_storage
*addr2
, int cmp_port
)
944 const struct sockaddr_in
*four1
, *four2
;
945 const struct sockaddr_in6
*six1
, *six2
;
947 if (addr1
->ss_family
!= addr2
->ss_family
)
950 if (addr1
->ss_family
== AF_INET
) {
951 four1
= (const struct sockaddr_in
*)addr1
;
952 four2
= (const struct sockaddr_in
*)addr2
;
953 return !((four1
->sin_port
== four2
->sin_port
|| !cmp_port
) &&
954 four1
->sin_addr
.s_addr
== four2
->sin_addr
.s_addr
);
955 } else if (addr1
->ss_family
== AF_INET6
) {
956 six1
= (const struct sockaddr_in6
*)addr1
;
957 six2
= (const struct sockaddr_in6
*)addr2
;
958 return !((six1
->sin6_port
== six2
->sin6_port
|| !cmp_port
) &&
959 !memcmp(&six1
->sin6_addr
, &six2
->sin6_addr
,
960 sizeof(struct in6_addr
)));
966 static int cmp_sock_addr(info_fn fn
, int sock1
,
967 const struct sockaddr_storage
*addr2
, int cmp_port
)
969 struct sockaddr_storage addr1
;
970 socklen_t len1
= sizeof(addr1
);
972 memset(&addr1
, 0, len1
);
973 if (fn(sock1
, (struct sockaddr
*)&addr1
, (socklen_t
*)&len1
) != 0)
976 return cmp_addr(&addr1
, addr2
, cmp_port
);
979 static int cmp_local_ip(int sock1
, const struct sockaddr_storage
*addr2
)
981 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 0);
984 static int cmp_local_addr(int sock1
, const struct sockaddr_storage
*addr2
)
986 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 1);
989 static int cmp_peer_addr(int sock1
, const struct sockaddr_storage
*addr2
)
991 return cmp_sock_addr(getpeername
, sock1
, addr2
, /*cmp_port*/ 1);
994 static int start_server(int type
, const struct sockaddr_storage
*addr
,
999 fd
= socket(addr
->ss_family
, type
, 0);
1001 log_err("Failed to create server socket");
1005 if (bind(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
1006 log_err("Failed to bind server socket");
1010 if (type
== SOCK_STREAM
) {
1011 if (listen(fd
, 128) == -1) {
1012 log_err("Failed to listen on server socket");
1025 static int connect_to_server(int type
, const struct sockaddr_storage
*addr
,
1031 domain
= addr
->ss_family
;
1033 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
1034 log_err("Unsupported address family");
1038 fd
= socket(domain
, type
, 0);
1040 log_err("Failed to create client socket");
1044 if (connect(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
1045 log_err("Fail to connect to server");
1057 int init_pktinfo(int domain
, struct cmsghdr
*cmsg
)
1059 struct in6_pktinfo
*pktinfo6
;
1060 struct in_pktinfo
*pktinfo4
;
1062 if (domain
== AF_INET
) {
1063 cmsg
->cmsg_level
= SOL_IP
;
1064 cmsg
->cmsg_type
= IP_PKTINFO
;
1065 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1066 pktinfo4
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
1067 memset(pktinfo4
, 0, sizeof(struct in_pktinfo
));
1068 if (inet_pton(domain
, SRC4_IP
,
1069 (void *)&pktinfo4
->ipi_spec_dst
) != 1)
1071 } else if (domain
== AF_INET6
) {
1072 cmsg
->cmsg_level
= SOL_IPV6
;
1073 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1074 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1075 pktinfo6
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
1076 memset(pktinfo6
, 0, sizeof(struct in6_pktinfo
));
1077 if (inet_pton(domain
, SRC6_IP
,
1078 (void *)&pktinfo6
->ipi6_addr
) != 1)
1087 static int sendmsg_to_server(int type
, const struct sockaddr_storage
*addr
,
1088 socklen_t addr_len
, int set_cmsg
, int flags
,
1092 char buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
1093 struct cmsghdr align
;
1096 char buf
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
1097 struct cmsghdr align
;
1105 domain
= addr
->ss_family
;
1107 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
1108 log_err("Unsupported address family");
1112 fd
= socket(domain
, type
, 0);
1114 log_err("Failed to create client socket");
1118 memset(&iov
, 0, sizeof(iov
));
1119 iov
.iov_base
= &data
;
1120 iov
.iov_len
= sizeof(data
);
1122 memset(&hdr
, 0, sizeof(hdr
));
1123 hdr
.msg_name
= (void *)addr
;
1124 hdr
.msg_namelen
= addr_len
;
1129 if (domain
== AF_INET
) {
1130 hdr
.msg_control
= &control4
;
1131 hdr
.msg_controllen
= sizeof(control4
.buf
);
1132 } else if (domain
== AF_INET6
) {
1133 hdr
.msg_control
= &control6
;
1134 hdr
.msg_controllen
= sizeof(control6
.buf
);
1136 if (init_pktinfo(domain
, CMSG_FIRSTHDR(&hdr
))) {
1137 log_err("Fail to init pktinfo");
1142 if (sendmsg(fd
, &hdr
, flags
) != sizeof(data
)) {
1143 log_err("Fail to send message to server");
1144 *syscall_err
= errno
;
1156 static int fastconnect_to_server(const struct sockaddr_storage
*addr
,
1161 return sendmsg_to_server(SOCK_STREAM
, addr
, addr_len
, /*set_cmsg*/0,
1162 MSG_FASTOPEN
, &sendmsg_err
);
1165 static int recvmsg_from_client(int sockfd
, struct sockaddr_storage
*src_addr
)
1174 FD_SET(sockfd
, &rfds
);
1179 if (select(sockfd
+ 1, &rfds
, NULL
, NULL
, &tv
) <= 0 ||
1180 !FD_ISSET(sockfd
, &rfds
))
1183 memset(&iov
, 0, sizeof(iov
));
1184 iov
.iov_base
= data
;
1185 iov
.iov_len
= sizeof(data
);
1187 memset(&hdr
, 0, sizeof(hdr
));
1188 hdr
.msg_name
= src_addr
;
1189 hdr
.msg_namelen
= sizeof(struct sockaddr_storage
);
1193 return recvmsg(sockfd
, &hdr
, 0);
1196 static int init_addrs(const struct sock_addr_test
*test
,
1197 struct sockaddr_storage
*requested_addr
,
1198 struct sockaddr_storage
*expected_addr
,
1199 struct sockaddr_storage
*expected_src_addr
)
1201 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1203 if (mk_sockaddr(test
->domain
, test
->expected_ip
, test
->expected_port
,
1204 (struct sockaddr
*)expected_addr
, addr_len
) == -1)
1207 if (mk_sockaddr(test
->domain
, test
->requested_ip
, test
->requested_port
,
1208 (struct sockaddr
*)requested_addr
, addr_len
) == -1)
1211 if (test
->expected_src_ip
&&
1212 mk_sockaddr(test
->domain
, test
->expected_src_ip
, 0,
1213 (struct sockaddr
*)expected_src_addr
, addr_len
) == -1)
1221 static int run_bind_test_case(const struct sock_addr_test
*test
)
1223 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1224 struct sockaddr_storage requested_addr
;
1225 struct sockaddr_storage expected_addr
;
1230 if (init_addrs(test
, &requested_addr
, &expected_addr
, NULL
))
1233 servfd
= start_server(test
->type
, &requested_addr
, addr_len
);
1237 if (cmp_local_addr(servfd
, &expected_addr
))
1240 /* Try to connect to server just in case */
1241 clientfd
= connect_to_server(test
->type
, &expected_addr
, addr_len
);
1254 static int run_connect_test_case(const struct sock_addr_test
*test
)
1256 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1257 struct sockaddr_storage expected_src_addr
;
1258 struct sockaddr_storage requested_addr
;
1259 struct sockaddr_storage expected_addr
;
1264 if (init_addrs(test
, &requested_addr
, &expected_addr
,
1265 &expected_src_addr
))
1268 /* Prepare server to connect to */
1269 servfd
= start_server(test
->type
, &expected_addr
, addr_len
);
1273 clientfd
= connect_to_server(test
->type
, &requested_addr
, addr_len
);
1277 /* Make sure src and dst addrs were overridden properly */
1278 if (cmp_peer_addr(clientfd
, &expected_addr
))
1281 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1284 if (test
->type
== SOCK_STREAM
) {
1285 /* Test TCP Fast Open scenario */
1286 clientfd
= fastconnect_to_server(&requested_addr
, addr_len
);
1290 /* Make sure src and dst addrs were overridden properly */
1291 if (cmp_peer_addr(clientfd
, &expected_addr
))
1294 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1307 static int run_xmsg_test_case(const struct sock_addr_test
*test
, int max_cmsg
)
1309 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1310 struct sockaddr_storage expected_addr
;
1311 struct sockaddr_storage server_addr
;
1312 struct sockaddr_storage sendmsg_addr
;
1313 struct sockaddr_storage recvmsg_addr
;
1319 if (test
->type
!= SOCK_DGRAM
)
1322 if (init_addrs(test
, &sendmsg_addr
, &server_addr
, &expected_addr
))
1325 /* Prepare server to sendmsg to */
1326 servfd
= start_server(test
->type
, &server_addr
, addr_len
);
1330 for (set_cmsg
= 0; set_cmsg
<= max_cmsg
; ++set_cmsg
) {
1334 clientfd
= sendmsg_to_server(test
->type
, &sendmsg_addr
,
1335 addr_len
, set_cmsg
, /*flags*/0,
1339 else if (clientfd
== -1)
1342 /* Try to receive message on server instead of using
1343 * getpeername(2) on client socket, to check that client's
1344 * destination address was rewritten properly, since
1345 * getpeername(2) doesn't work with unconnected datagram
1348 * Get source address from recvmsg(2) as well to make sure
1349 * source was rewritten properly: getsockname(2) can't be used
1350 * since socket is unconnected and source defined for one
1351 * specific packet may differ from the one used by default and
1352 * returned by getsockname(2).
1354 if (recvmsg_from_client(servfd
, &recvmsg_addr
) == -1)
1357 if (cmp_addr(&recvmsg_addr
, &expected_addr
, /*cmp_port*/0))
1370 static int run_test_case(int cgfd
, const struct sock_addr_test
*test
)
1375 printf("Test case: %s .. ", test
->descr
);
1377 progfd
= test
->loadfn(test
);
1378 if (test
->expected_result
== LOAD_REJECT
&& progfd
< 0)
1380 else if (test
->expected_result
== LOAD_REJECT
|| progfd
< 0)
1383 err
= bpf_prog_attach(progfd
, cgfd
, test
->attach_type
,
1384 BPF_F_ALLOW_OVERRIDE
);
1385 if (test
->expected_result
== ATTACH_REJECT
&& err
) {
1386 err
= 0; /* error was expected, reset it */
1388 } else if (test
->expected_result
== ATTACH_REJECT
|| err
) {
1390 } else if (test
->expected_result
== ATTACH_OKAY
) {
1395 switch (test
->attach_type
) {
1396 case BPF_CGROUP_INET4_BIND
:
1397 case BPF_CGROUP_INET6_BIND
:
1398 err
= run_bind_test_case(test
);
1400 case BPF_CGROUP_INET4_CONNECT
:
1401 case BPF_CGROUP_INET6_CONNECT
:
1402 err
= run_connect_test_case(test
);
1404 case BPF_CGROUP_UDP4_SENDMSG
:
1405 case BPF_CGROUP_UDP6_SENDMSG
:
1406 err
= run_xmsg_test_case(test
, 1);
1408 case BPF_CGROUP_UDP4_RECVMSG
:
1409 case BPF_CGROUP_UDP6_RECVMSG
:
1410 err
= run_xmsg_test_case(test
, 0);
1416 if (test
->expected_result
== SYSCALL_EPERM
&& err
== EPERM
) {
1417 err
= 0; /* error was expected, reset it */
1421 if (test
->expected_result
== SYSCALL_ENOTSUPP
&& err
== ENOTSUPP
) {
1422 err
= 0; /* error was expected, reset it */
1426 if (err
|| test
->expected_result
!= SUCCESS
)
1433 /* Detaching w/o checking return code: best effort attempt. */
1435 bpf_prog_detach(cgfd
, test
->attach_type
);
1437 printf("[%s]\n", err
? "FAIL" : "PASS");
1441 static int run_tests(int cgfd
)
1447 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
1448 if (run_test_case(cgfd
, &tests
[i
]))
1453 printf("Summary: %d PASSED, %d FAILED\n", passes
, fails
);
1454 return fails
? -1 : 0;
1457 int main(int argc
, char **argv
)
1464 "%s has to be run via %s.sh. Skip direct run.\n",
1469 cgfd
= cgroup_setup_and_join(CG_PATH
);
1473 if (run_tests(cgfd
))
1481 cleanup_cgroup_environment();