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
148 int __cgroup_bpf_run_filter_getsockopt_kern(struct sock *sk, int level,
149 int optname, void *optval,
150 int *optlen, int retval);
151
cgroup_storage_type(struct bpf_map * map)152 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
153 struct bpf_map *map)
154 {
155 if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
156 return BPF_CGROUP_STORAGE_PERCPU;
157
158 return BPF_CGROUP_STORAGE_SHARED;
159 }
160
161 struct bpf_cgroup_storage *
162 cgroup_storage_lookup(struct bpf_cgroup_storage_map *map,
163 void *key, bool locked);
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
173 int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *value);
174 int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
175 void *value, u64 flags);
176
177 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
178 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
179 ({ \
180 int __ret = 0; \
181 if (cgroup_bpf_enabled) \
182 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
183 BPF_CGROUP_INET_INGRESS); \
184 \
185 __ret; \
186 })
187
188 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
189 ({ \
190 int __ret = 0; \
191 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
192 typeof(sk) __sk = sk_to_full_sk(sk); \
193 if (sk_fullsock(__sk)) \
194 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
195 BPF_CGROUP_INET_EGRESS); \
196 } \
197 __ret; \
198 })
199
200 #define BPF_CGROUP_RUN_SK_PROG(sk, type) \
201 ({ \
202 int __ret = 0; \
203 if (cgroup_bpf_enabled) { \
204 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
205 } \
206 __ret; \
207 })
208
209 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
210 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
211
212 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \
213 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
214
215 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
216 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
217
218 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
219 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
220
221 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
222 ({ \
223 int __ret = 0; \
224 if (cgroup_bpf_enabled) \
225 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
226 NULL); \
227 __ret; \
228 })
229
230 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
231 ({ \
232 int __ret = 0; \
233 if (cgroup_bpf_enabled) { \
234 lock_sock(sk); \
235 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
236 t_ctx); \
237 release_sock(sk); \
238 } \
239 __ret; \
240 })
241
242 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
243 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
244
245 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
246 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
247
248 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
249 sk->sk_prot->pre_connect)
250
251 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
252 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
253
254 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
255 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
256
257 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
258 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
259
260 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
261 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
262
263 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
264 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
265
266 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
267 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
268
269 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
270 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
271
272 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
273 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
274
275 /* The SOCK_OPS"_SK" macro should be used when sock_ops->sk is not a
276 * fullsock and its parent fullsock cannot be traced by
277 * sk_to_full_sk().
278 *
279 * e.g. sock_ops->sk is a request_sock and it is under syncookie mode.
280 * Its listener-sk is not attached to the rsk_listener.
281 * In this case, the caller holds the listener-sk (unlocked),
282 * set its sock_ops->sk to req_sk, and call this SOCK_OPS"_SK" with
283 * the listener-sk such that the cgroup-bpf-progs of the
284 * listener-sk will be run.
285 *
286 * Regardless of syncookie mode or not,
287 * calling bpf_setsockopt on listener-sk will not make sense anyway,
288 * so passing 'sock_ops->sk == req_sk' to the bpf prog is appropriate here.
289 */
290 #define BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(sock_ops, sk) \
291 ({ \
292 int __ret = 0; \
293 if (cgroup_bpf_enabled) \
294 __ret = __cgroup_bpf_run_filter_sock_ops(sk, \
295 sock_ops, \
296 BPF_CGROUP_SOCK_OPS); \
297 __ret; \
298 })
299
300 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
301 ({ \
302 int __ret = 0; \
303 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
304 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
305 if (__sk && sk_fullsock(__sk)) \
306 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
307 sock_ops, \
308 BPF_CGROUP_SOCK_OPS); \
309 } \
310 __ret; \
311 })
312
313 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
314 ({ \
315 int __ret = 0; \
316 if (cgroup_bpf_enabled) \
317 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
318 access, \
319 BPF_CGROUP_DEVICE); \
320 \
321 __ret; \
322 })
323
324
325 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos) \
326 ({ \
327 int __ret = 0; \
328 if (cgroup_bpf_enabled) \
329 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
330 buf, count, pos, \
331 BPF_CGROUP_SYSCTL); \
332 __ret; \
333 })
334
335 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
336 kernel_optval) \
337 ({ \
338 int __ret = 0; \
339 if (cgroup_bpf_enabled) \
340 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level, \
341 optname, optval, \
342 optlen, \
343 kernel_optval); \
344 __ret; \
345 })
346
347 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \
348 ({ \
349 int __ret = 0; \
350 if (cgroup_bpf_enabled) \
351 get_user(__ret, optlen); \
352 __ret; \
353 })
354
355 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen, \
356 max_optlen, retval) \
357 ({ \
358 int __ret = retval; \
359 if (cgroup_bpf_enabled) \
360 if (!(sock)->sk_prot->bpf_bypass_getsockopt || \
361 !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, \
362 tcp_bpf_bypass_getsockopt, \
363 level, optname)) \
364 __ret = __cgroup_bpf_run_filter_getsockopt( \
365 sock, level, optname, optval, optlen, \
366 max_optlen, retval); \
367 __ret; \
368 })
369
370 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
371 optlen, retval) \
372 ({ \
373 int __ret = retval; \
374 if (cgroup_bpf_enabled) \
375 __ret = __cgroup_bpf_run_filter_getsockopt_kern( \
376 sock, level, optname, optval, optlen, retval); \
377 __ret; \
378 })
379
380 int cgroup_bpf_prog_attach(const union bpf_attr *attr,
381 enum bpf_prog_type ptype, struct bpf_prog *prog);
382 int cgroup_bpf_prog_detach(const union bpf_attr *attr,
383 enum bpf_prog_type ptype);
384 int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
385 int cgroup_bpf_prog_query(const union bpf_attr *attr,
386 union bpf_attr __user *uattr);
387 #else
388
389 struct bpf_prog;
390 struct cgroup_bpf {};
cgroup_bpf_inherit(struct cgroup * cgrp)391 static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
cgroup_bpf_offline(struct cgroup * cgrp)392 static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
393
cgroup_bpf_prog_attach(const union bpf_attr * attr,enum bpf_prog_type ptype,struct bpf_prog * prog)394 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
395 enum bpf_prog_type ptype,
396 struct bpf_prog *prog)
397 {
398 return -EINVAL;
399 }
400
cgroup_bpf_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)401 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
402 enum bpf_prog_type ptype)
403 {
404 return -EINVAL;
405 }
406
cgroup_bpf_link_attach(const union bpf_attr * attr,struct bpf_prog * prog)407 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
408 struct bpf_prog *prog)
409 {
410 return -EINVAL;
411 }
412
cgroup_bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)413 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
414 union bpf_attr __user *uattr)
415 {
416 return -EINVAL;
417 }
418
bpf_cgroup_storage_assign(struct bpf_prog_aux * aux,struct bpf_map * map)419 static inline int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux,
420 struct bpf_map *map) { return 0; }
bpf_cgroup_storage_alloc(struct bpf_prog * prog,enum bpf_cgroup_storage_type stype)421 static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
422 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return NULL; }
bpf_cgroup_storage_free(struct bpf_cgroup_storage * storage)423 static inline void bpf_cgroup_storage_free(
424 struct bpf_cgroup_storage *storage) {}
bpf_percpu_cgroup_storage_copy(struct bpf_map * map,void * key,void * value)425 static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key,
426 void *value) {
427 return 0;
428 }
bpf_percpu_cgroup_storage_update(struct bpf_map * map,void * key,void * value,u64 flags)429 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
430 void *key, void *value, u64 flags) {
431 return 0;
432 }
433
434 #define cgroup_bpf_enabled (0)
435 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) ({ 0; })
436 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
437 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
438 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
439 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
440 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
441 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
442 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
443 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
444 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
445 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
446 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
447 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
448 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
449 #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
450 #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
451 #define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
452 #define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
453 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
454 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
455 #define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
456 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
457 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, \
458 optlen, max_optlen, retval) ({ retval; })
459 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname, optval, \
460 optlen, retval) ({ retval; })
461 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen, \
462 kernel_optval) ({ 0; })
463
464 #define for_each_cgroup_storage_type(stype) for (; false; )
465
466 #endif /* CONFIG_CGROUP_BPF */
467
468 #endif /* _BPF_CGROUP_H */
469