<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 4b729a06b15fc5ee3694dcc62346dcb718ae4290 Mon Sep 17 00:00:00 2001
From: Oliver Hartkopp &lt;socketcan@hartkopp.net&gt;
Date: Sun, 26 Mar 2023 13:59:11 +0200
Subject: [PATCH] can: isotp: add module parameter for maximum pdu size

commit 96d1c81e6a0478535342dff6c730adb076cd84e8 upstream.

With ISO 15765-2:2016 the PDU size is not limited to 2^12 - 1 (4095)
bytes but can be represented as a 32 bit unsigned integer value which
allows 2^32 - 1 bytes (~4GB). The use-cases like automotive unified
diagnostic services (UDS) and flashing of ECUs still use the small
static buffers which are provided at socket creation time.

When a use-case requires to transfer PDUs up to 1025 kByte the maximum
PDU size can now be extended by setting the module parameter
max_pdu_size. The extended size buffers are only allocated on a
per-socket/connection base when needed at run-time.

changes since v2: https://lore.kernel.org/all/20230313172510.3851-1-socketcan@hartkopp.net
- use ARRAY_SIZE() to reference DEFAULT_MAX_PDU_SIZE only at one place

changes since v1: https://lore.kernel.org/all/20230311143446.3183-1-socketcan@hartkopp.net
- limit the minimum 'max_pdu_size' to 4095 to maintain the classic
  behavior before ISO 15765-2:2016

Link: https://github.com/raspberrypi/linux/issues/5371
Signed-off-by: Oliver Hartkopp &lt;socketcan@hartkopp.net&gt;
Link: https://lore.kernel.org/all/20230326115911.15094-1-socketcan@hartkopp.net
Signed-off-by: Marc Kleine-Budde &lt;mkl@pengutronix.de&gt;
---
 net/can/isotp.c | 65 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 9 deletions(-)

--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -85,10 +85,21 @@ MODULE_ALIAS("can-proto-6");
 
 /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
  * take full 32 bit values (4 Gbyte). We would need some good concept to handle
- * this between user space and kernel space. For now increase the static buffer
- * to something about 64 kbyte to be able to test this new functionality.
+ * this between user space and kernel space. For now set the static buffer to
+ * something about 8 kbyte to be able to test this new functionality.
  */
-#define MAX_MSG_LENGTH 66000
+#define DEFAULT_MAX_PDU_SIZE 8300
+
+/* maximum PDU size before ISO 15765-2:2016 extension was 4095 */
+#define MAX_12BIT_PDU_SIZE 4095
+
+/* limit the isotp pdu size from the optional module parameter to 1MByte */
+#define MAX_PDU_SIZE (1025 * 1024U)
+
+static unsigned int max_pdu_size __read_mostly = DEFAULT_MAX_PDU_SIZE;
+module_param(max_pdu_size, uint, 0444);
+MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
+		 __stringify(DEFAULT_MAX_PDU_SIZE) ")");
 
 /* N_PCI type values in bits 7-4 of N_PCI bytes */
 #define N_PCI_SF 0x00	/* single frame */
@@ -124,13 +135,15 @@ enum {
 };
 
 struct tpcon {
-	unsigned int idx;
+	u8 *buf;
+	unsigned int buflen;
 	unsigned int len;
+	unsigned int idx;
 	u32 state;
 	u8 bs;
 	u8 sn;
 	u8 ll_dl;
-	u8 buf[MAX_MSG_LENGTH + 1];
+	u8 sbuf[DEFAULT_MAX_PDU_SIZE];
 };
 
 struct isotp_sock {
@@ -498,7 +511,17 @@ static int isotp_rcv_ff(struct sock *sk,
 	if (so-&gt;rx.len + ae + off + ff_pci_sz &lt; so-&gt;rx.ll_dl)
 		return 1;
 
-	if (so-&gt;rx.len &gt; MAX_MSG_LENGTH) {
+	/* PDU size &gt; default =&gt; try max_pdu_size */
+	if (so-&gt;rx.len &gt; so-&gt;rx.buflen &amp;&amp; so-&gt;rx.buflen &lt; max_pdu_size) {
+		u8 *newbuf = kmalloc(max_pdu_size, GFP_ATOMIC);
+
+		if (newbuf) {
+			so-&gt;rx.buf = newbuf;
+			so-&gt;rx.buflen = max_pdu_size;
+		}
+	}
+
+	if (so-&gt;rx.len &gt; so-&gt;rx.buflen) {
 		/* send FC frame with overflow status */
 		isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
 		return 1;
@@ -802,7 +825,7 @@ static void isotp_create_fframe(struct c
 		cf-&gt;data[0] = so-&gt;opt.ext_address;
 
 	/* create N_PCI bytes with 12/32 bit FF_DL data length */
-	if (so-&gt;tx.len &gt; 4095) {
+	if (so-&gt;tx.len &gt; MAX_12BIT_PDU_SIZE) {
 		/* use 32 bit FF_DL notation */
 		cf-&gt;data[ae] = N_PCI_FF;
 		cf-&gt;data[ae + 1] = 0;
@@ -939,7 +962,17 @@ static int isotp_sendmsg(struct socket *
 			goto err_event_drop;
 	}
 
-	if (!size || size &gt; MAX_MSG_LENGTH) {
+	/* PDU size &gt; default =&gt; try max_pdu_size */
+	if (size &gt; so-&gt;tx.buflen &amp;&amp; so-&gt;tx.buflen &lt; max_pdu_size) {
+		u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL);
+
+		if (newbuf) {
+			so-&gt;tx.buf = newbuf;
+			so-&gt;tx.buflen = max_pdu_size;
+		}
+	}
+
+	if (!size || size &gt; so-&gt;tx.buflen) {
 		err = -EINVAL;
 		goto err_out_drop;
 	}
@@ -1194,6 +1227,12 @@ static int isotp_release(struct socket *
 	so-&gt;ifindex = 0;
 	so-&gt;bound = 0;
 
+	if (so-&gt;rx.buf != so-&gt;rx.sbuf)
+		kfree(so-&gt;rx.buf);
+
+	if (so-&gt;tx.buf != so-&gt;tx.sbuf)
+		kfree(so-&gt;tx.buf);
+
 	sock_orphan(sk);
 	sock-&gt;sk = NULL;
 
@@ -1588,6 +1627,11 @@ static int isotp_init(struct sock *sk)
 	so-&gt;rx.state = ISOTP_IDLE;
 	so-&gt;tx.state = ISOTP_IDLE;
 
+	so-&gt;rx.buf = so-&gt;rx.sbuf;
+	so-&gt;tx.buf = so-&gt;tx.sbuf;
+	so-&gt;rx.buflen = ARRAY_SIZE(so-&gt;rx.sbuf);
+	so-&gt;tx.buflen = ARRAY_SIZE(so-&gt;tx.sbuf);
+
 	hrtimer_init(&amp;so-&gt;rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 	so-&gt;rxtimer.function = isotp_rx_timer_handler;
 	hrtimer_init(&amp;so-&gt;txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
@@ -1670,7 +1714,10 @@ static __init int isotp_module_init(void
 {
 	int err;
 
-	pr_info("can: isotp protocol\n");
+	max_pdu_size = max_t(unsigned int, max_pdu_size, MAX_12BIT_PDU_SIZE);
+	max_pdu_size = min_t(unsigned int, max_pdu_size, MAX_PDU_SIZE);
+
+	pr_info("can: isotp protocol (max_pdu_size %d)\n", max_pdu_size);
 
 	err = can_proto_register(&amp;isotp_can_proto);
 	if (err &lt; 0)
</pre></body></html>