Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups
@ 2026-12-05 13:19 Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 01/24] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
                   ` (23 more replies)
  0 siblings, 24 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can
  Cc: Marc Kleine-Budde, Oliver Hartkopp, Sashiko, Zihan Xi,
	Oleksij Rempel, Wentian Ye

Patches 1-3 are the remaining CAN patches of the former lock_sock() and
memcg series, whose first patches have been applied via the net tree.
They are followed by CAN_ISOTP fixes and cleanups that have been found
while preparing the CAN XL support for CAN_ISOTP.

Patches 1-2 read the CAN_RAW and CAN_ISOTP socket options under
conf_lock, so that a concurrent setsockopt() can not lead to a mix of
old and new values in getsockopt().

Patch 3 enables the memcg accounting for the CAN allocations that can
be triggered by unprivileged users (socket structures, receivers,
CAN_BCM ops and frames, CAN_RAW/J1939 filters, the CAN_ISOTP TX buffer
for large PDUs and the J1939 per device data).

Patch 4 fixes a CAN_ISOTP socket in listen mode that sent a FC.OVFLW on
the bus. It carries a Fixes tag and applies to net as well.

Patches 5-12 improve the CAN_ISOTP socket options and timing:
- check the CAN_ISOTP_OPTS values before applying them
- add the frame transmission time to the CF broadcast gap and limit it
  to the N_As/N_Ar timeout
- add the N_As/N_Ar timeout monitoring with the local echo and start
  the N_Bs/N_Cr timeouts with the transmission confirmation
- reject undefined CAN FD tx_flags and reserved STmin values
- ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING as
  documented

Patches 13-16 fix the CAN_ISOTP reception against ISO 15765-2:
- drop frames without N_PCI and FFs with an invalid length extension
- check FF_DLmin and the data length of the last CF

Patches 17-24 are cleanups in preparation of the CAN XL support for
CAN_ISOTP.

Patches 5-16 only affect invalid configurations and frames, tighten the
uAPI or implement the ISO 15765-2 timeouts and therefore do not carry
Fixes tags.

Changes in v6:
- can: raw: read socket options under conf_lock in raw_getsockopt():
  reference the private mutex commit with its hash
- can: isotp: read socket options under conf_lock in isotp_getsockopt():
  zero the local variable, so that no uninitialized padding bytes are
  copied to user space, reference the private mutex commit with its hash
- can: enable memcg accounting for user triggered allocations:
  removed the outdated paragraph about dedicated slab caches
- can: isotp: check the CAN_ISOTP_OPTS values before applying them:
  reference the private mutex commit with its hash
- can: isotp: add N_As timeout monitoring for SF/FF/CF:
  stop all tx timers when can_send() fails for the first frame, mention
  the later ECOMM error for a missing FC after a delayed FF, name the
  SF/FF/CF in the comments of the echo timer and the echo tag
- can: isotp: add N_Ar timeout monitoring for FC:
  clear the FC echo tag when a new FC is created, when a CF is received
  before the local echo of the FC and in isotp_bind(), document the
  ETIMEDOUT error for the N_Ar timeout
- can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS:
  reworked the tx_flags comments in isotp.h and isotp.c, explain the
  CANFD_FDF compatibility handling in the commit message
- can: isotp: rename rx_lock as it has become state machine lock:
  describe what sm_lock serializes in the commit message
- Link to v5: https://lore.kernel.org/linux-can/20261203-getsockopt-memcg-isotp-v5-0-bbe89b3d3b71@hartkopp.net/

Changes in v5:
- can: isotp: add the frame transmission time to the protocol timeouts:
  replaced by "can: isotp: limit the frame transmission time to the
  N_As/N_Ar timeout", which only limits and documents frame_txtime
- can: isotp: use the N_As timeout for the local echo: replaced by
  "can: isotp: add N_As timeout monitoring for SF/FF/CF", which
  supervises the transmission of all SF/FF/CF with the local echo and
  starts the FC timeout (N_Bs) with the local echo of the FF
- new patch: can: isotp: add N_Ar timeout monitoring for FC, which
  supervises the transmission of the FC with the local echo and starts
  the CF timeout (N_Cr) with the local echo of the FC
- Link to v4: https://lore.kernel.org/linux-can/20261201-getsockopt-memcg-isotp-v4-0-97261be8e38e@hartkopp.net/

Changes in v4:
- can: enable memcg accounting for user triggered allocations:
  reworded the reason for the unaccounted j1939 RX sessions
- can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING:
  reworded "misconfiguration" to "flag combination"
- can: isotp: check the minimum FF_DL: describe the two kinds of
  accepted FFs separately in the commit message
- Link to v3: https://lore.kernel.org/linux-can/20261130-getsockopt-memcg-isotp-v3-0-21d72c15ad1d@hartkopp.net/

Changes in v3:
- can: isotp: add the frame transmission time to the CF broadcast gap:
  explain the missing Fixes tag for the unit fix in iso15765-2.rst
- can: isotp: add the frame transmission time to the protocol timeouts:
  add frame_txtime to the CF echo timeouts as well
- new patch: can: isotp: use the N_As timeout for the local echo
  (1 s instead of 2 s, document ECOMM for the local echo timeout)
- can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC:
  state the intentional uAPI tightening, update the remark in isotp.h,
  document the rejected values and fix the struct name in iso15765-2.rst
- can: isotp: drop frames without N_PCI: reworded commit message
- can: isotp: drop FF frames with invalid length extension: describe the
  impact and the reason for the missing Fixes tag
- can: isotp: rename rx_lock as it has become state machine lock:
  precise the locking comment for isotp_release()
- Link to v2: https://lore.kernel.org/linux-can/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4@hartkopp.net/

Changes in v2:
- can: enable memcg accounting for user triggered allocations:
  added Acked-by from Oleksij Rempel
- can: isotp: do not send FC.OVFLW in listen mode:
  moved to patch 4, describe the impact on a correct listen mode
  configuration, add Fixes tag and Cc stable
- new patch: can: isotp: add the frame transmission time to the CF
  broadcast gap (also fixes the unit of the forced stmin values in
  iso15765-2.rst)
- can: isotp: add the frame transmission time to the protocol timeouts:
  limit frame_txtime to the N_As/N_Ar timeout of 1 s from ISO 15765-2
  instead of 100 ms, document the limit in isotp.h and iso15765-2.rst
- can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS:
  state the intentional uAPI tightening, always set CANFD_FDF for
  CAN FD, document the accepted tx_flags
- can: isotp: reject CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
  replaced by "can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without
  CAN_ISOTP_RX_PADDING" which fixes check_pad() as documented instead
  of rejecting the option
- can: isotp: drop frames without N_PCI, check the minimum FF_DL, check
  the data length of the last CF: describe the impact and the reason
  for the missing Fixes tag in the commit messages
- dropped: can: isotp: ignore invalid SF and FF frames during a reception
- can: isotp: rename rx_lock as it has become state machine lock:
  fixed typo in the subject, updated the locking comment in isotp_rcv()
- can: isotp: change return type of functions to void:
  removed the trailing return statements
- can: isotp: introduce CAN_ISOTP_MIN_TX_DL:
  use it only for TX_DL checks, separate block in isotp.h
- Link to v1: https://patch.msgid.link/20261126-getsockopt-memcg-isotp-v1-0-681ef8d8c2c9@hartkopp.net

---
Oliver Hartkopp (24):
      can: raw: read socket options under conf_lock in raw_getsockopt()
      can: isotp: read socket options under conf_lock in isotp_getsockopt()
      can: enable memcg accounting for user triggered allocations
      can: isotp: do not send FC.OVFLW in listen mode
      can: isotp: check the CAN_ISOTP_OPTS values before applying them
      can: isotp: add the frame transmission time to the CF broadcast gap
      can: isotp: limit the frame transmission time to the N_As/N_Ar timeout
      can: isotp: add N_As timeout monitoring for SF/FF/CF
      can: isotp: add N_Ar timeout monitoring for FC
      can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
      can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
      can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
      can: isotp: drop frames without N_PCI
      can: isotp: drop FF frames with invalid length extension
      can: isotp: check the minimum FF_DL
      can: isotp: check the data length of the last CF
      can: isotp: drop inline keyword, let the compiler decide
      can: isotp: rename rx_lock as it has become state machine lock
      can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs
      can: isotp: rename variable for the required content length
      can: isotp: rename checkpad functions returning bool values
      can: isotp: change return type of functions to void
      can: isotp: rename pointer in isotp_rcv() prototype
      can: isotp: introduce CAN_ISOTP_MIN_TX_DL

 Documentation/networking/iso15765-2.rst |  37 ++-
 include/uapi/linux/can/isotp.h          |  23 +-
 net/can/af_can.c                        |  12 +-
 net/can/bcm.c                           |  10 +-
 net/can/isotp.c                         | 445 +++++++++++++++++++-------------
 net/can/j1939/main.c                    |   2 +-
 net/can/j1939/socket.c                  |  12 +-
 net/can/raw.c                           |  78 +++---
 8 files changed, 372 insertions(+), 247 deletions(-)
---
base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
change-id: 20261126-getsockopt-memcg-isotp-052433c7adda

Best regards,
--  
Oliver Hartkopp <socketcan@hartkopp.net>


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 01/24] can: raw: read socket options under conf_lock in raw_getsockopt()
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 02/24] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
                   ` (22 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

raw_getsockopt() only takes conf_lock for CAN_RAW_FILTER. All other
CAN_RAW socket options are read without the mutex, while
raw_setsockopt() updates them under conf_lock. CAN_RAW_XL_VCID_OPTS is
even copied to user space directly from the socket, so a concurrent
setsockopt() can lead to a mix of old and new values.

Read the option values under conf_lock into a local variable and copy
them to user space after mutex_unlock(), like raw_setsockopt() does it
in the other direction. So no user space access happens while the
mutex is held and a stalled copy can not block raw_notify() under RTNL.

CAN_RAW_FILTER is handled in front of the switch statement, as it takes
the mutex on its own for the duplication of the filters. Its code is
not changed.

The socket options have been read without a lock before commit
f7c5b6e19684 ("can: raw: use a private mutex instead of lock_sock()")
as well, so this is no fix. As the only visible change, a failing copy
to user space for CAN_RAW_XL_VCID_OPTS now returns the clamped option
length together with -EFAULT, like the other socket options.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/raw.c | 64 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/net/can/raw.c b/net/can/raw.c
index b69eaadc5e93..cf1770432dad 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -101,11 +101,11 @@ struct raw_sock {
 	can_err_mask_t err_mask;
 	int count;                 /* number of active filters */
 	struct can_filter dfilter; /* default/single filter */
 	struct can_filter *filter; /* pointer to filter(s) */
 	struct uniqframe __percpu *uniq;
-	struct mutex conf_lock; /* protects bind, filters and setsockopt */
+	struct mutex conf_lock; /* protects bind, filters and sockopts */
 };
 
 static LIST_HEAD(raw_notifier_list);
 static DEFINE_SPINLOCK(raw_notifier_lock);
 static struct raw_sock *raw_busy_notifier;
@@ -817,24 +817,26 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 static int raw_getsockopt(struct socket *sock, int level, int optname,
 			  sockopt_t *opt)
 {
 	struct sock *sk = sock->sk;
 	struct raw_sock *ro = raw_sk(sk);
-	int flag;
+	union {
+		can_err_mask_t err_mask;
+		int flag;
+		struct can_raw_vcid_options vcid_opts;
+	} val;
 	int len;
-	void *val;
+	int err = 0;
 
 	if (level != SOL_CAN_RAW)
 		return -EINVAL;
 	len = opt->optlen;
 	if (len < 0)
 		return -EINVAL;
 
-	switch (optname) {
-	case CAN_RAW_FILTER: {
+	if (optname == CAN_RAW_FILTER) {
 		struct can_filter *filter = NULL;
-		int err = 0;
 
 		mutex_lock(&ro->conf_lock);
 		if (ro->count > 0) {
 			int fsize = ro->count * sizeof(struct can_filter);
 
@@ -864,76 +866,76 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 
 		if (!err)
 			opt->optlen = len;
 		return err;
 	}
+
+	/* Read the option value under ro->conf_lock and copy it to user
+	 * space after mutex_unlock(), see raw_setsockopt().
+	 */
+	mutex_lock(&ro->conf_lock);
+
+	switch (optname) {
 	case CAN_RAW_ERR_FILTER:
 		if (len > sizeof(can_err_mask_t))
 			len = sizeof(can_err_mask_t);
-		val = &ro->err_mask;
+		val.err_mask = ro->err_mask;
 		break;
 
 	case CAN_RAW_LOOPBACK:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->loopback;
-		val = &flag;
+		val.flag = ro->loopback;
 		break;
 
 	case CAN_RAW_RECV_OWN_MSGS:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->recv_own_msgs;
-		val = &flag;
+		val.flag = ro->recv_own_msgs;
 		break;
 
 	case CAN_RAW_FD_FRAMES:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->fd_frames;
-		val = &flag;
+		val.flag = ro->fd_frames;
 		break;
 
 	case CAN_RAW_XL_FRAMES:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->xl_frames;
-		val = &flag;
+		val.flag = ro->xl_frames;
 		break;
 
-	case CAN_RAW_XL_VCID_OPTS: {
-		int err = 0;
-
+	case CAN_RAW_XL_VCID_OPTS:
 		/* user space buffer to small for VCID opts? */
 		if (len < sizeof(ro->raw_vcid_opts)) {
 			/* return -ERANGE and needed space in optlen */
 			err = -ERANGE;
 			opt->optlen = sizeof(ro->raw_vcid_opts);
 		} else {
-			if (len > sizeof(ro->raw_vcid_opts))
-				len = sizeof(ro->raw_vcid_opts);
-			if (copy_to_iter(&ro->raw_vcid_opts, len,
-					 &opt->iter_out) != len)
-				err = -EFAULT;
+			len = sizeof(ro->raw_vcid_opts);
+			val.vcid_opts = ro->raw_vcid_opts;
 		}
-		if (!err)
-			opt->optlen = len;
-		return err;
-	}
+		break;
+
 	case CAN_RAW_JOIN_FILTERS:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->join_filters;
-		val = &flag;
+		val.flag = ro->join_filters;
 		break;
 
 	default:
-		return -ENOPROTOOPT;
+		err = -ENOPROTOOPT;
 	}
 
+	mutex_unlock(&ro->conf_lock);
+
+	if (err)
+		return err;
+
 	opt->optlen = len;
-	if (copy_to_iter(val, len, &opt->iter_out) != len)
+	if (copy_to_iter(&val, len, &opt->iter_out) != len)
 		return -EFAULT;
 	return 0;
 }
 
 static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 02/24] can: isotp: read socket options under conf_lock in isotp_getsockopt()
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 01/24] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 03/24] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
                   ` (21 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_getsockopt() copies the CAN_ISOTP socket options to user space
directly from the socket without taking conf_lock, while
isotp_setsockopt() updates them under conf_lock. For the options that
consist of several values, i.e. CAN_ISOTP_OPTS, CAN_ISOTP_RECV_FC and
CAN_ISOTP_LL_OPTS, a concurrent setsockopt() can lead to a mix of old
and new values.

Read the option values under conf_lock into a local variable and copy
them to user space after mutex_unlock(), like isotp_setsockopt() does
it in the other direction. So no user space access happens while the
mutex is held and a stalled copy can not block isotp_notify() under
RTNL. The local variable is zeroed, so that no uninitialized padding
bytes are copied to user space.

The socket options have been read without a lock before commit
c781fc329528 ("can: isotp: use a private mutex instead of lock_sock()")
as well, so this is no fix.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 642fec500af9..80ea1f168adb 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -180,11 +180,11 @@ struct isotp_sock {
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
 	spinlock_t rx_lock; /* protect single thread state machine */
-	struct mutex conf_lock; /* protects bind and setsockopt */
+	struct mutex conf_lock; /* protects bind and sockopts */
 };
 
 static LIST_HEAD(isotp_notifier_list);
 static DEFINE_SPINLOCK(isotp_notifier_lock);
 static struct isotp_sock *isotp_busy_notifier;
@@ -1860,53 +1860,72 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
 static int isotp_getsockopt(struct socket *sock, int level, int optname,
 			    char __user *optval, int __user *optlen)
 {
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
+	union {
+		struct can_isotp_options opt;
+		struct can_isotp_fc_options rxfc;
+		struct can_isotp_ll_options ll;
+		u32 stmin;
+	} val;
 	int len;
-	void *val;
+	int err = 0;
 
 	if (level != SOL_CAN_ISOTP)
 		return -EINVAL;
 	if (get_user(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
 
+	/* no uninitialized padding bytes of the structures to user space */
+	memset(&val, 0, sizeof(val));
+
+	/* Read the option value under so->conf_lock and copy it to user
+	 * space after mutex_unlock(), see isotp_setsockopt().
+	 */
+	mutex_lock(&so->conf_lock);
+
 	switch (optname) {
 	case CAN_ISOTP_OPTS:
 		len = min_t(int, len, sizeof(struct can_isotp_options));
-		val = &so->opt;
+		val.opt = so->opt;
 		break;
 
 	case CAN_ISOTP_RECV_FC:
 		len = min_t(int, len, sizeof(struct can_isotp_fc_options));
-		val = &so->rxfc;
+		val.rxfc = so->rxfc;
 		break;
 
 	case CAN_ISOTP_TX_STMIN:
 		len = min_t(int, len, sizeof(u32));
-		val = &so->force_tx_stmin;
+		val.stmin = so->force_tx_stmin;
 		break;
 
 	case CAN_ISOTP_RX_STMIN:
 		len = min_t(int, len, sizeof(u32));
-		val = &so->force_rx_stmin;
+		val.stmin = so->force_rx_stmin;
 		break;
 
 	case CAN_ISOTP_LL_OPTS:
 		len = min_t(int, len, sizeof(struct can_isotp_ll_options));
-		val = &so->ll;
+		val.ll = so->ll;
 		break;
 
 	default:
-		return -ENOPROTOOPT;
+		err = -ENOPROTOOPT;
 	}
 
+	mutex_unlock(&so->conf_lock);
+
+	if (err)
+		return err;
+
 	if (put_user(len, optlen))
 		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
 	return 0;
 }
 
 static void isotp_notify(struct isotp_sock *so, unsigned long msg,

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 03/24] can: enable memcg accounting for user triggered allocations
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 01/24] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 02/24] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 04/24] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
                   ` (20 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can
  Cc: Marc Kleine-Budde, Sashiko, Zihan Xi, Oleksij Rempel,
	Oliver Hartkopp

Unprivileged users can create CAN sockets and trigger allocations that
stay in the kernel for the lifetime of the socket or of the related
objects. E.g. each CAN_BCM RX_SETUP for a new CAN ID creates a bcm_op
with optional frame arrays of up to 257 CAN FD frames and a CAN
receiver, without requiring a CAN device (ifindex 0) and without a
limit for the number of ops per socket.

None of these allocations are accounted to the memory cgroup of the
process that triggers them. So a process can bypass the memory limit
of its cgroup, e.g. of a container or a systemd service, and exhaust
the memory of the whole system. The OOM killer can not attribute this
kernel memory to the responsible process either, which has nearly no
RSS, and terminates unrelated processes instead.

Enable memcg accounting for these allocations, as it has been done for
other objects that can be created by unprivileged users. This does not
introduce any new CAN specific limit and does not make assumptions
about the number of sockets, filters or BCM operations an application
needs. The CAN related kernel memory is just charged to the cgroup of
the allocating process, so that the memory limits which are already
configured for this cgroup apply to it as well. Without a configured
limit the behaviour is unchanged.

A per socket limit for the number or the memory of the BCM operations,
as suggested in the reports, does not bound the memory consumption, as
a process can open many sockets and fork. Hard-coded limits can also
break existing applications, e.g. one that monitors all 2048 SFF CAN
IDs with individual RX operations, without a possibility to adjust
them.

The following allocations are accounted:

- af_can: the CAN socket structures (e.g. struct isotp_sock contains
  two DEFAULT_MAX_PDU_SIZE buffers), the CAN receivers of all CAN
  protocols and the per-netns receive lists and statistics
- bcm: the bcm_op structures and their frames/last_frames arrays
- raw: the per-cpu uniq data and the CAN_RAW_FILTER filters
- isotp: the TX buffer for PDUs > DEFAULT_MAX_PDU_SIZE
- j1939: the SO_J1939_FILTER filters and the per device j1939_priv,
  which is created by the first bind() to a CAN device

memdup_sockptr() does not support memcg accounting, so the filters are
allocated with the GFP_USER | __GFP_NOWARN flags of memdup_sockptr()
plus __GFP_ACCOUNT and copied explicitly.

The isotp RX buffer for PDUs > DEFAULT_MAX_PDU_SIZE is allocated in
softirq context and is not accounted. It could be charged to the memory
cgroup of the socket with set_active_memcg(), but a charge in atomic
context can not reclaim memory. With GFP_ATOMIC the charge is forced,
which allows a cgroup to exceed its limit by up to max_pdu_size per
socket. Without __GFP_HIGH the allocation fails at the limit, so the
reception of the PDU fails, and it loses the access to the atomic
reserves on systems without a memory limit as well. This buffer is only
used when the administrator has increased the max_pdu_size module
parameter. The j1939 RX sessions are allocated in softirq context and
can not be attributed to a single process, as their data is delivered
to all matching sockets.

The j1939 TX sessions only exist for the duration of a transfer and the
can-gw jobs require CAP_NET_ADMIN in the initial user namespace, so
they are not changed. The local j1939 ECUs created by bind() are
allocated with BH disabled and their number is limited by the number of
bound j1939 sockets, which are accounted. Temporary buffers that are
freed before the syscall returns are not accounted either.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260707-bcm_fixes-v5-0-c3c74f8d8ff7@hartkopp.net
Reported-by: Zihan Xi <zihanx@nebusec.ai>
Closes: https://lore.kernel.org/linux-can/cover.1793089479.git.zihanx@nebusec.ai/
Link: https://lore.kernel.org/linux-can/350df96b-5f69-490e-b39e-ed6e9bca4754@hartkopp.net/
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 net/can/af_can.c       | 12 +++++++-----
 net/can/bcm.c          | 10 +++++-----
 net/can/isotp.c        |  9 +++++++--
 net/can/j1939/main.c   |  2 +-
 net/can/j1939/socket.c | 12 +++++++++---
 net/can/raw.c          | 14 ++++++++++----
 6 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/net/can/af_can.c b/net/can/af_can.c
index 0f3051a61aab..6f958c2bb49d 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -153,11 +153,11 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
 		goto errout;
 	}
 
 	sock->ops = cp->ops;
 
-	sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, kern);
+	sk = sk_alloc(net, PF_CAN, GFP_KERNEL_ACCOUNT, cp->prot, kern);
 	if (!sk) {
 		err = -ENOMEM;
 		goto errout;
 	}
 
