blob: 9a144ddbbc8f4145721894de61d97187b9724539 [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 Gushchinf292b872018-07-06 14:34:29 -07005#include <linux/errno.h>
Daniel Mack30070982016-11-23 16:52:26 +01006#include <linux/jump_label.h>
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -07007#include <linux/percpu.h>
Roman Gushchinde9cbba2018-08-02 14:27:18 -07008#include <linux/rbtree.h>
Daniel Mack30070982016-11-23 16:52:26 +01009#include <uapi/linux/bpf.h>
10
11struct sock;
Andrey Ignatov4fbac772018-03-30 15:08:02 -070012struct sockaddr;
Daniel Mack30070982016-11-23 16:52:26 +010013struct cgroup;
14struct sk_buff;
Roman Gushchinde9cbba2018-08-02 14:27:18 -070015struct bpf_map;
16struct bpf_prog;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070017struct bpf_sock_ops_kern;
Roman Gushchinde9cbba2018-08-02 14:27:18 -070018struct bpf_cgroup_storage;
Daniel Mack30070982016-11-23 16:52:26 +010019
20#ifdef CONFIG_CGROUP_BPF
21
22extern struct static_key_false cgroup_bpf_enabled_key;
23#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
24
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -070025DECLARE_PER_CPU(void*, bpf_cgroup_storage);
26
Roman Gushchinde9cbba2018-08-02 14:27:18 -070027struct bpf_cgroup_storage_map;
28
29struct bpf_storage_buffer {
30 struct rcu_head rcu;
31 char data[0];
32};
33
34struct bpf_cgroup_storage {
35 struct bpf_storage_buffer *buf;
36 struct bpf_cgroup_storage_map *map;
37 struct bpf_cgroup_storage_key key;
38 struct list_head list;
39 struct rb_node node;
40 struct rcu_head rcu;
41};
42
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070043struct bpf_prog_list {
44 struct list_head node;
45 struct bpf_prog *prog;
46};
47
48struct bpf_prog_array;
49
Daniel Mack30070982016-11-23 16:52:26 +010050struct cgroup_bpf {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070051 /* array of effective progs in this cgroup */
52 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
53
54 /* attached progs to this cgroup and attach flags
55 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
56 * have either zero or one element
57 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
Daniel Mack30070982016-11-23 16:52:26 +010058 */
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070059 struct list_head progs[MAX_BPF_ATTACH_TYPE];
60 u32 flags[MAX_BPF_ATTACH_TYPE];
61
62 /* temp storage for effective prog array used by prog_attach/detach */
63 struct bpf_prog_array __rcu *inactive;
Daniel Mack30070982016-11-23 16:52:26 +010064};
65
66void cgroup_bpf_put(struct cgroup *cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070067int cgroup_bpf_inherit(struct cgroup *cgrp);
Daniel Mack30070982016-11-23 16:52:26 +010068
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070069int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
70 enum bpf_attach_type type, u32 flags);
71int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
72 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070073int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
74 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010075
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070076/* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
77int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
78 enum bpf_attach_type type, u32 flags);
79int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
80 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070081int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
82 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010083
David Ahernb2cd1252016-12-01 08:48:03 -080084int __cgroup_bpf_run_filter_skb(struct sock *sk,
85 struct sk_buff *skb,
86 enum bpf_attach_type type);
Daniel Mack30070982016-11-23 16:52:26 +010087
David Ahern610236582016-12-01 08:48:04 -080088int __cgroup_bpf_run_filter_sk(struct sock *sk,
89 enum bpf_attach_type type);
90
Andrey Ignatov4fbac772018-03-30 15:08:02 -070091int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
92 struct sockaddr *uaddr,
Andrey Ignatov1cedee12018-05-25 08:55:23 -070093 enum bpf_attach_type type,
94 void *t_ctx);
Andrey Ignatov4fbac772018-03-30 15:08:02 -070095
Lawrence Brakmo40304b22017-06-30 20:02:40 -070096int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
97 struct bpf_sock_ops_kern *sock_ops,
98 enum bpf_attach_type type);
99
Roman Gushchinebc614f2017-11-05 08:15:32 -0500100int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
101 short access, enum bpf_attach_type type);
102
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -0700103static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage *storage)
104{
105 struct bpf_storage_buffer *buf;
106
107 if (!storage)
108 return;
109
110 buf = READ_ONCE(storage->buf);
111 this_cpu_write(bpf_cgroup_storage, &buf->data[0]);
112}
113
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700114struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog);
115void bpf_cgroup_storage_free(struct bpf_cgroup_storage *storage);
116void bpf_cgroup_storage_link(struct bpf_cgroup_storage *storage,
117 struct cgroup *cgroup,
118 enum bpf_attach_type type);
119void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage);
120int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *map);
121void bpf_cgroup_storage_release(struct bpf_prog *prog, struct bpf_map *map);
122
David Ahernb2cd1252016-12-01 08:48:03 -0800123/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
124#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
125({ \
126 int __ret = 0; \
127 if (cgroup_bpf_enabled) \
128 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
129 BPF_CGROUP_INET_INGRESS); \
130 \
131 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +0100132})
133
David Ahernb2cd1252016-12-01 08:48:03 -0800134#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
135({ \
136 int __ret = 0; \
137 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
138 typeof(sk) __sk = sk_to_full_sk(sk); \
139 if (sk_fullsock(__sk)) \
140 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
141 BPF_CGROUP_INET_EGRESS); \
142 } \
143 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +0100144})
145
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700146#define BPF_CGROUP_RUN_SK_PROG(sk, type) \
David Ahern610236582016-12-01 08:48:04 -0800147({ \
148 int __ret = 0; \
Yafang Shaoee078622018-02-23 14:58:41 +0800149 if (cgroup_bpf_enabled) { \
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700150 __ret = __cgroup_bpf_run_filter_sk(sk, type); \
David Ahern610236582016-12-01 08:48:04 -0800151 } \
152 __ret; \
153})
154
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700155#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
156 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
157
158#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
159 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
160
161#define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \
162 BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND)
163
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700164#define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
165({ \
166 int __ret = 0; \
167 if (cgroup_bpf_enabled) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700168 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
169 NULL); \
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700170 __ret; \
171})
172
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700173#define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700174({ \
175 int __ret = 0; \
176 if (cgroup_bpf_enabled) { \
177 lock_sock(sk); \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700178 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \
179 t_ctx); \
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700180 release_sock(sk); \
181 } \
182 __ret; \
183})
184
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700185#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
186 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
187
188#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
189 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
190
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700191#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
192 sk->sk_prot->pre_connect)
193
194#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
195 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
196
197#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
198 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
199
200#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700201 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700202
203#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700204 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL)
205
206#define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \
207 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx)
208
209#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
210 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700211
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700212#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
213({ \
214 int __ret = 0; \
215 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
216 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
WANG Congdf39a9f2017-07-17 11:42:55 -0700217 if (__sk && sk_fullsock(__sk)) \
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700218 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
219 sock_ops, \
220 BPF_CGROUP_SOCK_OPS); \
221 } \
222 __ret; \
223})
Roman Gushchinebc614f2017-11-05 08:15:32 -0500224
225#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
226({ \
227 int __ret = 0; \
228 if (cgroup_bpf_enabled) \
229 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
230 access, \
231 BPF_CGROUP_DEVICE); \
232 \
233 __ret; \
234})
Sean Youngfdb5c452018-06-19 00:04:24 +0100235int cgroup_bpf_prog_attach(const union bpf_attr *attr,
236 enum bpf_prog_type ptype, struct bpf_prog *prog);
237int cgroup_bpf_prog_detach(const union bpf_attr *attr,
238 enum bpf_prog_type ptype);
239int cgroup_bpf_prog_query(const union bpf_attr *attr,
240 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +0100241#else
242
Sean Youngfdb5c452018-06-19 00:04:24 +0100243struct bpf_prog;
Daniel Mack30070982016-11-23 16:52:26 +0100244struct cgroup_bpf {};
245static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700246static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
Daniel Mack30070982016-11-23 16:52:26 +0100247
Sean Youngfdb5c452018-06-19 00:04:24 +0100248static inline int cgroup_bpf_prog_attach(const union bpf_attr *attr,
249 enum bpf_prog_type ptype,
250 struct bpf_prog *prog)
251{
252 return -EINVAL;
253}
254
255static inline int cgroup_bpf_prog_detach(const union bpf_attr *attr,
256 enum bpf_prog_type ptype)
257{
258 return -EINVAL;
259}
260
261static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
262 union bpf_attr __user *uattr)
263{
264 return -EINVAL;
265}
266
Roman Gushchinaa0ad5b2018-08-02 14:27:19 -0700267static inline void bpf_cgroup_storage_set(struct bpf_cgroup_storage *storage) {}
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700268static inline int bpf_cgroup_storage_assign(struct bpf_prog *prog,
269 struct bpf_map *map) { return 0; }
270static inline void bpf_cgroup_storage_release(struct bpf_prog *prog,
271 struct bpf_map *map) {}
272static inline struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(
273 struct bpf_prog *prog) { return 0; }
274static inline void bpf_cgroup_storage_free(
275 struct bpf_cgroup_storage *storage) {}
276
Andrey Ignatov13193b02018-05-25 08:55:22 -0700277#define cgroup_bpf_enabled (0)
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700278#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
Daniel Mack30070982016-11-23 16:52:26 +0100279#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
280#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
David Ahern610236582016-12-01 08:48:04 -0800281#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700282#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
283#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700284#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })
285#define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; })
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700286#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
287#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
288#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
289#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
Andrey Ignatov1cedee12018-05-25 08:55:23 -0700290#define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
291#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700292#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
Roman Gushchinebc614f2017-11-05 08:15:32 -0500293#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
Daniel Mack30070982016-11-23 16:52:26 +0100294
295#endif /* CONFIG_CGROUP_BPF */
296
297#endif /* _BPF_CGROUP_H */