diff options
| author | Andrey Ignatov <rdna@fb.com> | 2018-03-30 18:08:03 -0400 |
|---|---|---|
| committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-03-30 20:15:30 -0400 |
| commit | e50b0a6f089308bec6b2d0198abed231dee4d277 (patch) | |
| tree | 66724709a340804aa54832903b69650130ff2983 /tools/include/uapi/linux | |
| parent | 4fbac77d2d092b475dda9eea66da674369665427 (diff) | |
selftests/bpf: Selftest for sys_bind hooks
Add selftest to work with bpf_sock_addr context from
`BPF_PROG_TYPE_CGROUP_SOCK_ADDR` programs.
Try to bind(2) on IP:port and apply:
* loads to make sure context can be read correctly, including narrow
loads (byte, half) for IP and full-size loads (word) for all fields;
* stores to those fields allowed by verifier.
All combination from IPv4/IPv6 and TCP/UDP are tested.
Both scenarios are tested:
* valid programs can be loaded and attached;
* invalid programs can be neither loaded nor attached.
Test passes when expected data can be read from context in the
BPF-program, and after the call to bind(2) socket is bound to IP:port
pair that was written by BPF-program to the context.
Example:
# ./test_sock_addr
Attached bind4 program.
Test case #1 (IPv4/TCP):
Requested: bind(192.168.1.254, 4040) ..
Actual: bind(127.0.0.1, 4444)
Test case #2 (IPv4/UDP):
Requested: bind(192.168.1.254, 4040) ..
Actual: bind(127.0.0.1, 4444)
Attached bind6 program.
Test case #3 (IPv6/TCP):
Requested: bind(face:b00c:1234:5678::abcd, 6060) ..
Actual: bind(::1, 6666)
Test case #4 (IPv6/UDP):
Requested: bind(face:b00c:1234:5678::abcd, 6060) ..
Actual: bind(::1, 6666)
### SUCCESS
Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/include/uapi/linux')
| -rw-r--r-- | tools/include/uapi/linux/bpf.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index e1c1fed63396..f2120c5c0578 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h | |||
| @@ -136,6 +136,7 @@ enum bpf_prog_type { | |||
| 136 | BPF_PROG_TYPE_CGROUP_DEVICE, | 136 | BPF_PROG_TYPE_CGROUP_DEVICE, |
| 137 | BPF_PROG_TYPE_SK_MSG, | 137 | BPF_PROG_TYPE_SK_MSG, |
| 138 | BPF_PROG_TYPE_RAW_TRACEPOINT, | 138 | BPF_PROG_TYPE_RAW_TRACEPOINT, |
| 139 | BPF_PROG_TYPE_CGROUP_SOCK_ADDR, | ||
| 139 | }; | 140 | }; |
| 140 | 141 | ||
| 141 | enum bpf_attach_type { | 142 | enum bpf_attach_type { |
| @@ -147,6 +148,8 @@ enum bpf_attach_type { | |||
| 147 | BPF_SK_SKB_STREAM_VERDICT, | 148 | BPF_SK_SKB_STREAM_VERDICT, |
| 148 | BPF_CGROUP_DEVICE, | 149 | BPF_CGROUP_DEVICE, |
| 149 | BPF_SK_MSG_VERDICT, | 150 | BPF_SK_MSG_VERDICT, |
| 151 | BPF_CGROUP_INET4_BIND, | ||
| 152 | BPF_CGROUP_INET6_BIND, | ||
| 150 | __MAX_BPF_ATTACH_TYPE | 153 | __MAX_BPF_ATTACH_TYPE |
| 151 | }; | 154 | }; |
| 152 | 155 | ||
| @@ -1009,6 +1012,26 @@ struct bpf_map_info { | |||
| 1009 | __u64 netns_ino; | 1012 | __u64 netns_ino; |
| 1010 | } __attribute__((aligned(8))); | 1013 | } __attribute__((aligned(8))); |
| 1011 | 1014 | ||
| 1015 | /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed | ||
| 1016 | * by user and intended to be used by socket (e.g. to bind to, depends on | ||
| 1017 | * attach attach type). | ||
| 1018 | */ | ||
| 1019 | struct bpf_sock_addr { | ||
| 1020 | __u32 user_family; /* Allows 4-byte read, but no write. */ | ||
| 1021 | __u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write. | ||
| 1022 | * Stored in network byte order. | ||
| 1023 | */ | ||
| 1024 | __u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write. | ||
| 1025 | * Stored in network byte order. | ||
| 1026 | */ | ||
| 1027 | __u32 user_port; /* Allows 4-byte read and write. | ||
| 1028 | * Stored in network byte order | ||
| 1029 | */ | ||
| 1030 | __u32 family; /* Allows 4-byte read, but no write */ | ||
| 1031 | __u32 type; /* Allows 4-byte read, but no write */ | ||
| 1032 | __u32 protocol; /* Allows 4-byte read, but no write */ | ||
| 1033 | }; | ||
| 1034 | |||
| 1012 | /* User bpf_sock_ops struct to access socket values and specify request ops | 1035 | /* User bpf_sock_ops struct to access socket values and specify request ops |
| 1013 | * and their replies. | 1036 | * and their replies. |
| 1014 | * Some of this fields are in network (bigendian) byte order and may need | 1037 | * Some of this fields are in network (bigendian) byte order and may need |
