From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
kernel@pengutronix.de, Oliver Hartkopp <socketcan@hartkopp.net>,
stable@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>,
Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net 06/12] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state
Date: Tue, 24 Nov 2026 09:51:33 +0100 [thread overview]
Message-ID: <20261124085757.1093632-7-mkl@pengutronix.de> (raw)
In-Reply-To: <20261124085757.1093632-1-mkl@pengutronix.de>
From: Oliver Hartkopp <socketcan@hartkopp.net>
The rx timeout is only started at the end of isotp_send_fc() and for
consecutive frames inside a block in isotp_rcv_cf(). So so->rx.state
can stay in ISOTP_WAIT_DATA without a running rx timeout:
- isotp_send_fc() returns early, when it can not allocate the skb or
does not find the CAN device anymore
- isotp_rcv_ff() does not send a flow control frame in listen mode
- isotp_rcv_cf() has stopped the rx timeout and does not restart it at
the end of a block, when the flow control frame is not sent
- isotp_rcv_cf() has stopped the rx timeout before it drops a
consecutive frame with an invalid length (reported by Sashiko)
On a bound socket the next first frame or single frame resets the
state. But when the CAN device has been removed, no further CAN frame
is received and so->rx.state remains in ISOTP_WAIT_DATA. isotp_bind()
then rejects a new binding with -EAGAIN until the socket is closed.
Start the rx timeout in isotp_rcv_ff() and isotp_rcv_cf() whenever the
reception continues in ISOTP_WAIT_DATA state, before a flow control
frame is sent. isotp_send_fc() restarts the rx timeout as before. All
this runs under so->rx_lock, so the order of the rx timeout start and
the transmission of the flow control frame does not matter.
In listen mode a first frame that is not followed by consecutive
frames now leads to ETIMEDOUT, like a missing consecutive frame inside
a block already does. So a listening socket reports a missing
consecutive frame consistently, independent of its position in the
PDU.
Check the length of a consecutive frame first in isotp_rcv_cf(), so
that an invalid frame is dropped without stopping the rx timeout. An
invalid frame also no longer updates the timestamp for
CAN_ISOTP_FORCE_RXSTMIN.
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/linux-can/179504035916.3136.12986898667697830880@kernel.org/
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-6-f6b84f5257b1@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
net/can/isotp.c | 37 +++++++++++++++++++++----------------
1 file changed, 21 insertions(+), 16 deletions(-)
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 8229b6a69ecd..0b7db1ead7a9 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -634,6 +634,10 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
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_MODE_REL_SOFT);
+
/* no creation of flow control frames */
if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
return 0;
@@ -650,6 +654,17 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
struct sk_buff *nskb;
int i;
+ /* CFs are never longer than the FF */
+ 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)
+ return 1;
+ }
+
if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
return 0;
@@ -662,6 +677,7 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
so->lastrxcf_tstamp = skb->tstamp;
}
+ /* stop the rx timeout only for valid CFs (checked above) */
hrtimer_cancel(&so->rxtimer);
/* isotp_rx_timer_handler() may have raced us for so->rx.state
@@ -670,17 +686,6 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
return 1;
- /* CFs are never longer than the FF */
- 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)
- return 1;
- }
-
if ((cf->data[ae] & 0x0F) != so->rx.sn) {
/* wrong sn detected - report 'illegal byte sequence' */
sk->sk_err = EILSEQ;
@@ -726,13 +731,13 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
return 0;
}
+ /* start rx timeout, isotp_send_fc() restarts it */
+ hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+ HRTIMER_MODE_REL_SOFT);
+
/* perform blocksize handling, if enabled */
- if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
- /* start rx timeout watchdog */
- hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
- HRTIMER_MODE_REL_SOFT);
+ 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)
--
2.53.0
next prev parent reply other threads:[~2026-11-24 8:58 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 8:51 [PATCH net 0/12] pull-request: can 2026-11-24 Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 01/12] can: bcm: copy the CAN frames outside of lock_sock() Marc Kleine-Budde
2026-11-24 9:04 ` netdev-bot+sinfo
2026-11-24 8:51 ` [PATCH net 02/12] can: bcm: do not remove the procfs entry in bcm_notify() Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 03/12] can: raw: use a private mutex instead of lock_sock() Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 04/12] can: isotp: " Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 05/12] can: bcm: " Marc Kleine-Budde
2026-11-24 8:51 ` Marc Kleine-Budde [this message]
2026-11-24 8:51 ` [PATCH net 07/12] can: isotp: reject socket options while a transfer is active Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 08/12] can: netlink: fix static FD-NON-ISO mode validation Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 09/12] can: netlink: fix IFLA_CAN_BITRATE_MAX attribute sizing Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 10/12] can: netlink: fix CAN XL PWM " Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 11/12] can: gs_usb: gs_can_start_xmit(): don't leak uninitialized memory over USB Marc Kleine-Budde
2026-11-24 8:51 ` [PATCH net 12/12] can: flexcan: Fix probing for m68k/coldfire Marc Kleine-Budde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124085757.1093632-7-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=davem@davemloft.net \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=sashiko-bot@kernel.org \
--cc=socketcan@hartkopp.net \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox