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; |
| 9 | struct cgroup; |
| 10 | struct sk_buff; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 11 | struct bpf_sock_ops_kern; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 12 | |
| 13 | #ifdef CONFIG_CGROUP_BPF |
| 14 | |
| 15 | extern struct static_key_false cgroup_bpf_enabled_key; |
| 16 | #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) |
| 17 | |
| 18 | struct cgroup_bpf { |
| 19 | /* |
| 20 | * Store two sets of bpf_prog pointers, one for programs that are |
| 21 | * pinned directly to this cgroup, and one for those that are effective |
| 22 | * when this cgroup is accessed. |
| 23 | */ |
| 24 | struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE]; |
Daniel Mack | dcdc43d | 2016-12-15 10:53:21 +0100 | [diff] [blame] | 25 | struct bpf_prog __rcu *effective[MAX_BPF_ATTACH_TYPE]; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 26 | bool disallow_override[MAX_BPF_ATTACH_TYPE]; |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | void cgroup_bpf_put(struct cgroup *cgrp); |
| 30 | void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent); |
| 31 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 32 | int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent, |
| 33 | struct bpf_prog *prog, enum bpf_attach_type type, |
| 34 | bool overridable); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 35 | |
| 36 | /* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 37 | int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog, |
| 38 | enum bpf_attach_type type, bool overridable); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 39 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 40 | int __cgroup_bpf_run_filter_skb(struct sock *sk, |
| 41 | struct sk_buff *skb, |
| 42 | enum bpf_attach_type type); |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 43 | |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 44 | int __cgroup_bpf_run_filter_sk(struct sock *sk, |
| 45 | enum bpf_attach_type type); |
| 46 | |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 47 | int __cgroup_bpf_run_filter_sock_ops(struct sock *sk, |
| 48 | struct bpf_sock_ops_kern *sock_ops, |
| 49 | enum bpf_attach_type type); |
| 50 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 51 | /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */ |
| 52 | #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \ |
| 53 | ({ \ |
| 54 | int __ret = 0; \ |
| 55 | if (cgroup_bpf_enabled) \ |
| 56 | __ret = __cgroup_bpf_run_filter_skb(sk, skb, \ |
| 57 | BPF_CGROUP_INET_INGRESS); \ |
| 58 | \ |
| 59 | __ret; \ |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 60 | }) |
| 61 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 62 | #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb) \ |
| 63 | ({ \ |
| 64 | int __ret = 0; \ |
| 65 | if (cgroup_bpf_enabled && sk && sk == skb->sk) { \ |
| 66 | typeof(sk) __sk = sk_to_full_sk(sk); \ |
| 67 | if (sk_fullsock(__sk)) \ |
| 68 | __ret = __cgroup_bpf_run_filter_skb(__sk, skb, \ |
| 69 | BPF_CGROUP_INET_EGRESS); \ |
| 70 | } \ |
| 71 | __ret; \ |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 72 | }) |
| 73 | |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 74 | #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \ |
| 75 | ({ \ |
| 76 | int __ret = 0; \ |
| 77 | if (cgroup_bpf_enabled && sk) { \ |
| 78 | __ret = __cgroup_bpf_run_filter_sk(sk, \ |
| 79 | BPF_CGROUP_INET_SOCK_CREATE); \ |
| 80 | } \ |
| 81 | __ret; \ |
| 82 | }) |
| 83 | |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 84 | #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \ |
| 85 | ({ \ |
| 86 | int __ret = 0; \ |
| 87 | if (cgroup_bpf_enabled && (sock_ops)->sk) { \ |
| 88 | typeof(sk) __sk = sk_to_full_sk((sock_ops)->sk); \ |
WANG Cong | df39a9f | 2017-07-17 11:42:55 -0700 | [diff] [blame] | 89 | if (__sk && sk_fullsock(__sk)) \ |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 90 | __ret = __cgroup_bpf_run_filter_sock_ops(__sk, \ |
| 91 | sock_ops, \ |
| 92 | BPF_CGROUP_SOCK_OPS); \ |
| 93 | } \ |
| 94 | __ret; \ |
| 95 | }) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 96 | #else |
| 97 | |
| 98 | struct cgroup_bpf {}; |
| 99 | static inline void cgroup_bpf_put(struct cgroup *cgrp) {} |
| 100 | static inline void cgroup_bpf_inherit(struct cgroup *cgrp, |
| 101 | struct cgroup *parent) {} |
| 102 | |
| 103 | #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; }) |
| 104 | #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; }) |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 105 | #define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; }) |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 106 | #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; }) |
Daniel Mack | 3007098 | 2016-11-23 16:52:26 +0100 | [diff] [blame] | 107 | |
| 108 | #endif /* CONFIG_CGROUP_BPF */ |
| 109 | |
| 110 | #endif /* _BPF_CGROUP_H */ |