treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / sockopt_multi.c
blob9d8c212dde9f5f8bf60af4fee88361ef413d9986
1 // SPDX-License-Identifier: GPL-2.0
2 #include <netinet/in.h>
3 #include <linux/bpf.h>
4 #include <bpf/bpf_helpers.h>
6 char _license[] SEC("license") = "GPL";
7 __u32 _version SEC("version") = 1;
9 SEC("cgroup/getsockopt/child")
10 int _getsockopt_child(struct bpf_sockopt *ctx)
12 __u8 *optval_end = ctx->optval_end;
13 __u8 *optval = ctx->optval;
15 if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
16 return 1;
18 if (optval + 1 > optval_end)
19 return 0; /* EPERM, bounds check */
21 if (optval[0] != 0x80)
22 return 0; /* EPERM, unexpected optval from the kernel */
24 ctx->retval = 0; /* Reset system call return value to zero */
26 optval[0] = 0x90;
27 ctx->optlen = 1;
29 return 1;
32 SEC("cgroup/getsockopt/parent")
33 int _getsockopt_parent(struct bpf_sockopt *ctx)
35 __u8 *optval_end = ctx->optval_end;
36 __u8 *optval = ctx->optval;
38 if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
39 return 1;
41 if (optval + 1 > optval_end)
42 return 0; /* EPERM, bounds check */
44 if (optval[0] != 0x90)
45 return 0; /* EPERM, unexpected optval from the kernel */
47 ctx->retval = 0; /* Reset system call return value to zero */
49 optval[0] = 0xA0;
50 ctx->optlen = 1;
52 return 1;
55 SEC("cgroup/setsockopt")
56 int _setsockopt(struct bpf_sockopt *ctx)
58 __u8 *optval_end = ctx->optval_end;
59 __u8 *optval = ctx->optval;
61 if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
62 return 1;
64 if (optval + 1 > optval_end)
65 return 0; /* EPERM, bounds check */
67 optval[0] += 0x10;
68 ctx->optlen = 1;
70 return 1;