git: b9e45c2dca5c - main - netipsec: Clear the crypto error before dispatching a request again

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 17:43:36 UTC
The branch main has been updated by rcm:

URL: https://cgit.FreeBSD.org/src/commit/?id=b9e45c2dca5cf003bb999dffba5eb3c8ed053e51

commit b9e45c2dca5cf003bb999dffba5eb3c8ed053e51
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-16 17:41:39 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-16 17:42:49 +0000

    netipsec: Clear the crypto error before dispatching a request again
    
    When a crypto driver unregisters, a request on one of its sessions
    completes with EAGAIN.  The ESP, AH and IPComp callbacks then move the
    SA to the new session and pass the same request to crypto_dispatch()
    again, with crp_etype still set to EAGAIN.
    
    crypto_dispatch() requires a request without an error.  A kernel with
    INVARIANTS panics there with "incoming crp with error".  The other
    consumers that dispatch a request again after EAGAIN clear the field
    first.
    
    The six callbacks prepare the request for the retry with the same
    code.  Move that code to a new function, ipsec_crypto_retry(), and
    clear the error there.  ipsec_updateid() has no other callers and
    becomes static.  The callbacks still dispatch the request themselves,
    because the input callbacks have to release the sahtree lock first.
    
    Reviewed by:            markj, kib
    Fixes:                  c03414326909 ("Refactor driver and consumer...")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61515
---
 sys/netipsec/ipsec.c        | 19 ++++++++++++++++++-
 sys/netipsec/ipsec.h        |  5 ++++-
 sys/netipsec/xform_ah.c     | 14 ++------------
 sys/netipsec/xform_esp.c    | 14 ++------------
 sys/netipsec/xform_ipcomp.c | 14 ++------------
 5 files changed, 28 insertions(+), 38 deletions(-)

diff --git a/sys/netipsec/ipsec.c b/sys/netipsec/ipsec.c
index 90dcdc8c8c28..ebdd071d942e 100644
--- a/sys/netipsec/ipsec.c
+++ b/sys/netipsec/ipsec.c
@@ -1536,7 +1536,7 @@ ipsec_updatereplay(uint32_t seq, struct secasvar *sav)
 	SECREPLAY_UNLOCK(replay);
 	return (0);
 }