@@ -828,18 +828,20 @@ void can_proto_unregister(const struct can_proto *cp)
 EXPORT_SYMBOL(can_proto_unregister);
 
 static int can_pernet_init(struct net *net)
 {
 	spin_lock_init(&net->can.rcvlists_lock);
-	net->can.rx_alldev_list = kzalloc_obj(*net->can.rx_alldev_list);
+	net->can.rx_alldev_list = kzalloc_obj(*net->can.rx_alldev_list,
+					      GFP_KERNEL_ACCOUNT);
 	if (!net->can.rx_alldev_list)
 		goto out;
 	spin_lock_init(&net->can.rx_alldev_list->lock);
-	net->can.pkg_stats = kzalloc_obj(*net->can.pkg_stats);
+	net->can.pkg_stats = kzalloc_obj(*net->can.pkg_stats, GFP_KERNEL_ACCOUNT);
 	if (!net->can.pkg_stats)
 		goto out_free_rx_alldev_list;
-	net->can.rcv_lists_stats = kzalloc_obj(*net->can.rcv_lists_stats);
+	net->can.rcv_lists_stats = kzalloc_obj(*net->can.rcv_lists_stats,
+					       GFP_KERNEL_ACCOUNT);
 	if (!net->can.rcv_lists_stats)
 		goto out_free_pkg_stats;
 
 	if (IS_ENABLED(CONFIG_PROC_FS)) {
 		/* the statistics are updated every second (timer triggered) */
@@ -917,11 +919,11 @@ static __init int can_init(void)
 		     offsetof(struct canfd_frame, data));
 
 	pr_info("can: controller area network core\n");
 
 	rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
-				      0, 0, NULL);
+				      0, SLAB_ACCOUNT, NULL);
 	if (!rcv_cache)
 		return -ENOMEM;
 
 	err = register_pernet_subsys(&can_pernet_ops);
 	if (err)
diff --git a/net/can/bcm.c b/net/can/bcm.c
index dc9e2890490a..f3d93a203071 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -1159,11 +1159,11 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 		kfree(new_frames);
 
 	} else {
 		/* insert new BCM operation for the given can_id */
 
-		op = kzalloc(OPSIZ, GFP_KERNEL);
+		op = kzalloc(OPSIZ, GFP_KERNEL_ACCOUNT);
 		if (!op)
 			return -ENOMEM;
 
 		spin_lock_init(&op->bcm_tx_lock);
 		op->can_id = msg_head->can_id;
@@ -1176,11 +1176,11 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 
 		/* create array for CAN frames and copy the data */
 		if (msg_head->nframes > 1) {
 			op->frames = kmalloc_array(msg_head->nframes,
 						   op->cfsiz,
-						   GFP_KERNEL);
+						   GFP_KERNEL_ACCOUNT);
 			if (!op->frames) {
 				kfree(op);
 				return -ENOMEM;
 			}
 		} else
@@ -1407,11 +1407,11 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 		 */
 		do_rx_register = (ifindex && !op->rx_reg_dev) ? 1 : 0;
 
 	} else {
 		/* insert new BCM operation for the given can_id */
-		op = kzalloc(OPSIZ, GFP_KERNEL);
+		op = kzalloc(OPSIZ, GFP_KERNEL_ACCOUNT);
 		if (!op)
 			return -ENOMEM;
 
 		spin_lock_init(&op->bcm_tx_lock);
 		spin_lock_init(&op->bcm_rx_update_lock);
@@ -1422,20 +1422,20 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 
 		if (msg_head->nframes > 1) {
 			/* create array for CAN frames and copy the data */
 			op->frames = kmalloc_array(msg_head->nframes,
 						   op->cfsiz,
-						   GFP_KERNEL);
+						   GFP_KERNEL_ACCOUNT);
 			if (!op->frames) {
 				kfree(op);
 				return -ENOMEM;
 			}
 
 			/* create and init array for received CAN frames */
 			op->last_frames = kcalloc(msg_head->nframes,
 						  op->cfsiz,
-						  GFP_KERNEL);
+						  GFP_KERNEL_ACCOUNT);
 			if (!op->last_frames) {
 				kfree(op->frames);
 				kfree(op);
 				return -ENOMEM;
 			}
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 80ea1f168adb..f8bf62ca1eb1 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -607,11 +607,16 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
 
 	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
 		return 1;
 
