Skip to contentSkip to conversation

tlshd: allow kernels without TLS_TX_MAX_PAYLOAD_LEN

PATCH In review Martin Belanger 3 participants 3 messages Nov 30 – Dec 1 Handshake upcall for transport layer security

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tlshd: allow kernels without TLS_TX_MAX_PAYLOAD_LEN

From: Martin Belanger <[email protected]>

configure detects TLS_TX_MAX_PAYLOAD_LEN in the installed kernel headers. A kernel older than 6.19 rejects the option with ENOPROTOOPT, and tlshd then fails every handshake. Distributions often ship newer headers than the running kernel.

Ignore ENOPROTOOPT when the peer did not negotiate a record size limit below 2^14 bytes. The kernel then sends records of at most 2^14 bytes, which the peer accepts. If the peer asked for smaller records, the error stays fatal.

Fixes c1610edc2f08 ("tlshd: support setting the record size limit")

---
Tested in a VM with nvme-tcp TLS to a loopback nvmet target, tlshd built against 7.x kernel headers:
- 6.18.63: without the patch, the handshake fails ("setsockopt (TLS_TX_MAX_PAYLOAD_LEN): Protocol not available"). With the patch, the connect succeeds.
- 7.3.4: with the patch, the option is set and the connect succeeds. Not tested: a peer that negotiates a smaller record_size_limit on an old kernel. That case still fails, as intended.

---
 src/tlshd/ktls.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
