Skip to content

Commit 8d20aab

Browse files
borkmanndavem330
authored andcommitted
ebpf: add helper to retrieve net_cls's classid cookie
It would be very useful to retrieve the net_cls's classid from an eBPF program to allow for a more fine-grained classification, it could be directly used or in conjunction with additional policies. I.e. docker, but also tooling such as cgexec, can easily run applications via net_cls cgroups: cgcreate -g net_cls:/foo echo 42 > foo/net_cls.classid cgexec -g net_cls:foo <prog> Thus, their respecitve classid cookie of foo can then be looked up on the egress path to apply further policies. The helper is desigend such that a non-zero value returns the cgroup id. Signed-off-by: Daniel Borkmann <[email protected]> Cc: Thomas Graf <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b87a173 commit 8d20aab

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

include/uapi/linux/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ enum bpf_func_id {
249249
* Return: 0 on success
250250
*/
251251
BPF_FUNC_get_current_comm,
252+
253+
/**
254+
* bpf_get_cgroup_classid(skb) - retrieve a proc's classid
255+
* @skb: pointer to skb
256+
* Return: classid if != 0
257+
*/
258+
BPF_FUNC_get_cgroup_classid,
252259
__BPF_FUNC_MAX_ID,
253260
};
254261

net/core/filter.c

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <linux/if_vlan.h>
4848
#include <linux/bpf.h>
4949
#include <net/sch_generic.h>
50+
#include <net/cls_cgroup.h>
5051

5152
/**
5253
* sk_filter - run a packet through a socket filter
@@ -1424,6 +1425,18 @@ const struct bpf_func_proto bpf_clone_redirect_proto = {
14241425
.arg3_type = ARG_ANYTHING,
14251426
};
14261427

1428+
static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1429+
{
1430+
return task_get_classid((struct sk_buff *) (unsigned long) r1);
1431+
}
1432+
1433+
static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1434+
.func = bpf_get_cgroup_classid,
1435+
.gpl_only = false,
1436+
.ret_type = RET_INTEGER,
1437+
.arg1_type = ARG_PTR_TO_CTX,
1438+
};
1439+
14271440
static const struct bpf_func_proto *
14281441
sk_filter_func_proto(enum bpf_func_id func_id)
14291442
{
@@ -1461,6 +1474,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
14611474
return &bpf_l4_csum_replace_proto;
14621475
case BPF_FUNC_clone_redirect:
14631476
return &bpf_clone_redirect_proto;
1477+
case BPF_FUNC_get_cgroup_classid:
1478+
return &bpf_get_cgroup_classid_proto;
14641479
default:
14651480
return sk_filter_func_proto(func_id);
14661481
}

0 commit comments

Comments
 (0)