blob: 7e0c9a1d48b78cb1b01b76033e646b8a0c48a3d3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Daniel Mack30070982016-11-23 16:52:26 +01002#ifndef _BPF_CGROUP_H
3#define _BPF_CGROUP_H
4
Roman Gushchin8bad74f2018-09-28 14:45:36 +00005#include <linux/bpf.h>
Roman Gushchinf292b872018-07-06 14:34:29 -07006#include <linux/errno.h>
Daniel Mack30070982016-11-23 16:52:26 +01007#include <linux/jump_label.h>
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -07008#include <linux/percpu.h>
Roman Gushchinde9cbba2018-08-02 14:27:18 -07009#include <linux/rbtree.h>
Daniel Mack30070982016-11-23 16:52:26 +010010#include <uapi/linux/bpf.h>
11
12struct sock;
Andrey Ignatov4fbac772018-03-30 15:08:02 -070013struct sockaddr;
Daniel Mack30070982016-11-23 16:52:26 +010014struct cgroup;
15struct sk_buff;
Roman Gushchinde9cbba2018-08-02 14:27:18 -070016struct bpf_map;
17struct bpf_prog;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070018struct bpf_sock_ops_kern;
Roman Gushchinde9cbba2018-08-02 14:27:18 -070019struct bpf_cgroup_storage;
Daniel Mack30070982016-11-23 16:52:26 +010020
21#ifdef CONFIG_CGROUP_BPF
22
23extern struct static_key_false cgroup_bpf_enabled_key;
24#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
25
Roman Gushchinf294b372018-09-28 14:45:40 +000026DECLARE_PER_CPU(struct bpf_cgroup_storage*,
27 bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
Roman Gushchin8bad74f2018-09-28 14:45:36 +000028
29#define for_each_cgroup_storage_type(stype) \
30 for (stype = 0; stype < MAX_BPF_CGROUP_STORAGE_TYPE; stype++)
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -070031
Roman Gushchinde9cbba2018-08-02 14:27:18 -070032struct bpf_cgroup_storage_map;
33
34struct bpf_storage_buffer {
35 struct rcu_head rcu;
36 char data[0];
37};
38
39struct bpf_cgroup_storage {
40 struct bpf_storage_buffer *buf;
41 struct bpf_cgroup_storage_map *map;
42 struct bpf_cgroup_storage_key key;
43 struct list_head list;
44 struct rb_node node;
45 struct rcu_head rcu;
46};
47
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070048struct bpf_prog_list {
49 struct list_head node;
50 struct bpf_prog *prog;
Roman Gushchin8bad74f2018-09-28 14:45:36 +000051 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070052};
53
54struct bpf_prog_array;
55
Daniel Mack30070982016-11-23 16:52:26 +010056struct cgroup_bpf {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070057 /* array of effective progs in this cgroup */
58 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
59
60 /* attached progs to this cgroup and attach flags
61 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
62 * have either zero or one element
63 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
Daniel Mack30070982016-11-23 16:52:26 +010064 */
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070065 struct list_head progs[MAX_BPF_ATTACH_TYPE];
66 u32 flags[MAX_BPF_ATTACH_TYPE];
67
68 /* temp storage for effective prog array used by prog_attach/detach */
69 struct bpf_prog_array __rcu *inactive;
Daniel Mack30070982016-11-23 16:52:26 +010070};
71
72void cgroup_bpf_put(struct cgroup *cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070073int cgroup_bpf_inherit(struct cgroup *cgrp);
Daniel Mack30070982016-11-23 16:52:26 +010074
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070075int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
76 enum bpf_attach_type type, u32 flags);
77int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
78 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070079int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
80 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010081
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070082/* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
83int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
84 enum bpf_attach_type type, u32 flags);
85int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
86 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070087int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
88 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010089
David Ahernb2cd1252016-12-01 08:48:03 -080090int __cgroup_bpf_run_filter_skb(struct sock *sk,
91 struct sk_buff *skb,
92 enum bpf_attach_type type);
Daniel Mack30070982016-11-23 16:52:26 +010093
David Ahern610236582016-12-01 08:48:04 -080094int __cgroup_bpf_run_filter_sk(struct sock *sk,
95 enum bpf_attach_type type);
96
Andrey Ignatov4fbac772018-03-30 15:08:02 -070097int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
98 struct sockaddr *uaddr,
Andrey Ignatov1cedee12018-05-25 08:55:23 -070099 enum bpf_attach_type type,
100 void *t_ctx);
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700101
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700102int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
103 struct bpf_sock_ops_kern *sock_ops,
104 enum bpf_attach_type type);
105
Roman Gushchinebc614f2017-11-05 08:15:32 -0500106int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
107 short access, enum bpf_attach_type type);
108
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000109static inline enum bpf_cgroup_storage_type cgroup_storage_type(
110 struct bpf_map *map)
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -0700111{
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000112 return BPF_CGROUP_STORAGE_SHARED;
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -0700113}
114
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000115static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage
116 *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
117{
118 enum bpf_cgroup_storage_type stype;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000119
Roman Gushchinf294b372018-09-28 14:45:40 +0000120 for_each_cgroup_storage_type(stype)
121 this_cpu_write(bpf_cgroup_storage[stype], storage[stype]);
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000122}
123
124struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog,
125 enum bpf_cgroup_storage_type stype);
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700126void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
127void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
128 struct cgroup *cgroup,
129 enum bpf_attach_type type);
130void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
131int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *map);
132void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *map);
133
David Ahernb2cd1252016-12-01 08:48:03 -0800134/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
135#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
136({ \
137 int __ret = 0; \
138 if (cgroup_bpf_enabled) \
139 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
140 BPF_CGROUP_INET_INGRESS); \
141 \
142 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +0100143})
144
David Ahernb2cd1252016-12-01 08:48:03 -0800145#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
146({ \
147 int __ret = 0; \
148 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
149 typeof(sk) __sk = sk_to_full_sk(sk); \
150 if (sk_fullsock(__sk)) \
151 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
152 BPF_CGROUP_INET_EGRESS); \
153 } \
154 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +0100155})
156
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700157#define BPF_CGROUP_RUN_SK_PROG(sk, type) \
David Ahern610236582016-12-01 08:48:04 -0800158({ \
159 int __ret = 0; \
Yafang Shaoee078622018-02-23 14:58:41 +0800160 if (cgroup_bpf_enabled) { \
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700161 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
David Ahern610236582016-12-01 08:48:04 -0800162 } \
163 __ret; \
164})
165
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700166#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
167 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
168
169#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
170 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
171
172#define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
173 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
174
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700175#define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
176({ \
177 int __ret = 0; \
178 if (cgroup_bpf_enabled) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700179 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
180 NULL); \
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700181 __ret; \
182})
183
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700184#define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700185({ \
186 int __ret = 0; \
187 if (cgroup_bpf_enabled) { \
188 lock_sock(sk); \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700189 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
190 t_ctx); \
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700191 release_sock(sk); \
192 } \
193 __ret; \
194})
195
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700196#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
197 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
198
199#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
200 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
201
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700202#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
203 sk->sk_prot->pre_connect)
204
205#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
206 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
207
208#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
209 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
210
211#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700212 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700213
214#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700215 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
216
217#define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
218 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
219
220#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
221 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700222
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700223#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
224({ \
225 int __ret = 0; \
226 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
227 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
WANG Congdf39a9f2017-07-17 11:42:55 -0700228 if (__sk && sk_fullsock(__sk)) \
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700229 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
230 sock_ops, \
231 BPF_CGROUP_SOCK_OPS); \
232 } \
233 __ret; \
234})
Roman Gushchinebc614f2017-11-05 08:15:32 -0500235
236#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
237({ \
238 int __ret = 0; \
239 if (cgroup_bpf_enabled) \
240 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
241 access, \
242 BPF_CGROUP_DEVICE); \
243 \
244 __ret; \
245})
Sean Youngfdb5c452018-06-19 00:04:24 +0100246int cgroup_bpf_prog_attach(const union bpf_attr *attr,
247 enum bpf_prog_type ptype, struct bpf_prog *prog);
248int cgroup_bpf_prog_detach(const union bpf_attr *attr,
249 enum bpf_prog_type ptype);
250int cgroup_bpf_prog_query(const union bpf_attr *attr,
251 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +0100252#else
253
Sean Youngfdb5c452018-06-19 00:04:24 +0100254struct bpf_prog;
Daniel Mack30070982016-11-23 16:52:26 +0100255struct cgroup_bpf {};
256static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700257static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
Daniel Mack30070982016-11-23 16:52:26 +0100258
Sean Youngfdb5c452018-06-19 00:04:24 +0100259static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
260 enum bpf_prog_type ptype,
261 struct bpf_prog *prog)
262{
263 return -EINVAL;
264}
265
266static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
267 enum bpf_prog_type ptype)
268{
269 return -EINVAL;
270}
271
272static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
273 union bpf_attr __user *uattr)
274{
275 return -EINVAL;
276}
277
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000278static inline void bpf_cgroup_storage_set(
279 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE]) {}
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700280static inline int bpf_cgroup_storage_assign(struct bpf_prog *prog,
281 struct bpf_map *map) { return 0; }
282static inline void bpf_cgroup_storage_release(struct bpf_prog *prog,
283 struct bpf_map *map) {}
284static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000285 struct bpf_prog *prog, enum bpf_cgroup_storage_type stype) { return 0; }
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700286static inline void bpf_cgroup_storage_free(
287 struct bpf_cgroup_storage *storage) {}
288
Andrey Ignatov13193b02018-05-25 08:55:22 -0700289#define cgroup_bpf_enabled (0)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700290#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
Daniel Mack30070982016-11-23 16:52:26 +0100291#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
292#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
David Ahern610236582016-12-01 08:48:04 -0800293#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700294#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
295#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700296#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
297#define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700298#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
299#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
300#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
301#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700302#define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
303#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700304#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
Roman Gushchinebc614f2017-11-05 08:15:32 -0500305#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
Daniel Mack30070982016-11-23 16:52:26 +0100306
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000307#define for_each_cgroup_storage_type(stype) for (; false; )
308
Daniel Mack30070982016-11-23 16:52:26 +0100309#endif /* CONFIG_CGROUP_BPF */
310
311#endif /* _BPF_CGROUP_H */