-	/* PDU size > default => try max_pdu_size */
+	/* PDU size > default => try max_pdu_size
+	 *
+	 * This buffer is not accounted to a memory cgroup: In softirq
+	 * context the charge would either have to be forced beyond the
+	 * limit of the cgroup or the reception would fail at the limit.
+	 */
 	if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
 		u8 *newbuf = kmalloc(max_pdu_size, GFP_ATOMIC);
 
 		if (newbuf) {
 			so->rx.buf = newbuf;
@@ -1192,11 +1197,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
 	wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
 
 	/* PDU size > default => try max_pdu_size */
 	if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
-		u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL);
+		u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL_ACCOUNT);
 
 		if (newbuf) {
 			so->tx.buf = newbuf;
 			so->tx.buflen = max_pdu_size;
 		}
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index d9384cf0e356..77adb817b36c 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -126,11 +126,11 @@ static DEFINE_MUTEX(j1939_netdev_lock);
 
 static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
 {
 	struct j1939_priv *priv;
 
-	priv = kzalloc_obj(*priv);
+	priv = kzalloc_obj(*priv, GFP_KERNEL_ACCOUNT);
 	if (!priv)
 		return NULL;
 
 	rwlock_init(&priv->lock);
 	INIT_LIST_HEAD(&priv->ecus);
diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
index 50a598ef5fd4..85b7e8847c06 100644
--- a/net/can/j1939/socket.c
+++ b/net/can/j1939/socket.c
@@ -714,13 +714,19 @@ static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
 			if (optlen > J1939_FILTER_MAX *
 			    sizeof(struct j1939_filter))
 				return -EINVAL;
 
 			count = optlen / sizeof(*filters);
-			filters = memdup_sockptr(optval, optlen);
-			if (IS_ERR(filters))
-				return PTR_ERR(filters);
+			filters = kmalloc(optlen, GFP_USER | __GFP_NOWARN |
+					  __GFP_ACCOUNT);
+			if (!filters)
+				return -ENOMEM;
+
+			if (copy_from_sockptr(filters, optval, optlen)) {
+				kfree(filters);
+				return -EFAULT;
+			}
 
 			for (f = filters, c = count; c; f++, c--) {
 				f->name &= f->name_mask;
 				f->pgn &= f->pgn_mask;
 				f->addr &= f->addr_mask;
diff --git a/net/can/raw.c b/net/can/raw.c
index cf1770432dad..da462e9874f1 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -416,11 +416,11 @@ static int raw_init(struct sock *sk)
 	ro->fd_frames        = 0;
 	ro->xl_frames        = 0;
 	ro->join_filters     = 0;
 
 	/* alloc_percpu provides zero'ed memory */
-	ro->uniq = alloc_percpu(struct uniqframe);
+	ro->uniq = alloc_percpu_gfp(struct uniqframe, GFP_KERNEL_ACCOUNT);
 	if (unlikely(!ro->uniq))
 		return -ENOMEM;
 
 	sk->sk_destruct = raw_sock_destruct;
 
@@ -761,13 +761,19 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 
 		count = optlen / sizeof(struct can_filter);
 
 		if (count > 1) {
 			/* filter does not fit into dfilter => alloc space */
-			filter = memdup_sockptr(optval, optlen);
-			if (IS_ERR(filter))
-				return PTR_ERR(filter);
+			filter = kmalloc(optlen, GFP_USER | __GFP_NOWARN |
+					 __GFP_ACCOUNT);
+			if (!filter)
+				return -ENOMEM;
+
+			if (copy_from_sockptr(filter, optval, optlen)) {
+				kfree(filter);
+				return -EFAULT;
+			}
 		} else if (count == 1) {
 			if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
 				return -EFAULT;
 			filter = &sfilter;
 		}

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 04/24] can: isotp: do not send FC.OVFLW in listen mode
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (2 preceding siblings ...)
  2026-12-05 13:19 ` [PATCH can-next v6 03/24] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 05/24] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
                   ` (19 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_rcv_ff() sends a FC with overflow status when the FF_DL exceeds
the receive buffer before CAN_ISOTP_LISTEN_MODE is checked. So a socket
in listen mode, which is intended to monitor the ISO-TP traffic
passively, sends a FC.OVFLW on the bus when a received FF exceeds its
receive buffer (max_pdu_size). This aborts the transfer between other
nodes using the same CAN IDs.

Move the listen mode check from the callers into isotp_send_fc() before
any state changes to cover all FC frames.

Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index f8bf62ca1eb1..b4cbf56253bf 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -292,10 +292,14 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 	struct can_skb_ext *csx;
 	struct canfd_frame *ncf;
 	struct isotp_sock *so = isotp_sk(sk);
 	int can_send_ret;
 
+	/* no creation of flow control frames in listen mode */
+	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
+		return 0;
+
 	nskb = alloc_skb(so->ll.mtu, gfp_any());
 	if (!nskb)
 		return 1;
 
 	csx = can_skb_ext_add(nskb);
@@ -641,14 +645,10 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 
 	/* start rx timeout, isotp_send_fc() restarts it */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
-	/* no creation of flow control frames */
-	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
-
 	/* send our first FC frame */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 	return 0;
 }
 
@@ -742,14 +742,10 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* perform blocksize handling, if enabled */
 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
 		return 0;
 
-	/* no creation of flow control frames */
-	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
-
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 	return 0;
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 05/24] can: isotp: check the CAN_ISOTP_OPTS values before applying them
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (3 preceding siblings ...)
  2026-12-05 13:19 ` [PATCH can-next v6 04/24] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 06/24] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
                   ` (18 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_setsockopt_locked() copies the CAN_ISOTP_OPTS values into so->opt
before it checks them. When CAN_ISOTP_SF_BROADCAST and
CAN_ISOTP_CF_BROADCAST are set together, CAN_ISOTP_CF_BROADCAST is
removed and the remaining options are applied, although -EINVAL is
returned to give user feedback on the wrong configuration.

Since commit c781fc329528 ("can: isotp: use a private mutex instead of
lock_sock()") the option values are copied from user space into a local
buffer before conf_lock is taken. Check the broadcast flags in this copy
and reject an invalid combination before so->opt is changed. A rejected
setsockopt() now leaves all socket options untouched instead of applying
a modified set of options.

Use hweight32() to check that only one of the broadcast flags is set, so
that further broadcast flags can be added to ISOTP_ALL_BC_FLAGS without
changing the check.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index b4cbf56253bf..b81a795524e7 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1738,34 +1738,32 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 	if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
 	    READ_ONCE(so->rx.state) != ISOTP_IDLE)
 		return -EAGAIN;
 
 	switch (optname) {
-	case CAN_ISOTP_OPTS:
-		memcpy(&so->opt, val, sizeof(so->opt));
+	case CAN_ISOTP_OPTS: {
+		const struct can_isotp_options *opt = val;
+
+		/* only one broadcast flag at a time */
+		if (hweight32(opt->flags & ISOTP_ALL_BC_FLAGS) > 1)
+			return -EINVAL;
+
+		memcpy(&so->opt, opt, sizeof(*opt));
 
 		/* no separate rx_ext_address is given => use ext_address */
 		if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
 			so->opt.rx_ext_address = so->opt.ext_address;
 
-		/* these broadcast flags are not allowed together */
-		if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) {
-			/* CAN_ISOTP_SF_BROADCAST is prioritized */
-			so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
-
-			/* give user feedback on wrong config attempt */
-			ret = -EINVAL;
-		}
-
 		/* check for frame_txtime changes (0 => no changes) */
 		if (so->opt.frame_txtime) {
 			if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
 				so->frame_txtime = 0;
 			else
 				so->frame_txtime = so->opt.frame_txtime;
 		}
 		break;
+	}
 
 	case CAN_ISOTP_RECV_FC:
 		memcpy(&so->rxfc, val, sizeof(so->rxfc));
 		break;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 06/24] can: isotp: add the frame transmission time to the CF broadcast gap
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (4 preceding siblings ...)
  2026-12-05 13:19 ` [PATCH can-next v6 05/24] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:19 ` [PATCH can-next v6 07/24] can: isotp: limit the frame transmission time to the N_As/N_Ar timeout Oliver Hartkopp
                   ` (17 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

With flow control the gap between the consecutive frames is the frame
transmission time (N_As) plus the minimum separation time (N_Cs). N_Cs is
the STmin value from the received FC or the value of the
CAN_ISOTP_TX_STMIN socket option when the CAN_ISOTP_FORCE_TXSTMIN flag is
set.

In CAN_ISOTP_CF_BROADCAST mode there is no FC and the gap is set in
isotp_sendmsg(). With the CAN_ISOTP_FORCE_TXSTMIN flag only the value of
the CAN_ISOTP_TX_STMIN socket option is used and the frame transmission
time is missing.

Add the frame transmission time to the CF broadcast gap in both cases
like in the flow control mode and document the gap in iso15765-2.rst.

Additionally fix the unit of the values of the CAN_ISOTP_TX_STMIN and
CAN_ISOTP_RX_STMIN socket options in iso15765-2.rst. Both values are
handled in nanoseconds by the implementation, as documented in isotp.h.
As the uAPI header isotp.h has always documented the stmin values in
nanoseconds, there is no Fixes tag for the documentation.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 9 ++++++---
 net/can/isotp.c                         | 8 ++++----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 37ebb2c417cb..badcd16c070b 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -197,11 +197,14 @@ where the ``can_isotp_options`` structure has the following contents:
 
   * ``CAN_ISOTP_SF_BROADCAST``: use 1-to-N functional addressing (cannot be
     specified alongside ``CAN_ISOTP_CF_BROADCAST``).
 
   * ``CAN_ISOTP_CF_BROADCAST``: use 1-to-N transmission without flow control
-    (cannot be specified alongside ``CAN_ISOTP_SF_BROADCAST``).
+    (cannot be specified alongside ``CAN_ISOTP_SF_BROADCAST``). The gap
+    between the consecutive frames is ``frame_txtime`` plus the value of the
+    ``CAN_ISOTP_TX_STMIN`` socket option when the ``CAN_ISOTP_FORCE_TXSTMIN``
+    flag is set.
     NOTE: this is not covered by the ISO 15765-2 standard.
 
   * ``CAN_ISOTP_DYN_FC_PARMS``: enable dynamic update of flow control
     parameters.
 
@@ -284,11 +287,11 @@ where the ``can_isotp_ll_options`` structure has the following contents:
 
 Transmission stmin
 ~~~~~~~~~~~~~~~~~~
 
 The transmission minimum separation time (stmin) can be forced using the
-``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in microseconds as
+``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in nanoseconds as
 a 32bit unsigned integer; this will overwrite the value sent by the receiver in
 flow control frames:
 
 .. code-block:: C
 
@@ -297,11 +300,11 @@ flow control frames:
 
 Reception stmin
 ~~~~~~~~~~~~~~~
 
 The reception minimum separation time (stmin) can be forced using the
-``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in microseconds as
+``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in nanoseconds as
 a 32bit unsigned integer; received Consecutive Frames (CF) which timestamps
 differ less than this value will be ignored:
 
 .. code-block:: C
 
diff --git a/net/can/isotp.c b/net/can/isotp.c
index b81a795524e7..e10f4f7bf711 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1290,15 +1290,15 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		/* send first frame */
 
 		isotp_create_fframe(cf, so, ae);
 
 		if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) {
-			/* set timer for FC-less operation (STmin = 0) */
+			/* set timer for FC-less operation (N_As + N_Cs) */
+			so->tx_gap = ktime_set(0, so->frame_txtime);
 			if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
-				so->tx_gap = ktime_set(0, so->force_tx_stmin);
-			else
-				so->tx_gap = ktime_set(0, so->frame_txtime);
+				so->tx_gap = ktime_add_ns(so->tx_gap,
+							  so->force_tx_stmin);
 
 			/* disable wait for FCs due to activated block size */
 			so->txfc.bs = 0;
 
 			/* set CF echo tag for isotp_rcv_echo() (CF-mode) */

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 07/24] can: isotp: limit the frame transmission time to the N_As/N_Ar timeout
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (5 preceding siblings ...)
  2026-12-05 13:19 ` [PATCH can-next v6 06/24] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
@ 2026-12-05 13:19 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 08/24] can: isotp: add N_As timeout monitoring for SF/FF/CF Oliver Hartkopp
                   ` (16 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:19 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The frame_txtime element of struct can_isotp_options is documented as
the frame transmission time (N_As/N_Ar). It is added to the gap between
the consecutive frames in the tx path.

ISO 15765-2 defines a timeout of 1000 ms for N_As/N_Ar, i.e. for the
transmission of a CAN frame on the sender and the receiver side. So far
frame_txtime accepted any value up to 0xFFFFFFFE ns (about 4.3 s).

This is an intentional tightening of the uAPI and no bug fix: reject
frame_txtime values above the N_As/N_Ar timeout of 1 s with -EINVAL
before the options are applied and document the limit and the unit in
isotp.h and iso15765-2.rst. Larger gaps between the consecutive frames
can be configured with the CAN_ISOTP_TX_STMIN socket option and the
CAN_ISOTP_FORCE_TXSTMIN flag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 11 +++++++----
 include/uapi/linux/can/isotp.h          |  1 +
 net/can/isotp.c                         |  6 ++++++
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index badcd16c070b..32d324543d65 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -206,14 +206,17 @@ where the ``can_isotp_options`` structure has the following contents:
     NOTE: this is not covered by the ISO 15765-2 standard.
 
   * ``CAN_ISOTP_DYN_FC_PARMS``: enable dynamic update of flow control
     parameters.
 
-* ``frame_txtime``: frame transmission time (defined as N_As/N_Ar inside the
-  ISO standard); if ``0``, the default (or the last set value) is used.
-  To set the transmission time to ``0``, the ``CAN_ISOTP_FRAME_TXTIME_ZERO``
-  macro (equal to 0xFFFFFFFF) shall be used.
+* ``frame_txtime``: frame transmission time in nanoseconds (defined as
+  N_As/N_Ar inside the ISO standard); if ``0``, the default (or the last set
+  value) is used. To set the transmission time to ``0``, the
+  ``CAN_ISOTP_FRAME_TXTIME_ZERO`` macro (equal to 0xFFFFFFFF) shall be used.
+  The frame transmission time is limited to the N_As/N_Ar timeout of 1 s
+  (1000000000 ns) defined in the ISO standard. It is added to the gap between
+  the consecutive frames.
 
 * ``ext_address``: extended addressing byte, used if the
   ``CAN_ISOTP_EXTEND_ADDR`` flag is specified.
 
 * ``txpad_content``: byte used as padding value for transmitted frames.
diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index bd990917f7c4..a2e5aa6f9e82 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -72,10 +72,11 @@ struct can_isotp_options {
 	__u32 flags;		/* set flags for isotp behaviour.	*/
 				/* __u32 value : flags see below	*/
 
 	__u32 frame_txtime;	/* frame transmission time (N_As/N_Ar)	*/
 				/* __u32 value : time in nano secs	*/
+				/* max. 1 s (N_As/N_Ar timeout)		*/
 
 	__u8  ext_address;	/* set address for extended addressing	*/
 				/* __u8 value : extended address	*/
 
 	__u8  txpad_content;	/* set content of padding byte (tx)	*/
diff --git a/net/can/isotp.c b/net/can/isotp.c
index e10f4f7bf711..d327b56333a0 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -125,10 +125,11 @@ MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
 #define ISOTP_FC_WT 1		/* wait */
 #define ISOTP_FC_OVFLW 2	/* overflow */
 
 #define ISOTP_FC_TIMEOUT 1	/* 1 sec */
 #define ISOTP_ECHO_TIMEOUT 2	/* 2 secs */
+#define ISOTP_MAX_FRAME_TXTIME_NS NSEC_PER_SEC /* N_As/N_Ar timeout */
 
 /* so->tx_result[so->tx_gen % ISOTP_TX_RESULT_SLOTS] holds the packed value
  * (err << ISOTP_TX_RESULT_GEN_BITS | gen) for each tx generation slot, so it
  * can be handled with a single READ_ONCE()/WRITE_ONCE() access.
  */
@@ -1745,10 +1746,15 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 
 		/* only one broadcast flag at a time */
 		if (hweight32(opt->flags & ISOTP_ALL_BC_FLAGS) > 1)
 			return -EINVAL;
 
+		/* frame_txtime must not exceed the N_As/N_Ar timeout */
+		if (opt->frame_txtime != CAN_ISOTP_FRAME_TXTIME_ZERO &&
+		    opt->frame_txtime > ISOTP_MAX_FRAME_TXTIME_NS)
+			return -EINVAL;
+
 		memcpy(&so->opt, opt, sizeof(*opt));
 
 		/* no separate rx_ext_address is given => use ext_address */
 		if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
 			so->opt.rx_ext_address = so->opt.ext_address;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 08/24] can: isotp: add N_As timeout monitoring for SF/FF/CF
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (6 preceding siblings ...)
  2026-12-05 13:19 ` [PATCH can-next v6 07/24] can: isotp: limit the frame transmission time to the N_As/N_Ar timeout Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC Oliver Hartkopp
                   ` (15 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Wentian Ye, Oliver Hartkopp

ISO 15765-2 supervises the transmission of a CAN frame on the sender
side with the N_As timeout of 1000 ms. The flow control timeout N_Bs is
started with the transmission confirmation of the first frame.

So far the transmission of SF and CF frames (and of the FF in the
CAN_ISOTP_CF_BROADCAST mode) was supervised with a local echo timeout
of 2 s (e.g. to cover N_As timeout + N_Bs timeout).

The FF with flow control was not supervised at all and the FC timeout
was started with the transmission request of the FF, so that a delayed
transmission of the FF shortened the time for the FC of the receiver.
The frame transmission time (frame_txtime), which is documented as
N_As/N_Ar, was not considered for this timeout.

Consistently supervise the transmission of all SF, FF and CF frames
with the specified N_As timeout of 1 s by using the local echo facility
that can provide the required L_Data confirmation trigger.

This enables the start of the FC timeout (N_Bs) at FF tx confirmation
time. An FC answer that is received before the local echo of the FF
also confirms the transmission of the FF.

When can_send() fails for the first frame, all tx timers are stopped,
as a FC that has been received in the meantime may have started them.

Replace ISOTP_FC_TIMEOUT (1 s) and ISOTP_ECHO_TIMEOUT (2 s) with the
ISO specified general ISOTP_TIMEOUT (1 s) for those defined cases and
document the ECOMM error for the N_As (local echo) timeout.

As the transmission is now supervised with the N_As timeout of 1 s
instead of the former local echo timeout of 2 s, the ECOMM error may be
reported earlier. As the FC timeout (N_Bs) is now started with the local
echo of the FF, a delayed transmission of the FF may lead to a later
ECOMM error for a missing FC.

Reported-by: Wentian Ye <yewentian395@gmail.com>
Closes: https://lore.kernel.org/linux-can/eb5e9618-3dbf-42cb-a635-752ff9fbcb99@hartkopp.net/
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst |  2 +-
 net/can/isotp.c                         | 68 ++++++++++++++++++++-------------
 2 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 32d324543d65..3f768ae1cde5 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -346,11 +346,11 @@ RX path errors
 
 TX path errors
 ~~~~~~~~~~~~~~
 
 ========== =================================================================
--ECOMM     flow control reception timeout
+-ECOMM     flow control or local echo reception timeout
 -EMSGSIZE  flow control reception overflow
 -EBADMSG   flow control reception with wrong layout/padding
 ========== =================================================================
 
 Examples
diff --git a/net/can/isotp.c b/net/can/isotp.c
index d327b56333a0..493bad6f9a6a 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -5,11 +5,11 @@
  * userspace.
  *
  * - RX path timeout of data reception leads to -ETIMEDOUT
  * - RX path SN mismatch leads to -EILSEQ
  * - RX path data reception with wrong padding leads to -EBADMSG
- * - TX path flowcontrol reception timeout leads to -ECOMM
+ * - TX path flowcontrol or local echo reception timeout leads to -ECOMM
  * - TX path flowcontrol reception overflow leads to -EMSGSIZE
  * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG
  * - when a transfer (tx) is on the run the next write() blocks until it's done
  * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent
  * - as we have static buffers the check whether the PDU fits into the buffer
@@ -123,12 +123,11 @@ MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
 /* Flow Status given in FC frame */
 #define ISOTP_FC_CTS 0		/* clear to send */
 #define ISOTP_FC_WT 1		/* wait */
 #define ISOTP_FC_OVFLW 2	/* overflow */
 
-#define ISOTP_FC_TIMEOUT 1	/* 1 sec */
-#define ISOTP_ECHO_TIMEOUT 2	/* 2 secs */
+#define ISOTP_TIMEOUT 1		/* 1 sec (N_As, N_Bs, N_Cr) */
 #define ISOTP_MAX_FRAME_TXTIME_NS NSEC_PER_SEC /* N_As/N_Ar timeout */
 
 /* so->tx_result[so->tx_gen % ISOTP_TX_RESULT_SLOTS] holds the packed value
  * (err << ISOTP_TX_RESULT_GEN_BITS | gen) for each tx generation slot, so it
  * can be handled with a single READ_ONCE()/WRITE_ONCE() access.
@@ -174,11 +173,11 @@ struct isotp_sock {
 	struct can_isotp_fc_options rxfc, txfc;
 	struct can_isotp_ll_options ll;
 	u32 frame_txtime;
 	u32 force_tx_stmin;
 	u32 force_rx_stmin;
-	u32 cfecho; /* consecutive frame echo tag */
+	u32 cfecho; /* SF/FF/CF echo tag */
 	u32 tx_gen; /* transfer generation, increased per new tx transfer */
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
@@ -352,11 +351,11 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 
 	/* reset last CF frame rx timestamp for rx stmin enforcement */
 	so->lastrxcf_tstamp = ktime_set(0, 0);
 
 	/* start rx timeout watchdog */
-	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 	return 0;
 }
 
 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
@@ -452,11 +451,20 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
 		return 0;
 
+	/* stop the FC timeout (N_Bs). A FC that is received before the
+	 * local echo of the FF confirms the transmission of the FF, so
+	 * stop its echo timeout (N_As) as well. Only one of the timers is
+	 * active, both are stopped before the state is checked again.
+	 */
 	hrtimer_cancel(&so->txtimer);
+	if (READ_ONCE(so->tx.state) == ISOTP_WAIT_FIRST_FC) {
+		hrtimer_cancel(&so->echotimer);
+		WRITE_ONCE(so->cfecho, 0);
+	}
 
 	/* isotp_tx_timeout() may have given up on this job while
 	 * hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
@@ -509,18 +517,18 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 	switch (cf->data[ae] & 0x0F) {
 	case ISOTP_FC_CTS:
 		so->tx.bs = 0;
 		WRITE_ONCE(so->tx.state, ISOTP_SENDING);
 		/* send CF frame and enable echo timeout handling */
-		hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
+		hrtimer_start(&so->echotimer, ktime_set(ISOTP_TIMEOUT, 0),
 			      HRTIMER_MODE_REL_SOFT);
 		isotp_send_cframe(so);
 		break;
 
 	case ISOTP_FC_WT:
 		/* start timer to wait for next FC frame */
-		hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+		hrtimer_start(&so->txtimer, ktime_set(ISOTP_TIMEOUT, 0),
 			      HRTIMER_MODE_REL_SOFT);
 		break;
 
 	case ISOTP_FC_OVFLW:
 		/* overflow on receiver side - report 'message too long' */
@@ -643,11 +651,11 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	/* initial setup for this pdu reception */
 	so->rx.sn = 1;
 	WRITE_ONCE(so->rx.state, ISOTP_WAIT_DATA);
 
 	/* start rx timeout, isotp_send_fc() restarts it */
-	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* send our first FC frame */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 	return 0;
@@ -736,11 +744,11 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 		isotp_rcv_skb(nskb, sk);
 		return 0;
 	}
 
 	/* start rx timeout, isotp_send_fc() restarts it */
-	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* perform blocksize handling, if enabled */
 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
 		return 0;
@@ -986,11 +994,11 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 {
 	struct sock *sk = (struct sock *)data;
 	struct isotp_sock *so = isotp_sk(sk);
 	struct can_skb_ext *csx = can_skb_ext_find(skb);
 
-	/* only handle my own local echo CF/SF skb's (no FF!) */
+	/* only handle my own local echo skb's */
 	if (skb->sk != sk)
 		return;
 
 	if (WARN_ON_ONCE(!csx))
 		return;
@@ -1005,13 +1013,20 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 		goto out_unlock;
 
 	/* cancel local echo timeout */
 	hrtimer_cancel(&so->echotimer);
 
-	/* local echo skb with consecutive frame has been consumed */
+	/* local echo skb has been consumed */
 	WRITE_ONCE(so->cfecho, 0);
 
+	/* the FF has been sent => start the FC timeout (N_Bs) */
+	if (READ_ONCE(so->tx.state) == ISOTP_WAIT_FIRST_FC) {
+		hrtimer_start(&so->txtimer, ktime_set(ISOTP_TIMEOUT, 0),
+			      HRTIMER_MODE_REL_SOFT);
+		goto out_unlock;
+	}
+
 	/* claiming a transfer also takes so->rx_lock, so a plain recheck
 	 * is enough: so->tx.state can't have flipped to ISOTP_SENDING for
 	 * a new claim while we're still in here
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_SENDING)
@@ -1028,19 +1043,19 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 	}
 
 	if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
 		/* stop and wait for FC with timeout */
 		WRITE_ONCE(so->tx.state, ISOTP_WAIT_FC);
-		hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+		hrtimer_start(&so->txtimer, ktime_set(ISOTP_TIMEOUT, 0),
 			      HRTIMER_MODE_REL_SOFT);
 		goto out_unlock;
 	}
 
 	/* no gap between data frames needed => use burst mode */
 	if (!so->tx_gap) {
 		/* enable echo timeout handling */
-		hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
+		hrtimer_start(&so->echotimer, ktime_set(ISOTP_TIMEOUT, 0),
 			      HRTIMER_MODE_REL_SOFT);
 		isotp_send_cframe(so);
 		goto out_unlock;
 	}
 
@@ -1095,11 +1110,11 @@ static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
 					     txtimer);
 
 	return isotp_tx_timeout(so);
 }
 
-/* so->echotimer: fires when a sent CF/SF's local echo does not arrive */
+/* so->echotimer: fires when a sent SF/FF/CF's local echo does not arrive */
 static enum hrtimer_restart isotp_echo_timer_handler(struct hrtimer *hrtimer)
 {
 	struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
 					     echotimer);
 
@@ -1110,11 +1125,11 @@ static enum hrtimer_restart isotp_txfr_timer_handler(struct hrtimer *hrtimer)
 {
 	struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
 					     txfrtimer);
 
 	/* start echo timeout handling and cover below protocol error */
-	hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
+	hrtimer_start(&so->echotimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* cfecho should be consumed by isotp_rcv_echo() here */
 	if (READ_ONCE(so->tx.state) == ISOTP_SENDING && !READ_ONCE(so->cfecho))
 		isotp_send_cframe(so);
@@ -1130,12 +1145,10 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	struct can_skb_ext *csx;
 	struct net_device *dev;
 	struct canfd_frame *cf;
 	int ae;
 	int wait_tx_done;
-	s64 hrtimer_sec = ISOTP_ECHO_TIMEOUT;
-	struct hrtimer *tx_hrt = &so->echotimer;
 	u32 new_state = ISOTP_SENDING;
 	u32 my_gen;
 	u32 old_cfecho;
 	int off;
 	int err;
@@ -1244,11 +1257,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		goto err_out_drop;
 	}
 
 	csx->can_iif = dev->ifindex;
 
-	/* set uid in tx skb to identify CF echo frames */
+	/* set uid in tx skb to identify the local echo frame */
 	can_set_skb_uid(csx);
 
 	so->tx.len = size;
 	so->tx.idx = 0;
 
@@ -1306,16 +1319,12 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 			WRITE_ONCE(so->cfecho, csx->can_skb_uid);
 		} else {
 			/* standard flow control check */
 			new_state = ISOTP_WAIT_FIRST_FC;
 
-			/* start timeout for FC */
-			hrtimer_sec = ISOTP_FC_TIMEOUT;
-			tx_hrt = &so->txtimer;
-
-			/* no CF echo tag for isotp_rcv_echo() (FF-mode) */
-			WRITE_ONCE(so->cfecho, 0);
+			/* set FF echo tag to start the FC timeout (FF-mode) */
+			WRITE_ONCE(so->cfecho, csx->can_skb_uid);
 		}
 	}
 
 	spin_lock_bh(&so->rx_lock);
 	if (READ_ONCE(so->tx.state) == ISOTP_SHUTDOWN) {
@@ -1328,11 +1337,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		wake_up_interruptible(&so->wait);
 		return -EADDRNOTAVAIL;
 	}
 	/* WAIT_FIRST_FC for standard FF, else stays ISOTP_SENDING */
 	WRITE_ONCE(so->tx.state, new_state);
-	hrtimer_start(tx_hrt, ktime_set(hrtimer_sec, 0),
+	hrtimer_start(&so->echotimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 	spin_unlock_bh(&so->rx_lock);
 
 	/* send the first or only CAN frame */
 	cf->flags = so->ll.tx_flags;
@@ -1354,12 +1363,17 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 			/* don't touch timers and state of the new transfer */
 			spin_unlock_bh(&so->rx_lock);
 			return err;
 		}
 
-		/* no transmission -> no timeout monitoring */
-		hrtimer_cancel(tx_hrt);
+		/* no transmission -> no timeout monitoring. A FC that has
+		 * been received in the meantime may have armed the other
+		 * tx timers as well.
+		 */
+		hrtimer_cancel(&so->txfrtimer);
+		hrtimer_cancel(&so->txtimer);
+		hrtimer_cancel(&so->echotimer);
 		goto err_out_drop_locked;
 	}
 
 	if (wait_tx_done) {
 		/* wake up for:

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (7 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 08/24] can: isotp: add N_As timeout monitoring for SF/FF/CF Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:41   ` sashiko-bot
  2026-12-05 13:20 ` [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
                   ` (14 subsequent siblings)
  23 siblings, 1 reply; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

ISO 15765-2 supervises the transmission of a CAN frame on the receiver
side with the N_Ar timeout of 1000 ms. The consecutive frame timeout
N_Cr is started with the transmission confirmation of the flow control
frame.

So far the transmission of the FC was not supervised at all and the CF
timeout was started with the transmission request of the FC, so that a
delayed transmission of the FC shortened the time for the CF of the
sender.

Supervise the transmission of the FC with the specified N_Ar timeout of
1 s by using the local echo facility that can provide the required
L_Data confirmation trigger. The FC is now marked with a separate skb
uid to identify its local echo which enables the start of the CF timeout
(N_Cr) at FC tx confirmation time.

A CF that is received before the local echo of the FC also confirms
the transmission of the FC: it clears the FC echo tag and restarts the
rx timeout as before. The tag is also cleared when a new FC is created
and in isotp_bind().

As the CF timeout is now started with the local echo of the FC, a
delayed transmission of the FC may lead to a later ETIMEDOUT error.

As the N_As/N_Ar timeouts are now monitored with the local echo,
frame_txtime only remains in use as an extension of the STmin value for
the gap between the consecutive frames. Document this and the ETIMEDOUT
error for the N_Ar (local echo) timeout in iso15765-2.rst.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst |  6 ++++--
 net/can/isotp.c                         | 35 ++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 3f768ae1cde5..1ab84754880c 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -212,11 +212,13 @@ where the ``can_isotp_options`` structure has the following contents:
   N_As/N_Ar inside the ISO standard); if ``0``, the default (or the last set
   value) is used. To set the transmission time to ``0``, the
   ``CAN_ISOTP_FRAME_TXTIME_ZERO`` macro (equal to 0xFFFFFFFF) shall be used.
   The frame transmission time is limited to the N_As/N_Ar timeout of 1 s
   (1000000000 ns) defined in the ISO standard. It is added to the gap between
-  the consecutive frames.
+  the consecutive frames. The timeouts for the flow control (N_Bs) and the
+  consecutive frames (N_Cr) are started with the local echo of the sent CAN
+  frame, which confirms its transmission (N_As/N_Ar).
 
 * ``ext_address``: extended addressing byte, used if the
   ``CAN_ISOTP_EXTEND_ADDR`` flag is specified.
 
 * ``txpad_content``: byte used as padding value for transmitted frames.
@@ -337,11 +339,11 @@ Following errors are reported to userspace:
 
 RX path errors
 ~~~~~~~~~~~~~~
 
 ============ ===============================================================
--ETIMEDOUT   timeout of data reception
+-ETIMEDOUT   timeout of data reception or FC local echo
 -EILSEQ      sequence number mismatch during a multi-frame reception
 -EBADMSG     data reception with wrong padding
 ============ ===============================================================
 
 TX path errors
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 493bad6f9a6a..a2817cf21cf0 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -2,11 +2,11 @@
 /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
  *
  * This implementation does not provide ISO-TP specific return values to the
  * userspace.
  *
- * - RX path timeout of data reception leads to -ETIMEDOUT
+ * - RX path timeout of data reception or FC local echo leads to -ETIMEDOUT
  * - RX path SN mismatch leads to -EILSEQ
  * - RX path data reception with wrong padding leads to -EBADMSG
  * - TX path flowcontrol or local echo reception timeout leads to -ECOMM
  * - TX path flowcontrol reception overflow leads to -EMSGSIZE
  * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG
