Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 2 | #ifndef _BPF_CGROUP_H |
| 3 | #define _BPF_CGROUP_H |
| 4 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 5 | #include <linux/jump_label.h> |
| 6 | #include <uapi/linux/bpf.h> |
| 7 | |
| 8 | struct sock; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 9 | struct sockaddr; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 10 | struct cgroup; |
| 11 | struct sk_buff; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 12 | struct bpf_sock_ops_kern; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 13 | |
| 14 | #ifdef CONFIG_CGROUP_BPF |
| 15 | |
| 16 | extern struct static_key_false cgroup_bpf_enabled_key; |
| 17 | #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) |
| 18 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 19 | struct bpf_prog_list { |
| 20 | struct list_head node; |
| 21 | struct bpf_prog *prog; |
| 22 | }; |
| 23 | |
| 24 | struct bpf_prog_array; |
| 25 | |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 26 | struct cgroup_bpf { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 27 | /* 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 Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 34 | */ |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 35 | 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 Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | void cgroup_bpf_put(struct cgroup *cgrp); |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 43 | int cgroup_bpf_inherit(struct cgroup *cgrp); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 44 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 45 | int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 46 | enum bpf_attach_type type, u32 flags); |
| 47 | int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 48 | enum bpf_attach_type type, u32 flags); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 49 | int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 50 | union bpf_attr __user *uattr); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 51 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 52 | /* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */ |
| 53 | int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 54 | enum bpf_attach_type type, u32 flags); |
| 55 | int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, |
| 56 | enum bpf_attach_type type, u32 flags); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 57 | int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, |
| 58 | union bpf_attr __user *uattr); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 59 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 60 | int __cgroup_bpf_run_filter_skb(struct sock *sk, |
| 61 | struct sk_buff *skb, |
| 62 | enum bpf_attach_type type); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 63 | |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 64 | int __cgroup_bpf_run_filter_sk(struct sock *sk, |
| 65 | enum bpf_attach_type type); |
| 66 | |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 67 | int __cgroup_bpf_run_filter_sock_addr(struct sock *sk, |
| 68 | struct sockaddr *uaddr, |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 69 | enum bpf_attach_type type, |
| 70 | void *t_ctx); |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 71 | |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 72 | int __cgroup_bpf_run_filter_sock_ops(struct sock *sk, |
| 73 | struct bpf_sock_ops_kern *sock_ops, |
| 74 | enum bpf_attach_type type); |
| 75 | |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 76 | int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor, |
| 77 | short access, enum bpf_attach_type type); |
| 78 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 79 | /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */ |
| 80 | #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \ |
| 81 | ({ \ |
| 82 | int __ret = 0; \ |
| 83 | if (cgroup_bpf_enabled) \ |
| 84 | __ret = __cgroup_bpf_run_filter_skb(sk, skb, \ |
| 85 | BPF_CGROUP_INET_INGRESS); \ |
| 86 | \ |
| 87 | __ret; \ |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 88 | }) |
| 89 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 90 | #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \ |
| 91 | ({ \ |
| 92 | int __ret = 0; \ |
| 93 | if (cgroup_bpf_enabled && sk && sk == skb->sk) { \ |
| 94 | typeof(sk) __sk = sk_to_full_sk(sk); \ |
| 95 | if (sk_fullsock(__sk)) \ |
| 96 | __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \ |
| 97 | BPF_CGROUP_INET_EGRESS); \ |
| 98 | } \ |
| 99 | __ret; \ |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 100 | }) |
| 101 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 102 | #define BPF_CGROUP_RUN_SK_PROG(sk, type) \ |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 103 | ({ \ |
| 104 | int __ret = 0; \ |
Yafang Shao | ee07862 | 2018-02-23 14:58:41 +0800 | [diff] [blame] | 105 | if (cgroup_bpf_enabled) { \ |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 106 | __ret = __cgroup_bpf_run_filter_sk(sk, type); \ |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 107 | } \ |
| 108 | __ret; \ |
| 109 | }) |
| 110 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 111 | #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \ |
| 112 | BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE) |
| 113 | |
| 114 | #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \ |
| 115 | BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND) |
| 116 | |
| 117 | #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) \ |
| 118 | BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET6_POST_BIND) |
| 119 | |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 120 | #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, type) \ |
| 121 | ({ \ |
| 122 | int __ret = 0; \ |
| 123 | if (cgroup_bpf_enabled) \ |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 124 | __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \ |
| 125 | NULL); \ |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 126 | __ret; \ |
| 127 | }) |
| 128 | |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 129 | #define BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, type, t_ctx) \ |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 130 | ({ \ |
| 131 | int __ret = 0; \ |
| 132 | if (cgroup_bpf_enabled) { \ |
| 133 | lock_sock(sk); \ |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 134 | __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, type, \ |
| 135 | t_ctx); \ |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 136 | release_sock(sk); \ |
| 137 | } \ |
| 138 | __ret; \ |
| 139 | }) |
| 140 | |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 141 | #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) \ |
| 142 | BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_BIND) |
| 143 | |
| 144 | #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) \ |
| 145 | BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_BIND) |
| 146 | |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 147 | #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (cgroup_bpf_enabled && \ |
| 148 | sk->sk_prot->pre_connect) |
| 149 | |
| 150 | #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) \ |
| 151 | BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET4_CONNECT) |
| 152 | |
| 153 | #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) \ |
| 154 | BPF_CGROUP_RUN_SA_PROG(sk, uaddr, BPF_CGROUP_INET6_CONNECT) |
| 155 | |
| 156 | #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) \ |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 157 | BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET4_CONNECT, NULL) |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 158 | |
| 159 | #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) \ |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 160 | BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_INET6_CONNECT, NULL) |
| 161 | |
| 162 | #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) \ |
| 163 | BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_SENDMSG, t_ctx) |
| 164 | |
| 165 | #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \ |
| 166 | BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx) |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 167 | |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 168 | #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \ |
| 169 | ({ \ |
| 170 | int __ret = 0; \ |
| 171 | if (cgroup_bpf_enabled && (sock_ops)->sk) { \ |
| 172 | typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \ |
WANG Cong | df39a9f | 2017-07-17 11:42:55 -0700 | [diff] [blame] | 173 | if (__sk && sk_fullsock(__sk)) \ |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 174 | __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \ |
| 175 | sock_ops, \ |
| 176 | BPF_CGROUP_SOCK_OPS); \ |
| 177 | } \ |
| 178 | __ret; \ |
| 179 | }) |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 180 | |
| 181 | #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access) \ |
| 182 | ({ \ |
| 183 | int __ret = 0; \ |
| 184 | if (cgroup_bpf_enabled) \ |
| 185 | __ret = __cgroup_bpf_check_dev_permission(type, major, minor, \ |
| 186 | access, \ |
| 187 | BPF_CGROUP_DEVICE); \ |
| 188 | \ |
| 189 | __ret; \ |
| 190 | }) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 191 | #else |
| 192 | |
| 193 | struct cgroup_bpf {}; |
| 194 | static inline void cgroup_bpf_put(struct cgroup *cgrp) {} |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 195 | static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; } |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 196 | |
Andrey Ignatov | 13193b0 | 2018-05-25 08:55:22 -0700 | [diff] [blame] | 197 | #define cgroup_bpf_enabled (0) |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 198 | #define BPF_CGROUP_PRE_CONNECT_ENABLED(sk) (0) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 199 | #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; }) |
| 200 | #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; }) |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 201 | #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; }) |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 202 | #define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; }) |
| 203 | #define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; }) |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 204 | #define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; }) |
| 205 | #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk) ({ 0; }) |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 206 | #define BPF_CGROUP_RUN_PROG_INET4_CONNECT(sk, uaddr) ({ 0; }) |
| 207 | #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr) ({ 0; }) |
| 208 | #define BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr) ({ 0; }) |
| 209 | #define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; }) |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame^] | 210 | #define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; }) |
| 211 | #define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; }) |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 212 | #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; }) |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 213 | #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; }) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 214 | |
| 215 | #endif /* CONFIG_CGROUP_BPF */ |
| 216 | |
| 217 | #endif /* _BPF_CGROUP_H */ |