treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / sockopt_sk.c
blobd5a5eeb5fb52798f2b989545fd2dff8e56ecbf0a
1 // SPDX-License-Identifier: GPL-2.0
2 #include <string.h>
3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
5 #include <linux/bpf.h>
6 #include <bpf/bpf_helpers.h>
8 char _license[] SEC("license") = "GPL";
9 __u32 _version SEC("version") = 1;
11 #define SOL_CUSTOM 0xdeadbeef
13 struct sockopt_sk {
14 __u8 val;
17 struct {
18 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
19 __uint(map_flags, BPF_F_NO_PREALLOC);
20 __type(key, int);
21 __type(value, struct sockopt_sk);
22 } socket_storage_map SEC(".maps");
24 SEC("cgroup/getsockopt")
25 int _getsockopt(struct bpf_sockopt *ctx)
27 __u8 *optval_end = ctx->optval_end;
28 __u8 *optval = ctx->optval;
29 struct sockopt_sk *storage;
31 if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
32 /* Not interested in SOL_IP:IP_TOS;
33 * let next BPF program in the cgroup chain or kernel
34 * handle it.
36 return 1;
38 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
39 /* Not interested in SOL_SOCKET:SO_SNDBUF;
40 * let next BPF program in the cgroup chain or kernel
41 * handle it.
43 return 1;
46 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
47 /* Not interested in SOL_TCP:TCP_CONGESTION;
48 * let next BPF program in the cgroup chain or kernel
49 * handle it.
51 return 1;
54 if (ctx->level != SOL_CUSTOM)
55 return 0; /* EPERM, deny everything except custom level */
57 if (optval + 1 > optval_end)
58 return 0; /* EPERM, bounds check */
60 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
61 BPF_SK_STORAGE_GET_F_CREATE);
62 if (!storage)
63 return 0; /* EPERM, couldn't get sk storage */
65 if (!ctx->retval)
66 return 0; /* EPERM, kernel should not have handled
67 * SOL_CUSTOM, something is wrong!
69 ctx->retval = 0; /* Reset system call return value to zero */
71 optval[0] = storage->val;
72 ctx->optlen = 1;
74 return 1;
77 SEC("cgroup/setsockopt")
78 int _setsockopt(struct bpf_sockopt *ctx)
80 __u8 *optval_end = ctx->optval_end;
81 __u8 *optval = ctx->optval;
82 struct sockopt_sk *storage;
84 if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
85 /* Not interested in SOL_IP:IP_TOS;
86 * let next BPF program in the cgroup chain or kernel
87 * handle it.
89 return 1;
91 if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
92 /* Overwrite SO_SNDBUF value */
94 if (optval + sizeof(__u32) > optval_end)
95 return 0; /* EPERM, bounds check */
97 *(__u32 *)optval = 0x55AA;
98 ctx->optlen = 4;
100 return 1;
103 if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
104 /* Always use cubic */
106 if (optval + 5 > optval_end)
107 return 0; /* EPERM, bounds check */
109 memcpy(optval, "cubic", 5);
110 ctx->optlen = 5;
112 return 1;
115 if (ctx->level != SOL_CUSTOM)
116 return 0; /* EPERM, deny everything except custom level */
118 if (optval + 1 > optval_end)
119 return 0; /* EPERM, bounds check */
121 storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
122 BPF_SK_STORAGE_GET_F_CREATE);
123 if (!storage)
124 return 0; /* EPERM, couldn't get sk storage */
126 storage->val = optval[0];
127 ctx->optlen = -1; /* BPF has consumed this option, don't call kernel
128 * setsockopt handler.
131 return 1;