[PATCH 2/2] can: j1939: check received packet count before completing session

From: Liu Chao

Date: Mon Sep 07 2026 - 11:11:06 EST


j1939_xtp_rx_eoma_one() marks a session complete as soon as it sees
an EOMA without verifying that all data packets arrived. Add a pkt.rx
check so a session with missing packets gets aborted instead of
delivering a short message to userspace.

Also tighten the existing EOMA size-mismatch warning to actually abort
for receive sessions instead of just logging.

Only unicast receive sessions are gated:

- Transmitter sessions track pkt.rx via loopback confirmations which
may legitimately lag behind the real transmit count, so the check
would cause false aborts on the tx path.

- BAM (broadcast) sessions never go through EOMA -- they complete via
the final flag in j1939_xtp_rx_dat_one() when pkt.rx reaches
pkt.total directly.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@xxxxxxxxxx
Signed-off-by: Liu Chao <liuc63@xxxxxxxxxxxx>
---
net/can/j1939/transport.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fdce5792..2ecce49ad 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -1397,6 +1397,7 @@ j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
{
struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
const u8 *dat;
+ unsigned int expected_total;
int len;

if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
@@ -1409,11 +1410,23 @@ j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
else
len = j1939_tp_ctl_to_size(dat);

- if (session->total_message_size != len) {
+ if (!session->transmission && session->total_message_size != len) {
netdev_warn_once(session->priv->ndev,
- "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
+ "%s: 0x%p: EOMA size mismatch, expected %i got %i\n",
__func__, session, session->total_message_size,
len);
+ goto out_session_cancel;
+ }
+
+ if (!session->transmission) {
+ expected_total = (session->total_message_size + 6) / 7;
+ if (session->pkt.rx < expected_total) {
+ netdev_warn(session->priv->ndev,
+ "%s: 0x%p: EOMA but only %u/%u data packets rx'd\n",
+ __func__, session,
+ session->pkt.rx, expected_total);
+ goto out_session_cancel;
+ }
}

netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
@@ -1422,6 +1435,11 @@ j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
j1939_session_timers_cancel(session);
/* transmitted without problems */
j1939_session_completed(session);
+ return;
+
+out_session_cancel:
+ j1939_session_timers_cancel(session);
+ j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
}

static void
--
2.50.1