Skip to content

Commit d692f11

Browse files
rdnaborkmann
authored andcommitted
bpf: Support bpf_get_socket_cookie in more prog types
bpf_get_socket_cookie() helper can be used to identify skb that correspond to the same socket. Though socket cookie can be useful in many other use-cases where socket is available in program context. Specifically BPF_PROG_TYPE_CGROUP_SOCK_ADDR and BPF_PROG_TYPE_SOCK_OPS programs can benefit from it so that one of them can augment a value in a map prepared earlier by other program for the same socket. The patch adds support to call bpf_get_socket_cookie() from BPF_PROG_TYPE_CGROUP_SOCK_ADDR and BPF_PROG_TYPE_SOCK_OPS. It doesn't introduce new helpers. Instead it reuses same helper name bpf_get_socket_cookie() but adds support to this helper to accept `struct bpf_sock_addr` and `struct bpf_sock_ops`. Documentation in bpf.h is changed in a way that should not break automatic generation of markdown. Signed-off-by: Andrey Ignatov <rdna@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 240b74f commit d692f11

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,20 @@ union bpf_attr {
13711371
* A 8-byte long non-decreasing number on success, or 0 if the
13721372
* socket field is missing inside *skb*.
13731373
*
1374+
* u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx)
1375+
* Description
1376+
* Equivalent to bpf_get_socket_cookie() helper that accepts
1377+
* *skb*, but gets socket from **struct bpf_sock_addr** contex.
1378+
* Return
1379+
* A 8-byte long non-decreasing number.
1380+
*
1381+
* u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx)
1382+
* Description
1383+
* Equivalent to bpf_get_socket_cookie() helper that accepts
1384+
* *skb*, but gets socket from **struct bpf_sock_ops** contex.
1385+
* Return
1386+
* A 8-byte long non-decreasing number.
1387+
*
13741388
* u32 bpf_get_socket_uid(struct sk_buff *skb)
13751389
* Return
13761390
* The owner UID of the socket associated to *skb*. If the socket

‎net/core/filter.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,6 +3812,30 @@ static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
38123812
.arg1_type = ARG_PTR_TO_CTX,
38133813
};
38143814

3815+
BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3816+
{
3817+
return sock_gen_cookie(ctx->sk);
3818+
}
3819+
3820+
static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3821+
.func = bpf_get_socket_cookie_sock_addr,
3822+
.gpl_only = false,
3823+
.ret_type = RET_INTEGER,
3824+
.arg1_type = ARG_PTR_TO_CTX,
3825+
};
3826+
3827+
BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3828+
{
3829+
return sock_gen_cookie(ctx->sk);
3830+
}
3831+
3832+
static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3833+
.func = bpf_get_socket_cookie_sock_ops,
3834+
.gpl_only = false,
3835+
.ret_type = RET_INTEGER,
3836+
.arg1_type = ARG_PTR_TO_CTX,
3837+
};
3838+
38153839
BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
38163840
{
38173841
struct sock *sk = sk_to_full_sk(skb->sk);
@@ -4818,6 +4842,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
48184842
default:
48194843
return NULL;
48204844
}
4845+
case BPF_FUNC_get_socket_cookie:
4846+
return &bpf_get_socket_cookie_sock_addr_proto;
48214847
default:
48224848
return bpf_base_func_proto(func_id);
48234849
}
@@ -4960,6 +4986,8 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
49604986
return &bpf_sock_map_update_proto;
49614987
case BPF_FUNC_sock_hash_update:
49624988
return &bpf_sock_hash_update_proto;
4989+
case BPF_FUNC_get_socket_cookie:
4990+
return &bpf_get_socket_cookie_sock_ops_proto;
49634991
default:
49644992
return bpf_base_func_proto(func_id);
49654993
}

0 commit comments

Comments
 (0)