From: Stéphane Grosjean <s.gr…@peak-system.fr> pcan_usb_encode_msg() appends a one-byte "writer id" whenever PCAN_USB_TX_SRR is set in can_id_flags, for both RTR and non-RTR frames: the data block and the SRR writer id byte are two independent, unconditional steps. pcan_usb_decode_data() only consumed that extra byte in the non-RTR branch, inside the same conditional block that reads the CAN data payload. A self-received RTR frame with the SRR bit set (e.g. a loopback echo with CAN_CTRLMODE_LOOPBACK) therefore left the RX cursor one byte short, causing the private id byte to be misparsed as the next record's status/len byte and the rest of the URB to be misparsed or rejected. Move the private id byte skip out of the RTR/non-RTR branch so it is applied whenever SRR is set, matching the encoder's layout. Since a frame can now be dropped when this byte is missing, update rx_bytes together with rx_packets, once the frame has been fully decoded. Fixes: 3a7939495ce8 ("can: peak_usb: PCAN-USB: add support of loopback and one-shot mode") Cc: stab…@vger.kernel.org Signed-off-by: Stéphane Grosjean <s.gr…@peak-system.fr> Link: https://patch.msgid.link/2026…@peak-system.fr Signed-off-by: Marc Kleine-Budde <m…@pengutronix.de> --- drivers/net/can/usb/peak_usb/pcan_usb.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c index 76cf042a9f11..90f69afcba97 100644 --- a/drivers/net/can/usb/peak_usb/pcan_usb.c +++ b/drivers/net/can/usb/peak_usb/pcan_usb.c @@ -655,6 +655,7 @@ static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len) struct sk_buff *skb; struct can_frame *cf; struct skb_shared_hwtstamps *hwts; + u8 rx_bytes = 0; u32 can_id_flags; skb = alloc_can_skb(mc->netdev, &cf); @@ -696,18 +697,21 @@ static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len) memcpy(cf->data, mc->ptr, cf->len); mc->ptr += rec_len; - - /* Ignore next byte (client private id) if SRR bit is set */ - if (can_id_flags & PCAN_USB_TX_SRR) { - if ((mc->ptr + 1) > mc->end) - goto decode_failed; - mc->ptr++; - } - - /* update statistics */ - mc->netdev->stats.rx_bytes += cf->len; + rx_bytes = cf->len; } + + /* Ignore next byte (client private id) if SRR bit is set. + * Note: this test must be done even when the frame is an RTR frame. + */ + if (can_id_flags & PCAN_USB_TX_SRR) { + if ((mc->ptr + 1) > mc->end) + goto decode_failed; + mc->ptr++; + } + + /* update statistics */ mc->netdev->stats.rx_packets++; + mc->netdev->stats.rx_bytes += rx_bytes; /* convert timestamp into kernel time */ hwts = skb_hwtstamps(skb);
-- 2.53.0