src/tlshd/ktls.c +14 -2
index 07651bc..2251753 100644
@@ -348,18 +348,30 @@ static bool tlshd_set_chacha20_poly1305_info(gnutls_session_t session, int sock,
 #endif
 
 #if defined(HAVE_GNUTLS_RECORD_GET_MAX_SEND_SIZE) && defined(HAVE_TLS_TX_MAX_PAYLOAD_LEN)
+/* RFC 8446, Section 5.1: largest TLSPlaintext.fragment */
+#define TLSHD_MAX_PLAINTEXT	16384
+
 static int tlshd_set_record_size(gnutls_session_t session)
 {
-	uint16_t max_send_size;
+	size_t limit = gnutls_record_get_max_send_size(session);
+	uint16_t max_send_size = limit;
 	int ret;
 
-	max_send_size = gnutls_record_get_max_send_size(session);
 	/* For TLS 1.3 kernel expects us to account for the ContentType */
 	if (gnutls_protocol_get_version(session) == GNUTLS_TLS1_3)
 		max_send_size -= 1;
 
 	ret = setsockopt(gnutls_transport_get_int(session), SOL_TLS,
 			 TLS_TX_MAX_PAYLOAD_LEN, &max_send_size, sizeof(max_send_size));
+	if (ret < 0 && errno == ENOPROTOOPT && limit >= TLSHD_MAX_PLAINTEXT) {
+		/*
+		 * The kernel predates TLS_TX_MAX_PAYLOAD_LEN. It sends
+		 * records of up to 2^14 bytes, which the peer accepts
+		 * because it did not ask for a smaller record size limit.
+		 */
+		tlshd_log_debug("kernel does not support TLS_TX_MAX_PAYLOAD_LEN");
+		return 0;
+	}
 	if (ret < 0)
 		tlshd_log_perror("setsockopt (TLS_TX_MAX_PAYLOAD_LEN)");
 

---
base-commit: e957590189ea88663dee97348dd42a5dceb4a6ef
change-id: 20261130-tx-payload-len-fallback-190b51565c57

Best regards,

Signature · 1 line
Martin Belanger <[email protected]>
maintainer
#1 lore raw
Quoted 7 lines · On Mon, Nov 30, 2026, at 6:32 AM, Martin Belanger via B4 Relay wrote:

From: Martin Belanger <[email protected]>

configure detects TLS_TX_MAX_PAYLOAD_LEN in the installed kernel headers. A kernel older than 6.19 rejects the option with ENOPROTOOPT, and tlshd then fails every handshake. Distributions often ship newer headers than the running kernel.

Quoted 8 lines

Ignore ENOPROTOOPT when the peer did not negotiate a record size limit below 2^14 bytes. The kernel then sends records of at most 2^14 bytes, which the peer accepts. If the peer asked for smaller records, the error stays fatal.

Fixes c1610edc2f08 ("tlshd: support setting the record size limit")

Hi Martin -

While we're waiting for review from others on the mailing list, I've opened https://github.com/linux-nfs/ktls-utils/pull/65

Signature · 1 line
Chuck Lever
Reviewed-by Alistair Francis on v1 1/1
#2 lore raw
In reply to Martin Belanger
Quoted 6 lines · On Mon, 2026-11-30 at 06:32 -0500, Martin Belanger via B4 Relay wrote:

From: Martin Belanger <[email protected]>

configure detects TLS_TX_MAX_PAYLOAD_LEN in the installed kernel headers. A kernel older than 6.19 rejects the option with ENOPROTOOPT, and tlshd then fails every handshake. Distributions often ship newer headers than the running kernel.

Do they? Doesn't that break lots of things?

What distros do that?

Quoted 9 lines

Ignore ENOPROTOOPT when the peer did not negotiate a record size limit below 2^14 bytes. The kernel then sends records of at most 2^14 bytes, which the peer accepts. If the peer asked for smaller records, the error stays fatal.

Fixes c1610edc2f08 ("tlshd: support setting the record size limit")

Alistair

Quoted 70 lines

---
Tested in a VM with nvme-tcp TLS to a loopback nvmet target, tlshd built against 7.x kernel headers:
- 6.18.63: without the patch, the handshake fails ("setsockopt (TLS_TX_MAX_PAYLOAD_LEN): Protocol not available"). With the patch, the connect succeeds.
- 7.3.4: with the patch, the option is set and the connect succeeds. Not tested: a peer that negotiates a smaller record_size_limit on an old kernel. That case still fails, as intended.
---
 src/tlshd/ktls.c | 16 ++++++++++++++--

 1 file changed, 14 insertions(+), 2 deletions(-)
src/tlshd/ktls.c +0 -0
index 07651bc..2251753 100644
@@ -348,18 +348,30 @@ static bool
 tlshd_set_chacha20_poly1305_info(gnutls_session_t session, int sock,
  #endif


 #if defined(HAVE_GNUTLS_RECORD_GET_MAX_SEND_SIZE) && defined(HAVE_TLS_TX_MAX_PAYLOAD_LEN)

src/tlshd/ktls.c +3 -0
+/* RFC 8446, Section 5.1: largest TLSPlaintext.fragment */
+#define TLSHD_MAX_PLAINTEXT	16384
+

 static int tlshd_set_record_size(gnutls_session_t session) {

src/tlshd/ktls.c +2 -1
-	uint16_t max_send_size;
+	size_t limit = gnutls_record_get_max_send_size(session);
+	uint16_t max_send_size = limit;

  int ret;

src/tlshd/ktls.c +0 -1
-	max_send_size = gnutls_record_get_max_send_size(session);

  /* For TLS 1.3 kernel expects us to account for the ContentType */
  if (gnutls_protocol_get_version(session) == GNUTLS_TLS1_3) max_send_size -= 1;

  ret = setsockopt(gnutls_transport_get_int(session), SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, &max_send_size, sizeof(max_send_size));

src/tlshd/ktls.c +1 -0
+	if (ret < 0 && errno == ENOPROTOOPT && limit >=

TLSHD_MAX_PLAINTEXT) {

src/tlshd/ktls.c +2 -0
+		/*
+		 * The kernel predates TLS_TX_MAX_PAYLOAD_LEN. It

sends

src/tlshd/ktls.c +1 -0
+		 * records of up to 2^14 bytes, which the peer

accepts

src/tlshd/ktls.c +1 -0
+		 * because it did not ask for a smaller record size

limit.

src/tlshd/ktls.c +2 -0
+		 */
+		tlshd_log_debug("kernel does not support

TLS_TX_MAX_PAYLOAD_LEN");

src/tlshd/ktls.c +2 -0
+		return 0;
+	}

  if (ret < 0)
  tlshd_log_perror("setsockopt
(TLS_TX_MAX_PAYLOAD_LEN)");

---
base-commit: e957590189ea88663dee97348dd42a5dceb4a6ef
change-id: 20261130-tx-payload-len-fallback-190b51565c57

Best regards,
--
Martin Belanger <[email protected]>