1 // SPDX-License-Identifier: GPL-2.0
5 #include <netinet/in.h>
6 #include <bpf/bpf_helpers.h>
8 char _license[] SEC("license") = "GPL";
10 int page_size = 0; /* userspace should set it */
13 #define SOL_TCP IPPROTO_TCP
16 #define SOL_CUSTOM 0xdeadbeef
23 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
24 __uint(map_flags, BPF_F_NO_PREALLOC);
26 __type(value, struct sockopt_sk);
27 } socket_storage_map SEC(".maps");
29 SEC("cgroup/getsockopt")
30 int _getsockopt(struct bpf_sockopt *ctx)
32 __u8 *optval_end = ctx->optval_end;
33 __u8 *optval = ctx->optval;
34 struct sockopt_sk *storage;
37 /* Bypass AF_NETLINK. */
39 if (sk && sk->family == AF_NETLINK)
42 /* Make sure bpf_get_netns_cookie is callable.
44 if (bpf_get_netns_cookie(NULL) == 0)
47 if (bpf_get_netns_cookie(ctx) == 0)
50 if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
51 /* Not interested in SOL_IP:IP_TOS;
52 * let next BPF program in the cgroup chain or kernel
58 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
59 /* Not interested in SOL_SOCKET:SO_SNDBUF;
60 * let next BPF program in the cgroup chain or kernel
66 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
67 /* Not interested in SOL_TCP:TCP_CONGESTION;
68 * let next BPF program in the cgroup chain or kernel
74 if (ctx->level == SOL_TCP && ctx->optname == TCP_ZEROCOPY_RECEIVE) {
75 /* Verify that TCP_ZEROCOPY_RECEIVE triggers.
76 * It has a custom implementation for performance
80 /* Check that optval contains address (__u64) */
81 if (optval + sizeof(__u64) > optval_end)
82 return 0; /* bounds check */
84 if (((struct tcp_zerocopy_receive *)optval)->address != 0)
85 return 0; /* unexpected data */
90 if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
91 if (optval + 1 > optval_end)
92 return 0; /* bounds check */
94 ctx->retval = 0; /* Reset system call return value to zero */
96 /* Always export 0x55 */
100 /* Userspace buffer is PAGE_SIZE * 2, but BPF
101 * program can only see the first PAGE_SIZE
104 if (optval_end - optval != page_size)
105 return 0; /* unexpected data size */
110 if (ctx->level != SOL_CUSTOM)
111 return 0; /* deny everything except custom level */
113 if (optval + 1 > optval_end)
114 return 0; /* bounds check */
116 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
117 BPF_SK_STORAGE_GET_F_CREATE);
119 return 0; /* couldn't get sk storage */
122 return 0; /* kernel should not have handled
123 * SOL_CUSTOM, something is wrong!
125 ctx->retval = 0; /* Reset system call return value to zero */
127 optval[0] = storage->val;
133 /* optval larger than PAGE_SIZE use kernel's buffer. */
134 if (ctx->optlen > page_size)
139 SEC("cgroup/setsockopt")
140 int _setsockopt(struct bpf_sockopt *ctx)
142 __u8 *optval_end = ctx->optval_end;
143 __u8 *optval = ctx->optval;
144 struct sockopt_sk *storage;
147 /* Bypass AF_NETLINK. */
149 if (sk && sk->family == AF_NETLINK)
152 /* Make sure bpf_get_netns_cookie is callable.
154 if (bpf_get_netns_cookie(NULL) == 0)
157 if (bpf_get_netns_cookie(ctx) == 0)
160 if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
161 /* Not interested in SOL_IP:IP_TOS;
162 * let next BPF program in the cgroup chain or kernel
165 ctx->optlen = 0; /* bypass optval>PAGE_SIZE */
169 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
170 /* Overwrite SO_SNDBUF value */
172 if (optval + sizeof(__u32) > optval_end)
173 return 0; /* bounds check */
175 *(__u32 *)optval = 0x55AA;
181 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
182 /* Always use cubic */
184 if (optval + 5 > optval_end)
185 return 0; /* bounds check */
187 memcpy(optval, "cubic", 5);
193 if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
194 /* Original optlen is larger than PAGE_SIZE. */
195 if (ctx->optlen != page_size * 2)
196 return 0; /* unexpected data size */
198 if (optval + 1 > optval_end)
199 return 0; /* bounds check */
201 /* Make sure we can trim the buffer. */
205 /* Usepace buffer is PAGE_SIZE * 2, but BPF
206 * program can only see the first PAGE_SIZE
209 if (optval_end - optval != page_size)
210 return 0; /* unexpected data size */
215 if (ctx->level != SOL_CUSTOM)
216 return 0; /* deny everything except custom level */
218 if (optval + 1 > optval_end)
219 return 0; /* bounds check */
221 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
222 BPF_SK_STORAGE_GET_F_CREATE);
224 return 0; /* couldn't get sk storage */
226 storage->val = optval[0];
227 ctx->optlen = -1; /* BPF has consumed this option, don't call kernel
228 * setsockopt handler.
234 /* optval larger than PAGE_SIZE use kernel's buffer. */
235 if (ctx->optlen > page_size)