blob: c6ab295e6dcbcb2b8e10ba7637a8bd2e5b73261d [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
Daniel Mack30070982016-11-23 16:52:26 +01005#include <linux/jump_label.h>
6#include <uapi/linux/bpf.h>
7
8struct sock;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07009struct sockaddr;
Daniel Mack30070982016-11-23 16:52:26 +010010struct cgroup;
11struct sk_buff;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070012struct bpf_sock_ops_kern;
Daniel Mack30070982016-11-23 16:52:26 +010013
14#ifdef CONFIG_CGROUP_BPF
15
16extern struct static_key_false cgroup_bpf_enabled_key;
17#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
18
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070019struct bpf_prog_list {
20 struct list_head node;
21 struct bpf_prog *prog;
22};
23
24struct bpf_prog_array;
25
Daniel Mack30070982016-11-23 16:52:26 +010026struct cgroup_bpf {
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070027 /* array of effective progs in this cgroup */
28 struct bpf_prog_array __rcu *effective[MAX_BPF_ATTACH_TYPE];
29
30 /* attached progs to this cgroup and attach flags
31 * when flags == 0 or BPF_F_ALLOW_OVERRIDE the progs list will
32 * have either zero or one element
33 * when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS
Daniel Mack30070982016-11-23 16:52:26 +010034 */
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070035 struct list_head progs[MAX_BPF_ATTACH_TYPE];
36 u32 flags[MAX_BPF_ATTACH_TYPE];
37
38 /* temp storage for effective prog array used by prog_attach/detach */
39 struct bpf_prog_array __rcu *inactive;
Daniel Mack30070982016-11-23 16:52:26 +010040};
41
42void cgroup_bpf_put(struct cgroup *cgrp);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070043int cgroup_bpf_inherit(struct cgroup *cgrp);
Daniel Mack30070982016-11-23 16:52:26 +010044
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070045int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
46 enum bpf_attach_type type, u32 flags);
47int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
48 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070049int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
50 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010051
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -070052/* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */
53int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
54 enum bpf_attach_type type, u32 flags);
55int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
56 enum bpf_attach_type type, u32 flags);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -070057int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
58 union bpf_attr __user *uattr);
Daniel Mack30070982016-11-23 16:52:26 +010059
David Ahernb2cd1252016-12-01 08:48:03 -080060int __cgroup_bpf_run_filter_skb(struct sock *sk,
61 struct sk_buff *skb,
62 enum bpf_attach_type type);
Daniel Mack30070982016-11-23 16:52:26 +010063
David Ahern610236582016-12-01 08:48:04 -080064int __cgroup_bpf_run_filter_sk(struct sock *sk,
65 enum bpf_attach_type type);
66
Andrey Ignatov4fbac772018-03-30 15:08:02 -070067int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
68 struct sockaddr *uaddr,
69 enum bpf_attach_type type);
70
Lawrence Brakmo40304b22017-06-30 20:02:40 -070071int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
72 struct bpf_sock_ops_kern *sock_ops,
73 enum bpf_attach_type type);
74
Roman Gushchinebc614f2017-11-05 08:15:32 -050075int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
76 short access, enum bpf_attach_type type);
77
David Ahernb2cd1252016-12-01 08:48:03 -080078/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
79#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \
80({ \
81 int __ret = 0; \
82 if (cgroup_bpf_enabled) \
83 __ret = __cgroup_bpf_run_filter_skb(sk, skb, \
84 BPF_CGROUP_INET_INGRESS); \
85 \
86 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +010087})
88
David Ahernb2cd1252016-12-01 08:48:03 -080089#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \
90({ \
91 int __ret = 0; \
92 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
93 typeof(sk) __sk = sk_to_full_sk(sk); \
94 if (sk_fullsock(__sk)) \
95 __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \
96 BPF_CGROUP_INET_EGRESS); \
97 } \
98 __ret; \
Daniel Mack30070982016-11-23 16:52:26 +010099})
100
David Ahern610236582016-12-01 08:48:04 -0800101#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
102({ \
103 int __ret = 0; \
Yafang Shaoee078622018-02-23 14:58:41 +0800104 if (cgroup_bpf_enabled) { \
David Ahern610236582016-12-01 08:48:04 -0800105 __ret = __cgroup_bpf_run_filter_sk(sk, \
106 BPF_CGROUP_INET_SOCK_CREATE); \
107 } \
108 __ret; \
109})
110
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700111#define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \
112({ \
113 int __ret = 0; \
114 if (cgroup_bpf_enabled) \
115 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type); \
116 __ret; \
117})
118
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700119#define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type) \
120({ \
121 int __ret = 0; \
122 if (cgroup_bpf_enabled) { \
123 lock_sock(sk); \
124 __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type); \
125 release_sock(sk); \
126 } \
127 __ret; \
128})
129
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700130#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \
131 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND)
132
133#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \
134 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND)
135
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700136#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \
137 sk->sk_prot->pre_connect)
138
139#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \
140 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
141
142#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \
143 BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
144
145#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \
146 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT)
147
148#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \
149 BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT)
150
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700151#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
152({ \
153 int __ret = 0; \
154 if (cgroup_bpf_enabled && (sock_ops)->sk) { \
155 typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \
WANG Congdf39a9f2017-07-17 11:42:55 -0700156 if (__sk && sk_fullsock(__sk)) \
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700157 __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \
158 sock_ops, \
159 BPF_CGROUP_SOCK_OPS); \
160 } \
161 __ret; \
162})
Roman Gushchinebc614f2017-11-05 08:15:32 -0500163
164#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \
165({ \
166 int __ret = 0; \
167 if (cgroup_bpf_enabled) \
168 __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \
169 access, \
170 BPF_CGROUP_DEVICE); \
171 \
172 __ret; \
173})
Daniel Mack30070982016-11-23 16:52:26 +0100174#else
175
176struct cgroup_bpf {};
177static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700178static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
Daniel Mack30070982016-11-23 16:52:26 +0100179
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700180#define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0)
Daniel Mack30070982016-11-23 16:52:26 +0100181#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
182#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
David Ahern610236582016-12-01 08:48:04 -0800183#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
Andrey Ignatov4fbac772018-03-30 15:08:02 -0700184#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
185#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700186#define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; })
187#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; })
188#define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; })
189#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700190#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
Roman Gushchinebc614f2017-11-05 08:15:32 -0500191#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
Daniel Mack30070982016-11-23 16:52:26 +0100192
193#endif /* CONFIG_CGROUP_BPF */
194
195#endif /* _BPF_CGROUP_H */