@@ -123,11 +123,11 @@ MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
 /* Flow Status given in FC frame */
 #define ISOTP_FC_CTS 0		/* clear to send */
 #define ISOTP_FC_WT 1		/* wait */
 #define ISOTP_FC_OVFLW 2	/* overflow */
 
-#define ISOTP_TIMEOUT 1		/* 1 sec (N_As, N_Bs, N_Cr) */
+#define ISOTP_TIMEOUT 1		/* 1 sec (N_As, N_Ar, N_Bs, N_Cr) */
 #define ISOTP_MAX_FRAME_TXTIME_NS NSEC_PER_SEC /* N_As/N_Ar timeout */
 
 /* so->tx_result[so->tx_gen % ISOTP_TX_RESULT_SLOTS] holds the packed value
  * (err << ISOTP_TX_RESULT_GEN_BITS | gen) for each tx generation slot, so it
  * can be handled with a single READ_ONCE()/WRITE_ONCE() access.
@@ -174,10 +174,11 @@ struct isotp_sock {
 	struct can_isotp_ll_options ll;
 	u32 frame_txtime;
 	u32 force_tx_stmin;
 	u32 force_rx_stmin;
 	u32 cfecho; /* SF/FF/CF echo tag */
+	u32 fcecho; /* flow control echo tag */
 	u32 tx_gen; /* transfer generation, increased per new tx transfer */
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
@@ -269,11 +270,11 @@ static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
 	struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
 					     rxtimer);
 	struct sock *sk = &so->sk;
 
 	if (READ_ONCE(so->rx.state) == ISOTP_WAIT_DATA) {
-		/* we did not get new data frames in time */
+		/* we did not get new data frames or the FC echo in time */
 
 		/* report 'connection timed out' */
 		sk->sk_err = ETIMEDOUT;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
@@ -296,10 +297,13 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 
 	/* no creation of flow control frames in listen mode */
 	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
 		return 0;
 
+	/* no FC echo pending until the new FC has been sent */
+	WRITE_ONCE(so->fcecho, 0);
+
 	nskb = alloc_skb(so->ll.mtu, gfp_any());
 	if (!nskb)
 		return 1;
 
 	csx = can_skb_ext_add(nskb);
@@ -313,10 +317,14 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 		kfree_skb(nskb);
 		return 1;
 	}
 
 	csx->can_iif = dev->ifindex;
+
+	/* set uid in tx skb to identify the FC echo frame */
+	can_set_skb_uid(csx);
+
 	nskb->dev = dev;
 	can_skb_set_owner(nskb, sk);
 	ncf = (struct canfd_frame *)nskb->data;
 	skb_put_zero(nskb, so->ll.mtu);
 
@@ -337,10 +345,13 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 	if (ae)
 		ncf->data[0] = so->opt.ext_address;
 
 	ncf->flags = so->ll.tx_flags;
 
+	/* set flow control echo tag to start the CF timeout */
+	WRITE_ONCE(so->fcecho, csx->can_skb_uid);
+
 	can_send_ret = can_send(nskb, 1);
 	if (can_send_ret)
 		pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
 			       __func__, ERR_PTR(can_send_ret));
 
@@ -350,11 +361,11 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 	so->rx.bs = 0;
 
 	/* reset last CF frame rx timestamp for rx stmin enforcement */
 	so->lastrxcf_tstamp = ktime_set(0, 0);
 
-	/* start rx timeout watchdog */
+	/* start rx timeout watchdog for the FC transmission (N_Ar) */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 	return 0;
 }
 
@@ -692,10 +703,13 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 	}
 
 	/* stop the rx timeout only for valid CFs (checked above) */
 	hrtimer_cancel(&so->rxtimer);
 
+	/* a CF confirms the transmission of the FC as well */
+	WRITE_ONCE(so->fcecho, 0);
+
 	/* isotp_rx_timer_handler() may have raced us for so->rx.state
 	 * while hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
 		return 1;
@@ -1006,10 +1020,20 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 	/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->rx_lock
 	 * (no isotp_rcv() caller here), so take it ourselves
 	 */
 	spin_lock(&so->rx_lock);
 
+	/* the FC has been sent => start the CF timeout (N_Cr) */
+	if (READ_ONCE(so->fcecho) == csx->can_skb_uid) {
+		WRITE_ONCE(so->fcecho, 0);
+		if (READ_ONCE(so->rx.state) == ISOTP_WAIT_DATA)
+			hrtimer_start(&so->rxtimer,
+				      ktime_set(ISOTP_TIMEOUT, 0),
+				      HRTIMER_MODE_REL_SOFT);
+		goto out_unlock;
+	}
+
 	/* so->cfecho may since belong to a new transfer; recheck under lock */
 	if (READ_ONCE(so->cfecho) != csx->can_skb_uid)
 		goto out_unlock;
 
 	/* cancel local echo timeout */
@@ -1660,12 +1684,13 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
 	/* switch to new settings */
 	so->ifindex = dev->ifindex;
 	so->rxid = rx_id;
 	so->txid = tx_id;
 
-	/* no consecutive frame echo skb in flight */
+	/* no consecutive frame or flow control echo skb in flight */
 	WRITE_ONCE(so->cfecho, 0);
+	WRITE_ONCE(so->fcecho, 0);
 
 	/* register for echo skb's first (no echo traffic at this point) */
 	err = can_rx_register(net, dev, tx_id, SINGLE_MASK(tx_id),
 			      isotp_rcv_echo, sk, sock_i_ino(sk), sk);
 	if (err)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (8 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:41   ` sashiko-bot
  2026-12-05 13:20 ` [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
                   ` (13 subsequent siblings)
  23 siblings, 1 reply; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The tx_flags of struct can_isotp_ll_options are set into the flags of
the created CAN FD frames. For CAN CC they have to be zero but for CAN
FD any value was accepted and copied into the CAN FD frames, including
CANFD_ESI and undefined flags.

CANFD_FDF is set by the kernel for compatibility reasons, as existing
applications only provide CANFD_BRS (or no flags) for CAN FD. A
read-modify-write of the socket options, e.g. to switch from CAN FD to
CAN CC with the tx_flags from getsockopt(), is no use case for CAN_ISOTP
sockets: the written options have to match the selected mtu.

Document the accepted tx_flags in isotp.h and iso15765-2.rst. Remove the
remark that the tx_flags become obsolete when the BRS flag is fixed by
the CAN driver configuration, as the bit rate switch is always selected
per CAN FD frame. Use the term CAN CC for CAN_MTU frames in isotp.h.

This is an intentional tightening of the uAPI and no bug fix: accept
CANFD_BRS and the CANFD_FDF marker (which is set by the CAN core anyway)
and reject other tx_flags with -EINVAL. CAN FD frames with CANFD_ESI
can still be sent with CAN_RAW sockets, e.g. for testing purposes.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 6 +++++-
 include/uapi/linux/can/isotp.h          | 9 ++++-----
 net/can/isotp.c                         | 9 +++++++++
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 1ab84754880c..7a0adbcc0969 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -286,11 +286,15 @@ where the ``can_isotp_ll_options`` structure has the following contents:
 * ``tx_dl``: maximum payload length for transmitted frames, can have one value
   among: 8, 12, 16, 20, 24, 32, 48, 64. Values above 8 only apply to CAN FD
   traffic (i.e.: ``mtu = CANFD_MTU``).
 
 * ``tx_flags``: flags set into ``struct canfd_frame.flags`` at frame creation.
-  Only applies to CAN FD traffic (i.e.: ``mtu = CANFD_MTU``).
+  Only applies to CAN FD traffic (i.e.: ``mtu = CANFD_MTU``) and has to be
+  ``0`` for classical CAN frames. Only the ``CANFD_BRS`` flag (and the
+  ``CANFD_FDF`` marker) can be set, other flags are rejected with ``-EINVAL``.
+  The ``CANFD_FDF`` marker is always added for CAN FD traffic, so that
+  ``getsockopt()`` returns the flags of the created CAN FD frames.
 
 Transmission stmin
 ~~~~~~~~~~~~~~~~~~
 
 The transmission minimum separation time (stmin) can be forced using the
diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index a2e5aa6f9e82..7f9fb59bad71 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -107,22 +107,21 @@ struct can_isotp_fc_options {
 
 struct can_isotp_ll_options {
 
 	__u8  mtu;		/* generated & accepted CAN frame type	*/
 				/* __u8 value :				*/
-				/* CAN_MTU   (16) -> standard CAN 2.0	*/
+				/* CAN_MTU   (16) -> CAN CC frame	*/
 				/* CANFD_MTU (72) -> CAN FD frame	*/
 
 	__u8  tx_dl;		/* tx link layer data length in bytes	*/
 				/* (configured maximum payload length)	*/
 				/* __u8 value : 8,12,16,20,24,32,48,64	*/
 				/* => rx path supports all LL_DL values */
 
-	__u8  tx_flags;		/* set into struct canfd_frame.flags	*/
-				/* at frame creation: e.g. CANFD_BRS	*/
-				/* Obsolete when the BRS flag is fixed	*/
-				/* by the CAN netdriver configuration	*/
+	__u8  tx_flags;		/* set into canfd_frame.flags at frame	*/
+				/* creation: CANFD_FDF and (optionally)	*/
+				/* CANFD_BRS are allowed in CAN FD mode	*/
 };
 
 /* flags for isotp behaviour */
 
 #define CAN_ISOTP_LISTEN_MODE	0x0001	/* listen only (do not send FC) */
diff --git a/net/can/isotp.c b/net/can/isotp.c
index a2817cf21cf0..cea50bd78632 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1832,12 +1832,21 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 
 		if (ll->mtu == CAN_MTU &&
 		    (ll->tx_dl > CAN_MAX_DLEN || ll->tx_flags != 0))
 			return -EINVAL;
 
+		/* allow only CANFD_FDF and CANFD_BRS for CAN FD frames */
+		if (ll->mtu == CANFD_MTU &&
+		    (ll->tx_flags & ~(CANFD_BRS | CANFD_FDF)))
+			return -EINVAL;
+
 		memcpy(&so->ll, ll, sizeof(*ll));
 
+		/* compat: CAN FD frames are always marked with CANFD_FDF */
+		if (so->ll.mtu == CANFD_MTU)
+			so->ll.tx_flags |= CANFD_FDF;
+
 		/* set ll_dl for tx path to similar place as for rx */
 		so->tx.ll_dl = ll->tx_dl;
 		break;
 	}
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (9 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:36   ` sashiko-bot
  2026-12-05 13:20 ` [PATCH can-next v6 12/24] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
                   ` (12 subsequent siblings)
  23 siblings, 1 reply; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The STmin value of struct can_isotp_fc_options is sent in the flow
control frames of the receiver. ISO 15765-2 reserves the values 0x80 -
0xF0 and 0xFA - 0xFF, but CAN_ISOTP_RECV_FC accepted any value and the
reserved values were sent on the bus. The sender side replaces received
reserved values with 0x7F already.

So far the STmin value was intentionally not checked, as stated in the
remark on the CAN_ISOTP_DEFAULT_RECV_* values in isotp.h, so that a
CAN_ISOTP socket could also act like a non-compliant ECU for testing
purposes. But a CAN_ISOTP socket should only send flow control frames
that comply with ISO 15765-2. Flow control frames with reserved STmin
values to test other ISO-TP implementations can be sent with CAN_RAW
sockets.

This is an intentional tightening of the uAPI and no bug fix: reject
the reserved STmin values with -EINVAL before the options are applied.

Update the remark in isotp.h and document the rejected values in
iso15765-2.rst. Additionally fix the name of struct
can_isotp_fc_options in iso15765-2.rst.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst |  5 +++--
 include/uapi/linux/can/isotp.h          |  7 +++----
 net/can/isotp.c                         | 12 ++++++++++--
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 7a0adbcc0969..e0f7df0a6043 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -241,20 +241,21 @@ to provide the communication parameters for receiving ISO-TP PDUs.
 
 where the ``can_isotp_fc_options`` structure has the following contents:
 
 .. code-block:: C
 
-    struct can_isotp_options {
+    struct can_isotp_fc_options {
         u8 bs;
         u8 stmin;
         u8 wftmax;
     };
 
 * ``bs``: blocksize provided in flow control frames.
 
 * ``stmin``: minimum separation time provided in flow control frames; can
-  have the following values (others are reserved):
+  have the following values (others are reserved and rejected with
+  ``-EINVAL``):
 
   * 0x00 - 0x7F : 0 - 127 ms
 
   * 0xF1 - 0xF9 : 100 us - 900 us
 
diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index 7f9fb59bad71..8b1cf7cc68ca 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -152,14 +152,13 @@ struct can_isotp_ll_options {
 /*
  * Remark on CAN_ISOTP_DEFAULT_RECV_* values:
  *
  * We can strongly assume, that the Linux Kernel implementation of
  * CAN_ISOTP is capable to run with BS=0, STmin=0 and WFTmax=0.
- * But as we like to be able to behave as a commonly available ECU,
- * these default settings can be changed via sockopts.
- * For that reason the STmin value is intentionally _not_ checked for
- * consistency and copied directly into the flow control (FC) frame.
+ * These default settings can be changed with the CAN_ISOTP_RECV_FC
+ * sockopt to behave like other ECUs. The reserved STmin values are
+ * rejected with -EINVAL.
  */
 
 /* link layer default values => make use of Classical CAN frames */
 
 #define CAN_ISOTP_DEFAULT_LL_MTU	CAN_MTU
diff --git a/net/can/isotp.c b/net/can/isotp.c
index cea50bd78632..01079ce4fa45 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1806,13 +1806,21 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 				so->frame_txtime = so->opt.frame_txtime;
 		}
 		break;
 	}
 
-	case CAN_ISOTP_RECV_FC:
-		memcpy(&so->rxfc, val, sizeof(so->rxfc));
+	case CAN_ISOTP_RECV_FC: {
+		const struct can_isotp_fc_options *fc = val;
+
+		/* reject the reserved STmin values 0x80 - 0xF0, 0xFA - 0xFF */
+		if (fc->stmin > 0x7F &&
+		    (fc->stmin < 0xF1 || fc->stmin > 0xF9))
+			return -EINVAL;
+
+		memcpy(&so->rxfc, fc, sizeof(*fc));
 		break;
+	}
 
 	case CAN_ISOTP_TX_STMIN:
 		so->force_tx_stmin = *(const u32 *)val;
 		break;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 12/24] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (10 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 13/24] can: isotp: drop frames without N_PCI Oliver Hartkopp
                   ` (11 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

CAN_ISOTP_CHK_PAD_DATA checks the padding bytes of received frames
against the rxpad_content, which is only defined together with
CAN_ISOTP_RX_PADDING. iso15765-2.rst documents that CAN_ISOTP_CHK_PAD_DATA
is ignored when CAN_ISOTP_RX_PADDING is not specified.

Together with CAN_ISOTP_CHK_PAD_LEN the flag is ignored as documented and
only the optimized frame length is checked. But when
CAN_ISOTP_CHK_PAD_DATA is set without CAN_ISOTP_RX_PADDING and without
CAN_ISOTP_CHK_PAD_LEN, check_pad() does not ignore the flag but rejects
every received frame: SFs and the last CF of a PDU are dropped with
EBADMSG and every FC aborts the transmission with EBADMSG.

Ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING in this case
as well, as documented. This only affects a flag combination that has
never been able to receive any PDU. Therefore the patch has no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 01079ce4fa45..bf2f29500ade 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -433,12 +433,12 @@ static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
 	/* no RX_PADDING value => check length of optimized frame length */
 	if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
 		if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
 			return check_optimized(cf, start_index);
 
-		/* no valid test against empty value => ignore frame */
-		return 1;
+		/* no rx padding content => ignore CAN_ISOTP_CHK_PAD_DATA */
+		return 0;
 	}
 
 	/* check datalength of correctly padded CAN frame */
 	if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
 	    cf->len != padlen(cf->len))

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 13/24] can: isotp: drop frames without N_PCI
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (11 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 12/24] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 14/24] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
                   ` (10 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_rcv() reads the address extension and the N_PCI without checking
the CAN frame data length. So frames with CAN_DL < ae + N_PCI_SZ are
processed with the data bytes beyond CAN_DL, e.g. as SF, CF or FC.

As the data of the CAN frame structure is always present, the reads stay
within the skb. But the processed values are no content of the received
CAN frame, e.g. a frame with CAN_DL 0 was processed as SF with SF_DL 0
from the zero-filled data of the CAN drivers.

Drop these frames as they are no valid N_PDUs. The new check only
ensures that the N_PCI (and the AE) is contained in the CAN frame. The
content of the N_PCI is checked afterwards when the specific N_PDU type
is processed. As only invalid frames are affected, there is no Fixes
tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index bf2f29500ade..69923f590157 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -794,10 +794,14 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		return;
 	}
 
 	cf = (struct canfd_frame *)skb->data;
 
+	/* frames without N_PCI (and AE) are no N_PDUs */
+	if (cf->len < ae + N_PCI_SZ)
+		return;
+
 	/* if enabled: check reception of my configured extended address */
 	if (ae && cf->data[0] != so->opt.rx_ext_address)
 		return;
 
 	n_pci_type = cf->data[ae] & 0xF0;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 14/24] can: isotp: drop FF frames with invalid length extension
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (12 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 13/24] can: isotp: drop frames without N_PCI Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 15/24] can: isotp: check the minimum FF_DL Oliver Hartkopp
                   ` (9 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The FF data length usually consists of 12 bits for values up to 4095 byte.
ISO 15765-2:2016 introduced an escape sequence where the 32 bit length
information is retrieved from the following 4 byte when the FF data length
is set to the reserved value 0.

This escape sequence is only allowed for FF data length values greater than
4095 and therefore PDUs with length values that would also fit into the 12
bit value representation shall be ignored according to ISO 15765-2:2016.

So far these FFs were accepted and answered with a FC. Drop them without
sending a FC. Like any other received FF, such a FF terminates an ongoing
reception. As only FFs from a non-compliant sender are affected, there is
no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 69923f590157..aa3f239ca498 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -623,10 +623,14 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		so->rx.len = cf->data[ae + 2] << 24;
 		so->rx.len += cf->data[ae + 3] << 16;
 		so->rx.len += cf->data[ae + 4] << 8;
 		so->rx.len += cf->data[ae + 5];
 		ff_pci_sz = FF_PCI_SZ32;
+
+		/* unneeded escape sequence => ignore PDU according to spec */
+		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
+			return 1;
 	}
 
 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
 	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 15/24] can: isotp: check the minimum FF_DL
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (13 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 14/24] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 16/24] can: isotp: check the data length of the last CF Oliver Hartkopp
                   ` (8 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

ISO 15765-2 requires FF_DL >= FF_DLmin: a PDU that fits into a SF must
not be segmented.

The rx check in isotp_rcv_ff() added a SF_DL escape offset for LL_DL > 8
on the wrong side and only rejected FF_DL < LL_DL - ae - off - ff_pci_sz.
So it did not even ensure that a CF is needed. It accepted FFs whose
FF_DL fits into the FF itself (e.g. FF_DL 6 for LL_DL 8, FF_DL 61 and 62
for LL_DL 64), where the receiver sent a FC and waited for a CF that is
never sent. And it accepted FFs whose FF_DL fits into a SF (e.g. FF_DL 7
for LL_DL 8), which a compliant sender never sends.

Check the FF_DL against the maximum SF_DL of the sender LL_DL like
isotp_sendmsg() does on the tx path, so that both kinds of FFs are
ignored. As only FF_DL is on the left side the check does not wrap
around for FF_DL values close to 2^32.

As only FFs from a non-compliant sender are affected and the copied FF
data stays within the receive buffer, there is no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index aa3f239ca498..f707fac03900 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -595,12 +595,12 @@ static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
 }
 
 static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 {
 	struct isotp_sock *so = isotp_sk(sk);
+	unsigned int sf_max;
 	int i;
-	int off;
 	int ff_pci_sz;
 
 	hrtimer_cancel(&so->rxtimer);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
@@ -629,14 +629,17 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		/* unneeded escape sequence => ignore PDU according to spec */
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
 			return 1;
 	}
 
