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;
24 #ifdef CONFIG_CGROUP_BPF
26 extern struct static_key_false cgroup_bpf_enabled_key;
27 #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
29 DECLARE_PER_CPU(struct bpf_cgroup_storage*,
30 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
32 #define for_each_cgroup_storage_type(stype) \
33 for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
35 struct bpf_cgroup_storage_map;
37 struct bpf_storage_buffer {
42 struct bpf_cgroup_storage {
44 struct bpf_storage_buffer *buf;
45 void __percpu *percpu_buf;
47 struct bpf_cgroup_storage_map *map;
48 struct bpf_cgroup_storage_key key;
49 struct list_head list;
54 struct bpf_cgroup_link {
56 struct cgroup *cgroup;
57 enum bpf_attach_type type;
60 struct bpf_prog_list {
61 struct list_head node;
62 struct bpf_prog *prog;
63 struct bpf_cgroup_link *link;
64 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
67 struct bpf_prog_array;
70 /* array of effective progs in this cgroup */
71 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
73 /* attached progs to this cgroup and attach flags
74 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
75 * have either zero or one element
76 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
78 struct list_head progs[MAX_BPF_ATTACH_TYPE];
79 u32 flags[MAX_BPF_ATTACH_TYPE];
81 /* temp storage for effective prog array used by prog_attach/detach */
82 struct bpf_prog_array *inactive;
84 /* reference counter used to detach bpf programs after cgroup removal */
85 struct percpu_ref refcnt;
87 /* cgroup_bpf is released using a work queue */
88 struct work_struct release_work;
91 int cgroup_bpf_inherit(struct cgroup *cgrp);
92 void cgroup_bpf_offline(struct cgroup *cgrp);
94 int __cgroup_bpf_attach(struct cgroup *cgrp,
95 struct bpf_prog *prog, struct bpf_prog *replace_prog,
96 struct bpf_cgroup_link *link,
97 enum bpf_attach_type type, u32 flags);
98 int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
99 struct bpf_cgroup_link *link,
100 enum bpf_attach_type type);
101 int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
102 union bpf_attr __user *uattr);
104 /* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
105 int cgroup_bpf_attach(struct cgroup *cgrp,
106 struct bpf_prog *prog, struct bpf_prog *replace_prog,
107 struct bpf_cgroup_link *link, enum bpf_attach_type type,
109 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
110 enum bpf_attach_type type);
111 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
112 union bpf_attr __user *uattr);
114 int __cgroup_bpf_run_filter_skb(struct sock *sk,
116 enum bpf_attach_type type);
118 int __cgroup_bpf_run_filter_sk(struct sock *sk,
119 enum bpf_attach_type type);
121 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
122 struct sockaddr *uaddr,
123 enum bpf_attach_type type,
126 int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
127 struct bpf_sock_ops_kern *sock_ops,
128 enum bpf_attach_type type);
130 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
131 short access, enum bpf_attach_type type);
133 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
134 struct ctl_table *table, int write,
135 void **buf, size_t *pcount, loff_t *ppos,
136 enum bpf_attach_type type);
138 int __cgroup_bpf_run_filter_setsockopt(struct sock *sock, int *level,
139 int *optname, char __user *optval,
140 int *optlen, char **kernel_optval);
141 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
142 int optname, char __user *optval,
143 int __user *optlen, int max_optlen,
146 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
149 if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
150 return BPF_CGROUP_STORAGE_PERCPU;
152 return BPF_CGROUP_STORAGE_SHARED;
155 static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage
156 *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
158 enum bpf_cgroup_storage_type stype;
160 for_each_cgroup_storage_type(stype)
161 this_cpu_write(bpf_cgroup_storage[stype], storage[stype]);
164 struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
165 enum bpf_cgroup_storage_type stype);
166 void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
167 void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
168 struct cgroup *cgroup,
169 enum bpf_attach_type type);
170 void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
171 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *map);
172 void bpf_cgroup_storage_release(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) \
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 && 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) { \
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) \
225 if (cgroup_bpf_enabled) \
226 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
231 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
234 if (cgroup_bpf_enabled) { \
236 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
243 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
244 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
246 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
247 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
249 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
250 sk->sk_prot->pre_connect)
252 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
253 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
255 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
256 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
258 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
259 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
261 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
262 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
264 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
265 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
267 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
268 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
270 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
271 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
273 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
274 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
276 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
279 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
280 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
281 if (__sk && sk_fullsock(__sk)) \
282 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
284 BPF_CGROUP_SOCK_OPS); \
289 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
292 if (cgroup_bpf_enabled) \
293 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
295 BPF_CGROUP_DEVICE); \
301 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos) \
304 if (cgroup_bpf_enabled) \
305 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
307 BPF_CGROUP_SYSCTL); \
311 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
315 if (cgroup_bpf_enabled) \
316 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \
323 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \
326 if (cgroup_bpf_enabled) \
327 get_user(__ret, optlen); \
331 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen, \
332 max_optlen, retval) \
334 int __ret = retval; \
335 if (cgroup_bpf_enabled) \
336 __ret = __cgroup_bpf_run_filter_getsockopt(sock, level, \
338 optlen, max_optlen, \
343 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
344 enum bpf_prog_type ptype, struct bpf_prog *prog);
345 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
346 enum bpf_prog_type ptype);
347 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
348 int cgroup_bpf_prog_query(const union bpf_attr *attr,
349 union bpf_attr __user *uattr);
353 struct cgroup_bpf {};
354 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
355 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
357 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
358 enum bpf_prog_type ptype,
359 struct bpf_prog *prog)
364 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
365 enum bpf_prog_type ptype)
370 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
371 struct bpf_prog *prog)
376 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
377 union bpf_attr __user *uattr)
382 static inline void bpf_cgroup_storage_set(
383 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) {}
384 static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
385 struct bpf_map *map) { return 0; }
386 static inline void bpf_cgroup_storage_release(struct bpf_prog_aux *aux,
387 struct bpf_map *map) {}
388 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
389 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
390 static inline void bpf_cgroup_storage_free(
391 struct bpf_cgroup_storage *storage) {}
392 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
396 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
397 void *key, void *value, u64 flags) {
401 #define cgroup_bpf_enabled (0)
402 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) ({ 0; })
403 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
404 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
405 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
406 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
407 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
408 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
409 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
410 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
411 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
412 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
413 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
414 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
415 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
416 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
417 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
418 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
419 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
420 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
421 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
422 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
423 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
424 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, \
425 optlen, max_optlen, retval) ({ retval; })
426 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
427 kernel_optval) ({ 0; })
429 #define for_each_cgroup_storage_type(stype) for (; false; )
431 #endif /* CONFIG_CGROUP_BPF */
433 #endif /* _BPF_CGROUP_H */