OSDN Git Service

bpf: Support bpf_get_socket_cookie in more prog types
authorAndrey Ignatov <rdna@fb.com>
Tue, 31 Jul 2018 00:42:28 +0000 (17:42 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 31 Jul 2018 07:33:48 +0000 (09:33 +0200)
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>
include/uapi/linux/bpf.h
net/core/filter.c

index 8701139..0ebaaf7 100644 (file)
@@ -1371,6 +1371,20 @@ union bpf_attr {
  *             A 8-byte long non-decreasing number on success, or 0 if the
  *             socket field is missing inside *skb*.
  *
+ * u64 bpf_get_socket_cookie(struct bpf_sock_addr *ctx)
+ *     Description
+ *             Equivalent to bpf_get_socket_cookie() helper that accepts
+ *             *skb*, but gets socket from **struct bpf_sock_addr** contex.
+ *     Return
+ *             A 8-byte long non-decreasing number.
+ *
+ * u64 bpf_get_socket_cookie(struct bpf_sock_ops *ctx)
+ *     Description
+ *             Equivalent to bpf_get_socket_cookie() helper that accepts
+ *             *skb*, but gets socket from **struct bpf_sock_ops** contex.
+ *     Return
+ *             A 8-byte long non-decreasing number.
+ *
  * u32 bpf_get_socket_uid(struct sk_buff *skb)
  *     Return
  *             The owner UID of the socket associated to *skb*. If the socket
index 7df1a0f..9bb9a44 100644 (file)
@@ -3812,6 +3812,30 @@ static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
        .arg1_type      = ARG_PTR_TO_CTX,
 };
 
+BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
+{
+       return sock_gen_cookie(ctx->sk);
+}
+
+static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
+       .func           = bpf_get_socket_cookie_sock_addr,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_PTR_TO_CTX,
+};
+
+BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
+{
+       return sock_gen_cookie(ctx->sk);
+}
+
+static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
+       .func           = bpf_get_socket_cookie_sock_ops,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_PTR_TO_CTX,
+};
+
 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
 {
        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)
                default:
                        return NULL;
                }
+       case BPF_FUNC_get_socket_cookie:
+               return &bpf_get_socket_cookie_sock_addr_proto;
        default:
                return bpf_base_func_proto(func_id);
        }
@@ -4960,6 +4986,8 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_sock_map_update_proto;
        case BPF_FUNC_sock_hash_update:
                return &bpf_sock_hash_update_proto;
+       case BPF_FUNC_get_socket_cookie:
+               return &bpf_get_socket_cookie_sock_ops_proto;
        default:
                return bpf_base_func_proto(func_id);
        }