-	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
-	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
+	/* the PDU must not fit into a SF (FF_DLmin) */
+	if (so->rx.ll_dl == CAN_MAX_DLEN)
+		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
+	else
+		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
-	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
+	if (so->rx.len <= sf_max)
 		return 1;
 
 	/* PDU size > default => try max_pdu_size
 	 *
 	 * This buffer is not accounted to a memory cgroup: In softirq

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 16/24] can: isotp: check the data length of the last CF
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (14 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 15/24] can: isotp: check the minimum FF_DL Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 17/24] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
                   ` (7 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

A CF shorter than LL_DL is only allowed as last CF and therefore has to
carry all missing bytes of the PDU. But the missing bytes were compared
with the payload of a full CF. So a short CF without all missing bytes
was accepted and the reception continued with further CFs.

Drop a short CF when the missing PDU bytes exceed its possible payload
(cf->len - ae - N_PCI_SZ). Padding is still possible as it only
increases cf->len. isotp_rcv() ensures cf->len >= ae + N_PCI_SZ, so
the payload size can not underflow.

As only CFs from a non-compliant sender are affected and the copied CF
data stays within the receive buffer, there is no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index f707fac03900..2c9c6dcbfb5e 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -690,12 +690,14 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 	if (cf->len > so->rx.ll_dl)
 		return 1;
 
 	/* CFs have usually the LL_DL length */
 	if (cf->len < so->rx.ll_dl) {
-		/* this is only allowed for the last CF */
-		if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
+		/* only the last CF may be shorter: drop it when the
+		 * missing PDU bytes exceed its possible payload
+		 */
+		if (so->rx.len - so->rx.idx > cf->len - ae - N_PCI_SZ)
 			return 1;
 	}
 
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
 		return 0;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 17/24] can: isotp: drop inline keyword, let the compiler decide
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (15 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 16/24] can: isotp: check the data length of the last CF Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 18/24] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
                   ` (6 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Modern kernel style avoids forcing inlining via the inline keyword on
static functions - the compiler heuristics decide better on their own.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 2c9c6dcbfb5e..ef57200bba8f 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -247,11 +247,11 @@ static bool isotp_tx_gen_done(struct isotp_sock *so, u32 gen)
 
 	return state == ISOTP_IDLE || state == ISOTP_SHUTDOWN ||
 	       READ_ONCE(so->tx_gen) != gen;
 }
 
-static inline struct isotp_sock *isotp_sk(const struct sock *sk)
+static struct isotp_sock *isotp_sk(const struct sock *sk)
 {
 	return (struct isotp_sock *)sk;
 }
 
 static u32 isotp_bc_flags(struct isotp_sock *so)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 18/24] can: isotp: rename rx_lock as it has become state machine lock
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (16 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 17/24] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 19/24] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
                   ` (5 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

rx_lock has been used to lock some receive path functionalities.
Meanwhile it serializes the rx path with the claim of a transfer in
isotp_sendmsg(), the local echo handling in isotp_rcv_echo() and the tx
shutdown in isotp_release(). So give it a proper name sm_lock.

The timer callbacks run without sm_lock, as the timers are cancelled
under sm_lock. Their state changes are limited to the timeouts to
ISOTP_IDLE.

Update the comment in isotp_rcv() which still describes the locking
as not needed in real world use cases.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 57 +++++++++++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 28 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index ef57200bba8f..6dcac294d891 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -180,11 +180,11 @@ struct isotp_sock {
 	u32 tx_gen; /* transfer generation, increased per new tx transfer */
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
-	spinlock_t rx_lock; /* protect single thread state machine */
+	spinlock_t sm_lock; /* protect single thread state machine */
 	struct mutex conf_lock; /* protects bind and sockopts */
 };
 
 static LIST_HEAD(isotp_notifier_list);
 static DEFINE_SPINLOCK(isotp_notifier_lock);
@@ -814,14 +814,15 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		return;
 
 	n_pci_type = cf->data[ae] & 0xF0;
 
 	/* Make sure the state changes and data structures stay consistent at
-	 * CAN frame reception time. This locking is not needed in real world
-	 * use cases but the inconsistency can be triggered with syzkaller.
+	 * CAN frame reception time: so->sm_lock serializes the rx path with
+	 * the claim of a transfer in isotp_sendmsg(), with isotp_rcv_echo()
+	 * and with the tx shutdown in isotp_release().
 	 */
-	spin_lock(&so->rx_lock);
+	spin_lock(&so->sm_lock);
 
 	if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
 		/* check rx/tx path half duplex expectations */
 		if ((READ_ONCE(so->tx.state) != ISOTP_IDLE &&
 		     n_pci_type != N_PCI_FC) ||
@@ -876,11 +877,11 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		isotp_rcv_cf(sk, cf, ae, skb);
 		break;
 	}
 
 out_unlock:
-	spin_unlock(&so->rx_lock);
+	spin_unlock(&so->sm_lock);
 }
 
 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 				 int ae, int off)
 {
@@ -1028,14 +1029,14 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 		return;
 
 	if (WARN_ON_ONCE(!csx))
 		return;
 
-	/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->rx_lock
+	/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->sm_lock
 	 * (no isotp_rcv() caller here), so take it ourselves
 	 */
-	spin_lock(&so->rx_lock);
+	spin_lock(&so->sm_lock);
 
 	/* the FC has been sent => start the CF timeout (N_Cr) */
 	if (READ_ONCE(so->fcecho) == csx->can_skb_uid) {
 		WRITE_ONCE(so->fcecho, 0);
 		if (READ_ONCE(so->rx.state) == ISOTP_WAIT_DATA)
@@ -1060,11 +1061,11 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 		hrtimer_start(&so->txtimer, ktime_set(ISOTP_TIMEOUT, 0),
 			      HRTIMER_MODE_REL_SOFT);
 		goto out_unlock;
 	}
 
-	/* claiming a transfer also takes so->rx_lock, so a plain recheck
+	/* claiming a transfer also takes so->sm_lock, so a plain recheck
 	 * is enough: so->tx.state can't have flipped to ISOTP_SENDING for
 	 * a new claim while we're still in here
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_SENDING)
 		goto out_unlock;
@@ -1098,17 +1099,17 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 
 	/* start timer to send next consecutive frame with correct delay */
 	hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
 
 out_unlock:
-	spin_unlock(&so->rx_lock);
+	spin_unlock(&so->sm_lock);
 }
 
 /* isotp_tx_timeout: we did not get any flow control or echo frame in time
  *
  * Shared by so->txtimer's and so->echotimer's callbacks. Both timers get
- * cancelled under so->rx_lock elsewhere, so this must stay lock-free.
+ * cancelled under so->sm_lock elsewhere, so this must stay lock-free.
  *
  * tx.state is acquired before tx_gen. Common sequence in isotp_tx_gen_done().
  * cmpxchg() only orders itself, not the two preceding loads.
  */
 static enum hrtimer_restart isotp_tx_timeout(struct isotp_sock *so)
@@ -1191,19 +1192,19 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	int err;
 
 	if (!READ_ONCE(so->bound) || READ_ONCE(so->tx.state) == ISOTP_SHUTDOWN)
 		return -EADDRNOTAVAIL;
 
-	/* claim the socket under so->rx_lock: this serializes the claim
+	/* claim the socket under so->sm_lock: this serializes the claim
 	 * with the RX path and with sendmsg()'s own error paths below, so
 	 * none of them can ever see a transfer mid-claim
 	 */
 	for (;;) {
-		spin_lock_bh(&so->rx_lock);
+		spin_lock_bh(&so->sm_lock);
 		if (READ_ONCE(so->tx.state) == ISOTP_IDLE)
 			break;
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 
 		/* we do not support multiple buffers - for now */
 		if (msg->msg_flags & MSG_DONTWAIT)
 			return -EAGAIN;
 
@@ -1228,11 +1229,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	isotp_set_tx_result(so, my_gen, ECOMM); /* prevent stale slot matching */
 	WRITE_ONCE(so->tx_gen, my_gen);
 	smp_wmb(); /* see smp_load_acquire() in isotp_tx_[timeout|gen_done] */
 	WRITE_ONCE(so->tx.state, ISOTP_SENDING);
 	WRITE_ONCE(so->cfecho, 0);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 
 	/* so->bound is only checked once above - a wakeup may have
 	 * unbound/rebound the socket meanwhile => recheck
 	 */
 	if (!READ_ONCE(so->bound)) {
@@ -1361,26 +1362,26 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 			/* set FF echo tag to start the FC timeout (FF-mode) */
 			WRITE_ONCE(so->cfecho, csx->can_skb_uid);
 		}
 	}
 
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	if (READ_ONCE(so->tx.state) == ISOTP_SHUTDOWN) {
 		/* isotp_release() has since taken over and already drained
 		 * our timers - don't send into a socket that's going away
 		 */
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 		kfree_skb(skb);
 		dev_put(dev);
 		wake_up_interruptible(&so->wait);
 		return -EADDRNOTAVAIL;
 	}
 	/* WAIT_FIRST_FC for standard FF, else stays ISOTP_SENDING */
 	WRITE_ONCE(so->tx.state, new_state);
 	hrtimer_start(&so->echotimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 
 	/* send the first or only CAN frame */
 	cf->flags = so->ll.tx_flags;
 
 	skb->dev = dev;
@@ -1389,18 +1390,18 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	dev_put(dev);
 	if (err) {
 		pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
 			       __func__, ERR_PTR(err));
 
-		spin_lock_bh(&so->rx_lock);
+		spin_lock_bh(&so->sm_lock);
 
 		/* new transfer already claimed by a concurrent completion,
 		 * timeout or sendmsg() while we were stuck in can_send()?
 		 */
 		if (READ_ONCE(so->tx_gen) != my_gen) {
 			/* don't touch timers and state of the new transfer */
-			spin_unlock_bh(&so->rx_lock);
+			spin_unlock_bh(&so->sm_lock);
 			return err;
 		}
 
 		/* no transmission -> no timeout monitoring. A FC that has
 		 * been received in the meantime may have armed the other
@@ -1444,34 +1445,34 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 
 	return size;
 
 err_out_drop:
 	/* claimed but nothing sent yet - no timer to cancel */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	goto err_out_drop_locked;
 err_event_drop:
 	/* interrupted or shut down while waiting on our own transfer */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 
 	/* new transfer already started by concurrent sendmsg()? */
 	if (READ_ONCE(so->tx_gen) != my_gen) {
 		/* don't touch timers and states of the new transfer */
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 		return err;
 	}
 
 	hrtimer_cancel(&so->txfrtimer);
 	hrtimer_cancel(&so->txtimer);
 	hrtimer_cancel(&so->echotimer);
 err_out_drop_locked:
-	/* release the claim; so->rx_lock still held from above */
+	/* release the claim; so->sm_lock still held from above */
 	WRITE_ONCE(so->cfecho, 0);
 
 	/* only claim to IDLE if isotp_release() has not taken over */
 	if (READ_ONCE(so->tx.state) != ISOTP_SHUTDOWN)
 		WRITE_ONCE(so->tx.state, ISOTP_IDLE);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 	wake_up_interruptible(&so->wait);
 
 	return err;
 }
 
