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 struct task_struct;
24
25 #ifdef CONFIG_CGROUP_BPF
26
27 extern struct static_key_false cgroup_bpf_enabled_key;
28 #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
29
30 #define for_each_cgroup_storage_type(stype) \
31 for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
32
33 struct bpf_cgroup_storage_map;
34
35 struct bpf_storage_buffer {
36 struct rcu_head rcu;
37 char data[];
38 };
39
40 struct bpf_cgroup_storage {
41 union {
42 struct bpf_storage_buffer *buf;
43 void __percpu *percpu_buf;
44 };
45 struct bpf_cgroup_storage_map *map;
46 struct bpf_cgroup_storage_key key;
47 struct list_head list_map;
48 struct list_head list_cg;
49 struct rb_node node;
50 struct rcu_head rcu;
51 };
52
53 struct bpf_cgroup_link {
54 struct bpf_link link;
55 struct cgroup *cgroup;
56 enum bpf_attach_type type;
57 };
58
59 struct bpf_prog_list {
60 struct list_head node;
61 struct bpf_prog *prog;
62 struct bpf_cgroup_link *link;
63 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
64 };
65
66 struct bpf_prog_array;
67
68 struct cgroup_bpf {
69 /* array of effective progs in this cgroup */
70 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
71
72 /* attached progs to this cgroup and attach flags
73 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
74 * have either zero or one element
75 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
76 */
77 struct list_head progs[MAX_BPF_ATTACH_TYPE];
78 u32 flags[MAX_BPF_ATTACH_TYPE];
79
80 /* list of cgroup shared storages */
81 struct list_head storages;
82
83 /* temp storage for effective prog array used by prog_attach/detach */
84 struct bpf_prog_array *inactive;
85
86 /* reference counter used to detach bpf programs after cgroup removal */
87 struct percpu_ref refcnt;
88
89 /* cgroup_bpf is released using a work queue */
90 struct work_struct release_work;
91 };
92
93 int cgroup_bpf_inherit(struct cgroup *cgrp);
94 void cgroup_bpf_offline(struct cgroup *cgrp);
95
96 int __cgroup_bpf_attach(struct cgroup *cgrp,
97 struct bpf_prog *prog, struct bpf_prog *replace_prog,
98 struct bpf_cgroup_link *link,
99 enum bpf_attach_type type, u32 flags);
100 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
101 struct bpf_cgroup_link *link,
102 enum bpf_attach_type type);
103 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
104 union bpf_attr __user *uattr);
105
106 /* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
107 int cgroup_bpf_attach(struct cgroup *cgrp,
108 struct bpf_prog *prog, struct bpf_prog *replace_prog,
109 struct bpf_cgroup_link *link, enum bpf_attach_type type,
110 u32 flags);
111 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
112 enum bpf_attach_type type);
113 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
114 union bpf_attr __user *uattr);
115
116 int __cgroup_bpf_run_filter_skb(struct sock *sk,
117 struct sk_buff *skb,
118 enum bpf_attach_type type);
119
120 int __cgroup_bpf_run_filter_sk(struct sock *sk,
121 enum bpf_attach_type type);
122
123 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
124 struct sockaddr *uaddr,
125 enum bpf_attach_type type,
126 void *t_ctx);
127
128 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
129 struct bpf_sock_ops_kern *sock_ops,
130 enum bpf_attach_type type);
131
132 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
133 short access, enum bpf_attach_type type);
134
135 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
136 struct ctl_table *table, int write,
137 char **buf, size_t *pcount, loff_t *ppos,
138 enum bpf_attach_type type);
139
140 int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level,
141 int *optname, char __user *optval,
142 int *optlen, char **kernel_optval);
143 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
144 int optname, char __user *optval,
145 int __user *optlen, int max_optlen,
146 int retval);
147
cgroup_storage_type(struct bpf_map * map)148 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
149 struct bpf_map *map)
150 {
151 if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
152 return BPF_CGROUP_STORAGE_PERCPU;
153
154 return BPF_CGROUP_STORAGE_SHARED;
155 }
156
157 struct bpf_cgroup_storage *
158 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
159 void *key, bool locked);
160 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
161 enum bpf_cgroup_storage_type stype);
162 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
163 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
164 struct cgroup *cgroup,
165 enum bpf_attach_type type);
166 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
167 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *map);
168
169 int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
170 int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
171 void *value, u64 flags);
172
173 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
174 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
175 ({ \
176 int __ret = 0; \
177 if (cgroup_bpf_enabled) \
178 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
179 BPF_CGROUP_INET_INGRESS); \
180 \
181 __ret; \
182 })
183
184 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
185 ({ \
186 int __ret = 0; \
187 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
188 typeof(sk) __sk = sk_to_full_sk(sk); \
189 if (sk_fullsock(__sk)) \
190 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
191 BPF_CGROUP_INET_EGRESS); \
192 } \
193 __ret; \
194 })
195
196 #define BPF_CGROUP_RUN_SK_PROG(sk, type) \
197 ({ \
198 int __ret = 0; \
199 if (cgroup_bpf_enabled) { \
200 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
201 } \
202 __ret; \
203 })
204
205 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
206 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
207
208 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \
209 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
210
211 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
212 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
213
214 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
215 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
216
217 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
218 ({ \
219 int __ret = 0; \
220 if (cgroup_bpf_enabled) \
221 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
222 NULL); \
223 __ret; \
224 })
225
226 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
227 ({ \
228 int __ret = 0; \
229 if (cgroup_bpf_enabled) { \
230 lock_sock(sk); \
231 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
232 t_ctx); \
233 release_sock(sk); \
234 } \
235 __ret; \
236 })
237
238 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
239 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
240
241 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
242 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
243
244 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
245 sk->sk_prot->pre_connect)
246
247 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
248 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
249
250 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
251 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
252
253 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
254 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
255
256 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
257 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
258
259 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
260 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
261
262 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
263 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
264
265 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
266 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
267
268 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
269 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
270
271 /* The SOCK_OPS"_SK" macro should be used when sock_ops->sk is not a
272 * fullsock and its parent fullsock cannot be traced by
273 * sk_to_full_sk().
274 *
275 * e.g. sock_ops->sk is a request_sock and it is under syncookie mode.
276 * Its listener-sk is not attached to the rsk_listener.
277 * In this case, the caller holds the listener-sk (unlocked),
278 * set its sock_ops->sk to req_sk, and call this SOCK_OPS"_SK" with
279 * the listener-sk such that the cgroup-bpf-progs of the
280 * listener-sk will be run.
281 *
282 * Regardless of syncookie mode or not,
283 * calling bpf_setsockopt on listener-sk will not make sense anyway,
284 * so passing 'sock_ops->sk == req_sk' to the bpf prog is appropriate here.
285 */
286 #define BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(sock_ops, sk) \
287 ({ \
288 int __ret = 0; \
289 if (cgroup_bpf_enabled) \
290 __ret = __cgroup_bpf_run_filter_sock_ops(sk, \
291 sock_ops, \
292 BPF_CGROUP_SOCK_OPS); \
293 __ret; \
294 })
295
296 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
297 ({ \
298 int __ret = 0; \
299 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
300 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
301 if (__sk && sk_fullsock(__sk)) \
302 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
303 sock_ops, \
304 BPF_CGROUP_SOCK_OPS); \
305 } \
306 __ret; \
307 })
308
309 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
310 ({ \
311 int __ret = 0; \
312 if (cgroup_bpf_enabled) \
313 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
314 access, \
315 BPF_CGROUP_DEVICE); \
316 \
317 __ret; \
318 })
319
320
321 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos) \
322 ({ \
323 int __ret = 0; \
324 if (cgroup_bpf_enabled) \
325 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
326 buf, count, pos, \
327 BPF_CGROUP_SYSCTL); \
328 __ret; \
329 })
330
331 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
332 kernel_optval) \
333 ({ \
334 int __ret = 0; \
335 if (cgroup_bpf_enabled) \
336 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \
337 optname, optval, \
338 optlen, \
339 kernel_optval); \
340 __ret; \
341 })
342
343 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \
344 ({ \
345 int __ret = 0; \
346 if (cgroup_bpf_enabled) \
347 get_user(__ret, optlen); \
348 __ret; \
349 })
350
351 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen, \
352 max_optlen, retval) \
353 ({ \
354 int __ret = retval; \
355 if (cgroup_bpf_enabled) \
356 __ret = __cgroup_bpf_run_filter_getsockopt(sock, level, \
357 optname, optval, \
358 optlen, max_optlen, \
359 retval); \
360 __ret; \
361 })
362
363 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
364 enum bpf_prog_type ptype, struct bpf_prog *prog);
365 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
366 enum bpf_prog_type ptype);
367 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
368 int cgroup_bpf_prog_query(const union bpf_attr *attr,
369 union bpf_attr __user *uattr);
370 #else
371
372 struct bpf_prog;
373 struct cgroup_bpf {};
cgroup_bpf_inherit(struct cgroup * cgrp)374 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
cgroup_bpf_offline(struct cgroup * cgrp)375 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
376
cgroup_bpf_prog_attach(const union bpf_attr * attr,enum bpf_prog_type ptype,struct bpf_prog * prog)377 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
378 enum bpf_prog_type ptype,
379 struct bpf_prog *prog)
380 {
381 return -EINVAL;
382 }
383
cgroup_bpf_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)384 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
385 enum bpf_prog_type ptype)
386 {
387 return -EINVAL;
388 }
389
cgroup_bpf_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)390 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
391 struct bpf_prog *prog)
392 {
393 return -EINVAL;
394 }
395
cgroup_bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)396 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
397 union bpf_attr __user *uattr)
398 {
399 return -EINVAL;
400 }
401
bpf_cgroup_storage_assign(struct bpf_prog_aux * aux,struct bpf_map * map)402 static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
403 struct bpf_map *map) { return 0; }
bpf_cgroup_storage_alloc(struct bpf_prog * prog,enum bpf_cgroup_storage_type stype)404 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
405 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
bpf_cgroup_storage_free(struct bpf_cgroup_storage * storage)406 static inline void bpf_cgroup_storage_free(
407 struct bpf_cgroup_storage *storage) {}
bpf_percpu_cgroup_storage_copy(struct bpf_map * map,void * key,void * value)408 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
409 void *value) {
410 return 0;
411 }
bpf_percpu_cgroup_storage_update(struct bpf_map * map,void * key,void * value,u64 flags)412 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
413 void *key, void *value, u64 flags) {
414 return 0;
415 }
416
417 #define cgroup_bpf_enabled (0)
418 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) ({ 0; })
419 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
420 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
421 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
422 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
423 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
424 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
425 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
426 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
427 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
428 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
429 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
430 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
431 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
432 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
433 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
434 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
435 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
436 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
437 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
438 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
439 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
440 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, \
441 optlen, max_optlen, retval) ({ retval; })
442 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
443 kernel_optval) ({ 0; })
444
445 #define for_each_cgroup_storage_type(stype) for (; false; )
446
447 #endif /* CONFIG_CGROUP_BPF */
448
449 #endif /* _BPF_CGROUP_H */
450