Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp259340pxt; Wed, 4 Aug 2021 10:22:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvL9elioXcocQKudbD0f3qzf6BEm+RIaM1Hx3FHsrfw0Jb5kLqouH9mU/6u107KgNl5p4O X-Received: by 2002:a92:c245:: with SMTP id k5mr520844ilo.143.1628097720487; Wed, 04 Aug 2021 10:22:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628097720; cv=none; d=google.com; s=arc-20160816; b=fd4Rxd6j+u+cAdP/JKpxJx1qZ9MUF939SK4AJg7PTOu9M8MQFrs6zCypOawaEgyyNv SRQtqSlNIm2L9u8cp/NnuFd9Cd+EK/MVPSrEfZGepek3t8Fzg1uuXzPLP3xliMnLvGJh g5ofYbB5LDfWuJcWM2bs9DypjJ7lchDZ9aRRCQTPz8kHOCXvIcUuJthrSuoMstMoRb+a bQS7Pnmpgz1qpd66+X/47mOhzxDNgP+JaT6qDdtosTxgNFuAnH/pEeFedm1OCIKtrmts eWC3RXRjNbXanfpdwLMsQn1RttHhgUX7JJ6JZWUeFHBnWKijnCZ2zNfiWef+t4GVTwzh GNAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SsLjeKiLuliSZOE+wUaPOjaCl3gVgPx+PNBKHSFfAc0=; b=pwOnsjkx7+jZ523NAnwu4k/jdIl+Av4tiO+QXKzt6d/FV3k98w6Zfk6BKHZr8oFadp 1PJeFaj7NIXHAyO7akOkIcURdwKX1AcCh/kD3y8sLzsZuJCuhBSJL6uWwggtCBeCYBQx F4X1yz04qT+szk429ezKzCNXhhEFJifydFxJkJnyA6cCEttk0VIGelDTai9bDnhtNfSg mzAFks5mHlcdMeZNl8072EyILcNt1u42s9vk6mDrmOCh3XucBa25BLgdwMQcM/AKk1XG +Iabye0g5lKRcwB8AsDEvqrCc2e9SCYOcWRTC+89ve/griLBBufCP5piMUw5ZX3g7Tnz FEEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=hVC8CZts; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j1si2883756jak.102.2021.08.04.10.21.46; Wed, 04 Aug 2021 10:22:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=hVC8CZts; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239816AbhHDRU4 (ORCPT + 99 others); Wed, 4 Aug 2021 13:20:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbhHDRUy (ORCPT ); Wed, 4 Aug 2021 13:20:54 -0400 Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10743C0613D5 for ; Wed, 4 Aug 2021 10:20:42 -0700 (PDT) Received: by mail-oi1-x234.google.com with SMTP id u10so3688576oiw.4 for ; Wed, 04 Aug 2021 10:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SsLjeKiLuliSZOE+wUaPOjaCl3gVgPx+PNBKHSFfAc0=; b=hVC8CZtsjkFOsvIinLJF30LOuTeXy5igNSPk7hFv9p92XBKklcEs4hRIgS5etqqoyk Ex95PKSsiEj/BIIpSYMCXBpJJqiH/5bSpUtu467tZBV8HYK5j+gEiLPcsWBFOX+A76Bz JQ70k6t5f/6C/3n/OaC5HAKi3bKsajwSq0Tl1GgURhePM/VohVmGQ8oqkiXqAisNMjXn eeHuS+zUQmQd6Fd1jtrlFXGp45uGhY89q+X9Iye1mtthZzGL2CodxGIT+vCYy9ESHOca nzkl3F/QNRx10oWyG57E0AG0qj+C3s6cveoKrfI/8lgHfHl936IVjVr+tKTpkIlR6IYn KIrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SsLjeKiLuliSZOE+wUaPOjaCl3gVgPx+PNBKHSFfAc0=; b=eNwTDLFkOZTPIBoNwVTt6/4IFTHtaronNfRhqJXEDfTDGl8J7+0FU+W+bpWe5SnPrE JeNuI/xnjhkue/CiWWwkwg33rUSZkvhZphKbSKj+yX1D2QylcgDrMI9K/mBeKYvfuun5 JwOnK1kn7EzrXB2YSoQiNXoblgxvqGvOfTMx/vgeOJsQNYEtFOn43oaLe1UU7ANnkszs kR4d8S2yUzph2H+ncnpnsffV44eCBIgO0qJJ+DQXCQ/LGDeiKvhw3ET/aHXJSQC7cO9w qMlYYZ+cYUZCfw3UNoXSo/o23PphmalU2zvEFz/kCeeCPEUP4Z1reo0eX5Bl4yoN3Iig JN4g== X-Gm-Message-State: AOAM530QB9cwhczYVH0UuRBEjQmcKUoR12ClkVPoQTZN1k1KrBy3tk+N xZjj+BMb9UqBBRH9Gg5PX1RB7naIKRNT5l+WM7C+1Q== X-Received: by 2002:a05:6808:d53:: with SMTP id w19mr447537oik.48.1628097641355; Wed, 04 Aug 2021 10:20:41 -0700 (PDT) MIME-Version: 1.0 References: <20210802211912.116329-1-jiang.wang@bytedance.com> <20210802211912.116329-3-jiang.wang@bytedance.com> <87zgtxcfig.fsf@cloudflare.com> In-Reply-To: <87zgtxcfig.fsf@cloudflare.com> From: "Jiang Wang ." Date: Wed, 4 Aug 2021 10:20:30 -0700 Message-ID: Subject: Re: Re: [PATCH bpf-next v3 2/5] af_unix: add unix_stream_proto for sockmap To: Jakub Sitnicki Cc: Networking , Cong Wang , Xiongchun Duan , Yongji Xie , =?UTF-8?B?5p+056iz?= , "David S. Miller" , Jakub Kicinski , John Fastabend , Daniel Borkmann , Lorenz Bauer , Alexei Starovoitov , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Shuah Khan , Johan Almbladh , linux-kernel@vger.kernel.org, bpf , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 4, 2021 at 9:59 AM Jakub Sitnicki wrote: > > On Mon, Aug 02, 2021 at 11:19 PM CEST, Jiang Wang wrote: > > [...] > > > diff --git a/net/core/sock_map.c b/net/core/sock_map.c > > index ae5fa4338..42f50ea7a 100644 > > --- a/net/core/sock_map.c > > +++ b/net/core/sock_map.c > > @@ -517,9 +517,15 @@ static bool sk_is_tcp(const struct sock *sk) > > sk->sk_protocol == IPPROTO_TCP; > > } > > > > +static bool sk_is_unix_stream(const struct sock *sk) > > +{ > > + return sk->sk_type == SOCK_STREAM && > > + sk->sk_protocol == PF_UNIX; > > +} > > + > > static bool sock_map_redirect_allowed(const struct sock *sk) > > { > > - if (sk_is_tcp(sk)) > > + if (sk_is_tcp(sk) || sk_is_unix_stream(sk)) > > return sk->sk_state != TCP_LISTEN; > > else > > return sk->sk_state == TCP_ESTABLISHED; > > Let me provide some context. > > The reason why we check != TCP_LISTEN for TCP sockets is that we want to > allow redirect redirect to sockets that are about to transition from > TCP_SYN_RECV to TCP_ESTABLISHED, in addition to sockets already in > TCP_ESTABLISHED state. > > That's because BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB callback happens when > socket is still in TCP_SYN_RECV state. With BPF sockops program, we can > insert such socket into a sockmap. Hence, there is a short window of > opportunity when we could redirect to a socket in TCP_SYN_RECV. > > UNIX sockets can be only in TCP_{CLOSE,LISTEN,ESTABLISHED} state, > AFAIK. So it is sufficient to rely on the default == TCP_ESTABLISHED > check. > Got it. Thanks for the explanation. I will change unix sockets to only check == TCP_ESTABLISHED condition. > > diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c > > index 0ae3fc4c8..9c1711c67 100644 > > --- a/net/unix/af_unix.c > > +++ b/net/unix/af_unix.c > > @@ -791,17 +791,35 @@ static void unix_close(struct sock *sk, long timeout) > > */ > > } > > > > -struct proto unix_proto = { > > - .name = "UNIX", > > +static void unix_unhash(struct sock *sk) > > +{ > > + /* Nothing to do here, unix socket does not need a ->unhash(). > > + * This is merely for sockmap. > > + */ > > +} > > + > > +struct proto unix_dgram_proto = { > > + .name = "UNIX-DGRAM", > > + .owner = THIS_MODULE, > > + .obj_size = sizeof(struct unix_sock), > > + .close = unix_close, > > +#ifdef CONFIG_BPF_SYSCALL > > + .psock_update_sk_prot = unix_dgram_bpf_update_proto, > > +#endif > > +}; > > + > > +struct proto unix_stream_proto = { > > + .name = "UNIX-STREAM", > > .owner = THIS_MODULE, > > .obj_size = sizeof(struct unix_sock), > > .close = unix_close, > > + .unhash = unix_unhash, > > #ifdef CONFIG_BPF_SYSCALL > > - .psock_update_sk_prot = unix_bpf_update_proto, > > + .psock_update_sk_prot = unix_stream_bpf_update_proto, > > #endif > > }; > > > > -static struct sock *unix_create1(struct net *net, struct socket *sock, int kern) > > +static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, int type) > > { > > struct sock *sk = NULL; > > struct unix_sock *u; > > @@ -810,7 +828,11 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern) > > if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) > > goto out; > > > > - sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto, kern); > > + if (type == SOCK_STREAM) > > + sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_stream_proto, kern); > > + else /*dgram and seqpacket */ > > + sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_dgram_proto, kern); > > + > > if (!sk) > > goto out; > > > > @@ -872,7 +894,7 @@ static int unix_create(struct net *net, struct socket *sock, int protocol, > > return -ESOCKTNOSUPPORT; > > } > > > > - return unix_create1(net, sock, kern) ? 0 : -ENOMEM; > > + return unix_create1(net, sock, kern, sock->type) ? 0 : -ENOMEM; > > } > > > > static int unix_release(struct socket *sock) > > @@ -1286,7 +1308,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, > > err = -ENOMEM; > > > > /* create new sock for complete connection */ > > - newsk = unix_create1(sock_net(sk), NULL, 0); > > + newsk = unix_create1(sock_net(sk), NULL, 0, sock->type); > > if (newsk == NULL) > > goto out; > > > > @@ -2214,7 +2236,7 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t si > > struct sock *sk = sock->sk; > > > > #ifdef CONFIG_BPF_SYSCALL > > - if (sk->sk_prot != &unix_proto) > > + if (sk->sk_prot != &unix_dgram_proto) > > return sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, > > flags & ~MSG_DONTWAIT, NULL); > > #endif > > > KASAN might be unhappy about access to sk->sk_prot not annotated with > READ_ONCE. In unix_bpf we have WRITE_ONCE(sk->sk_prot, ...) [1] > Got it. Will check and add READ_ONCE if necessary. > [...] > > [1] https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#why-kernel-code-should-use-read_once-and-write_once-for-shared-memory-accesses