@@ -1537,17 +1538,17 @@ static int isotp_release(struct socket *sock)
 	while (READ_ONCE(so->tx.state) != ISOTP_IDLE &&
 	       wait_event_interruptible(so->wait,
 					READ_ONCE(so->tx.state) == ISOTP_IDLE) == 0)
 		;
 
-	/* claim the socket under so->rx_lock like sendmsg() does, so its
+	/* claim the socket under so->sm_lock like sendmsg() does, so its
 	 * claim can't race the forced ISOTP_SHUTDOWN below; force it
 	 * unconditionally, even when a signal cut the wait above short
 	 */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	WRITE_ONCE(so->tx.state, ISOTP_SHUTDOWN);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 	/* forced SHUTDOWN may have skipped IDLE (gave up on a signal) */
 	wake_up_interruptible(&so->wait);
 
@@ -2139,11 +2140,11 @@ static int isotp_init(struct sock *sk)
 		      CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 	hrtimer_setup(&so->txfrtimer, isotp_txfr_timer_handler,
 		      CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 
 	init_waitqueue_head(&so->wait);
-	spin_lock_init(&so->rx_lock);
+	spin_lock_init(&so->sm_lock);
 
 	spin_lock(&isotp_notifier_lock);
 	list_add_tail(&so->notifier, &isotp_notifier_list);
 	spin_unlock(&isotp_notifier_lock);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 19/24] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (17 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 18/24] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 20/24] can: isotp: rename variable for the required content length Oliver Hartkopp
                   ` (4 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Move duplicated code into a separate function as preparation for the
CAN XL support which will need this functionality too.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 6dcac294d891..210df3b35aaa 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1605,10 +1605,27 @@ static int isotp_release(struct socket *sock)
 	sock_put(sk);
 
 	return 0;
 }
 
+static bool isotp_invalid_canid(canid_t canid)
+{
+	canid_t sanitized_id = canid;
+
+	/* sanitize CAN identifier */
+	if (sanitized_id & CAN_EFF_FLAG)
+		sanitized_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
+	else
+		sanitized_id &= CAN_SFF_MASK;
+
+	/* give feedback on wrong CAN-ID value */
+	if (sanitized_id != canid)
+		return true;
+
+	return false;
+}
+
 static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len)
 {
 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
@@ -1623,31 +1640,17 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
 		return -EINVAL;
 
 	if (addr->can_family != AF_CAN)
 		return -EINVAL;
 
-	/* sanitize tx CAN identifier */
-	if (tx_id & CAN_EFF_FLAG)
-		tx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
-	else
-		tx_id &= CAN_SFF_MASK;
-
-	/* give feedback on wrong CAN-ID value */
-	if (tx_id != addr->can_addr.tp.tx_id)
+	/* check for correct tx CAN identifier */
+	if (isotp_invalid_canid(tx_id))
 		return -EINVAL;
 
-	/* sanitize rx CAN identifier (if needed) */
-	if (isotp_register_rxid(so)) {
-		if (rx_id & CAN_EFF_FLAG)
-			rx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
-		else
-			rx_id &= CAN_SFF_MASK;
-
-		/* give feedback on wrong CAN-ID value */
-		if (rx_id != addr->can_addr.tp.rx_id)
-			return -EINVAL;
-	}
+	/* check for correct rx CAN identifier (if needed) */
+	if (isotp_register_rxid(so) && isotp_invalid_canid(rx_id))
+		return -EINVAL;
 
 	if (!addr->can_ifindex)
 		return -ENODEV;
 
 	/* so->conf_lock serializes the binding with isotp_notify() */

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 20/24] can: isotp: rename variable for the required content length
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (18 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 19/24] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 21/24] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
                   ` (3 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Rename the meaningless 'num' variable to 'reqlen' to simplify the review
for the CAN XL integration.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 210df3b35aaa..3590099581d4 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -885,17 +885,17 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 				 int ae, int off)
 {
 	int pcilen = N_PCI_SZ + ae + off;
 	int space = so->tx.ll_dl - pcilen;
-	int num = min_t(int, so->tx.len - so->tx.idx, space);
+	int reqlen = min_t(int, so->tx.len - so->tx.idx, space);
 	int i;
 
 	cf->can_id = so->txid;
-	cf->len = num + pcilen;
+	cf->len = reqlen + pcilen;
 
-	if (num < space) {
+	if (reqlen < space) {
 		if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
 			/* user requested padding */
 			cf->len = padlen(cf->len);
 			memset(cf->data, so->opt.txpad_content, cf->len);
 		} else if (cf->len > CAN_MAX_DLEN) {
@@ -904,11 +904,11 @@ static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 			memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
 			       cf->len);
 		}
 	}
 
-	for (i = 0; i < num; i++)
+	for (i = 0; i < reqlen; i++)
 		cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
 
 	if (ae)
 		cf->data[0] = so->opt.ext_address;
 }

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 21/24] can: isotp: rename checkpad functions returning bool values
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (19 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 20/24] can: isotp: rename variable for the required content length Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 22/24] can: isotp: change return type of functions to void Oliver Hartkopp
                   ` (2 subsequent siblings)
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The checkpad functions return true when failing. Rename the functions
to reflect their functionality together with a conversion to bool
return values.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 3590099581d4..66ea8530720f 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -402,12 +402,12 @@ static u8 padlen(u8 datalen)
 		return 64;
 
 	return plen[datalen];
 }
 
-/* check for length optimization and return 1/true when the check fails */
-static int check_optimized(struct canfd_frame *cf, int start_index)
+/* check for length optimization and return true when the check fails */
+static bool chk_optimized_fail(struct canfd_frame *cf, int start_index)
 {
 	/* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
 	 * padding would start at this point. E.g. if the padding would
 	 * start at cf.data[7] cf->len has to be 7 to be optimal.
 	 * Note: The data[] index starts with zero.
@@ -422,37 +422,38 @@ static int check_optimized(struct canfd_frame *cf, int start_index)
 	 * point the padlen(cf->len) is always equal to cf->len.
 	 */
 	return (cf->len != padlen(start_index));
 }
 
-/* check padding and return 1/true when the check fails */
-static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
-		     int start_index, u8 content)
+/* check padding and return true when the check fails */
+static bool chk_pad_fail(struct isotp_sock *so, struct canfd_frame *cf,
+			 int start_index, u8 content)
 {
 	int i;
 
 	/* no RX_PADDING value => check length of optimized frame length */
 	if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
 		if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
-			return check_optimized(cf, start_index);
+			return chk_optimized_fail(cf, start_index);
 
 		/* no rx padding content => ignore CAN_ISOTP_CHK_PAD_DATA */
-		return 0;
+		return false;
 	}
 
 	/* check datalength of correctly padded CAN frame */
 	if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
 	    cf->len != padlen(cf->len))
-		return 1;
+		return true;
 
 	/* check padding content */
 	if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
 		for (i = start_index; i < cf->len; i++)
 			if (cf->data[i] != content)
-				return 1;
+				return true;
 	}
-	return 0;
+
+	return false;
 }
 
 static void isotp_send_cframe(struct isotp_sock *so);
 
 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
@@ -482,11 +483,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
 		return 1;
 
 	if ((cf->len < ae + FC_CONTENT_SZ) ||
 	    ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-	     check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
+	     chk_pad_fail(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 
@@ -572,11 +573,11 @@ static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
 
 	if (!len || len > cf->len - pcilen)
 		return 1;
 
 	if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-	    check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
+	    chk_pad_fail(so, cf, pcilen + len, so->opt.rxpad_content)) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 		return 1;
@@ -745,11 +746,11 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 	if (so->rx.idx >= so->rx.len) {
 		/* we are done */
 		WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 		if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-		    check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
+		    chk_pad_fail(so, cf, i + 1, so->opt.rxpad_content)) {
 			/* malformed PDU - report 'not a data message' */
 			sk->sk_err = EBADMSG;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk_error_report(sk);
 			return 1;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 22/24] can: isotp: change return type of functions to void
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (20 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 21/24] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 23/24] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 24/24] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The return values of some functions have never been used and needed.
Make those functions void to remove unused return values.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 67 ++++++++++++++++++++++++++-------------------------------
 1 file changed, 31 insertions(+), 36 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 66ea8530720f..879213bdb8e5 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -284,40 +284,40 @@ static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
 	}
 
 	return HRTIMER_NORESTART;
 }
 
-static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
+static void isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 {
 	struct net_device *dev;
 	struct sk_buff *nskb;
 	struct can_skb_ext *csx;
 	struct canfd_frame *ncf;
 	struct isotp_sock *so = isotp_sk(sk);
 	int can_send_ret;
 
 	/* no creation of flow control frames in listen mode */
 	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
+		return;
 
 	/* no FC echo pending until the new FC has been sent */
 	WRITE_ONCE(so->fcecho, 0);
 
 	nskb = alloc_skb(so->ll.mtu, gfp_any());
 	if (!nskb)
-		return 1;
+		return;
 
 	csx = can_skb_ext_add(nskb);
 	if (!csx) {
 		kfree_skb(nskb);
-		return 1;
+		return;
 	}
 
 	dev = dev_get_by_index(sock_net(sk), so->ifindex);
 	if (!dev) {
 		kfree_skb(nskb);
-		return 1;
+		return;
 	}
 
 	csx->can_iif = dev->ifindex;
 
 	/* set uid in tx skb to identify the FC echo frame */
@@ -364,11 +364,10 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 	so->lastrxcf_tstamp = ktime_set(0, 0);
 
 	/* start rx timeout watchdog for the FC transmission (N_Ar) */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
-	return 0;
 }
 
 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
 {
 	struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
@@ -454,18 +453,18 @@ static bool chk_pad_fail(struct isotp_sock *so, struct canfd_frame *cf,
 	return false;
 }
 
 static void isotp_send_cframe(struct isotp_sock *so);
 
-static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
+static void isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 {
 	struct sock *sk = &so->sk;
 	int tx_err = EBADMSG; /* default for unknown FC status */
 
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
-		return 0;
+		return;
 
 	/* stop the FC timeout (N_Bs). A FC that is received before the
 	 * local echo of the FF confirms the transmission of the FF, so
 	 * stop its echo timeout (N_As) as well. Only one of the timers is
 	 * active, both are stopped before the state is checked again.
@@ -479,11 +478,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 	/* isotp_tx_timeout() may have given up on this job while
 	 * hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
-		return 1;
+		return;
 
 	if ((cf->len < ae + FC_CONTENT_SZ) ||
 	    ((so->opt.flags & ISOTP_CHECK_PADDING) &&
 	     chk_pad_fail(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
 		/* malformed PDU - report 'not a data message' */
@@ -493,11 +492,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 
 		isotp_set_tx_result(so, so->tx_gen, EBADMSG);
 		/* set to IDLE after publishing tx_result */
 		smp_store_release(&so->tx.state, ISOTP_IDLE);
 		wake_up_interruptible(&so->wait);
-		return 1;
+		return;
 	}
 
 	/* get static/dynamic communication params from first/every FC frame */
 	if (READ_ONCE(so->tx.state) == ISOTP_WAIT_FIRST_FC ||
 	    so->opt.flags & CAN_ISOTP_DYN_FC_PARMS) {
@@ -557,47 +556,45 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 		isotp_set_tx_result(so, so->tx_gen, tx_err);
 		/* set to IDLE after publishing tx_result */
 		smp_store_release(&so->tx.state, ISOTP_IDLE);
 		wake_up_interruptible(&so->wait);
 	}
-	return 0;
 }
 
-static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
-			struct sk_buff *skb, int len)
+static void isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
+			 struct sk_buff *skb, int len)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	struct sk_buff *nskb;
 
 	hrtimer_cancel(&so->rxtimer);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 	if (!len || len > cf->len - pcilen)
-		return 1;
+		return;
 
 	if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
 	    chk_pad_fail(so, cf, pcilen + len, so->opt.rxpad_content)) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
-		return 1;
+		return;
 	}
 
 	nskb = alloc_skb(len, gfp_any());
 	if (!nskb)
-		return 1;
+		return;
 
 	memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
 
 	nskb->tstamp = skb->tstamp;
 	nskb->dev = skb->dev;
 	isotp_rcv_skb(nskb, sk);
-	return 0;
 }
 
-static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
+static void isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	unsigned int sf_max;
 	int i;
 	int ff_pci_sz;
@@ -608,11 +605,11 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	/* get the used sender LL_DL from the (first) CAN frame data length */
 	so->rx.ll_dl = padlen(cf->len);
 
 	/* the first frame has to use the entire frame up to LL_DL length */
 	if (cf->len != so->rx.ll_dl)
-		return 1;
+		return;
 
 	/* get the FF_DL */
 	so->rx.len = (cf->data[ae] & 0x0F) << 8;
 	so->rx.len += cf->data[ae + 1];
 
@@ -627,21 +624,21 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		so->rx.len += cf->data[ae + 5];
 		ff_pci_sz = FF_PCI_SZ32;
 
 		/* unneeded escape sequence => ignore PDU according to spec */
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
-			return 1;
+			return;
 	}
 
 	/* the PDU must not fit into a SF (FF_DLmin) */
 	if (so->rx.ll_dl == CAN_MAX_DLEN)
 		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
 	else
 		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
 	if (so->rx.len <= sf_max)
-		return 1;
+		return;
 
 	/* PDU size > default => try max_pdu_size
 	 *
 	 * This buffer is not accounted to a memory cgroup: In softirq
 	 * context the charge would either have to be forced beyond the
@@ -657,11 +654,11 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	}
 
 	if (so->rx.len > so->rx.buflen) {
 		/* send FC frame with overflow status */
 		isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
-		return 1;
+		return;
 	}
 
 	/* copy the first received data bytes */
 	so->rx.idx = 0;
 	for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
@@ -675,41 +672,40 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* send our first FC frame */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
-	return 0;
 }
 
-static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
-			struct sk_buff *skb)
+static void isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
+			 struct sk_buff *skb)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	struct sk_buff *nskb;
 	int i;
 
 	/* CFs are never longer than the FF */
 	if (cf->len > so->rx.ll_dl)
-		return 1;
+		return;
 
 	/* CFs have usually the LL_DL length */
 	if (cf->len < so->rx.ll_dl) {
 		/* only the last CF may be shorter: drop it when the
 		 * missing PDU bytes exceed its possible payload
 		 */
 		if (so->rx.len - so->rx.idx > cf->len - ae - N_PCI_SZ)
-			return 1;
+			return;
 	}
 
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
-		return 0;
+		return;
 
 	/* drop if timestamp gap is less than force_rx_stmin nano secs */
 	if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
 		if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
 		    so->force_rx_stmin)
-			return 0;
+			return;
 
 		so->lastrxcf_tstamp = skb->tstamp;
 	}
 
 	/* stop the rx timeout only for valid CFs (checked above) */
@@ -720,21 +716,21 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* isotp_rx_timer_handler() may have raced us for so->rx.state
 	 * while hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
-		return 1;
+		return;
 
 	if ((cf->data[ae] & 0x0F) != so->rx.sn) {
 		/* wrong sn detected - report 'illegal byte sequence' */
 		sk->sk_err = EILSEQ;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 
 		/* reset rx state */
 		WRITE_ONCE(so->rx.state, ISOTP_IDLE);
-		return 1;
+		return;
 	}
 	so->rx.sn++;
 	so->rx.sn %= 16;
 
 	for (i = ae + N_PCI_SZ; i < cf->len; i++) {
@@ -751,37 +747,36 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 		    chk_pad_fail(so, cf, i + 1, so->opt.rxpad_content)) {
 			/* malformed PDU - report 'not a data message' */
 			sk->sk_err = EBADMSG;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk_error_report(sk);
-			return 1;
+			return;
 		}
 
 		nskb = alloc_skb(so->rx.len, gfp_any());
 		if (!nskb)
-			return 1;
+			return;
 
 		memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
 		       so->rx.len);
 
 		nskb->tstamp = skb->tstamp;
 		nskb->dev = skb->dev;
 		isotp_rcv_skb(nskb, sk);
-		return 0;
+		return;
 	}
 
 	/* start rx timeout, isotp_send_fc() restarts it */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* perform blocksize handling, if enabled */
 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
-		return 0;
+		return;
 
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
-	return 0;
 }
 
 static void isotp_rcv(struct sk_buff *skb, void *data)
 {
 	struct sock *sk = (struct sock *)data;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 23/24] can: isotp: rename pointer in isotp_rcv() prototype
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (21 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 22/24] can: isotp: change return type of functions to void Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  2026-12-05 13:20 ` [PATCH can-next v6 24/24] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Preparation for CAN XL support where a 'data' pointer is commonly used.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 879213bdb8e5..87c52dbf216b 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -775,13 +775,13 @@ static void isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 }
 
