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[MAX_BPF_ATTACH_TYPE];
28 #define cgroup_bpf_enabled(type) static_branch_unlikely(&cgroup_bpf_enabled_key[type])
30 #define for_each_cgroup_storage_type(stype) \
31 for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
33 struct bpf_cgroup_storage_map;
35 struct bpf_storage_buffer {
40 struct bpf_cgroup_storage {
42 struct bpf_storage_buffer *buf;
43 void __percpu *percpu_buf;
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;
53 struct bpf_cgroup_link {
55 struct cgroup *cgroup;
56 enum bpf_attach_type type;
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];
66 struct bpf_prog_array;
69 /* array of effective progs in this cgroup */
70 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
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
77 struct list_head progs[MAX_BPF_ATTACH_TYPE];
78 u32 flags[MAX_BPF_ATTACH_TYPE];
80 /* list of cgroup shared storages */
81 struct list_head storages;
83 /* temp storage for effective prog array used by prog_attach/detach */
84 struct bpf_prog_array *inactive;
86 /* reference counter used to detach bpf programs after cgroup removal */
87 struct percpu_ref refcnt;
89 /* cgroup_bpf is released using a work queue */
90 struct work_struct release_work;
93 int cgroup_bpf_inherit(struct cgroup *cgrp);
94 void cgroup_bpf_offline(struct cgroup *cgrp);
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);
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,
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);
116 int __cgroup_bpf_run_filter_skb(struct sock *sk,
118 enum bpf_attach_type type);
120 int __cgroup_bpf_run_filter_sk(struct sock *sk,
121 enum bpf_attach_type type);
123 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
124 struct sockaddr *uaddr,
125 enum bpf_attach_type type,
129 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
130 struct bpf_sock_ops_kern *sock_ops,
131 enum bpf_attach_type type);
133 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
134 short access, enum bpf_attach_type type);
136 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
137 struct ctl_table *table, int write,
138 char **buf, size_t *pcount, loff_t *ppos,
139 enum bpf_attach_type type);
141 int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level,
142 int *optname, char __user *optval,
143 int *optlen, char **kernel_optval);
144 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
145 int optname, char __user *optval,
146 int __user *optlen, int max_optlen,
149 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
150 int optname, void *optval,
151 int *optlen, int retval);
153 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
156 if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
157 return BPF_CGROUP_STORAGE_PERCPU;
159 return BPF_CGROUP_STORAGE_SHARED;
162 struct bpf_cgroup_storage *
163 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
164 void *key, bool locked);
165 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
166 enum bpf_cgroup_storage_type stype);
167 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
168 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
169 struct cgroup *cgroup,
170 enum bpf_attach_type type);
171 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
172 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *map);
174 int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
175 int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
176 void *value, u64 flags);
178 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
179 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
182 if (cgroup_bpf_enabled(BPF_CGROUP_INET_INGRESS)) \
183 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
184 BPF_CGROUP_INET_INGRESS); \
189 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
192 if (cgroup_bpf_enabled(BPF_CGROUP_INET_EGRESS) && sk && sk == skb->sk) { \
193 typeof(sk) __sk = sk_to_full_sk(sk); \
194 if (sk_fullsock(__sk)) \
195 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
196 BPF_CGROUP_INET_EGRESS); \
201 #define BPF_CGROUP_RUN_SK_PROG(sk, type) \
204 if (cgroup_bpf_enabled(type)) { \
205 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
210 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
211 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
213 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \
214 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
216 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
217 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
219 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
220 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
222 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
224 u32 __unused_flags; \
226 if (cgroup_bpf_enabled(type)) \
227 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
233 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
235 u32 __unused_flags; \
237 if (cgroup_bpf_enabled(type)) { \
239 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
247 /* BPF_CGROUP_INET4_BIND and BPF_CGROUP_INET6_BIND can return extra flags
248 * via upper bits of return code. The only flag that is supported
249 * (at bit position 0) is to indicate CAP_NET_BIND_SERVICE capability check
250 * should be bypassed (BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE).
252 #define BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, type, bind_flags) \
256 if (cgroup_bpf_enabled(type)) { \
258 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
261 if (__flags & BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE) \
262 *bind_flags |= BIND_NO_CAP_NET_BIND_SERVICE; \
267 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) \
268 ((cgroup_bpf_enabled(BPF_CGROUP_INET4_CONNECT) || \
269 cgroup_bpf_enabled(BPF_CGROUP_INET6_CONNECT)) && \
270 (sk)->sk_prot->pre_connect)
272 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
273 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
275 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
276 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
278 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
279 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
281 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
282 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
284 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
285 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
287 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
288 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
290 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
291 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
293 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
294 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
296 /* The SOCK_OPS"_SK" macro should be used when sock_ops->sk is not a
297 * fullsock and its parent fullsock cannot be traced by
300 * e.g. sock_ops->sk is a request_sock and it is under syncookie mode.
301 * Its listener-sk is not attached to the rsk_listener.
302 * In this case, the caller holds the listener-sk (unlocked),
303 * set its sock_ops->sk to req_sk, and call this SOCK_OPS"_SK" with
304 * the listener-sk such that the cgroup-bpf-progs of the
305 * listener-sk will be run.
307 * Regardless of syncookie mode or not,
308 * calling bpf_setsockopt on listener-sk will not make sense anyway,
309 * so passing 'sock_ops->sk == req_sk' to the bpf prog is appropriate here.
311 #define BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(sock_ops, sk) \
314 if (cgroup_bpf_enabled(BPF_CGROUP_SOCK_OPS)) \
315 __ret = __cgroup_bpf_run_filter_sock_ops(sk, \
317 BPF_CGROUP_SOCK_OPS); \
321 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
324 if (cgroup_bpf_enabled(BPF_CGROUP_SOCK_OPS) && (sock_ops)->sk) { \
325 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
326 if (__sk && sk_fullsock(__sk)) \
327 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
329 BPF_CGROUP_SOCK_OPS); \
334 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
337 if (cgroup_bpf_enabled(BPF_CGROUP_DEVICE)) \
338 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
340 BPF_CGROUP_DEVICE); \
346 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos) \
349 if (cgroup_bpf_enabled(BPF_CGROUP_SYSCTL)) \
350 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
352 BPF_CGROUP_SYSCTL); \
356 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
360 if (cgroup_bpf_enabled(BPF_CGROUP_SETSOCKOPT)) \
361 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \
368 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \
371 if (cgroup_bpf_enabled(BPF_CGROUP_GETSOCKOPT)) \
372 get_user(__ret, optlen); \
376 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen, \
377 max_optlen, retval) \
379 int __ret = retval; \
380 if (cgroup_bpf_enabled(BPF_CGROUP_GETSOCKOPT)) \
381 if (!(sock)->sk_prot->bpf_bypass_getsockopt || \
382 !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
383 tcp_bpf_bypass_getsockopt, \
385 __ret = __cgroup_bpf_run_filter_getsockopt( \
386 sock, level, optname, optval, optlen, \
387 max_optlen, retval); \
391 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
394 int __ret = retval; \
395 if (cgroup_bpf_enabled(BPF_CGROUP_GETSOCKOPT)) \
396 __ret = __cgroup_bpf_run_filter_getsockopt_kern( \
397 sock, level, optname, optval, optlen, retval); \
401 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
402 enum bpf_prog_type ptype, struct bpf_prog *prog);
403 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
404 enum bpf_prog_type ptype);
405 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
406 int cgroup_bpf_prog_query(const union bpf_attr *attr,
407 union bpf_attr __user *uattr);
410 struct cgroup_bpf {};
411 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
412 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
414 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
415 enum bpf_prog_type ptype,
416 struct bpf_prog *prog)
421 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
422 enum bpf_prog_type ptype)
427 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
428 struct bpf_prog *prog)
433 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
434 union bpf_attr __user *uattr)
439 static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
440 struct bpf_map *map) { return 0; }
441 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
442 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
443 static inline void bpf_cgroup_storage_free(
444 struct bpf_cgroup_storage *storage) {}
445 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
449 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
450 void *key, void *value, u64 flags) {
454 #define cgroup_bpf_enabled(type) (0)
455 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) ({ 0; })
456 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
457 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
458 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
459 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
460 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
461 #define BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, type, flags) ({ 0; })
462 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
463 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
464 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
465 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
466 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
467 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
468 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
469 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
470 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
471 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
472 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
473 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
474 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
475 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
476 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, \
477 optlen, max_optlen, retval) ({ retval; })
478 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
479 optlen, retval) ({ retval; })
480 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
481 kernel_optval) ({ 0; })
483 #define for_each_cgroup_storage_type(stype) for (; false; )
485 #endif /* CONFIG_CGROUP_BPF */
487 #endif /* _BPF_CGROUP_H */