]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/bpf-cgroup.h
bd79ae32909a897b49318a0f2d6ed3fce5d5cbce
[linux.git] / include / linux / bpf-cgroup.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BPF_CGROUP_H
3 #define _BPF_CGROUP_H
4
5 #include <linux/bpf.h>
6 #include <linux/errno.h>
7 #include <linux/jump_label.h>
8 #include <linux/percpu.h>
9 #include <linux/percpu-refcount.h>
10 #include <linux/rbtree.h>
11 #include <uapi/linux/bpf.h>
12
13 struct sock;
14 struct sockaddr;
15 struct cgroup;
16 struct sk_buff;
17 struct bpf_map;
18 struct bpf_prog;
19 struct bpf_sock_ops_kern;
20 struct bpf_cgroup_storage;
21 struct ctl_table;
22 struct ctl_table_header;
23
24 #ifdef CONFIG_CGROUP_BPF
25
26 extern struct static_key_false cgroup_bpf_enabled_key;
27 #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
28
29 DECLARE_PER_CPU(struct bpf_cgroup_storage*,
30                 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
31
32 #define for_each_cgroup_storage_type(stype) \
33         for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
34
35 struct bpf_cgroup_storage_map;
36
37 struct bpf_storage_buffer {
38         struct rcu_head rcu;
39         char data[0];
40 };
41
42 struct bpf_cgroup_storage {
43         union {
44                 struct bpf_storage_buffer *buf;
45                 void __percpu *percpu_buf;
46         };
47         struct bpf_cgroup_storage_map *map;
48         struct bpf_cgroup_storage_key key;
49         struct list_head list;
50         struct rb_node node;
51         struct rcu_head rcu;
52 };
53
54 struct bpf_prog_list {
55         struct list_head node;
56         struct bpf_prog *prog;
57         struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
58 };
59
60 struct bpf_prog_array;
61
62 struct cgroup_bpf {
63         /* array of effective progs in this cgroup */
64         struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
65
66         /* attached progs to this cgroup and attach flags
67          * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
68          * have either zero or one element
69          * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
70          */
71         struct list_head progs[MAX_BPF_ATTACH_TYPE];
72         u32 flags[MAX_BPF_ATTACH_TYPE];
73
74         /* temp storage for effective prog array used by prog_attach/detach */
75         struct bpf_prog_array *inactive;
76
77         /* reference counter used to detach bpf programs after cgroup removal */
78         struct percpu_ref refcnt;
79
80         /* cgroup_bpf is released using a work queue */
81         struct work_struct release_work;
82 };
83
84 int cgroup_bpf_inherit(struct cgroup *cgrp);
85 void cgroup_bpf_offline(struct cgroup *cgrp);
86
87 int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
88                         enum bpf_attach_type type, u32 flags);
89 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
90                         enum bpf_attach_type type);
91 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
92                        union bpf_attr __user *uattr);
93
94 /* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
95 int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
96                       enum bpf_attach_type type, u32 flags);
97 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
98                       enum bpf_attach_type type, u32 flags);
99 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
100                      union bpf_attr __user *uattr);
101
102 int __cgroup_bpf_run_filter_skb(struct sock *sk,
103                                 struct sk_buff *skb,
104                                 enum bpf_attach_type type);
105
106 int __cgroup_bpf_run_filter_sk(struct sock *sk,
107                                enum bpf_attach_type type);
108
109 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
110                                       struct sockaddr *uaddr,
111                                       enum bpf_attach_type type,
112                                       void *t_ctx);
113
114 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
115                                      struct bpf_sock_ops_kern *sock_ops,
116                                      enum bpf_attach_type type);
117
118 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
119                                       short access, enum bpf_attach_type type);
120
121 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
122                                    struct ctl_table *table, int write,
123                                    void __user *buf, size_t *pcount,
124                                    loff_t *ppos, void **new_buf,
125                                    enum bpf_attach_type type);
126
127 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
128         struct bpf_map *map)
129 {
130         if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
131                 return BPF_CGROUP_STORAGE_PERCPU;
132
133         return BPF_CGROUP_STORAGE_SHARED;
134 }
135
136 static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage
137                                           *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
138 {
139         enum bpf_cgroup_storage_type stype;
140
141         for_each_cgroup_storage_type(stype)
142                 this_cpu_write(bpf_cgroup_storage[stype], storage[stype]);
143 }
144
145 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
146                                         enum bpf_cgroup_storage_type stype);
147 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
148 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
149                              struct cgroup *cgroup,
150                              enum bpf_attach_type type);
151 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
152 int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *map);
153 void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *map);
154
155 int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
156 int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
157                                      void *value, u64 flags);
158
159 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
160 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb)                             \
161 ({                                                                            \
162         int __ret = 0;                                                        \
163         if (cgroup_bpf_enabled)                                               \
164                 __ret = __cgroup_bpf_run_filter_skb(sk, skb,                  \
165                                                     BPF_CGROUP_INET_INGRESS); \
166                                                                               \
167         __ret;                                                                \
168 })
169
170 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)                               \
171 ({                                                                             \
172         int __ret = 0;                                                         \
173         if (cgroup_bpf_enabled && sk && sk == skb->sk) {                       \
174                 typeof(sk) __sk = sk_to_full_sk(sk);                           \
175                 if (sk_fullsock(__sk))                                         \
176                         __ret = __cgroup_bpf_run_filter_skb(__sk, skb,         \
177                                                       BPF_CGROUP_INET_EGRESS); \
178         }                                                                      \
179         __ret;                                                                 \
180 })
181
182 #define BPF_CGROUP_RUN_SK_PROG(sk, type)                                       \
183 ({                                                                             \
184         int __ret = 0;                                                         \
185         if (cgroup_bpf_enabled) {                                              \
186                 __ret = __cgroup_bpf_run_filter_sk(sk, type);                  \
187         }                                                                      \
188         __ret;                                                                 \
189 })
190
191 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk)                                      \
192         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
193
194 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk)                                \
195         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
196
197 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk)                                \
198         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
199
200 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type)                                \
201 ({                                                                             \
202         int __ret = 0;                                                         \
203         if (cgroup_bpf_enabled)                                                \
204                 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type,     \
205                                                           NULL);               \
206         __ret;                                                                 \
207 })
208
209 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx)                    \
210 ({                                                                             \
211         int __ret = 0;                                                         \
212         if (cgroup_bpf_enabled) {                                              \
213                 lock_sock(sk);                                                 \
214                 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type,     \
215                                                           t_ctx);              \
216                 release_sock(sk);                                              \
217         }                                                                      \
218         __ret;                                                                 \
219 })
220
221 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr)                              \
222         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
223
224 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr)                              \
225         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
226
227 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
228                                             sk->sk_prot->pre_connect)
229
230 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr)                           \
231         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
232
233 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr)                           \
234         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
235
236 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr)                      \
237         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
238
239 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr)                      \
240         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
241
242 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx)                \
243         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
244
245 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx)                \
246         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
247
248 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr)                        \
249         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
250
251 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr)                        \
252         BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
253
254 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops)                                 \
255 ({                                                                             \
256         int __ret = 0;                                                         \
257         if (cgroup_bpf_enabled && (sock_ops)->sk) {            \
258                 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk);               \
259                 if (__sk && sk_fullsock(__sk))                                 \
260                         __ret = __cgroup_bpf_run_filter_sock_ops(__sk,         \
261                                                                  sock_ops,     \
262                                                          BPF_CGROUP_SOCK_OPS); \
263         }                                                                      \
264         __ret;                                                                 \
265 })
266
267 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access)         \
268 ({                                                                            \
269         int __ret = 0;                                                        \
270         if (cgroup_bpf_enabled)                                               \
271                 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
272                                                           access,             \
273                                                           BPF_CGROUP_DEVICE); \
274                                                                               \
275         __ret;                                                                \
276 })
277
278
279 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos, nbuf)  \
280 ({                                                                             \
281         int __ret = 0;                                                         \
282         if (cgroup_bpf_enabled)                                                \
283                 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write,     \
284                                                        buf, count, pos, nbuf,  \
285                                                        BPF_CGROUP_SYSCTL);     \
286         __ret;                                                                 \
287 })
288
289 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
290                            enum bpf_prog_type ptype, struct bpf_prog *prog);
291 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
292                            enum bpf_prog_type ptype);
293 int cgroup_bpf_prog_query(const union bpf_attr *attr,
294                           union bpf_attr __user *uattr);
295 #else
296
297 struct bpf_prog;
298 struct cgroup_bpf {};
299 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
300 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
301
302 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
303                                          enum bpf_prog_type ptype,
304                                          struct bpf_prog *prog)
305 {
306         return -EINVAL;
307 }
308
309 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
310                                          enum bpf_prog_type ptype)
311 {
312         return -EINVAL;
313 }
314
315 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
316                                         union bpf_attr __user *uattr)
317 {
318         return -EINVAL;
319 }
320
321 static inline void bpf_cgroup_storage_set(
322         struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) {}
323 static inline int bpf_cgroup_storage_assign(struct bpf_prog *prog,
324                                             struct bpf_map *map) { return 0; }
325 static inline void bpf_cgroup_storage_release(struct bpf_prog *prog,
326                                               struct bpf_map *map) {}
327 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
328         struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
329 static inline void bpf_cgroup_storage_free(
330         struct bpf_cgroup_storage *storage) {}
331 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
332                                                  void *value) {
333         return 0;
334 }
335 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
336                                         void *key, void *value, u64 flags) {
337         return 0;
338 }
339
340 #define cgroup_bpf_enabled (0)
341 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
342 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
343 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
344 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
345 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
346 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
347 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
348 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
349 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
350 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
351 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
352 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
353 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
354 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
355 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
356 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
357 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
358 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
359 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos,nbuf) ({ 0; })
360
361 #define for_each_cgroup_storage_type(stype) for (; false; )
362
363 #endif /* CONFIG_CGROUP_BPF */
364
365 #endif /* _BPF_CGROUP_H */