]> Git Repo - linux.git/blob - include/linux/bpf-cgroup.h
hinic: add firmware update support
[linux.git] / include / linux / bpf-cgroup.h
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
24 #ifdef CONFIG_CGROUP_BPF
25
26 extern struct static_key_false cgroup_bpf_enabled_key;
27 #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
28
29 DECLARE_PER_CPU(struct bpf_cgroup_storage*,
30                 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
31
32 #define for_each_cgroup_storage_type(stype) \
33         for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
34
35 struct bpf_cgroup_storage_map;
36
37 struct bpf_storage_buffer {
38         struct rcu_head rcu;
39         char data[];
40 };
41
42 struct bpf_cgroup_storage {
43         union {
44                 struct bpf_storage_buffer *buf;
45                 void __percpu *percpu_buf;
46         };
47         struct bpf_cgroup_storage_map *map;
48         struct bpf_cgroup_storage_key key;
49         struct list_head list;
50         struct rb_node node;
51         struct rcu_head rcu;
52 };
53
54 struct bpf_cgroup_link {
55         struct bpf_link link;
56         struct cgroup *cgroup;
57         enum bpf_attach_type type;
58 };
59
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];
65 };
66
67 struct bpf_prog_array;
68
69 struct cgroup_bpf {
70         /* array of effective progs in this cgroup */
71         struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
72
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
77          */
78         struct list_head progs[MAX_BPF_ATTACH_TYPE];
79         u32 flags[MAX_BPF_ATTACH_TYPE];
80
81         /* temp storage for effective prog array used by prog_attach/detach */
82         struct bpf_prog_array *inactive;
83
84         /* reference counter used to detach bpf programs after cgroup removal */
85         struct percpu_ref refcnt;
86
87         /* cgroup_bpf is released using a work queue */
88         struct work_struct release_work;
89 };
90
91 int cgroup_bpf_inherit(struct cgroup *cgrp);
92 void cgroup_bpf_offline(struct cgroup *cgrp);
93
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);
103
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,
108                       u32 flags);
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);
113
114 int __cgroup_bpf_run_filter_skb(struct sock *sk,
115                                 struct sk_buff *skb,
116                                 enum bpf_attach_type type);
117
118 int __cgroup_bpf_run_filter_sk(struct sock *sk,
119                                enum bpf_attach_type type);
120
121 int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
122                                       struct sockaddr *uaddr,
123                                       enum bpf_attach_type type,
124                                       void *t_ctx);
125
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);
129
130 int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
131                                       short access, enum bpf_attach_type type);
132
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);
137
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,
144                                        int retval);
145
146 static inline enum bpf_cgroup_storage_type cgroup_storage_type(
147         struct bpf_map *map)
148 {
149         if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
150                 return BPF_CGROUP_STORAGE_PERCPU;
151
152         return BPF_CGROUP_STORAGE_SHARED;
153 }
154
155 static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage
156                                           *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
157 {
158         enum bpf_cgroup_storage_type stype;
159
160         for_each_cgroup_storage_type(stype)
161                 this_cpu_write(bpf_cgroup_storage[stype], storage[stype]);
162 }
163
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);
173
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);
177
178 /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
179 #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb)                             \
180 ({                                                                            \
181         int __ret = 0;                                                        \
182         if (cgroup_bpf_enabled)                                               \
183                 __ret = __cgroup_bpf_run_filter_skb(sk, skb,                  \
184                                                     BPF_CGROUP_INET_INGRESS); \
185                                                                               \
186         __ret;                                                                \
187 })
188
189 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)                               \
190 ({                                                                             \
191         int __ret = 0;                                                         \
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); \
197         }                                                                      \
198         __ret;                                                                 \
199 })
200
201 #define BPF_CGROUP_RUN_SK_PROG(sk, type)                                       \
202 ({                                                                             \
203         int __ret = 0;                                                         \
204         if (cgroup_bpf_enabled) {                                              \
205                 __ret = __cgroup_bpf_run_filter_sk(sk, type);                  \
206         }                                                                      \
207         __ret;                                                                 \
208 })
209
210 #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk)                                      \
211         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
212
213 #define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk)                              \
214         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
215
216 #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk)                                \
217         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
218
219 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk)                                \
220         BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
221
222 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type)                                \
223 ({                                                                             \
224         int __ret = 0;                                                         \
225         if (cgroup_bpf_enabled)                                                \
226                 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type,     \
227                                                           NULL);               \
228         __ret;                                                                 \
229 })
230
231 #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx)                    \
232 ({                                                                             \
233         int __ret = 0;                                                         \
234         if (cgroup_bpf_enabled) {                                              \
235                 lock_sock(sk);                                                 \
236                 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type,     \
237                                                           t_ctx);              \
238                 release_sock(sk);                                              \
239         }                                                                      \
240         __ret;                                                                 \
241 })
242
243 #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr)                              \
244         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
245
246 #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr)                              \
247         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
248
249 #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
250                                             sk->sk_prot->pre_connect)
251
252 #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr)                           \
253         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
254
255 #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr)                           \
256         BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
257
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)
260
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)
263
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)
266
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)
269
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)
272
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)
275
276 #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops)                                 \
277 ({                                                                             \
278         int __ret = 0;                                                         \
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,         \
283                                                                  sock_ops,     \
284                                                          BPF_CGROUP_SOCK_OPS); \
285         }                                                                      \
286         __ret;                                                                 \
287 })
288
289 #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access)         \
290 ({                                                                            \
291         int __ret = 0;                                                        \
292         if (cgroup_bpf_enabled)                                               \
293                 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
294                                                           access,             \
295                                                           BPF_CGROUP_DEVICE); \
296                                                                               \
297         __ret;                                                                \
298 })
299
300
301 #define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, count, pos)  \
302 ({                                                                             \
303         int __ret = 0;                                                         \
304         if (cgroup_bpf_enabled)                                                \
305                 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write,     \
306                                                        buf, count, pos,        \
307                                                        BPF_CGROUP_SYSCTL);     \
308         __ret;                                                                 \
309 })
310
311 #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname, optval, optlen,   \
312                                        kernel_optval)                          \
313 ({                                                                             \
314         int __ret = 0;                                                         \
315         if (cgroup_bpf_enabled)                                                \
316                 __ret = __cgroup_bpf_run_filter_setsockopt(sock, level,        \
317                                                            optname, optval,    \
318                                                            optlen,             \
319                                                            kernel_optval);     \
320         __ret;                                                                 \
321 })
322
323 #define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen)                               \
324 ({                                                                             \
325         int __ret = 0;                                                         \
326         if (cgroup_bpf_enabled)                                                \
327                 get_user(__ret, optlen);                                       \
328         __ret;                                                                 \
329 })
330
331 #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname, optval, optlen,   \
332                                        max_optlen, retval)                     \
333 ({                                                                             \
334         int __ret = retval;                                                    \
335         if (cgroup_bpf_enabled)                                                \
336                 __ret = __cgroup_bpf_run_filter_getsockopt(sock, level,        \
337                                                            optname, optval,    \
338                                                            optlen, max_optlen, \
339                                                            retval);            \
340         __ret;                                                                 \
341 })
342
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);
350 #else
351
352 struct bpf_prog;
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) {}
356
357 static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
358                                          enum bpf_prog_type ptype,
359                                          struct bpf_prog *prog)
360 {
361         return -EINVAL;
362 }
363
364 static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
365                                          enum bpf_prog_type ptype)
366 {
367         return -EINVAL;
368 }
369
370 static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
371                                          struct bpf_prog *prog)
372 {
373         return -EINVAL;
374 }
375
376 static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
377                                         union bpf_attr __user *uattr)
378 {
379         return -EINVAL;
380 }
381
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,
393                                                  void *value) {
394         return 0;
395 }
396 static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
397                                         void *key, void *value, u64 flags) {
398         return 0;
399 }
400
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; })
428
429 #define for_each_cgroup_storage_type(stype) for (; false; )
430
431 #endif /* CONFIG_CGROUP_BPF */
432
433 #endif /* _BPF_CGROUP_H */
This page took 0.066865 seconds and 4 git commands to generate.