From patchwork Sun Oct 21 04:43:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Long X-Patchwork-Id: 987280 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="B9bieENt"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42d6WG593gz9sCw for ; Sun, 21 Oct 2018 15:44:26 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727129AbeJUM5J (ORCPT ); Sun, 21 Oct 2018 08:57:09 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:32883 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726895AbeJUM5J (ORCPT ); Sun, 21 Oct 2018 08:57:09 -0400 Received: by mail-pl1-f193.google.com with SMTP id x6-v6so728245pln.0; Sat, 20 Oct 2018 21:44:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=xDpo8gogWsXU220D6cDIpGV/0kNdqhcUS+hgkHtYYAM=; b=B9bieENtWxRZnUetC0/ptKgjprBBgPRqpT7T4HNQzFr7tODG9mhVUtfUJqsO02vtqR Sdu2/iORN2gn72Oqv2mF+64t6viGBZs+Y/l08fy0wDPBquFZswRmTNrWMO4j4FdCrChQ 2/XM1oLb0iKiaJGVsUeR68BtLpkZUkXcSX1IwwhQ4NLUvITkMz1BoveQq+vlbOFp+ZdI AQ3ho/hwievyqFZ/NYhki4/jrKDXbX03K81w4jYJKSHSMFUUzfpVWMh9YEtDPkZWK0II ZeHv9dLYG0ZPmvvj/X0xlhsAT9rq8PYoaLIJSAX1k7vu9q9mNoo4Tz59356m70HqrP0a TD6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=xDpo8gogWsXU220D6cDIpGV/0kNdqhcUS+hgkHtYYAM=; b=Df88SgnoT18T2iQXp+Bi/TVkk1oxXEjLuL1SVHMERljoiMN4UMMW1AVu9Biz7m14/U OFzHp8qcgDZSwOOQjX59g4CBIWrZqT8UUO/kOQjIKef+QRj4gHiGlosK75qQnFg3svjv JCJbNNyHkpvsm5ITn6ptMvGf3vzbtHPDt1KQje4VU+cKzK8CErJFG/4KZoAVhPAfXzIY VghftCe0m1fdDobLqRe63gKkRIT67tfJYtdgjRwTekEzH9neK707Ujf1ShdbgR54TxTa 6M43veFMBB6Z6EDKapoyb8KONw0075U0qpr0svxi9Qd7Bgfxk4oUeZo7Gwrt7hUkfriZ ZC2g== X-Gm-Message-State: ABuFfohvGLG0+7Lr+laChwo70D8fxV58lJY0xtkhKR4wDndWnNptAvVj 7KGNM5vBt9/BrdJ7/LNH9vl9KJrqB2Q= X-Google-Smtp-Source: ACcGV61hcs3BLMXGhXROz0NjUtcEmsLmBwbXlZ9fHz+VLo2VyU9UhsubdkiBlS0Wm0vkv5VdnmaH3Q== X-Received: by 2002:a17:902:5a89:: with SMTP id r9-v6mr39442175pli.95.1540097050726; Sat, 20 Oct 2018 21:44:10 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id t69-v6sm35156759pgd.43.2018.10.20.21.44.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Oct 2018 21:44:10 -0700 (PDT) From: Xin Long To: network dev , linux-sctp@vger.kernel.org Cc: Marcelo Ricardo Leitner , Neil Horman , davem@davemloft.net Subject: [PATCH net-next 3/3] sctp: process sk_reuseport in sctp_get_port_local Date: Sun, 21 Oct 2018 12:43:38 +0800 Message-Id: <8695b919f856029566aa477b966ec61ab5611338.1540095102.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <661578e3134c79c575d934b3267b327773fd34f7.1540095102.git.lucien.xin@gmail.com> References: <08b533092a01a8f7cf2eb4c459fe3570a8df702b.1540095102.git.lucien.xin@gmail.com> <661578e3134c79c575d934b3267b327773fd34f7.1540095102.git.lucien.xin@gmail.com> In-Reply-To: References: Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When socks' sk_reuseport is set, the same port and address are allowed to be bound into these socks who have the same uid. Note that the difference from sk_reuse is that it allows multiple socks to listen on the same port and address. Signed-off-by: Xin Long --- include/net/sctp/structs.h | 4 +++- net/sctp/socket.c | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/include/net/sctp/structs.h b/include/net/sctp/structs.h index 15d017f..af9d494 100644 --- a/include/net/sctp/structs.h +++ b/include/net/sctp/structs.h @@ -96,7 +96,9 @@ struct sctp_stream; struct sctp_bind_bucket { unsigned short port; - unsigned short fastreuse; + signed char fastreuse; + signed char fastreuseport; + kuid_t fastuid; struct hlist_node node; struct hlist_head owner; struct net *net; diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 44e7d8c..8605705 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -7642,8 +7642,10 @@ static struct sctp_bind_bucket *sctp_bucket_create( static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) { - bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse); + struct sctp_sock *sp = sctp_sk(sk); + bool reuse = (sk->sk_reuse || sp->reuse); struct sctp_bind_hashbucket *head; /* hash list */ + kuid_t uid = sock_i_uid(sk); struct sctp_bind_bucket *pp; unsigned short snum; int ret; @@ -7719,7 +7721,10 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) pr_debug("%s: found a possible match\n", __func__); - if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING) + if ((pp->fastreuse && reuse && + sk->sk_state != SCTP_SS_LISTENING) || + (pp->fastreuseport && sk->sk_reuseport && + uid_eq(pp->fastuid, uid))) goto success; /* Run through the list of sockets bound to the port @@ -7733,16 +7738,18 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) * in an endpoint. */ sk_for_each_bound(sk2, &pp->owner) { - struct sctp_endpoint *ep2; - ep2 = sctp_sk(sk2)->ep; + struct sctp_sock *sp2 = sctp_sk(sk2); + struct sctp_endpoint *ep2 = sp2->ep; if (sk == sk2 || - (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) && - sk2->sk_state != SCTP_SS_LISTENING)) + (reuse && (sk2->sk_reuse || sp2->reuse) && + sk2->sk_state != SCTP_SS_LISTENING) || + (sk->sk_reuseport && sk2->sk_reuseport && + uid_eq(uid, sock_i_uid(sk2)))) continue; - if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, - sctp_sk(sk2), sctp_sk(sk))) { + if (sctp_bind_addr_conflict(&ep2->base.bind_addr, + addr, sp2, sp)) { ret = (long)sk2; goto fail_unlock; } @@ -7765,19 +7772,32 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) pp->fastreuse = 1; else pp->fastreuse = 0; - } else if (pp->fastreuse && - (!reuse || sk->sk_state == SCTP_SS_LISTENING)) - pp->fastreuse = 0; + + if (sk->sk_reuseport) { + pp->fastreuseport = 1; + pp->fastuid = uid; + } else { + pp->fastreuseport = 0; + } + } else { + if (pp->fastreuse && + (!reuse || sk->sk_state == SCTP_SS_LISTENING)) + pp->fastreuse = 0; + + if (pp->fastreuseport && + (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid))) + pp->fastreuseport = 0; + } /* We are set, so fill up all the data in the hash table * entry, tie the socket list information with the rest of the * sockets FIXME: Blurry, NPI (ipg). */ success: - if (!sctp_sk(sk)->bind_hash) { + if (!sp->bind_hash) { inet_sk(sk)->inet_num = snum; sk_add_bind_node(sk, &pp->owner); - sctp_sk(sk)->bind_hash = pp; + sp->bind_hash = pp; } ret = 0;