Skip to content

Commit 1d43688

Browse files
rdnaborkmann
authored andcommitted
selftests/bpf: Selftest for sys_bind post-hooks.
Add selftest for attach types `BPF_CGROUP_INET4_POST_BIND` and `BPF_CGROUP_INET6_POST_BIND`. The main things tested are: * prog load behaves as expected (valid/invalid accesses in prog); * prog attach behaves as expected (load- vs attach-time attach types); * `BPF_CGROUP_INET_SOCK_CREATE` can be attached in a backward compatible way; * post-hooks return expected result and errno. Example: # ./test_sock Test case: bind4 load with invalid access: src_ip6 .. [PASS] Test case: bind4 load with invalid access: mark .. [PASS] Test case: bind6 load with invalid access: src_ip4 .. [PASS] Test case: sock_create load with invalid access: src_port .. [PASS] Test case: sock_create load w/o expected_attach_type (compat mode) .. [PASS] Test case: sock_create load w/ expected_attach_type .. [PASS] Test case: attach type mismatch bind4 vs bind6 .. [PASS] Test case: attach type mismatch bind6 vs bind4 .. [PASS] Test case: attach type mismatch default vs bind4 .. [PASS] Test case: attach type mismatch bind6 vs sock_create .. [PASS] Test case: bind4 reject all .. [PASS] Test case: bind6 reject all .. [PASS] Test case: bind6 deny specific IP & port .. [PASS] Test case: bind4 allow specific IP & port .. [PASS] Test case: bind4 allow all .. [PASS] Test case: bind6 allow all .. [PASS] Summary: 16 PASSED, 0 FAILED Signed-off-by: Andrey Ignatov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent aac3fc3 commit 1d43688

File tree

3 files changed

+493
-1
lines changed

3 files changed

+493
-1
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ enum bpf_attach_type {
152152
BPF_CGROUP_INET6_BIND,
153153
BPF_CGROUP_INET4_CONNECT,
154154
BPF_CGROUP_INET6_CONNECT,
155+
BPF_CGROUP_INET4_POST_BIND,
156+
BPF_CGROUP_INET6_POST_BIND,
155157
__MAX_BPF_ATTACH_TYPE
156158
};
157159

@@ -947,6 +949,15 @@ struct bpf_sock {
947949
__u32 protocol;
948950
__u32 mark;
949951
__u32 priority;
952+
__u32 src_ip4; /* Allows 1,2,4-byte read.
953+
* Stored in network byte order.
954+
*/
955+
__u32 src_ip6[4]; /* Allows 1,2,4-byte read.
956+
* Stored in network byte order.
957+
*/
958+
__u32 src_port; /* Allows 4-byte read.
959+
* Stored in host byte order
960+
*/
950961
};
951962

952963
#define XDP_PACKET_HEADROOM 256

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ urandom_read: urandom_read.c
2323

2424
# Order correspond to 'make run_tests' order
2525
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
26-
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user test_sock_addr
26+
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
27+
test_sock test_sock_addr
2728

2829
TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
2930
test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \
@@ -52,6 +53,7 @@ $(TEST_GEN_PROGS): $(BPFOBJ)
5253
$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/libbpf.a
5354

5455
$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
56+
$(OUTPUT)/test_sock: cgroup_helpers.c
5557
$(OUTPUT)/test_sock_addr: cgroup_helpers.c
5658

5759
.PHONY: force

0 commit comments

Comments
 (0)