-int
+static int
 ipsec_updateid(struct secasvar *sav, crypto_session_t *new,
     crypto_session_t *old)
 {
@@ -1583,6 +1583,23 @@ ipsec_updateid(struct secasvar *sav, crypto_session_t *new,
 	return (0);
 }
 
+/*
+ * Prepare a request that failed with EAGAIN to be dispatched again.
+ * The caller must release its locks before it dispatches the request.
+ */
+void
+ipsec_crypto_retry(struct secasvar *sav, struct xform_data *xd,
+    struct cryptop *crp)
+{
+	crypto_session_t cryptoid;
+
+	cryptoid = xd->cryptoid;
+	if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
+		crypto_freesession(cryptoid);
+	xd->cryptoid = crp->crp_session;
+	crp->crp_etype = 0;
+}
+
 int
 ipsec_initialized(void)
 {
diff --git a/sys/netipsec/ipsec.h b/sys/netipsec/ipsec.h
index 260c1cd6b901..1f613df2160d 100644
--- a/sys/netipsec/ipsec.h
+++ b/sys/netipsec/ipsec.h
@@ -329,6 +329,7 @@ VNET_DECLARE(int, natt_cksum_policy);
 #define	DPRINTF(x)
 #endif
 
+struct cryptop;
 struct inpcb;
 struct ip;
 struct m_tag;
@@ -336,6 +337,7 @@ struct rm_priotracker;
 struct secasvar;
 struct sockopt;
 struct tcphdr;
+struct xform_data;
 union sockaddr_union;
 
 int ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t,
@@ -353,7 +355,8 @@ int udp_ipsec_output(struct mbuf *, struct secasvar *);
 
 int ipsec_chkreplay(uint32_t, uint32_t *, struct secasvar *);
 int ipsec_updatereplay(uint32_t, struct secasvar *);
-int ipsec_updateid(struct secasvar *, crypto_session_t *, crypto_session_t *);
+void ipsec_crypto_retry(struct secasvar *, struct xform_data *,
+    struct cryptop *);
 int ipsec_initialized(void);
 size_t ipsec_hdrsiz_internal(struct secpolicy *);
 
diff --git a/sys/netipsec/xform_ah.c b/sys/netipsec/xform_ah.c
index cea329eaa827..ffab85f8128e 100644
--- a/sys/netipsec/xform_ah.c
+++ b/sys/netipsec/xform_ah.c
@@ -695,7 +695,6 @@ ah_input_cb(struct cryptop *crp)
 	struct secasvar *sav;
 	struct secasindex *saidx;
 	caddr_t ptr;
-	crypto_session_t cryptoid;
 	int authsize, rplen, ahsize, error, skip, protoff;
 	uint8_t nxt;
 
@@ -708,7 +707,6 @@ ah_input_cb(struct cryptop *crp)
 	skip = xd->skip;
 	nxt = xd->nxt;
 	protoff = xd->protoff;
-	cryptoid = xd->cryptoid;
 	ipsec_sahtree_rlock(&sahtree_tracker);
 	if (sav->state >= SADB_SASTATE_DEAD) {
 		/* saidx is freed */
@@ -725,10 +723,7 @@ ah_input_cb(struct cryptop *crp)
 	/* Check for crypto errors. */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			ipsec_sahtree_runlock(&sahtree_tracker);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
@@ -1100,7 +1095,6 @@ ah_output_cb(struct cryptop *crp)
 	struct secpolicy *sp;
 	struct secasvar *sav;
 	struct mbuf *m;
-	crypto_session_t cryptoid;
 	caddr_t ptr;
 	u_int idx;
 	int skip, error;
@@ -1112,16 +1106,12 @@ ah_output_cb(struct cryptop *crp)
 	sav = xd->sav;
 	skip = xd->skip;
 	idx = xd->idx;
-	cryptoid = xd->cryptoid;
 	ptr = (caddr_t) (xd + 1);
 
 	/* Check for crypto errors. */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
 		}
diff --git a/sys/netipsec/xform_esp.c b/sys/netipsec/xform_esp.c
index 618556fa84a6..ff84c6b0a000 100644
--- a/sys/netipsec/xform_esp.c
+++ b/sys/netipsec/xform_esp.c
@@ -520,7 +520,6 @@ esp_input_cb(struct cryptop *crp)
 	struct xform_data *xd;
 	struct secasvar *sav;
 	struct secasindex *saidx;
-	crypto_session_t cryptoid;
 	int hlen, skip, protoff, error, alen;
 
 	SECASVAR_RLOCK_TRACKER;
@@ -540,17 +539,13 @@ esp_input_cb(struct cryptop *crp)
 	}
 	skip = xd->skip;
 	protoff = xd->protoff;
-	cryptoid = xd->cryptoid;
 	saidx = &sav->sah->saidx;
 	esph = sav->tdb_authalgxform;
 
 	/* Check for crypto errors */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			ipsec_sahtree_runlock(&sahtree_tracker);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
@@ -1005,7 +1000,6 @@ esp_output_cb(struct cryptop *crp)
 	struct secpolicy *sp;
 	struct secasvar *sav;
 	struct mbuf *m;
-	crypto_session_t cryptoid;
 	u_int idx;
 	int error;
 
@@ -1016,15 +1010,11 @@ esp_output_cb(struct cryptop *crp)
 	sp = xd->sp;
 	sav = xd->sav;
 	idx = xd->idx;
-	cryptoid = xd->cryptoid;
 
 	/* Check for crypto errors. */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
 		}
diff --git a/sys/netipsec/xform_ipcomp.c b/sys/netipsec/xform_ipcomp.c
index aecaf79f39bd..87bdf9eeb8e8 100644
--- a/sys/netipsec/xform_ipcomp.c
+++ b/sys/netipsec/xform_ipcomp.c
@@ -289,7 +289,6 @@ ipcomp_input_cb(struct cryptop *crp)
 	struct secasvar *sav;
 	struct secasindex *saidx;
 	caddr_t addr;
-	crypto_session_t cryptoid;
 	int hlen = IPCOMP_HLENGTH, error, clen;
 	int skip, protoff;
 	uint8_t nproto;
@@ -300,7 +299,6 @@ ipcomp_input_cb(struct cryptop *crp)
 	sav = xd->sav;
 	skip = xd->skip;
 	protoff = xd->protoff;
-	cryptoid = xd->cryptoid;
 	ipsec_sahtree_rlock(&sahtree_tracker);
 	if (sav->state >= SADB_SASTATE_DEAD) {
 		/* saidx is freed */
@@ -317,10 +315,7 @@ ipcomp_input_cb(struct cryptop *crp)
 	/* Check for crypto errors */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			ipsec_sahtree_runlock(&sahtree_tracker);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
@@ -548,7 +543,6 @@ ipcomp_output_cb(struct cryptop *crp)
 	struct secpolicy *sp;
 	struct secasvar *sav;
 	struct mbuf *m;
-	crypto_session_t cryptoid;
 	u_int idx;
 	int error, skip, protoff;
 
@@ -560,15 +554,11 @@ ipcomp_output_cb(struct cryptop *crp)
 	sav = xd->sav;
 	skip = xd->skip;
 	protoff = xd->protoff;
-	cryptoid = xd->cryptoid;
 
 	/* Check for crypto errors */
 	if (crp->crp_etype) {
 		if (crp->crp_etype == EAGAIN) {
-			/* Reset the session ID */
-			if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
-				crypto_freesession(cryptoid);
-			xd->cryptoid = crp->crp_session;
+			ipsec_crypto_retry(sav, xd, crp);
 			CURVNET_RESTORE();
 			return (crypto_dispatch(crp));
 		}