-static void isotp_rcv(struct sk_buff *skb, void *data)
+static void isotp_rcv(struct sk_buff *skb, void *skdata)
 {
-	struct sock *sk = (struct sock *)data;
+	struct sock *sk = (struct sock *)skdata;
 	struct isotp_sock *so = isotp_sk(sk);
 	struct canfd_frame *cf;
 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
 	u8 n_pci_type, sf_dl;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* [PATCH can-next v6 24/24] can: isotp: introduce CAN_ISOTP_MIN_TX_DL
  2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (22 preceding siblings ...)
  2026-12-05 13:20 ` [PATCH can-next v6 23/24] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
@ 2026-12-05 13:20 ` Oliver Hartkopp
  23 siblings, 0 replies; 28+ messages in thread
From: Oliver Hartkopp @ 2026-12-05 13:20 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The minimum TX_DL value is 8 and the CAN_MAX_DLEN is also 8.
To not mix the intention and context of the defines introduce a separate
CAN_ISOTP_MIN_TX_DL definition to perform TX_DL related checks.

Use CAN_ISOTP_MIN_TX_DL where a TX_DL or LL_DL value is checked and keep
CAN_MAX_DLEN for the checks of the CAN_DL of a CAN frame.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 include/uapi/linux/can/isotp.h | 6 +++++-
 net/can/isotp.c                | 6 +++---
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index 8b1cf7cc68ca..a4a325f0053e 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -157,14 +157,18 @@ struct can_isotp_ll_options {
  * These default settings can be changed with the CAN_ISOTP_RECV_FC
  * sockopt to behave like other ECUs. The reserved STmin values are
  * rejected with -EINVAL.
  */
 
+/* minimum TX_DL value (link layer data length of a Classical CAN frame) */
+
+#define CAN_ISOTP_MIN_TX_DL		CAN_MAX_DLEN
+
 /* link layer default values => make use of Classical CAN frames */
 
 #define CAN_ISOTP_DEFAULT_LL_MTU	CAN_MTU
-#define CAN_ISOTP_DEFAULT_LL_TX_DL	CAN_MAX_DLEN
+#define CAN_ISOTP_DEFAULT_LL_TX_DL	CAN_ISOTP_MIN_TX_DL
 #define CAN_ISOTP_DEFAULT_LL_TX_FLAGS	0
 
 /*
  * The CAN_ISOTP_DEFAULT_FRAME_TXTIME has become a non-zero value as
  * it only makes sense for isotp implementation tests to run without
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 87c52dbf216b..1da35201f679 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -628,12 +628,12 @@ static void isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
 			return;
 	}
 
 	/* the PDU must not fit into a SF (FF_DLmin) */
-	if (so->rx.ll_dl == CAN_MAX_DLEN)
-		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
+	if (so->rx.ll_dl == CAN_ISOTP_MIN_TX_DL)
+		sf_max = CAN_ISOTP_MIN_TX_DL - SF_PCI_SZ4 - ae;
 	else
 		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
 	if (so->rx.len <= sf_max)
 		return;
@@ -1255,11 +1255,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		err = -EINVAL;
 		goto err_out_drop;
 	}
 
 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
-	off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
+	off = (so->tx.ll_dl > CAN_ISOTP_MIN_TX_DL) ? 1 : 0;
 
 	/* does the given data fit into a single frame for SF_BROADCAST? */
 	if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
 	    (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
 		err = -EINVAL;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 28+ messages in thread

* Re: [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
  2026-12-05 13:20 ` [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
@ 2026-12-05 13:36   ` sashiko-bot
  0 siblings, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-12-05 13:36 UTC (permalink / raw)
  To: Oliver Hartkopp, linux-can
  Cc: Marc Kleine-Budde, Vincent Mailhol, Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] can: isotp: isotp_setsockopt_locked() breaks documented STmin
  UAPI contract
--

commit e411f95ace9edaf2da83a5a7f6bcf6411de8b25e
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC

This commit rejects ISO 15765-2 reserved STmin values (0x80 - 0xF0 and
0xFA - 0xFF) with -EINVAL in the CAN_ISOTP_RECV_FC socket option, and
updates the uAPI header comment and documentation accordingly.

[ ... ]

> diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
> index 7f9fb59bad71c..8b1cf7cc68ca0 100644
> --- a/include/uapi/linux/can/isotp.h
> +++ b/include/uapi/linux/can/isotp.h
> @@ -154,10 +154,9 @@ struct can_isotp_ll_options {
>   *
>   * We can strongly assume, that the Linux Kernel implementation of
>   * CAN_ISOTP is capable to run with BS=0, STmin=0 and WFTmax=0.
> - * But as we like to be able to behave as a commonly available ECU,
> - * these default settings can be changed via sockopts.
> - * For that reason the STmin value is intentionally _not_ checked for
> - * consistency and copied directly into the flow control (FC) frame.
> + * These default settings can be changed with the CAN_ISOTP_RECV_FC
> + * sockopt to behave like other ECUs. The reserved STmin values are
> + * rejected with -EINVAL.
>   */
>  
>  /* link layer default values => make use of Classical CAN frames */
> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index cea50bd78632d..01079ce4fa458 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -1808,9 +1808,17 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
>  		break;
>  	}
>  
> -	case CAN_ISOTP_RECV_FC:
> -		memcpy(&so->rxfc, val, sizeof(so->rxfc));
> +	case CAN_ISOTP_RECV_FC: {
> +		const struct can_isotp_fc_options *fc = val;
> +
> +		/* reject the reserved STmin values 0x80 - 0xF0, 0xFA - 0xFF */
> +		if (fc->stmin > 0x7F &&
> +		    (fc->stmin < 0xF1 || fc->stmin > 0xF9))
> +			return -EINVAL;
> +
> +		memcpy(&so->rxfc, fc, sizeof(*fc));
>  		break;
> +	}
>  
>  	case CAN_ISOTP_TX_STMIN:
>  		so->force_tx_stmin = *(const u32 *)val;

[Severity: High]
Does rejecting reserved STmin values in CAN_ISOTP_RECV_FC introduce a
uAPI regression for existing userspace?

Since commit e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol"),
isotp.h has documented that stmin is intentionally unchecked and copied
into flow control frames.  Existing conformance tests and tools like
isotprecv that configure reserved STmin values will now fail with -EINVAL.

In the kernel, so->rxfc.stmin is only copied verbatim into the outgoing
frame payload in isotp_send_fc() and read back via getsockopt.  Accepting
reserved values introduces no kernel timer, driver, or state-machine issue.

CAN_RAW is also not a drop-in replacement for testing peer handling of
reserved STmin values (such as clamping to 0x7F in isotp_rcv_fc()), since
it lacks the ISO-TP RX state machine for flow control replies, blocksize
counting, and consecutive frame reassembly.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-getsockopt-memcg-isotp-v6-0-9d42c52a90df@hartkopp.net?part=11

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC
  2026-12-05 13:20 ` [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC Oliver Hartkopp
@ 2026-12-05 13:41   ` sashiko-bot
  0 siblings, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-12-05 13:41 UTC (permalink / raw)
  To: Oliver Hartkopp, linux-can
  Cc: Marc Kleine-Budde, Vincent Mailhol, Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] can: isotp: stale fcecho tag can hijack TX echo on UID
  wraparound
--

commit db13776480d3beadbc91c0325a1b7fc5d7a67feb
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: isotp: add N_Ar timeout monitoring for FC

This commit adds N_Ar timeout monitoring for flow control frames by
tagging outgoing FC skbs with a uid in so->fcecho and starting the N_Cr
watchdog in isotp_rcv_echo() when the local echo arrives.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index 493bad6f9a6a4..a2817cf21cf0c 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c

[ ... ]

> @@ -1008,6 +1022,16 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
>  	 */
>  	spin_lock(&so->rx_lock);
>  
> +	/* the FC has been sent => start the CF timeout (N_Cr) */
> +	if (READ_ONCE(so->fcecho) == csx->can_skb_uid) {
> +		WRITE_ONCE(so->fcecho, 0);
> +		if (READ_ONCE(so->rx.state) == ISOTP_WAIT_DATA)
> +			hrtimer_start(&so->rxtimer,
> +				      ktime_set(ISOTP_TIMEOUT, 0),
> +				      HRTIMER_MODE_REL_SOFT);
> +		goto out_unlock;
> +	}
> +
>  	/* so->cfecho may since belong to a new transfer; recheck under lock */
>  	if (READ_ONCE(so->cfecho) != csx->can_skb_uid)
>  		goto out_unlock;

[Severity: Low]
Can a stale so->fcecho value hijack a later TX echo here?

When can_send() fails in isotp_send_fc(), nskb is freed without an echo
and so->fcecho remains non-zero.  Because can_set_skb_uid() allocates
32-bit UIDs from the global skbcounter, wraparound can assign that same
UID to a later TX frame on this socket.

isotp_rcv_echo() checks so->fcecho before so->cfecho and jumps to
out_unlock even when so->rx.state is ISOTP_IDLE, swallowing the TX echo
and leaving so->echotimer to fail the transfer with -ECOMM.  Clearing
so->fcecho on can_send() failure and when so->rx.state returns to
ISOTP_IDLE would prevent stale tags from matching.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-getsockopt-memcg-isotp-v6-0-9d42c52a90df@hartkopp.net?part=9

^ permalink raw reply	[flat|nested] 28+ messages in thread

* Re: [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
  2026-12-05 13:20 ` [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
@ 2026-12-05 13:41   ` sashiko-bot
  0 siblings, 0 replies; 28+ messages in thread
From: sashiko-bot @ 2026-12-05 13:41 UTC (permalink / raw)
  To: Oliver Hartkopp, linux-can
  Cc: Marc Kleine-Budde, Vincent Mailhol, Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: isotp: isotp_setsockopt_locked() mutates tx_flags
  breaking getsockopt()
--

commit 74e28d49a8440367be8803529dcb07349b313cc1
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS

This commit rejects CAN FD tx_flags other than CANFD_BRS and CANFD_FDF in
CAN_ISOTP_LL_OPTS, sets CANFD_FDF in so->ll.tx_flags for CAN FD sockets, and
updates the header comments and documentation.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index a2817cf21cf0c..cea50bd78632d 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -1834,8 +1834,17 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
>  		    (ll->tx_dl > CAN_MAX_DLEN || ll->tx_flags != 0))
>  			return -EINVAL;
>  
> +		/* allow only CANFD_FDF and CANFD_BRS for CAN FD frames */
> +		if (ll->mtu == CANFD_MTU &&
> +		    (ll->tx_flags & ~(CANFD_BRS | CANFD_FDF)))
> +			return -EINVAL;
> +
>  		memcpy(&so->ll, ll, sizeof(*ll));
>  
> +		/* compat: CAN FD frames are always marked with CANFD_FDF */
> +		if (so->ll.mtu == CANFD_MTU)
> +			so->ll.tx_flags |= CANFD_FDF;

[Severity: Medium]
Does mutating so->ll.tx_flags here break getsockopt() round-trip
compatibility for existing CAN FD sockets?

isotp_getsockopt() returns so->ll directly, so sockets configured with
tx_flags of 0 or CANFD_BRS now read back CANFD_FDF or CANFD_BRS | CANFD_FDF.
This breaks callers that check getsockopt() round-trip equality, and causes a
pre-bind switch from CANFD_MTU back to CAN_MTU to fail with -EINVAL.

Since can_send() in net/can/af_can.c already sets CANFD_FDF unconditionally
on transmitted CAN FD frames, leaving so->ll.tx_flags unmodified while
keeping the -EINVAL validation avoids this uAPI regression.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-getsockopt-memcg-isotp-v6-0-9d42c52a90df@hartkopp.net?part=10

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2026-12-05 13:41 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 13:19 [PATCH can-next v6 00/24] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 01/24] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 02/24] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 03/24] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 04/24] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 05/24] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 06/24] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
2026-12-05 13:19 ` [PATCH can-next v6 07/24] can: isotp: limit the frame transmission time to the N_As/N_Ar timeout Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 08/24] can: isotp: add N_As timeout monitoring for SF/FF/CF Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 09/24] can: isotp: add N_Ar timeout monitoring for FC Oliver Hartkopp
2026-12-05 13:41   ` sashiko-bot
2026-12-05 13:20 ` [PATCH can-next v6 10/24] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
2026-12-05 13:41   ` sashiko-bot
2026-12-05 13:20 ` [PATCH can-next v6 11/24] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
2026-12-05 13:36   ` sashiko-bot
2026-12-05 13:20 ` [PATCH can-next v6 12/24] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 13/24] can: isotp: drop frames without N_PCI Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 14/24] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 15/24] can: isotp: check the minimum FF_DL Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 16/24] can: isotp: check the data length of the last CF Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 17/24] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 18/24] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 19/24] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 20/24] can: isotp: rename variable for the required content length Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 21/24] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 22/24] can: isotp: change return type of functions to void Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 23/24] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
2026-12-05 13:20 ` [PATCH can-next v6 24/24] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox