1 /* SPDX-License-Identifier: GPL-2.0 */
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>
19 struct bpf_sock_ops_kern;
20 struct bpf_cgroup_storage;
22 struct ctl_table_header;
25 #ifdef CONFIG_CGROUP_BPF
27 extern struct static_key_false cgroup_bpf_enabled_key;
28 #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
30 #define BPF_CGROUP_STORAGE_NEST_MAX 8
32 struct bpf_cgroup_storage_info {
33 struct task_struct *task;
34 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
37 /* For each cpu, permit maximum BPF_CGROUP_STORAGE_NEST_MAX number of tasks
38 * to use bpf cgroup storage simultaneously.
40 DECLARE_PER_CPU(struct bpf_cgroup_storage_info,
41 bpf_cgroup_storage_info[BPF_CGROUP_STORAGE_NEST_MAX]);
43 #define for_each_cgroup_storage_type(stype) \
44 for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
46 struct bpf_cgroup_storage_map;
48 struct bpf_storage_buffer {
53 struct bpf_cgroup_storage {
55 struct bpf_storage_buffer *buf;
56 void __percpu *percpu_buf;
58 struct bpf_cgroup_storage_map *map;
59 struct bpf_cgroup_storage_key key;
60 struct list_head list_map;
61 struct list_head list_cg;
66 struct bpf_cgroup_link {
68 struct cgroup *cgroup;
69 enum bpf_attach_type type;
72 struct bpf_prog_list {
73 struct list_head node;
74 struct bpf_prog *prog;
75 struct bpf_cgroup_link *link;
76 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
79 struct bpf_prog_array;
82 /* array of effective progs in this cgroup */
83 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
85 /* attached progs to this cgroup and attach flags
86 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
87 * have either zero or one element
88 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
90 struct list_head progs[MAX_BPF_ATTACH_TYPE];
91 u32 flags[MAX_BPF_ATTACH_TYPE];
93 /* list of cgroup shared storages */
94 struct list_head storages;
96 /* temp storage for effective prog array used by prog_attach/detach */
97 struct bpf_prog_array *inactive;
99 /* reference counter used to detach bpf programs after cgroup removal */
100 struct percpu_ref refcnt;
102 /* cgroup_bpf is released using a work queue */
103 struct work_struct release_work;
106 int cgroup_bpf_inherit(struct cgroup *cgrp);
107 void cgroup_bpf_offline(struct cgroup *cgrp);
109 int __cgroup_bpf_attach(struct cgroup *cgrp,
110 struct bpf_prog *prog, struct bpf_prog *replace_prog,
111 struct bpf_cgroup_link *link,
112 enum bpf_attach_type type, u32 flags);
113 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
114 struct bpf_cgroup_link *link,
115 enum bpf_attach_type type);
116 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
117 union bpf_attr __user *uattr);
119 /* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
120 int cgroup_bpf_attach(struct cgroup *cgrp,
121 struct bpf_prog *prog, struct bpf_prog *replace_prog,
122 struct bpf_cgroup_link *link, enum bpf_attach_type type,
124 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
125 enum bpf_attach_type type);
126 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
127 union bpf_attr __user *uattr);
129 int __cgroup_bpf_run_filter_skb(struct sock *sk,
131 enum bpf_attach_type type);
133 int __cgroup_bpf_run_filter_sk(struct sock *sk,
134 enum bpf_attach_type type);
136 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
137 struct sockaddr *uaddr,
138 enum bpf_attach_type type,
141 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
142 struct bpf_sock_ops_kern *sock_ops,
143 enum bpf_attach_type type);
145 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
146 short access, enum bpf_attach_type type);
148 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
149 struct ctl_table *table, int write,
150 char **buf, size_t *pcount, loff_t *ppos,
151 enum bpf_attach_type type);
153 int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level,
154 int *optname, char __user *optval,
155 int *optlen, char **kernel_optval);
156 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
157 int optname, char __user *optval,
158 int __user *optlen, int max_optlen,
161 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
162 int optname, void *optval,
163 int *optlen, int retval);
165 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
168 if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
169 return BPF_CGROUP_STORAGE_PERCPU;
171 return BPF_CGROUP_STORAGE_SHARED;
174 static inline int bpf_cgroup_storage_set(struct bpf_cgroup_storage
175 *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
177 enum bpf_cgroup_storage_type stype;
181 for (i = 0; i < BPF_CGROUP_STORAGE_NEST_MAX; i++) {
182 if (unlikely(this_cpu_read(bpf_cgroup_storage_info[i].task) != NULL))
185 this_cpu_write(bpf_cgroup_storage_info[i].task, current);
186 for_each_cgroup_storage_type(stype)
187 this_cpu_write(bpf_cgroup_storage_info[i].storage[stype],
199 static inline void bpf_cgroup_storage_unset(void)
203 for (i = BPF_CGROUP_STORAGE_NEST_MAX - 1; i >= 0; i--) {
204 if (likely(this_cpu_read(bpf_cgroup_storage_info[i].task) != current))
207 this_cpu_write(bpf_cgroup_storage_info[i].task, NULL);
212 struct bpf_cgroup_storage *
213 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
214 void *key, bool locked);
215 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
216 enum bpf_cgroup_storage_type stype);
217 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
218 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
219 struct cgroup *cgroup,
220 enum bpf_attach_type type);
221 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
222 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *map);
224 int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
225 int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
226 void *value, u64 flags);
228 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
229 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
232 if (cgroup_bpf_enabled) \
233 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
234 BPF_CGROUP_INET_INGRESS); \
239 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
242 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
243 typeof(sk) __sk = sk_to_full_sk(sk); \
244 if (sk_fullsock(__sk)) \
245 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
246 BPF_CGROUP_INET_EGRESS); \
251 #define BPF_CGROUP_RUN_SK_PROG(sk, type) \
254 if (cgroup_bpf_enabled) { \
255 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
260 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
261 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
263 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \
264 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
266 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
267 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
269 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
270 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
272 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
275 if (cgroup_bpf_enabled) \
276 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
281 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
284 if (cgroup_bpf_enabled) { \
286 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
293 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
294 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
296 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
297 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
299 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
300 sk->sk_prot->pre_connect)
302 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
303 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
305 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
306 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
308 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
309 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
311 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
312 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
314 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
315 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
317 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
318 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
320 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
321 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
323 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
324 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
326 /* The SOCK_OPS"_SK" macro should be used when sock_ops->sk is not a
327 * fullsock and its parent fullsock cannot be traced by
330 * e.g. sock_ops->sk is a request_sock and it is under syncookie mode.
331 * Its listener-sk is not attached to the rsk_listener.
332 * In this case, the caller holds the listener-sk (unlocked),
333 * set its sock_ops->sk to req_sk, and call this SOCK_OPS"_SK" with
334 * the listener-sk such that the cgroup-bpf-progs of the
335 * listener-sk will be run.
337 * Regardless of syncookie mode or not,
338 * calling bpf_setsockopt on listener-sk will not make sense anyway,
339 * so passing 'sock_ops->sk == req_sk' to the bpf prog is appropriate here.
341 #define BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(sock_ops, sk) \
344 if (cgroup_bpf_enabled) \
345 __ret = __cgroup_bpf_run_filter_sock_ops(sk, \
347 BPF_CGROUP_SOCK_OPS); \
351 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
354 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
355 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
356 if (__sk && sk_fullsock(__sk)) \
357 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
359 BPF_CGROUP_SOCK_OPS); \
364 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
367 if (cgroup_bpf_enabled) \
368 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
370 BPF_CGROUP_DEVICE); \
376 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos) \
379 if (cgroup_bpf_enabled) \
380 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
382 BPF_CGROUP_SYSCTL); \
386 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
390 if (cgroup_bpf_enabled) \
391 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \
398 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \
401 if (cgroup_bpf_enabled) \
402 get_user(__ret, optlen); \
406 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen, \
407 max_optlen, retval) \
409 int __ret = retval; \
410 if (cgroup_bpf_enabled) \
411 if (!(sock)->sk_prot->bpf_bypass_getsockopt || \
412 !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
413 tcp_bpf_bypass_getsockopt, \
415 __ret = __cgroup_bpf_run_filter_getsockopt( \
416 sock, level, optname, optval, optlen, \
417 max_optlen, retval); \
421 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
424 int __ret = retval; \
425 if (cgroup_bpf_enabled) \
426 __ret = __cgroup_bpf_run_filter_getsockopt_kern( \
427 sock, level, optname, optval, optlen, retval); \
431 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
432 enum bpf_prog_type ptype, struct bpf_prog *prog);
433 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
434 enum bpf_prog_type ptype);
435 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
436 int cgroup_bpf_prog_query(const union bpf_attr *attr,
437 union bpf_attr __user *uattr);
441 struct cgroup_bpf {};
442 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
443 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
445 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
446 enum bpf_prog_type ptype,
447 struct bpf_prog *prog)
452 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
453 enum bpf_prog_type ptype)
458 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
459 struct bpf_prog *prog)
464 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
465 union bpf_attr __user *uattr)
470 static inline int bpf_cgroup_storage_set(
471 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) { return 0; }
472 static inline void bpf_cgroup_storage_unset(void) {}
473 static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
474 struct bpf_map *map) { return 0; }
475 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
476 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
477 static inline void bpf_cgroup_storage_free(
478 struct bpf_cgroup_storage *storage) {}
479 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
483 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
484 void *key, void *value, u64 flags) {
488 #define cgroup_bpf_enabled (0)
489 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) ({ 0; })
490 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
491 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
492 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
493 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
494 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
495 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
496 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
497 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
498 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
499 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
500 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
501 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
502 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
503 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
504 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
505 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
506 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
507 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
508 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
509 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
510 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
511 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, \
512 optlen, max_optlen, retval) ({ retval; })
513 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
514 optlen, retval) ({ retval; })
515 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
516 kernel_optval) ({ 0; })
518 #define for_each_cgroup_storage_type(stype) for (; false; )
520 #endif /* CONFIG_CGROUP_BPF */
522 #endif /* _BPF_CGROUP_H */