Skip to content

Commit 0e33661

Browse files
zonquedavem330
authored andcommitted
bpf: add new prog type for cgroup socket filtering
This program type is similar to BPF_PROG_TYPE_SOCKET_FILTER, except that it does not allow BPF_LD_[ABS|IND] instructions and hooks up the bpf_skb_load_bytes() helper. Programs of this type will be attached to cgroups for network filtering and accounting. Signed-off-by: Daniel Mack <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 619228d commit 0e33661

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,17 @@ enum bpf_prog_type {
9898
BPF_PROG_TYPE_TRACEPOINT,
9999
BPF_PROG_TYPE_XDP,
100100
BPF_PROG_TYPE_PERF_EVENT,
101+
BPF_PROG_TYPE_CGROUP_SKB,
101102
};
102103

104+
enum bpf_attach_type {
105+
BPF_CGROUP_INET_INGRESS,
106+
BPF_CGROUP_INET_EGRESS,
107+
__MAX_BPF_ATTACH_TYPE
108+
};
109+
110+
#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
111+
103112
#define BPF_PSEUDO_MAP_FD 1
104113

105114
/* flags for BPF_MAP_UPDATE_ELEM command */

net/core/filter.c

+23
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,17 @@ xdp_func_proto(enum bpf_func_id func_id)
26302630
}
26312631
}
26322632

2633+
static const struct bpf_func_proto *
2634+
cg_skb_func_proto(enum bpf_func_id func_id)
2635+
{
2636+
switch (func_id) {
2637+
case BPF_FUNC_skb_load_bytes:
2638+
return &bpf_skb_load_bytes_proto;
2639+
default:
2640+
return sk_filter_func_proto(func_id);
2641+
}
2642+
}
2643+
26332644
static bool __is_valid_access(int off, int size, enum bpf_access_type type)
26342645
{
26352646
if (off < 0 || off >= sizeof(struct __sk_buff))
@@ -2992,6 +3003,12 @@ static const struct bpf_verifier_ops xdp_ops = {
29923003
.convert_ctx_access = xdp_convert_ctx_access,
29933004
};
29943005

3006+
static const struct bpf_verifier_ops cg_skb_ops = {
3007+
.get_func_proto = cg_skb_func_proto,
3008+
.is_valid_access = sk_filter_is_valid_access,
3009+
.convert_ctx_access = sk_filter_convert_ctx_access,
3010+
};
3011+
29953012
static struct bpf_prog_type_list sk_filter_type __read_mostly = {
29963013
.ops = &sk_filter_ops,
29973014
.type = BPF_PROG_TYPE_SOCKET_FILTER,
@@ -3012,12 +3029,18 @@ static struct bpf_prog_type_list xdp_type __read_mostly = {
30123029
.type = BPF_PROG_TYPE_XDP,
30133030
};
30143031

3032+
static struct bpf_prog_type_list cg_skb_type __read_mostly = {
3033+
.ops = &cg_skb_ops,
3034+
.type = BPF_PROG_TYPE_CGROUP_SKB,
3035+
};
3036+
30153037
static int __init register_sk_filter_ops(void)
30163038
{
30173039
bpf_register_prog_type(&sk_filter_type);
30183040
bpf_register_prog_type(&sched_cls_type);
30193041
bpf_register_prog_type(&sched_act_type);
30203042
bpf_register_prog_type(&xdp_type);
3043+
bpf_register_prog_type(&cg_skb_type);
30213044

30223045
return 0;
30233046
}

0 commit comments

Comments
 (0)