WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / sockopt_inherit.c
blobc6d428a8d78574acb69e32cd53621e19105b099d
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <bpf/bpf_helpers.h>
5 char _license[] SEC("license") = "GPL";
6 __u32 _version SEC("version") = 1;
8 #define SOL_CUSTOM 0xdeadbeef
9 #define CUSTOM_INHERIT1 0
10 #define CUSTOM_INHERIT2 1
11 #define CUSTOM_LISTENER 2
13 struct sockopt_inherit {
14 __u8 val;
17 struct {
18 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
19 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
20 __type(key, int);
21 __type(value, struct sockopt_inherit);
22 } cloned1_map SEC(".maps");
24 struct {
25 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
26 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
27 __type(key, int);
28 __type(value, struct sockopt_inherit);
29 } cloned2_map SEC(".maps");
31 struct {
32 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
33 __uint(map_flags, BPF_F_NO_PREALLOC);
34 __type(key, int);
35 __type(value, struct sockopt_inherit);
36 } listener_only_map SEC(".maps");
38 static __inline struct sockopt_inherit *get_storage(struct bpf_sockopt *ctx)
40 if (ctx->optname == CUSTOM_INHERIT1)
41 return bpf_sk_storage_get(&cloned1_map, ctx->sk, 0,
42 BPF_SK_STORAGE_GET_F_CREATE);
43 else if (ctx->optname == CUSTOM_INHERIT2)
44 return bpf_sk_storage_get(&cloned2_map, ctx->sk, 0,
45 BPF_SK_STORAGE_GET_F_CREATE);
46 else
47 return bpf_sk_storage_get(&listener_only_map, ctx->sk, 0,
48 BPF_SK_STORAGE_GET_F_CREATE);
51 SEC("cgroup/getsockopt")
52 int _getsockopt(struct bpf_sockopt *ctx)
54 __u8 *optval_end = ctx->optval_end;
55 struct sockopt_inherit *storage;
56 __u8 *optval = ctx->optval;
58 if (ctx->level != SOL_CUSTOM)
59 return 1; /* only interested in SOL_CUSTOM */
61 if (optval + 1 > optval_end)
62 return 0; /* EPERM, bounds check */
64 storage = get_storage(ctx);
65 if (!storage)
66 return 0; /* EPERM, couldn't get sk storage */
68 ctx->retval = 0; /* Reset system call return value to zero */
70 optval[0] = storage->val;
71 ctx->optlen = 1;
73 return 1;
76 SEC("cgroup/setsockopt")
77 int _setsockopt(struct bpf_sockopt *ctx)
79 __u8 *optval_end = ctx->optval_end;
80 struct sockopt_inherit *storage;
81 __u8 *optval = ctx->optval;
83 if (ctx->level != SOL_CUSTOM)
84 return 1; /* only interested in SOL_CUSTOM */
86 if (optval + 1 > optval_end)
87 return 0; /* EPERM, bounds check */
89 storage = get_storage(ctx);
90 if (!storage)
91 return 0; /* EPERM, couldn't get sk storage */
93 storage->val = optval[0];
94 ctx->optlen = -1;
96 return 1;