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 */
85 static int bind4_prog_load(const struct sock_addr_test
*test
);
86 static int bind6_prog_load(const struct sock_addr_test
*test
);
87 static int connect4_prog_load(const struct sock_addr_test
*test
);
88 static int connect6_prog_load(const struct sock_addr_test
*test
);
89 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
);
90 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
);
91 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
);
92 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
);
93 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
);
94 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
);
95 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
);
96 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
);
98 static struct sock_addr_test tests
[] = {
101 "bind4: load prog with wrong expected attach type",
103 BPF_CGROUP_INET6_BIND
,
104 BPF_CGROUP_INET4_BIND
,
115 "bind4: attach prog with wrong attach type",
117 BPF_CGROUP_INET4_BIND
,
118 BPF_CGROUP_INET6_BIND
,
129 "bind4: rewrite IP & TCP port in",
131 BPF_CGROUP_INET4_BIND
,
132 BPF_CGROUP_INET4_BIND
,
143 "bind4: rewrite IP & UDP port in",
145 BPF_CGROUP_INET4_BIND
,
146 BPF_CGROUP_INET4_BIND
,
157 "bind6: load prog with wrong expected attach type",
159 BPF_CGROUP_INET4_BIND
,
160 BPF_CGROUP_INET6_BIND
,
171 "bind6: attach prog with wrong attach type",
173 BPF_CGROUP_INET6_BIND
,
174 BPF_CGROUP_INET4_BIND
,
185 "bind6: rewrite IP & TCP port in",
187 BPF_CGROUP_INET6_BIND
,
188 BPF_CGROUP_INET6_BIND
,
199 "bind6: rewrite IP & UDP port in",
201 BPF_CGROUP_INET6_BIND
,
202 BPF_CGROUP_INET6_BIND
,
215 "connect4: load prog with wrong expected attach type",
217 BPF_CGROUP_INET6_CONNECT
,
218 BPF_CGROUP_INET4_CONNECT
,
229 "connect4: attach prog with wrong attach type",
231 BPF_CGROUP_INET4_CONNECT
,
232 BPF_CGROUP_INET6_CONNECT
,
243 "connect4: rewrite IP & TCP port",
245 BPF_CGROUP_INET4_CONNECT
,
246 BPF_CGROUP_INET4_CONNECT
,
257 "connect4: rewrite IP & UDP port",
259 BPF_CGROUP_INET4_CONNECT
,
260 BPF_CGROUP_INET4_CONNECT
,
271 "connect6: load prog with wrong expected attach type",
273 BPF_CGROUP_INET4_CONNECT
,
274 BPF_CGROUP_INET6_CONNECT
,
285 "connect6: attach prog with wrong attach type",
287 BPF_CGROUP_INET6_CONNECT
,
288 BPF_CGROUP_INET4_CONNECT
,
299 "connect6: rewrite IP & TCP port",
301 BPF_CGROUP_INET6_CONNECT
,
302 BPF_CGROUP_INET6_CONNECT
,
313 "connect6: rewrite IP & UDP port",
315 BPF_CGROUP_INET6_CONNECT
,
316 BPF_CGROUP_INET6_CONNECT
,
329 "sendmsg4: load prog with wrong expected attach type",
330 sendmsg4_rw_asm_prog_load
,
331 BPF_CGROUP_UDP6_SENDMSG
,
332 BPF_CGROUP_UDP4_SENDMSG
,
343 "sendmsg4: attach prog with wrong attach type",
344 sendmsg4_rw_asm_prog_load
,
345 BPF_CGROUP_UDP4_SENDMSG
,
346 BPF_CGROUP_UDP6_SENDMSG
,
357 "sendmsg4: rewrite IP & port (asm)",
358 sendmsg4_rw_asm_prog_load
,
359 BPF_CGROUP_UDP4_SENDMSG
,
360 BPF_CGROUP_UDP4_SENDMSG
,
371 "sendmsg4: rewrite IP & port (C)",
372 sendmsg4_rw_c_prog_load
,
373 BPF_CGROUP_UDP4_SENDMSG
,
374 BPF_CGROUP_UDP4_SENDMSG
,
385 "sendmsg4: deny call",
386 sendmsg_deny_prog_load
,
387 BPF_CGROUP_UDP4_SENDMSG
,
388 BPF_CGROUP_UDP4_SENDMSG
,
399 "sendmsg6: load prog with wrong expected attach type",
400 sendmsg6_rw_asm_prog_load
,
401 BPF_CGROUP_UDP4_SENDMSG
,
402 BPF_CGROUP_UDP6_SENDMSG
,
413 "sendmsg6: attach prog with wrong attach type",
414 sendmsg6_rw_asm_prog_load
,
415 BPF_CGROUP_UDP6_SENDMSG
,
416 BPF_CGROUP_UDP4_SENDMSG
,
427 "sendmsg6: rewrite IP & port (asm)",
428 sendmsg6_rw_asm_prog_load
,
429 BPF_CGROUP_UDP6_SENDMSG
,
430 BPF_CGROUP_UDP6_SENDMSG
,
441 "sendmsg6: rewrite IP & port (C)",
442 sendmsg6_rw_c_prog_load
,
443 BPF_CGROUP_UDP6_SENDMSG
,
444 BPF_CGROUP_UDP6_SENDMSG
,
455 "sendmsg6: IPv4-mapped IPv6",
456 sendmsg6_rw_v4mapped_prog_load
,
457 BPF_CGROUP_UDP6_SENDMSG
,
458 BPF_CGROUP_UDP6_SENDMSG
,
469 "sendmsg6: set dst IP = [::] (BSD'ism)",
470 sendmsg6_rw_wildcard_prog_load
,
471 BPF_CGROUP_UDP6_SENDMSG
,
472 BPF_CGROUP_UDP6_SENDMSG
,
483 "sendmsg6: preserve dst IP = [::] (BSD'ism)",
484 sendmsg_allow_prog_load
,
485 BPF_CGROUP_UDP6_SENDMSG
,
486 BPF_CGROUP_UDP6_SENDMSG
,
497 "sendmsg6: deny call",
498 sendmsg_deny_prog_load
,
499 BPF_CGROUP_UDP6_SENDMSG
,
500 BPF_CGROUP_UDP6_SENDMSG
,
512 static int mk_sockaddr(int domain
, const char *ip
, unsigned short port
,
513 struct sockaddr
*addr
, socklen_t addr_len
)
515 struct sockaddr_in6
*addr6
;
516 struct sockaddr_in
*addr4
;
518 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
519 log_err("Unsupported address family");
523 memset(addr
, 0, addr_len
);
525 if (domain
== AF_INET
) {
526 if (addr_len
< sizeof(struct sockaddr_in
))
528 addr4
= (struct sockaddr_in
*)addr
;
529 addr4
->sin_family
= domain
;
530 addr4
->sin_port
= htons(port
);
531 if (inet_pton(domain
, ip
, (void *)&addr4
->sin_addr
) != 1) {
532 log_err("Invalid IPv4: %s", ip
);
535 } else if (domain
== AF_INET6
) {
536 if (addr_len
< sizeof(struct sockaddr_in6
))
538 addr6
= (struct sockaddr_in6
*)addr
;
539 addr6
->sin6_family
= domain
;
540 addr6
->sin6_port
= htons(port
);
541 if (inet_pton(domain
, ip
, (void *)&addr6
->sin6_addr
) != 1) {
542 log_err("Invalid IPv6: %s", ip
);
550 static int load_insns(const struct sock_addr_test
*test
,
551 const struct bpf_insn
*insns
, size_t insns_cnt
)
553 struct bpf_load_program_attr load_attr
;
556 memset(&load_attr
, 0, sizeof(struct bpf_load_program_attr
));
557 load_attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
558 load_attr
.expected_attach_type
= test
->expected_attach_type
;
559 load_attr
.insns
= insns
;
560 load_attr
.insns_cnt
= insns_cnt
;
561 load_attr
.license
= "GPL";
563 ret
= bpf_load_program_xattr(&load_attr
, bpf_log_buf
, BPF_LOG_BUF_SIZE
);
564 if (ret
< 0 && test
->expected_result
!= LOAD_REJECT
) {
565 log_err(">>> Loading program error.\n"
566 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf
);
572 /* [1] These testing programs try to read different context fields, including
573 * narrow loads of different sizes from user_ip4 and user_ip6, and write to
574 * those allowed to be overridden.
576 * [2] BPF_LD_IMM64 & BPF_JMP_REG are used below whenever there is a need to
577 * compare a register with unsigned 32bit integer. BPF_JMP_IMM can't be used
578 * in such cases since it accepts only _signed_ 32bit integer as IMM
579 * argument. Also note that BPF_LD_IMM64 contains 2 instructions what matters
580 * to count jumps properly.
583 static int bind4_prog_load(const struct sock_addr_test
*test
)
587 uint16_t u4_addr16
[2];
590 struct sockaddr_in addr4_rw
;
592 if (inet_pton(AF_INET
, SERV4_IP
, (void *)&ip4
) != 1) {
593 log_err("Invalid IPv4: %s", SERV4_IP
);
597 if (mk_sockaddr(AF_INET
, SERV4_REWRITE_IP
, SERV4_REWRITE_PORT
,
598 (struct sockaddr
*)&addr4_rw
, sizeof(addr4_rw
)) == -1)
602 struct bpf_insn insns
[] = {
603 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
605 /* if (sk.family == AF_INET && */
606 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
607 offsetof(struct bpf_sock_addr
, family
)),
608 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 16),
610 /* (sk.type == SOCK_DGRAM || sk.type == SOCK_STREAM) && */
611 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
612 offsetof(struct bpf_sock_addr
, type
)),
613 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 1),
615 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_STREAM
, 12),
617 /* 1st_byte_of_user_ip4 == expected && */
618 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
619 offsetof(struct bpf_sock_addr
, user_ip4
)),
620 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr8
[0], 10),
622 /* 1st_half_of_user_ip4 == expected && */
623 BPF_LDX_MEM(BPF_H
, BPF_REG_7
, BPF_REG_6
,
624 offsetof(struct bpf_sock_addr
, user_ip4
)),
625 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip4
.u4_addr16
[0], 8),
627 /* whole_user_ip4 == expected) { */
628 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
629 offsetof(struct bpf_sock_addr
, user_ip4
)),
630 BPF_LD_IMM64(BPF_REG_8
, ip4
.u4_addr32
), /* See [2]. */
631 BPF_JMP_REG(BPF_JNE
, BPF_REG_7
, BPF_REG_8
, 4),
633 /* user_ip4 = addr4_rw.sin_addr */
634 BPF_MOV32_IMM(BPF_REG_7
, addr4_rw
.sin_addr
.s_addr
),
635 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
636 offsetof(struct bpf_sock_addr
, user_ip4
)),
638 /* user_port = addr4_rw.sin_port */
639 BPF_MOV32_IMM(BPF_REG_7
, addr4_rw
.sin_port
),
640 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
641 offsetof(struct bpf_sock_addr
, user_port
)),
645 BPF_MOV64_IMM(BPF_REG_0
, 1),
649 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
652 static int bind6_prog_load(const struct sock_addr_test
*test
)
654 struct sockaddr_in6 addr6_rw
;
657 if (inet_pton(AF_INET6
, SERV6_IP
, (void *)&ip6
) != 1) {
658 log_err("Invalid IPv6: %s", SERV6_IP
);
662 if (mk_sockaddr(AF_INET6
, SERV6_REWRITE_IP
, SERV6_REWRITE_PORT
,
663 (struct sockaddr
*)&addr6_rw
, sizeof(addr6_rw
)) == -1)
667 struct bpf_insn insns
[] = {
668 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
670 /* if (sk.family == AF_INET6 && */
671 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
672 offsetof(struct bpf_sock_addr
, family
)),
673 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 18),
675 /* 5th_byte_of_user_ip6 == expected && */
676 BPF_LDX_MEM(BPF_B
, BPF_REG_7
, BPF_REG_6
,
677 offsetof(struct bpf_sock_addr
, user_ip6
[1])),
678 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip6
.s6_addr
[4], 16),
680 /* 3rd_half_of_user_ip6 == expected && */
681 BPF_LDX_MEM(BPF_H
, BPF_REG_7
, BPF_REG_6
,
682 offsetof(struct bpf_sock_addr
, user_ip6
[1])),
683 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, ip6
.s6_addr16
[2], 14),
685 /* last_word_of_user_ip6 == expected) { */
686 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
687 offsetof(struct bpf_sock_addr
, user_ip6
[3])),
688 BPF_LD_IMM64(BPF_REG_8
, ip6
.s6_addr32
[3]), /* See [2]. */
689 BPF_JMP_REG(BPF_JNE
, BPF_REG_7
, BPF_REG_8
, 10),
692 #define STORE_IPV6_WORD(N) \
693 BPF_MOV32_IMM(BPF_REG_7, addr6_rw.sin6_addr.s6_addr32[N]), \
694 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
695 offsetof(struct bpf_sock_addr, user_ip6[N]))
697 /* user_ip6 = addr6_rw.sin6_addr */
703 /* user_port = addr6_rw.sin6_port */
704 BPF_MOV32_IMM(BPF_REG_7
, addr6_rw
.sin6_port
),
705 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
706 offsetof(struct bpf_sock_addr
, user_port
)),
711 BPF_MOV64_IMM(BPF_REG_0
, 1),
715 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
718 static int load_path(const struct sock_addr_test
*test
, const char *path
)
720 struct bpf_prog_load_attr attr
;
721 struct bpf_object
*obj
;
724 memset(&attr
, 0, sizeof(struct bpf_prog_load_attr
));
726 attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
727 attr
.expected_attach_type
= test
->expected_attach_type
;
729 if (bpf_prog_load_xattr(&attr
, &obj
, &prog_fd
)) {
730 if (test
->expected_result
!= LOAD_REJECT
)
731 log_err(">>> Loading program (%s) error.\n", path
);
738 static int connect4_prog_load(const struct sock_addr_test
*test
)
740 return load_path(test
, CONNECT4_PROG_PATH
);
743 static int connect6_prog_load(const struct sock_addr_test
*test
)
745 return load_path(test
, CONNECT6_PROG_PATH
);
748 static int sendmsg_ret_only_prog_load(const struct sock_addr_test
*test
,
751 struct bpf_insn insns
[] = {
753 BPF_MOV64_IMM(BPF_REG_0
, rc
),
756 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
759 static int sendmsg_allow_prog_load(const struct sock_addr_test
*test
)
761 return sendmsg_ret_only_prog_load(test
, /*rc*/ 1);
764 static int sendmsg_deny_prog_load(const struct sock_addr_test
*test
)
766 return sendmsg_ret_only_prog_load(test
, /*rc*/ 0);
769 static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test
*test
)
771 struct sockaddr_in dst4_rw_addr
;
772 struct in_addr src4_rw_ip
;
774 if (inet_pton(AF_INET
, SRC4_REWRITE_IP
, (void *)&src4_rw_ip
) != 1) {
775 log_err("Invalid IPv4: %s", SRC4_REWRITE_IP
);
779 if (mk_sockaddr(AF_INET
, SERV4_REWRITE_IP
, SERV4_REWRITE_PORT
,
780 (struct sockaddr
*)&dst4_rw_addr
,
781 sizeof(dst4_rw_addr
)) == -1)
784 struct bpf_insn insns
[] = {
785 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
787 /* if (sk.family == AF_INET && */
788 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
789 offsetof(struct bpf_sock_addr
, family
)),
790 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET
, 8),
792 /* sk.type == SOCK_DGRAM) { */
793 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
794 offsetof(struct bpf_sock_addr
, type
)),
795 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, SOCK_DGRAM
, 6),
797 /* msg_src_ip4 = src4_rw_ip */
798 BPF_MOV32_IMM(BPF_REG_7
, src4_rw_ip
.s_addr
),
799 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
800 offsetof(struct bpf_sock_addr
, msg_src_ip4
)),
802 /* user_ip4 = dst4_rw_addr.sin_addr */
803 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_addr
.s_addr
),
804 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
805 offsetof(struct bpf_sock_addr
, user_ip4
)),
807 /* user_port = dst4_rw_addr.sin_port */
808 BPF_MOV32_IMM(BPF_REG_7
, dst4_rw_addr
.sin_port
),
809 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
810 offsetof(struct bpf_sock_addr
, user_port
)),
814 BPF_MOV64_IMM(BPF_REG_0
, 1),
818 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
821 static int sendmsg4_rw_c_prog_load(const struct sock_addr_test
*test
)
823 return load_path(test
, SENDMSG4_PROG_PATH
);
826 static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test
*test
,
827 const char *rw_dst_ip
)
829 struct sockaddr_in6 dst6_rw_addr
;
830 struct in6_addr src6_rw_ip
;
832 if (inet_pton(AF_INET6
, SRC6_REWRITE_IP
, (void *)&src6_rw_ip
) != 1) {
833 log_err("Invalid IPv6: %s", SRC6_REWRITE_IP
);
837 if (mk_sockaddr(AF_INET6
, rw_dst_ip
, SERV6_REWRITE_PORT
,
838 (struct sockaddr
*)&dst6_rw_addr
,
839 sizeof(dst6_rw_addr
)) == -1)
842 struct bpf_insn insns
[] = {
843 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
845 /* if (sk.family == AF_INET6) { */
846 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
,
847 offsetof(struct bpf_sock_addr
, family
)),
848 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, AF_INET6
, 18),
850 #define STORE_IPV6_WORD_N(DST, SRC, N) \
851 BPF_MOV32_IMM(BPF_REG_7, SRC[N]), \
852 BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_7, \
853 offsetof(struct bpf_sock_addr, DST[N]))
855 #define STORE_IPV6(DST, SRC) \
856 STORE_IPV6_WORD_N(DST, SRC, 0), \
857 STORE_IPV6_WORD_N(DST, SRC, 1), \
858 STORE_IPV6_WORD_N(DST, SRC, 2), \
859 STORE_IPV6_WORD_N(DST, SRC, 3)
861 STORE_IPV6(msg_src_ip6
, src6_rw_ip
.s6_addr32
),
862 STORE_IPV6(user_ip6
, dst6_rw_addr
.sin6_addr
.s6_addr32
),
864 /* user_port = dst6_rw_addr.sin6_port */
865 BPF_MOV32_IMM(BPF_REG_7
, dst6_rw_addr
.sin6_port
),
866 BPF_STX_MEM(BPF_W
, BPF_REG_6
, BPF_REG_7
,
867 offsetof(struct bpf_sock_addr
, user_port
)),
872 BPF_MOV64_IMM(BPF_REG_0
, 1),
876 return load_insns(test
, insns
, sizeof(insns
) / sizeof(struct bpf_insn
));
879 static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test
*test
)
881 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_REWRITE_IP
);
884 static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test
*test
)
886 return sendmsg6_rw_dst_asm_prog_load(test
, SERV6_V4MAPPED_IP
);
889 static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test
*test
)
891 return sendmsg6_rw_dst_asm_prog_load(test
, WILDCARD6_IP
);
894 static int sendmsg6_rw_c_prog_load(const struct sock_addr_test
*test
)
896 return load_path(test
, SENDMSG6_PROG_PATH
);
899 static int cmp_addr(const struct sockaddr_storage
*addr1
,
900 const struct sockaddr_storage
*addr2
, int cmp_port
)
902 const struct sockaddr_in
*four1
, *four2
;
903 const struct sockaddr_in6
*six1
, *six2
;
905 if (addr1
->ss_family
!= addr2
->ss_family
)
908 if (addr1
->ss_family
== AF_INET
) {
909 four1
= (const struct sockaddr_in
*)addr1
;
910 four2
= (const struct sockaddr_in
*)addr2
;
911 return !((four1
->sin_port
== four2
->sin_port
|| !cmp_port
) &&
912 four1
->sin_addr
.s_addr
== four2
->sin_addr
.s_addr
);
913 } else if (addr1
->ss_family
== AF_INET6
) {
914 six1
= (const struct sockaddr_in6
*)addr1
;
915 six2
= (const struct sockaddr_in6
*)addr2
;
916 return !((six1
->sin6_port
== six2
->sin6_port
|| !cmp_port
) &&
917 !memcmp(&six1
->sin6_addr
, &six2
->sin6_addr
,
918 sizeof(struct in6_addr
)));
924 static int cmp_sock_addr(info_fn fn
, int sock1
,
925 const struct sockaddr_storage
*addr2
, int cmp_port
)
927 struct sockaddr_storage addr1
;
928 socklen_t len1
= sizeof(addr1
);
930 memset(&addr1
, 0, len1
);
931 if (fn(sock1
, (struct sockaddr
*)&addr1
, (socklen_t
*)&len1
) != 0)
934 return cmp_addr(&addr1
, addr2
, cmp_port
);
937 static int cmp_local_ip(int sock1
, const struct sockaddr_storage
*addr2
)
939 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 0);
942 static int cmp_local_addr(int sock1
, const struct sockaddr_storage
*addr2
)
944 return cmp_sock_addr(getsockname
, sock1
, addr2
, /*cmp_port*/ 1);
947 static int cmp_peer_addr(int sock1
, const struct sockaddr_storage
*addr2
)
949 return cmp_sock_addr(getpeername
, sock1
, addr2
, /*cmp_port*/ 1);
952 static int start_server(int type
, const struct sockaddr_storage
*addr
,
957 fd
= socket(addr
->ss_family
, type
, 0);
959 log_err("Failed to create server socket");
963 if (bind(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
964 log_err("Failed to bind server socket");
968 if (type
== SOCK_STREAM
) {
969 if (listen(fd
, 128) == -1) {
970 log_err("Failed to listen on server socket");
983 static int connect_to_server(int type
, const struct sockaddr_storage
*addr
,
989 domain
= addr
->ss_family
;
991 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
992 log_err("Unsupported address family");
996 fd
= socket(domain
, type
, 0);
998 log_err("Failed to create client socket");
1002 if (connect(fd
, (const struct sockaddr
*)addr
, addr_len
) == -1) {
1003 log_err("Fail to connect to server");
1015 int init_pktinfo(int domain
, struct cmsghdr
*cmsg
)
1017 struct in6_pktinfo
*pktinfo6
;
1018 struct in_pktinfo
*pktinfo4
;
1020 if (domain
== AF_INET
) {
1021 cmsg
->cmsg_level
= SOL_IP
;
1022 cmsg
->cmsg_type
= IP_PKTINFO
;
1023 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1024 pktinfo4
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
1025 memset(pktinfo4
, 0, sizeof(struct in_pktinfo
));
1026 if (inet_pton(domain
, SRC4_IP
,
1027 (void *)&pktinfo4
->ipi_spec_dst
) != 1)
1029 } else if (domain
== AF_INET6
) {
1030 cmsg
->cmsg_level
= SOL_IPV6
;
1031 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1032 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1033 pktinfo6
= (struct in6_pktinfo
*)CMSG_DATA(cmsg
);
1034 memset(pktinfo6
, 0, sizeof(struct in6_pktinfo
));
1035 if (inet_pton(domain
, SRC6_IP
,
1036 (void *)&pktinfo6
->ipi6_addr
) != 1)
1045 static int sendmsg_to_server(int type
, const struct sockaddr_storage
*addr
,
1046 socklen_t addr_len
, int set_cmsg
, int flags
,
1050 char buf
[CMSG_SPACE(sizeof(struct in6_pktinfo
))];
1051 struct cmsghdr align
;
1054 char buf
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
1055 struct cmsghdr align
;
1063 domain
= addr
->ss_family
;
1065 if (domain
!= AF_INET
&& domain
!= AF_INET6
) {
1066 log_err("Unsupported address family");
1070 fd
= socket(domain
, type
, 0);
1072 log_err("Failed to create client socket");
1076 memset(&iov
, 0, sizeof(iov
));
1077 iov
.iov_base
= &data
;
1078 iov
.iov_len
= sizeof(data
);
1080 memset(&hdr
, 0, sizeof(hdr
));
1081 hdr
.msg_name
= (void *)addr
;
1082 hdr
.msg_namelen
= addr_len
;
1087 if (domain
== AF_INET
) {
1088 hdr
.msg_control
= &control4
;
1089 hdr
.msg_controllen
= sizeof(control4
.buf
);
1090 } else if (domain
== AF_INET6
) {
1091 hdr
.msg_control
= &control6
;
1092 hdr
.msg_controllen
= sizeof(control6
.buf
);
1094 if (init_pktinfo(domain
, CMSG_FIRSTHDR(&hdr
))) {
1095 log_err("Fail to init pktinfo");
1100 if (sendmsg(fd
, &hdr
, flags
) != sizeof(data
)) {
1101 log_err("Fail to send message to server");
1102 *syscall_err
= errno
;
1114 static int fastconnect_to_server(const struct sockaddr_storage
*addr
,
1119 return sendmsg_to_server(SOCK_STREAM
, addr
, addr_len
, /*set_cmsg*/0,
1120 MSG_FASTOPEN
, &sendmsg_err
);
1123 static int recvmsg_from_client(int sockfd
, struct sockaddr_storage
*src_addr
)
1132 FD_SET(sockfd
, &rfds
);
1137 if (select(sockfd
+ 1, &rfds
, NULL
, NULL
, &tv
) <= 0 ||
1138 !FD_ISSET(sockfd
, &rfds
))
1141 memset(&iov
, 0, sizeof(iov
));
1142 iov
.iov_base
= data
;
1143 iov
.iov_len
= sizeof(data
);
1145 memset(&hdr
, 0, sizeof(hdr
));
1146 hdr
.msg_name
= src_addr
;
1147 hdr
.msg_namelen
= sizeof(struct sockaddr_storage
);
1151 return recvmsg(sockfd
, &hdr
, 0);
1154 static int init_addrs(const struct sock_addr_test
*test
,
1155 struct sockaddr_storage
*requested_addr
,
1156 struct sockaddr_storage
*expected_addr
,
1157 struct sockaddr_storage
*expected_src_addr
)
1159 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1161 if (mk_sockaddr(test
->domain
, test
->expected_ip
, test
->expected_port
,
1162 (struct sockaddr
*)expected_addr
, addr_len
) == -1)
1165 if (mk_sockaddr(test
->domain
, test
->requested_ip
, test
->requested_port
,
1166 (struct sockaddr
*)requested_addr
, addr_len
) == -1)
1169 if (test
->expected_src_ip
&&
1170 mk_sockaddr(test
->domain
, test
->expected_src_ip
, 0,
1171 (struct sockaddr
*)expected_src_addr
, addr_len
) == -1)
1179 static int run_bind_test_case(const struct sock_addr_test
*test
)
1181 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1182 struct sockaddr_storage requested_addr
;
1183 struct sockaddr_storage expected_addr
;
1188 if (init_addrs(test
, &requested_addr
, &expected_addr
, NULL
))
1191 servfd
= start_server(test
->type
, &requested_addr
, addr_len
);
1195 if (cmp_local_addr(servfd
, &expected_addr
))
1198 /* Try to connect to server just in case */
1199 clientfd
= connect_to_server(test
->type
, &expected_addr
, addr_len
);
1212 static int run_connect_test_case(const struct sock_addr_test
*test
)
1214 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1215 struct sockaddr_storage expected_src_addr
;
1216 struct sockaddr_storage requested_addr
;
1217 struct sockaddr_storage expected_addr
;
1222 if (init_addrs(test
, &requested_addr
, &expected_addr
,
1223 &expected_src_addr
))
1226 /* Prepare server to connect to */
1227 servfd
= start_server(test
->type
, &expected_addr
, addr_len
);
1231 clientfd
= connect_to_server(test
->type
, &requested_addr
, addr_len
);
1235 /* Make sure src and dst addrs were overridden properly */
1236 if (cmp_peer_addr(clientfd
, &expected_addr
))
1239 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1242 if (test
->type
== SOCK_STREAM
) {
1243 /* Test TCP Fast Open scenario */
1244 clientfd
= fastconnect_to_server(&requested_addr
, addr_len
);
1248 /* Make sure src and dst addrs were overridden properly */
1249 if (cmp_peer_addr(clientfd
, &expected_addr
))
1252 if (cmp_local_ip(clientfd
, &expected_src_addr
))
1265 static int run_sendmsg_test_case(const struct sock_addr_test
*test
)
1267 socklen_t addr_len
= sizeof(struct sockaddr_storage
);
1268 struct sockaddr_storage expected_src_addr
;
1269 struct sockaddr_storage requested_addr
;
1270 struct sockaddr_storage expected_addr
;
1271 struct sockaddr_storage real_src_addr
;
1277 if (test
->type
!= SOCK_DGRAM
)
1280 if (init_addrs(test
, &requested_addr
, &expected_addr
,
1281 &expected_src_addr
))
1284 /* Prepare server to sendmsg to */
1285 servfd
= start_server(test
->type
, &expected_addr
, addr_len
);
1289 for (set_cmsg
= 0; set_cmsg
<= 1; ++set_cmsg
) {
1293 clientfd
= sendmsg_to_server(test
->type
, &requested_addr
,
1294 addr_len
, set_cmsg
, /*flags*/0,
1298 else if (clientfd
== -1)
1301 /* Try to receive message on server instead of using
1302 * getpeername(2) on client socket, to check that client's
1303 * destination address was rewritten properly, since
1304 * getpeername(2) doesn't work with unconnected datagram
1307 * Get source address from recvmsg(2) as well to make sure
1308 * source was rewritten properly: getsockname(2) can't be used
1309 * since socket is unconnected and source defined for one
1310 * specific packet may differ from the one used by default and
1311 * returned by getsockname(2).
1313 if (recvmsg_from_client(servfd
, &real_src_addr
) == -1)
1316 if (cmp_addr(&real_src_addr
, &expected_src_addr
, /*cmp_port*/0))
1329 static int run_test_case(int cgfd
, const struct sock_addr_test
*test
)
1334 printf("Test case: %s .. ", test
->descr
);
1336 progfd
= test
->loadfn(test
);
1337 if (test
->expected_result
== LOAD_REJECT
&& progfd
< 0)
1339 else if (test
->expected_result
== LOAD_REJECT
|| progfd
< 0)
1342 err
= bpf_prog_attach(progfd
, cgfd
, test
->attach_type
,
1343 BPF_F_ALLOW_OVERRIDE
);
1344 if (test
->expected_result
== ATTACH_REJECT
&& err
) {
1345 err
= 0; /* error was expected, reset it */
1347 } else if (test
->expected_result
== ATTACH_REJECT
|| err
) {
1351 switch (test
->attach_type
) {
1352 case BPF_CGROUP_INET4_BIND
:
1353 case BPF_CGROUP_INET6_BIND
:
1354 err
= run_bind_test_case(test
);
1356 case BPF_CGROUP_INET4_CONNECT
:
1357 case BPF_CGROUP_INET6_CONNECT
:
1358 err
= run_connect_test_case(test
);
1360 case BPF_CGROUP_UDP4_SENDMSG
:
1361 case BPF_CGROUP_UDP6_SENDMSG
:
1362 err
= run_sendmsg_test_case(test
);
1368 if (test
->expected_result
== SYSCALL_EPERM
&& err
== EPERM
) {
1369 err
= 0; /* error was expected, reset it */
1373 if (test
->expected_result
== SYSCALL_ENOTSUPP
&& err
== ENOTSUPP
) {
1374 err
= 0; /* error was expected, reset it */
1378 if (err
|| test
->expected_result
!= SUCCESS
)
1385 /* Detaching w/o checking return code: best effort attempt. */
1387 bpf_prog_detach(cgfd
, test
->attach_type
);
1389 printf("[%s]\n", err
? "FAIL" : "PASS");
1393 static int run_tests(int cgfd
)
1399 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
1400 if (run_test_case(cgfd
, &tests
[i
]))
1405 printf("Summary: %d PASSED, %d FAILED\n", passes
, fails
);
1406 return fails
? -1 : 0;
1409 int main(int argc
, char **argv
)
1416 "%s has to be run via %s.sh. Skip direct run.\n",
1421 if (setup_cgroup_environment())
1424 cgfd
= create_and_get_cgroup(CG_PATH
);
1428 if (join_cgroup(CG_PATH
))
1431 if (run_tests(cgfd
))
1439 cleanup_cgroup_environment();