Re: [PATCH v5 bpf-next 5/7] bpf: Add bpf_sock_destroy kfunc

Martin KaFai Lau
2023-03-31T22:25:09Z

On 3/30/23 8:17 AM, Aditi Ghag wrote:
> +/* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code.
> + *
> + * The helper expects a non-NULL pointer to a socket. It invokes the
> + * protocol specific socket destroy handlers.
> + *
> + * The helper can only be called from BPF contexts that have acquired the socket
> + * locks.
> + *
> + * Parameters:
> + * @sock: Pointer to socket to be destroyed
> + *
> + * Return:
> + * On error, may return EPROTONOSUPPORT, EINVAL.
> + * EPROTONOSUPPORT if protocol specific destroy handler is not implemented.
> + * 0 otherwise
> + */
> +__bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)
> +{
> + struct sock *sk = (struct sock *)sock;
> +
> + if (!sk)
> + return -EINVAL;
> +
> + /* The locking semantics that allow for synchronous execution of the
> + * destroy handlers are only supported for TCP and UDP.
> + */
> + if (!sk->sk_prot->diag_destroy || sk->sk_protocol == IPPROTO_RAW)
> + return -EOPNOTSUPP;
> +
> + return sk->sk_prot->diag_destroy(sk, ECONNABORTED);
> +}
> +
> +__diag_pop()
> +
> +BTF_SET8_START(sock_destroy_kfunc_set)
> +BTF_ID_FLAGS(func, bpf_sock_destroy)
> +BTF_SET8_END(sock_destroy_kfunc_set)
> +
> +static const struct btf_kfunc_id_set bpf_sock_destroy_kfunc_set = {
> + .owner = THIS_MODULE,
> + .set = &sock_destroy_kfunc_set,
> +};
> +
> +static int init_subsystem(void)
> +{
> + return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sock_destroy_kfunc_set);

This still needs to be restricted to BPF_TRACE_ITER which has held the
lock_sock. May be adding BTF_KFUNC_HOOK_TRACING_ITER. Then,
register_btf_kfunc_id_set() and btf_kfunc_id_set_contains() need to consider the
prog->expected_attach_type also?