[PATCH net-next 2/4] psp: move code from psp_sock_assoc_set_tx() into helper functions

From: Daniel Zahka

Date: Thu Sep 03 2026 - 21:35:05 EST


No functional changes.

Lift code that needs to be called from both initial tx establishment
and tx rekeying into functions that can be reused in both paths.

The plaintext in recv queue checks and mss adjustment only run on
initial tx keying. The dummy psp_assoc machinery will be used in both
paths.

psp_dev_tx_key_add() absorbs the dummy assoc machinery, as its main
purpose is to populate the device specific psp_assoc:drv_data.

psp_assoc_set_tx() exists so that a future change can allow
psp_dev_tx_key_add() to be skipped entirely for devices that don't use
an SADB, while copying of the tx spi and key into the assoc needs to
happen regardless of SADB vs. no SADB.

Signed-off-by: Daniel Zahka <daniel.zahka@xxxxxxxxx>
---
net/psp/psp_sock.c | 102 ++++++++++++++++++++++++++++++++++-------------------
1 file changed, 65 insertions(+), 37 deletions(-)

diff --git a/net/psp/psp_sock.c b/net/psp/psp_sock.c
index 045389671d7f..36eb06faa54a 100644
--- a/net/psp/psp_sock.c
+++ b/net/psp/psp_sock.c
@@ -78,9 +78,28 @@ static struct psp_assoc *psp_assoc_dummy(struct psp_assoc *pas)
}

static int psp_dev_tx_key_add(struct psp_dev *psd, struct psp_assoc *pas,
+ struct psp_key_parsed *key,
struct netlink_ext_ack *extack)
{
- return psd->ops->tx_key_add(psd, pas, extack);
+ struct psp_assoc *dummy;
+ int err;
+
+ /* Pass a fake association to drivers to make sure they don't
+ * try to store pointers to it. For re-keying we'll need to
+ * re-allocate the assoc structures.
+ */
+ dummy = psp_assoc_dummy(pas);
+ if (!dummy)
+ return -ENOMEM;
+
+ memcpy(&dummy->tx, key, sizeof(*key));
+ err = psd->ops->tx_key_add(psd, dummy, extack);
+ if (!err)
+ memcpy(pas->drv_data, dummy->drv_data,
+ psd->caps->assoc_drv_spc);
+
+ kfree(dummy);
+ return err;
}

void psp_dev_tx_key_del(struct psp_dev *psd, struct psp_assoc *pas)
@@ -156,6 +175,20 @@ int psp_sock_assoc_set_rx(struct sock *sk, struct psp_assoc *pas,
return err;
}

+static int psp_assoc_set_tx(struct psp_dev *psd, struct psp_assoc *pas,
+ struct psp_key_parsed *key,
+ struct netlink_ext_ack *extack)
+{
+ int err;
+
+ err = psp_dev_tx_key_add(psd, pas, key, extack);
+ if (err)
+ return err;
+
+ memcpy(&pas->tx, key, sizeof(*key));
+ return 0;
+}
+
static int psp_sock_recv_queue_check(struct sock *sk, struct psp_assoc *pas)
{
struct psp_skb_ext *pse;
@@ -175,12 +208,40 @@ static int psp_sock_recv_queue_check(struct sock *sk, struct psp_assoc *pas)
return 0;
}

+static int
+psp_sock_set_tx_key(struct sock *sk, struct psp_dev *psd, struct psp_assoc *pas,
+ struct psp_key_parsed *key, struct netlink_ext_ack *extack)
+{
+ struct inet_connection_sock *icsk;
+ int err;
+
+ err = psp_sock_recv_queue_check(sk, pas);
+ if (err) {
+ NL_SET_ERR_MSG(extack,
+ "Socket has incompatible segments already in the recv queue");
+ return err;
+ }
+
+ err = psp_assoc_set_tx(psd, pas, key, extack);
+ if (err)
+ return err;
+
+ WRITE_ONCE(sk->sk_validate_xmit_skb, psp_validate_xmit);
+ tcp_write_collapse_fence(sk);
+ pas->upgrade_seq = tcp_sk(sk)->rcv_nxt;
+
+ icsk = inet_csk(sk);
+ icsk->icsk_ext_hdr_len += psp_sk_overhead(sk);
+ icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
+
+ return err;
+}
+
int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
u32 version, struct psp_key_parsed *key,
struct netlink_ext_ack *extack)
{
- struct inet_connection_sock *icsk;
- struct psp_assoc *pas, *dummy;
+ struct psp_assoc *pas;
int err;

lock_sock(sk);
@@ -208,40 +269,7 @@ int psp_sock_assoc_set_tx(struct sock *sk, struct psp_dev *psd,
goto exit_unlock;
}

- err = psp_sock_recv_queue_check(sk, pas);
- if (err) {
- NL_SET_ERR_MSG(extack, "Socket has incompatible segments already in the recv queue");
- goto exit_unlock;
- }
-
- /* Pass a fake association to drivers to make sure they don't
- * try to store pointers to it. For re-keying we'll need to
- * re-allocate the assoc structures.
- */
- dummy = psp_assoc_dummy(pas);
- if (!dummy) {
- err = -ENOMEM;
- goto exit_unlock;
- }
-
- memcpy(&dummy->tx, key, sizeof(*key));
- err = psp_dev_tx_key_add(psd, dummy, extack);
- if (err)
- goto exit_free_dummy;
-
- memcpy(pas->drv_data, dummy->drv_data, psd->caps->assoc_drv_spc);
- memcpy(&pas->tx, key, sizeof(*key));
-
- WRITE_ONCE(sk->sk_validate_xmit_skb, psp_validate_xmit);
- tcp_write_collapse_fence(sk);
- pas->upgrade_seq = tcp_sk(sk)->rcv_nxt;
-
- icsk = inet_csk(sk);
- icsk->icsk_ext_hdr_len += psp_sk_overhead(sk);
- icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
-
-exit_free_dummy:
- kfree(dummy);
+ err = psp_sock_set_tx_key(sk, psd, pas, key, extack);
exit_unlock:
release_sock(sk);
return err;

--
2.52.0