From nobody Tue Sep 10 14:59:30 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4X36LB69FPz5Vxyc; Tue, 10 Sep 2024 14:59:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4X36LB5SNtz4d1l; Tue, 10 Sep 2024 14:59:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725980370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/4w/XSJSrlkw6/58OgoSxLl+GTiHNtAeIK4xcE4oiJA=; b=A6PGHZOyFSroq4KWg8sk2EoKFvTSlSJQ2YQ0gQEY3X7WeXMAJGVNYlhLlLDdXX8WHbfFvR TGa/RKev3smRaZOzWaZ1QflzXAxubv4t5tQejlxUtwyNhERhoFKXvA1tJSOLWzWYwlM47X +o7+i8ADpRijuhtVJ0ZB8O8VymWzgkQ7AEfaUT1k4ZrILlO0B7rt34EZ80zQLrySKogZD/ 6mooyjUqfRTG/07vlvi7J0f0eVSoaelX3XfTaAE8EnUyB2Agum1iSt97dI6AHm+zH6sHnK AdjIKRpjXQ8kRq3MXVjWrl2Yta7sk7pBWM/WiwkfElRFSUkihIlCOBtpSncqGQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725980370; a=rsa-sha256; cv=none; b=na86SVJTjdFr1Q1CSPa0nzg29RbHUo4QiDWVL+85ZEwF4L9Yd55tll+E83PWmRCm/zjc0Y J+nnZPcKtYp4jfbZUMhGYEkfjAXlEr++HaSl94uKBvOMZtBJb/EguhWIVusCPwTaJ0m5NX 8DIXr3RuQijBlRIA6PkbIkeCD9+v0VM0zxIxk+B/Z1HTqLIDmOTg2yksKrnybZ2CLo/9vj h00t3LFTk95gRfDewG5MlKd7IZTnw66hhsLZLG5RMqkDMxYqIVnF+UP47Oyjosj4twVGxq WLt1UlbPgU9tvrvTV4YNe4CbwsR0iLjpgZeFqxfVcUwPoPC0KTudHcvgUf2JZQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725980370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/4w/XSJSrlkw6/58OgoSxLl+GTiHNtAeIK4xcE4oiJA=; b=lYC2TqJYzN6EY8vp9x1yPkpNrtsfKWz2fcgmW/sWpdjUL+eMCLCQi2uzeo5PEKuH8LOj/M lXD/weOu56U5kojHqg4hbRJ3vWWaTBKeqPSr8uX5fcjlBZf+Ny+eRYkrh7M77vnJ8aPQtf /qyZTzU1wMX+s0WbWby1oWWQ+M3JGM4vIMDDRN0SejrXM9X9CLR/aznXHTfaA6tOb2BpIR YeNRYTm1ULK6mnsWMOu5bVLhhQBCgLYQXGd17zewgTHJkTbgAjso4Ow/zCZvkMhnszkUfB uttgZEe4VnHSHC3ng9g0JLVeIgYlq+l0dOSXSSO5he2suuruCamp17AZ4C+wTQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4X36LB4l3Tzs9b; Tue, 10 Sep 2024 14:59:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48AExUGb080542; Tue, 10 Sep 2024 14:59:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48AExUiI080539; Tue, 10 Sep 2024 14:59:30 GMT (envelope-from git) Date: Tue, 10 Sep 2024 14:59:30 GMT Message-Id: <202409101459.48AExUiI080539@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 339a1977c324 - main - pf: Add a sysctl to limit work done for rdr source port rewriting List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 339a1977c32414f3d23733504955245ca6f3802d Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=339a1977c32414f3d23733504955245ca6f3802d commit 339a1977c32414f3d23733504955245ca6f3802d Author: Mark Johnston AuthorDate: 2024-09-10 14:33:47 +0000 Commit: Mark Johnston CommitDate: 2024-09-10 14:58:19 +0000 pf: Add a sysctl to limit work done for rdr source port rewriting It was pointed out that the current approach of exhaustively searching for a free source port might be very time consuming. Limit the amount of work that we might do before giving up. Reviewed by: kp Reported by: Eirik Øverby MFC after: 3 months Sponsored by: Klara, Inc. Sponsored by: Modirum Differential Revision: https://reviews.freebsd.org/D46495 --- share/man/man4/pf.4 | 7 ++++++- share/man/man5/pf.conf.5 | 6 +++++- sys/netpfil/pf/pf_lb.c | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/share/man/man4/pf.4 b/share/man/man4/pf.4 index 3855d07faead..92e1a6fdf87b 100644 --- a/share/man/man4/pf.4 +++ b/share/man/man4/pf.4 @@ -102,8 +102,13 @@ This tells .Nm to also filter on the loopback output hook. This is typically used to allow redirect rules to adjust the source address. -.It net.pf.request_maxcount +.It Va net.pf.request_maxcount The maximum number of items in a single ioctl call. +.It Va net.pf.rdr_srcport_rewrite_tries +The maximum number of times to try and find a free source port when handling +redirects. +Such rules are typically applied to external traffic, so an exhaustive search +may be too expensive. .El .Pp Read only diff --git a/share/man/man5/pf.conf.5 b/share/man/man5/pf.conf.5 index 5aa936d509ed..733b00cc6732 100644 --- a/share/man/man5/pf.conf.5 +++ b/share/man/man5/pf.conf.5 @@ -1407,7 +1407,11 @@ A .Ar rdr rule may cause the source port to be modified if doing so avoids a conflict with an existing connection. -A random source port in the range 50001-65535 is chosen in this case. +A random source port in the range 50001-65535 is chosen in this case; to +avoid excessive CPU consumption, the number of searches for a free port is +limited by the +.Va net.pf.rdr_srcport_rewrite_tries +sysctl. Port numbers are never translated with a .Ar binat rule. diff --git a/sys/netpfil/pf/pf_lb.c b/sys/netpfil/pf/pf_lb.c index cdd68aaf5dab..dbd85d530bb7 100644 --- a/sys/netpfil/pf/pf_lb.c +++ b/sys/netpfil/pf/pf_lb.c @@ -52,6 +52,13 @@ #include #include +/* + * Limit the amount of work we do to find a free source port for redirects that + * introduce a state conflict. + */ +#define V_pf_rdr_srcport_rewrite_tries VNET(pf_rdr_srcport_rewrite_tries) +VNET_DEFINE_STATIC(int, pf_rdr_srcport_rewrite_tries) = 16; + #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x static void pf_hash(struct pf_addr *, struct pf_addr *, @@ -822,6 +829,7 @@ pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, break; case PF_RDR: { struct pf_state_key_cmp key; + int tries; uint16_t cut, low, high, nport; reason = pf_map_addr(pd->af, r, saddr, naddr, NULL, NULL, sn); @@ -873,11 +881,15 @@ pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, if (!pf_find_state_all_exists(&key, PF_OUT)) break; + tries = 0; + low = 50001; /* XXX-MJ PF_NAT_PROXY_PORT_LOW/HIGH */ high = 65535; cut = arc4random() % (1 + high - low) + low; for (uint32_t tmp = cut; - tmp <= high && tmp <= UINT16_MAX; tmp++) { + tmp <= high && tmp <= UINT16_MAX && + tries < V_pf_rdr_srcport_rewrite_tries; + tmp++, tries++) { key.port[0] = htons(tmp); if (!pf_find_state_all_exists(&key, PF_OUT)) { /* Update the source port. */ @@ -885,7 +897,9 @@ pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, goto out; } } - for (uint32_t tmp = cut - 1; tmp >= low; tmp--) { + for (uint32_t tmp = cut - 1; + tmp >= low && tries < V_pf_rdr_srcport_rewrite_tries; + tmp--, tries++) { key.port[0] = htons(tmp); if (!pf_find_state_all_exists(&key, PF_OUT)) { /* Update the source port. */