Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/bpf.h>
3#include <bpf/bpf_helpers.h>
4
5char _license[] SEC("license") = "GPL";
6
7#define SOL_CUSTOM 0xdeadbeef
8#define CUSTOM_INHERIT1 0
9#define CUSTOM_INHERIT2 1
10#define CUSTOM_LISTENER 2
11
12__s32 page_size = 0;
13
14struct sockopt_inherit {
15 __u8 val;
16};
17
18struct {
19 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
20 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
21 __type(key, int);
22 __type(value, struct sockopt_inherit);
23} cloned1_map SEC(".maps");
24
25struct {
26 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
27 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
28 __type(key, int);
29 __type(value, struct sockopt_inherit);
30} cloned2_map SEC(".maps");
31
32struct {
33 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
34 __uint(map_flags, BPF_F_NO_PREALLOC);
35 __type(key, int);
36 __type(value, struct sockopt_inherit);
37} listener_only_map SEC(".maps");
38
39static __inline struct sockopt_inherit *get_storage(struct bpf_sockopt *ctx)
40{
41 if (ctx->optname == CUSTOM_INHERIT1)
42 return bpf_sk_storage_get(&cloned1_map, ctx->sk, 0,
43 BPF_SK_STORAGE_GET_F_CREATE);
44 else if (ctx->optname == CUSTOM_INHERIT2)
45 return bpf_sk_storage_get(&cloned2_map, ctx->sk, 0,
46 BPF_SK_STORAGE_GET_F_CREATE);
47 else
48 return bpf_sk_storage_get(&listener_only_map, ctx->sk, 0,
49 BPF_SK_STORAGE_GET_F_CREATE);
50}
51
52SEC("cgroup/getsockopt")
53int _getsockopt(struct bpf_sockopt *ctx)
54{
55 __u8 *optval_end = ctx->optval_end;
56 struct sockopt_inherit *storage;
57 __u8 *optval = ctx->optval;
58
59 if (ctx->level != SOL_CUSTOM)
60 goto out; /* only interested in SOL_CUSTOM */
61
62 if (optval + 1 > optval_end)
63 return 0; /* EPERM, bounds check */
64
65 storage = get_storage(ctx);
66 if (!storage)
67 return 0; /* EPERM, couldn't get sk storage */
68
69 ctx->retval = 0; /* Reset system call return value to zero */
70
71 optval[0] = storage->val;
72 ctx->optlen = 1;
73
74 return 1;
75
76out:
77 /* optval larger than PAGE_SIZE use kernel's buffer. */
78 if (ctx->optlen > page_size)
79 ctx->optlen = 0;
80 return 1;
81}
82
83SEC("cgroup/setsockopt")
84int _setsockopt(struct bpf_sockopt *ctx)
85{
86 __u8 *optval_end = ctx->optval_end;
87 struct sockopt_inherit *storage;
88 __u8 *optval = ctx->optval;
89
90 if (ctx->level != SOL_CUSTOM)
91 goto out; /* only interested in SOL_CUSTOM */
92
93 if (optval + 1 > optval_end)
94 return 0; /* EPERM, bounds check */
95
96 storage = get_storage(ctx);
97 if (!storage)
98 return 0; /* EPERM, couldn't get sk storage */
99
100 storage->val = optval[0];
101 ctx->optlen = -1;
102
103 return 1;
104
105out:
106 /* optval larger than PAGE_SIZE use kernel's buffer. */
107 if (ctx->optlen > page_size)
108 ctx->optlen = 0;
109 return 1;
110}
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/bpf.h>
3#include <bpf/bpf_helpers.h>
4
5char _license[] SEC("license") = "GPL";
6
7#define SOL_CUSTOM 0xdeadbeef
8#define CUSTOM_INHERIT1 0
9#define CUSTOM_INHERIT2 1
10#define CUSTOM_LISTENER 2
11
12struct sockopt_inherit {
13 __u8 val;
14};
15
16struct {
17 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
18 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
19 __type(key, int);
20 __type(value, struct sockopt_inherit);
21} cloned1_map SEC(".maps");
22
23struct {
24 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
25 __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
26 __type(key, int);
27 __type(value, struct sockopt_inherit);
28} cloned2_map SEC(".maps");
29
30struct {
31 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
32 __uint(map_flags, BPF_F_NO_PREALLOC);
33 __type(key, int);
34 __type(value, struct sockopt_inherit);
35} listener_only_map SEC(".maps");
36
37static __inline struct sockopt_inherit *get_storage(struct bpf_sockopt *ctx)
38{
39 if (ctx->optname == CUSTOM_INHERIT1)
40 return bpf_sk_storage_get(&cloned1_map, ctx->sk, 0,
41 BPF_SK_STORAGE_GET_F_CREATE);
42 else if (ctx->optname == CUSTOM_INHERIT2)
43 return bpf_sk_storage_get(&cloned2_map, ctx->sk, 0,
44 BPF_SK_STORAGE_GET_F_CREATE);
45 else
46 return bpf_sk_storage_get(&listener_only_map, ctx->sk, 0,
47 BPF_SK_STORAGE_GET_F_CREATE);
48}
49
50SEC("cgroup/getsockopt")
51int _getsockopt(struct bpf_sockopt *ctx)
52{
53 __u8 *optval_end = ctx->optval_end;
54 struct sockopt_inherit *storage;
55 __u8 *optval = ctx->optval;
56
57 if (ctx->level != SOL_CUSTOM)
58 return 1; /* only interested in SOL_CUSTOM */
59
60 if (optval + 1 > optval_end)
61 return 0; /* EPERM, bounds check */
62
63 storage = get_storage(ctx);
64 if (!storage)
65 return 0; /* EPERM, couldn't get sk storage */
66
67 ctx->retval = 0; /* Reset system call return value to zero */
68
69 optval[0] = storage->val;
70 ctx->optlen = 1;
71
72 return 1;
73}
74
75SEC("cgroup/setsockopt")
76int _setsockopt(struct bpf_sockopt *ctx)
77{
78 __u8 *optval_end = ctx->optval_end;
79 struct sockopt_inherit *storage;
80 __u8 *optval = ctx->optval;
81
82 if (ctx->level != SOL_CUSTOM)
83 return 1; /* only interested in SOL_CUSTOM */
84
85 if (optval + 1 > optval_end)
86 return 0; /* EPERM, bounds check */
87
88 storage = get_storage(ctx);
89 if (!storage)
90 return 0; /* EPERM, couldn't get sk storage */
91
92 storage->val = optval[0];
93 ctx->optlen = -1;
94
95 return 1;
96}