Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1391316pxv; Fri, 16 Jul 2021 08:20:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz6Yau75Jfse1GRp90XJvU+nE6nbQI9+jvjTnAdM0ODhznUQ6CHUjgE73yj2UuMIugHg1RH X-Received: by 2002:a05:6402:1118:: with SMTP id u24mr15443368edv.229.1626448822961; Fri, 16 Jul 2021 08:20:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626448822; cv=none; d=google.com; s=arc-20160816; b=zAnRAnlXomdVgWHpg5UOiMPyMzj1P09xS6R6V1o9GeEyeew1oJRVB4ebAWdFVcTFA1 kxQkxyV2lWNLwQm7Jwz8a2RwXmq3VjvzP4urUbXvDSrUBGZRfg44E8UPU+nASaI7e/19 zhFzMNaRWstxfBFtuPAaeJoOuFr1QT7t72buDujbp0bMkwzdzJoLciI70FzJT5YI820N IYQNWiokeoqYI2YkKwmL+AM8iR61IK4lWWeytB/zT4OVPOUzYAEr99VRv9JUDuzL0V70 8ZWG7n0J7/8oA6xo2Pg8r6K7FHF47JUweNjoRzm7VUKjWoqxGHE7RoUA2YTudH9Y14WA Fkkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=wO0yE5GEGQqh7DN5Xw4BxDTNY2iq6aBQ/N2rp/V2adE=; b=Il0kRxnb+m0yZlJ48jkgkQXdI9LhK7lLejd4+wWgIZkeGNPVip4tUmebw4fEIK19VC tWyIOREeESIMpiWVe1u813+/Jl2bXHHgra76UtMKx0oYSP6wIo3/IvqpPrrU6S3yDfXD t+O3SXAG+gCqMxogDeQ4wq9nGnpEQBcit1EvrX76Kl9mF9kK6fFbf39JaGmCQfimNdsy /d4ZFcTJH2hds8Uy6uxfu2p0j+xiblyaYVbccQ6jrk/8Yj6+sHudRPBDrruLVNM5hmpL +C9lrbJsXbUF64bOi8Y1fsk7kx+OLUrtnEimUvJGUF9x5SOwTxGyjCmQKQquDs9yTbNk yibg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w10si12976801ejv.754.2021.07.16.08.19.57; Fri, 16 Jul 2021 08:20:22 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240573AbhGPPVk (ORCPT + 99 others); Fri, 16 Jul 2021 11:21:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233094AbhGPPVi (ORCPT ); Fri, 16 Jul 2021 11:21:38 -0400 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [IPv6:2a0a:51c0:0:12e:520::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20E62C06175F; Fri, 16 Jul 2021 08:18:42 -0700 (PDT) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1m4Pbl-0004ys-49; Fri, 16 Jul 2021 17:18:33 +0200 Date: Fri, 16 Jul 2021 17:18:33 +0200 From: Florian Westphal To: Cole Dishington Cc: pablo@netfilter.org, kadlec@netfilter.org, fw@strlen.de, davem@davemloft.net, kuba@kernel.org, shuah@kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Anthony Lineham , Scott Parlane , Blair Steven Subject: Re: [PATCH 2/3] net: netfilter: Add RFC-7597 Section 5.1 PSID support Message-ID: <20210716151833.GD9904@breakpoint.cc> References: <20210705103959.GG18022@breakpoint.cc> <20210716002742.31078-1-Cole.Dishington@alliedtelesis.co.nz> <20210716002742.31078-3-Cole.Dishington@alliedtelesis.co.nz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210716002742.31078-3-Cole.Dishington@alliedtelesis.co.nz> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Cole Dishington wrote: > diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c > index 7de595ead06a..4a9448684504 100644 > --- a/net/netfilter/nf_nat_core.c > +++ b/net/netfilter/nf_nat_core.c > @@ -195,13 +195,36 @@ static bool nf_nat_inet_in_range(const struct nf_conntrack_tuple *t, > static bool l4proto_in_range(const struct nf_conntrack_tuple *tuple, > enum nf_nat_manip_type maniptype, > const union nf_conntrack_man_proto *min, > - const union nf_conntrack_man_proto *max) > + const union nf_conntrack_man_proto *max, > + const union nf_conntrack_man_proto *base, > + bool is_psid) > { > __be16 port; > + u16 psid, psid_mask, offset_mask; > + > + /* In this case we are in PSID mode, avoid checking all ranges by computing bitmasks */ > + if (is_psid) { > + u16 power_j = ntohs(max->all) - ntohs(min->all) + 1; > + u32 offset = ntohs(base->all); > + u16 power_a; > + > + if (offset == 0) > + offset = 1 << 16; > + > + power_a = (1 << 16) / offset; Since the dividie is only needed nat setup and not for each packet I think its ok. > + if (range->flags & NF_NAT_RANGE_PSID) { > + u16 base = ntohs(range->base_proto.all); > + u16 min = ntohs(range->min_proto.all); > + u16 off = 0; > + > + /* If offset=0, port range is in one contiguous block */ > + if (base) > + off = prandom_u32() % (((1 << 16) / base) - 1); Bases 32769 > gives 0 for the modulo value, so perhaps compute that independently. You could reject > 32769 in the iptables checkentry target. Also, base of 21846 and above always give 0 result (% 1). I don't know psid well enough to give a recommendation here. If such inputs are nonsensical, just reject it when userspace asks for this and add a if (WARN_ON_ONCE(base > bogus)) return NF_DROP; with s small coment explaining that xtables is supposed to not provide such value. Other than this I think its ok. I still dislike the 'bool is_psid' in the nat core, but I can't find a better solution.