From nobody Thu Nov 12 12:57:43 2026 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 4jQL5g1gMgz70ZW5 for ; Thu, 12 Nov 2026 12:57:43 +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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jQL5g0mjqz4ZHc for ; Thu, 12 Nov 2026 12:57:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794488263; 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=TlZEEh2S9PQdlupT3N7aTEE83dN9gmXA0wLWYYTlZPQ=; b=yOt2ZP4uGzlb3QnPOEmhfJqQzdcB5NVFHj/TqdppZGWz10D+9B9p/qinRu7QjeOlq6G386 UouH4flGutw2QJOGb2GTW8KvLf6gPmTi79rbcscD58s4AegCWAbcfN8upxcgpjFNqlMZO9 uITRiab3Q1gHF152qbF0xBF+eyNnjrcZijGnLPbqV1/OirizE1hDmdUIpcCQxBr0YUiiYC bk0V+cilwN7IGeapxaBmPuAjJ8eYCJpWltG0zIFRdJesKFRy/YzE9zJUugvToyX6PlPTJY UcaLTGVbN+kGuYZTArP9wjS44xGizeSaU8TLiJDO3h0YJjhp2U0klRxBF3nA9A== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794488263; b=m5UlhgT8Z80FyK4l5QfmDbIYekhd2saoUzt0ixvgJtd6TXmmXoUQ2FMF34vlvW8R9pJAwo eoWXjUjchqdI2MlHV8cffbXiZj0POR80vcQzxJueb+XN0fQvUjd1yoBGQ8xCUn+SFPfIBu URTwvDXe1ZUJfacT58Y1qiFZn2lzh+pe7yPDCQGP2Gw1RcvIdZlSlMTF7Vr17RH5/x8XDe UZyB058Shu0Ie5EEK5zvyEkSP3lw1oYJGedWmHb0HIbRqjU1g3vqWbxhMl+0KuszFNSTlT 1EBOueGYrAiSvzVVe42YQSIFoCMruPkzfYYlPQRv0MTv/J7cQ6prISfO4OOOEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794488263; 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=TlZEEh2S9PQdlupT3N7aTEE83dN9gmXA0wLWYYTlZPQ=; b=ETF2eO60c2IAAJZFcQVbgRoBLNcx9zm0NxBPt/MyldQ531dJYkDEIX3B2orExL+nQ+4jag +LtQ4Ea8lRJWX7eJgUY4a9dq5lETM624azYk4Ptb5fVzMAuLA0mxGo00Hg5ahyzdU1MDJF VLE3O8+REyJakSYwmWPT3rdy3SGF7u70qaXLDIIR/hemc6mGxw1kQ7xcKNifP+Of6JbEWV kYmrMX+HgAbKxpfPeVgB2R+KKJrp8uckNYVpownlvP0qyBE9dUIMj/VLE3keYLA14ZQZ02 rGelPl3HWkZ42f312gbDnPmMUTPA4BIDrZcMJT0I3RsgwQmyRmEZjloVlQT60g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jQL5g0Q5Rz13lh for ; Thu, 12 Nov 2026 12:57:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id a3d7 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 12 Nov 2026 12:57:43 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: R. Christian McDonald Subject: git: 53d8733b3dd7 - main - ipsec: Do not reference the default policy for packets without a PCB List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rcm X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 53d8733b3dd754852ec7a2c22b01c82906b8130e Auto-Submitted: auto-generated Date: Thu, 12 Nov 2026 12:57:43 +0000 Message-Id: <6af5b7c7.a3d7.10981504@gitrepo.freebsd.org> The branch main has been updated by rcm: URL: https://cgit.FreeBSD.org/src/commit/?id=53d8733b3dd754852ec7a2c22b01c82906b8130e commit 53d8733b3dd754852ec7a2c22b01c82906b8130e Author: R. Christian McDonald AuthorDate: 2026-11-12 12:53:25 +0000 Commit: R. Christian McDonald CommitDate: 2026-11-12 12:57:21 +0000 ipsec: Do not reference the default policy for packets without a PCB When no security policy matches a packet, ipsec[46]_getpolicy() returns the default policy with a new reference, and the caller drops it right after looking at the policy type. Once any security policy is configured, every forwarded packet goes through ip_forward() and ipsec4_forward(), which checks the inbound and the outbound policy. That is four atomic operations per packet on one reference count that all CPUs write. The default policy is allocated with its vnet and freed only when the vnet goes away, and the sysctl limits it to NONE or DISCARD. A packet without a PCB has no policy cache to keep it in. In that case, check the policy type directly and take no reference, for NONE as well as for DISCARD. Packets with a PCB still take the reference, because the PCB caches the policy. No functional change intended. DISCARD still drops the packet, and the policy violation counters are incremented as before. Reviewed by: kib MFC after: 1 week Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D61384 --- sys/netipsec/ipsec.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/sys/netipsec/ipsec.c b/sys/netipsec/ipsec.c index 12d1b7945e64..d7e72c630f1f 100644 --- a/sys/netipsec/ipsec.c +++ b/sys/netipsec/ipsec.c @@ -568,6 +568,13 @@ ipsec4_setspidx_ipaddr(const struct mbuf *m, struct ip *ip1, spidx->prefd = sizeof(struct in_addr) << 3; } +/* + * Return a held reference to the SP for the packet. Return NULL when no + * SP matches and there is no PCB: the default policy applies then, and + * the caller checks its type, NONE or DISCARD. The default SP lives as + * long as its vnet, so that needs no reference, and the reference count + * would be one cache line written by every CPU for each forwarded packet. + */ static struct secpolicy * ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1, u_int dir, int needport) @@ -583,7 +590,8 @@ ipsec4_getpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1, spidx.dir = dir; sp = key_allocsp(&spidx, dir); } - if (sp == NULL) /* No SP found, use system default. */ + /* No SP found, the PCB caches the system default. */ + if (sp == NULL && inp != NULL) sp = key_allocsp_default(); return (sp); } @@ -601,6 +609,8 @@ ipsec4_checkpolicy(const struct mbuf *m, struct inpcb *inp, struct ip *ip1, sp = ipsec4_getpolicy(m, inp, ip1, IPSEC_DIR_OUTBOUND, needport); if (sp != NULL) sp = ipsec_checkpolicy(sp, inp, error); + else if (V_def_policy->policy == IPSEC_POLICY_DISCARD) + *error = -EINVAL; /* Packet is discarded by caller. */ if (sp == NULL) { switch (*error) { case 0: /* No IPsec required: BYPASS or NONE */ @@ -648,8 +658,11 @@ ipsec4_in_reject1(const struct mbuf *m, struct ip *ip1, struct inpcb *inp) } sp = ipsec4_getpolicy(m, inp, ip1, IPSEC_DIR_INBOUND, 0); - result = ipsec_in_reject(sp, inp, m); - key_freesp(&sp); + if (sp != NULL) { + result = ipsec_in_reject(sp, inp, m); + key_freesp(&sp); + } else + result = (V_def_policy->policy == IPSEC_POLICY_DISCARD); if (result != 0) IPSECSTAT_INC(ips_in_polvio); return (result); @@ -757,6 +770,11 @@ ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx) spidx->prefd = sizeof(struct in6_addr) << 3; } +/* + * Return a held reference to the SP for the packet. Return NULL when no + * SP matches and there is no PCB: the default policy applies then, and + * the caller checks its type without a reference, see ipsec4_getpolicy(). + */ static struct secpolicy * ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, int needport) @@ -772,7 +790,8 @@ ipsec6_getpolicy(const struct mbuf *m, struct inpcb *inp, u_int dir, spidx.dir = dir; sp = key_allocsp(&spidx, dir); } - if (sp == NULL) /* No SP found, use system default. */ + /* No SP found, the PCB caches the system default. */ + if (sp == NULL && inp != NULL) sp = key_allocsp_default(); return (sp); } @@ -790,6 +809,8 @@ ipsec6_checkpolicy(const struct mbuf *m, struct inpcb *inp, int *error, sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_OUTBOUND, needport); if (sp != NULL) sp = ipsec_checkpolicy(sp, inp, error); + else if (V_def_policy->policy == IPSEC_POLICY_DISCARD) + *error = -EINVAL; /* Packet is discarded by caller. */ if (sp == NULL) { switch (*error) { case 0: /* No IPsec required: BYPASS or NONE */ @@ -828,8 +849,11 @@ ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp) return (0); #endif sp = ipsec6_getpolicy(m, inp, IPSEC_DIR_INBOUND, 0); - result = ipsec_in_reject(sp, inp, m); - key_freesp(&sp); + if (sp != NULL) { + result = ipsec_in_reject(sp, inp, m); + key_freesp(&sp); + } else + result = (V_def_policy->policy == IPSEC_POLICY_DISCARD); if (result) IPSEC6STAT_INC(ips_in_polvio); return (result);