blob: ec80d0c0953e63182ef79c72baa6901162cce009 [file] [log] [blame]
Daniel Mack30070982016-11-23 16:52:26 +01001#ifndef _BPF_CGROUP_H
2#define _BPF_CGROUP_H
3
4#include <linux/bpf.h>
5#include <linux/jump_label.h>
6#include <uapi/linux/bpf.h>
7
8struct sock;
9struct cgroup;
10struct sk_buff;
11
12#ifdef CONFIG_CGROUP_BPF
13
14extern struct static_key_false cgroup_bpf_enabled_key;
15#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
16
17struct cgroup_bpf {
18 /*
19 * Store two sets of bpf_prog pointers, one for programs that are
20 * pinned directly to this cgroup, and one for those that are effective
21 * when this cgroup is accessed.
22 */
23 struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE];
24 struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE];
25};
26
27void cgroup_bpf_put(struct cgroup *cgrp);
28void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent);
29
30void __cgroup_bpf_update(struct cgroup *cgrp,
31 struct cgroup *parent,
32 struct bpf_prog *prog,
33 enum bpf_attach_type type);
34
35/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */
36void cgroup_bpf_update(struct cgroup *cgrp,
37 struct bpf_prog *prog,
38 enum bpf_attach_type type);
39
40int __cgroup_bpf_run_filter(struct sock *sk,
41 struct sk_buff *skb,
42 enum bpf_attach_type type);
43
44/* Wrappers for __cgroup_bpf_run_filter() guarded by cgroup_bpf_enabled. */
45#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) \
46({ \
47 int __ret = 0; \
48 if (cgroup_bpf_enabled) \
49 __ret = __cgroup_bpf_run_filter(sk, skb, \
50 BPF_CGROUP_INET_INGRESS); \
51 \
52 __ret; \
53})
54
55#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) \
56({ \
57 int __ret = 0; \
58 if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
59 typeof(sk) __sk = sk_to_full_sk(sk); \
60 if (sk_fullsock(__sk)) \
61 __ret = __cgroup_bpf_run_filter(__sk, skb, \
62 BPF_CGROUP_INET_EGRESS); \
63 } \
64 __ret; \
65})
66
67#else
68
69struct cgroup_bpf {};
70static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
71static inline void cgroup_bpf_inherit(struct cgroup *cgrp,
72 struct cgroup *parent) {}
73
74#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
75#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
76
77#endif /* CONFIG_CGROUP_BPF */
78
79#endif /* _BPF_CGROUP_H */