1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2010-2011 EIA Electronics,
3 // Kurt Van Dijck <kurt.van.dijck@eia.be>
4 // Copyright (c) 2018 Protonic,
5 // Robin van der Gracht <robin@protonic.nl>
6 // Copyright (c) 2017-2019 Pengutronix,
7 // Marc Kleine-Budde <kernel@pengutronix.de>
8 // Copyright (c) 2017-2019 Pengutronix,
9 // Oleksij Rempel <kernel@pengutronix.de>
11 #include <linux/can/skb.h>
13 #include "j1939-priv.h"
15 #define J1939_XTP_TX_RETRY_LIMIT 100
17 #define J1939_ETP_PGN_CTL 0xc800
18 #define J1939_ETP_PGN_DAT 0xc700
19 #define J1939_TP_PGN_CTL 0xec00
20 #define J1939_TP_PGN_DAT 0xeb00
22 #define J1939_TP_CMD_RTS 0x10
23 #define J1939_TP_CMD_CTS 0x11
24 #define J1939_TP_CMD_EOMA 0x13
25 #define J1939_TP_CMD_BAM 0x20
26 #define J1939_TP_CMD_ABORT 0xff
28 #define J1939_ETP_CMD_RTS 0x14
29 #define J1939_ETP_CMD_CTS 0x15
30 #define J1939_ETP_CMD_DPO 0x16
31 #define J1939_ETP_CMD_EOMA 0x17
32 #define J1939_ETP_CMD_ABORT 0xff
34 enum j1939_xtp_abort {
35 J1939_XTP_NO_ABORT = 0,
36 J1939_XTP_ABORT_BUSY = 1,
37 /* Already in one or more connection managed sessions and
38 * cannot support another.
41 * Operation already in progress
44 J1939_XTP_ABORT_RESOURCE = 2,
45 /* System resources were needed for another task so this
46 * connection managed session was terminated.
49 * The socket type requires that message be sent atomically,
50 * and the size of the message to be sent made this
54 J1939_XTP_ABORT_TIMEOUT = 3,
55 /* A timeout occurred and this is the connection abort to
59 * The destination host cannot be reached (probably because
60 * the host is down or a remote router cannot reach it).
63 J1939_XTP_ABORT_GENERIC = 4,
64 /* CTS messages received when data transfer is in progress
70 J1939_XTP_ABORT_FAULT = 5,
71 /* Maximal retransmit request limit reached
74 * State not recoverable
77 J1939_XTP_ABORT_UNEXPECTED_DATA = 6,
78 /* Unexpected data transfer packet
81 * Transport endpoint is not connected
84 J1939_XTP_ABORT_BAD_SEQ = 7,
85 /* Bad sequence number (and software is not able to recover)
88 * Illegal byte sequence
91 J1939_XTP_ABORT_DUP_SEQ = 8,
92 /* Duplicate sequence number (and software is not able to
96 J1939_XTP_ABORT_EDPO_UNEXPECTED = 9,
97 /* Unexpected EDPO packet (ETP) or Message size > 1785 bytes
101 J1939_XTP_ABORT_BAD_EDPO_PGN = 10,
102 /* Unexpected EDPO PGN (PGN in EDPO is bad) */
104 J1939_XTP_ABORT_EDPO_OUTOF_CTS = 11,
105 /* EDPO number of packets is greater than CTS */
107 J1939_XTP_ABORT_BAD_EDPO_OFFSET = 12,
108 /* Bad EDPO offset */
110 J1939_XTP_ABORT_OTHER_DEPRECATED = 13,
111 /* Deprecated. Use 250 instead (Any other reason) */
113 J1939_XTP_ABORT_ECTS_UNXPECTED_PGN = 14,
114 /* Unexpected ECTS PGN (PGN in ECTS is bad) */
116 J1939_XTP_ABORT_ECTS_TOO_BIG = 15,
117 /* ECTS requested packets exceeds message size */
119 J1939_XTP_ABORT_OTHER = 250,
120 /* Any other reason (if a Connection Abort reason is
121 * identified that is not listed in the table use code 250)
125 static unsigned int j1939_tp_block = 255;
126 static unsigned int j1939_tp_packet_delay;
127 static unsigned int j1939_tp_padding = 1;
130 static const char *j1939_xtp_abort_to_str(enum j1939_xtp_abort abort)
133 case J1939_XTP_ABORT_BUSY:
134 return "Already in one or more connection managed sessions and cannot support another.";
135 case J1939_XTP_ABORT_RESOURCE:
136 return "System resources were needed for another task so this connection managed session was terminated.";
137 case J1939_XTP_ABORT_TIMEOUT:
138 return "A timeout occurred and this is the connection abort to close the session.";
139 case J1939_XTP_ABORT_GENERIC:
140 return "CTS messages received when data transfer is in progress";
141 case J1939_XTP_ABORT_FAULT:
142 return "Maximal retransmit request limit reached";
143 case J1939_XTP_ABORT_UNEXPECTED_DATA:
144 return "Unexpected data transfer packet";
145 case J1939_XTP_ABORT_BAD_SEQ:
146 return "Bad sequence number (and software is not able to recover)";
147 case J1939_XTP_ABORT_DUP_SEQ:
148 return "Duplicate sequence number (and software is not able to recover)";
149 case J1939_XTP_ABORT_EDPO_UNEXPECTED:
150 return "Unexpected EDPO packet (ETP) or Message size > 1785 bytes (TP)";
151 case J1939_XTP_ABORT_BAD_EDPO_PGN:
152 return "Unexpected EDPO PGN (PGN in EDPO is bad)";
153 case J1939_XTP_ABORT_EDPO_OUTOF_CTS:
154 return "EDPO number of packets is greater than CTS";
155 case J1939_XTP_ABORT_BAD_EDPO_OFFSET:
156 return "Bad EDPO offset";
157 case J1939_XTP_ABORT_OTHER_DEPRECATED:
158 return "Deprecated. Use 250 instead (Any other reason)";
159 case J1939_XTP_ABORT_ECTS_UNXPECTED_PGN:
160 return "Unexpected ECTS PGN (PGN in ECTS is bad)";
161 case J1939_XTP_ABORT_ECTS_TOO_BIG:
162 return "ECTS requested packets exceeds message size";
163 case J1939_XTP_ABORT_OTHER:
164 return "Any other reason (if a Connection Abort reason is identified that is not listed in the table use code 250)";
170 static int j1939_xtp_abort_to_errno(struct j1939_priv *priv,
171 enum j1939_xtp_abort abort)
176 case J1939_XTP_NO_ABORT:
177 WARN_ON_ONCE(abort == J1939_XTP_NO_ABORT);
180 case J1939_XTP_ABORT_BUSY:
183 case J1939_XTP_ABORT_RESOURCE:
186 case J1939_XTP_ABORT_TIMEOUT:
189 case J1939_XTP_ABORT_GENERIC:
192 case J1939_XTP_ABORT_FAULT:
193 err = ENOTRECOVERABLE;
195 case J1939_XTP_ABORT_UNEXPECTED_DATA:
198 case J1939_XTP_ABORT_BAD_SEQ:
201 case J1939_XTP_ABORT_DUP_SEQ:
204 case J1939_XTP_ABORT_EDPO_UNEXPECTED:
207 case J1939_XTP_ABORT_BAD_EDPO_PGN:
210 case J1939_XTP_ABORT_EDPO_OUTOF_CTS:
213 case J1939_XTP_ABORT_BAD_EDPO_OFFSET:
216 case J1939_XTP_ABORT_OTHER_DEPRECATED:
219 case J1939_XTP_ABORT_ECTS_UNXPECTED_PGN:
222 case J1939_XTP_ABORT_ECTS_TOO_BIG:
225 case J1939_XTP_ABORT_OTHER:
229 netdev_warn(priv->ndev, "Unknown abort code %i", abort);
236 static inline void j1939_session_list_lock(struct j1939_priv *priv)
238 spin_lock_bh(&priv->active_session_list_lock);
241 static inline void j1939_session_list_unlock(struct j1939_priv *priv)
243 spin_unlock_bh(&priv->active_session_list_lock);
246 void j1939_session_get(struct j1939_session *session)
248 kref_get(&session->kref);
251 /* session completion functions */
252 static void __j1939_session_drop(struct j1939_session *session)
254 if (!session->transmission)
257 j1939_sock_pending_del(session->sk);
258 sock_put(session->sk);
261 static void j1939_session_destroy(struct j1939_session *session)
263 if (session->transmission) {
265 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
267 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ACK);
268 } else if (session->err) {
269 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
272 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
274 WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
275 WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
277 skb_queue_purge(&session->skb_queue);
278 __j1939_session_drop(session);
279 j1939_priv_put(session->priv);
283 static void __j1939_session_release(struct kref *kref)
285 struct j1939_session *session = container_of(kref, struct j1939_session,
288 j1939_session_destroy(session);
291 void j1939_session_put(struct j1939_session *session)
293 kref_put(&session->kref, __j1939_session_release);
296 static void j1939_session_txtimer_cancel(struct j1939_session *session)
298 if (hrtimer_cancel(&session->txtimer))
299 j1939_session_put(session);
302 static void j1939_session_rxtimer_cancel(struct j1939_session *session)
304 if (hrtimer_cancel(&session->rxtimer))
305 j1939_session_put(session);
308 void j1939_session_timers_cancel(struct j1939_session *session)
310 j1939_session_txtimer_cancel(session);
311 j1939_session_rxtimer_cancel(session);
314 static inline bool j1939_cb_is_broadcast(const struct j1939_sk_buff_cb *skcb)
316 return (!skcb->addr.dst_name && (skcb->addr.da == 0xff));
319 static void j1939_session_skb_drop_old(struct j1939_session *session)
321 struct sk_buff *do_skb;
322 struct j1939_sk_buff_cb *do_skcb;
323 unsigned int offset_start;
326 if (skb_queue_len(&session->skb_queue) < 2)
329 offset_start = session->pkt.tx_acked * 7;
331 spin_lock_irqsave(&session->skb_queue.lock, flags);
332 do_skb = skb_peek(&session->skb_queue);
333 do_skcb = j1939_skb_to_cb(do_skb);
335 if ((do_skcb->offset + do_skb->len) < offset_start) {
336 __skb_unlink(do_skb, &session->skb_queue);
337 /* drop ref taken in j1939_session_skb_queue() */
342 spin_unlock_irqrestore(&session->skb_queue.lock, flags);
345 void j1939_session_skb_queue(struct j1939_session *session,
348 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
349 struct j1939_priv *priv = session->priv;
351 j1939_ac_fixup(priv, skb);
353 if (j1939_address_is_unicast(skcb->addr.da) &&
354 priv->ents[skcb->addr.da].nusers)
355 skcb->flags |= J1939_ECU_LOCAL_DST;
357 skcb->flags |= J1939_ECU_LOCAL_SRC;
360 skb_queue_tail(&session->skb_queue, skb);
364 sk_buff *j1939_session_skb_get_by_offset(struct j1939_session *session,
365 unsigned int offset_start)
367 struct j1939_priv *priv = session->priv;
368 struct j1939_sk_buff_cb *do_skcb;
369 struct sk_buff *skb = NULL;
370 struct sk_buff *do_skb;
373 spin_lock_irqsave(&session->skb_queue.lock, flags);
374 skb_queue_walk(&session->skb_queue, do_skb) {
375 do_skcb = j1939_skb_to_cb(do_skb);
377 if (offset_start >= do_skcb->offset &&
378 offset_start < (do_skcb->offset + do_skb->len)) {
386 spin_unlock_irqrestore(&session->skb_queue.lock, flags);
389 netdev_dbg(priv->ndev, "%s: 0x%p: no skb found for start: %i, queue size: %i\n",
390 __func__, session, offset_start,
391 skb_queue_len(&session->skb_queue));
396 static struct sk_buff *j1939_session_skb_get(struct j1939_session *session)
398 unsigned int offset_start;
400 offset_start = session->pkt.dpo * 7;
401 return j1939_session_skb_get_by_offset(session, offset_start);
404 /* see if we are receiver
405 * returns 0 for broadcasts, although we will receive them
407 static inline int j1939_tp_im_receiver(const struct j1939_sk_buff_cb *skcb)
409 return skcb->flags & J1939_ECU_LOCAL_DST;
412 /* see if we are sender */
413 static inline int j1939_tp_im_transmitter(const struct j1939_sk_buff_cb *skcb)
415 return skcb->flags & J1939_ECU_LOCAL_SRC;
418 /* see if we are involved as either receiver or transmitter */
419 static int j1939_tp_im_involved(const struct j1939_sk_buff_cb *skcb, bool swap)
422 return j1939_tp_im_receiver(skcb);
424 return j1939_tp_im_transmitter(skcb);
427 static int j1939_tp_im_involved_anydir(struct j1939_sk_buff_cb *skcb)
429 return skcb->flags & (J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
432 /* extract pgn from flow-ctl message */
433 static inline pgn_t j1939_xtp_ctl_to_pgn(const u8 *dat)
437 pgn = (dat[7] << 16) | (dat[6] << 8) | (dat[5] << 0);
438 if (j1939_pgn_is_pdu1(pgn))
443 static inline unsigned int j1939_tp_ctl_to_size(const u8 *dat)
445 return (dat[2] << 8) + (dat[1] << 0);
448 static inline unsigned int j1939_etp_ctl_to_packet(const u8 *dat)
450 return (dat[4] << 16) | (dat[3] << 8) | (dat[2] << 0);
453 static inline unsigned int j1939_etp_ctl_to_size(const u8 *dat)
455 return (dat[4] << 24) | (dat[3] << 16) |
456 (dat[2] << 8) | (dat[1] << 0);
459 /* find existing session:
460 * reverse: swap cb's src & dst
461 * there is no problem with matching broadcasts, since
462 * broadcasts (no dst, no da) would never call this
463 * with reverse == true
465 static bool j1939_session_match(struct j1939_addr *se_addr,
466 struct j1939_addr *sk_addr, bool reverse)
468 if (se_addr->type != sk_addr->type)
472 if (se_addr->src_name) {
473 if (se_addr->src_name != sk_addr->dst_name)
475 } else if (se_addr->sa != sk_addr->da) {
479 if (se_addr->dst_name) {
480 if (se_addr->dst_name != sk_addr->src_name)
482 } else if (se_addr->da != sk_addr->sa) {
486 if (se_addr->src_name) {
487 if (se_addr->src_name != sk_addr->src_name)
489 } else if (se_addr->sa != sk_addr->sa) {
493 if (se_addr->dst_name) {
494 if (se_addr->dst_name != sk_addr->dst_name)
496 } else if (se_addr->da != sk_addr->da) {
505 j1939_session *j1939_session_get_by_addr_locked(struct j1939_priv *priv,
506 struct list_head *root,
507 struct j1939_addr *addr,
508 bool reverse, bool transmitter)
510 struct j1939_session *session;
512 lockdep_assert_held(&priv->active_session_list_lock);
514 list_for_each_entry(session, root, active_session_list_entry) {
515 j1939_session_get(session);
516 if (j1939_session_match(&session->skcb.addr, addr, reverse) &&
517 session->transmission == transmitter)
519 j1939_session_put(session);
526 j1939_session *j1939_session_get_simple(struct j1939_priv *priv,
529 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
530 struct j1939_session *session;
532 lockdep_assert_held(&priv->active_session_list_lock);
534 list_for_each_entry(session, &priv->active_session_list,
535 active_session_list_entry) {
536 j1939_session_get(session);
537 if (session->skcb.addr.type == J1939_SIMPLE &&
538 session->tskey == skcb->tskey && session->sk == skb->sk)
540 j1939_session_put(session);
547 j1939_session *j1939_session_get_by_addr(struct j1939_priv *priv,
548 struct j1939_addr *addr,
549 bool reverse, bool transmitter)
551 struct j1939_session *session;
553 j1939_session_list_lock(priv);
554 session = j1939_session_get_by_addr_locked(priv,
555 &priv->active_session_list,
556 addr, reverse, transmitter);
557 j1939_session_list_unlock(priv);
562 static void j1939_skbcb_swap(struct j1939_sk_buff_cb *skcb)
566 swap(skcb->addr.dst_name, skcb->addr.src_name);
567 swap(skcb->addr.da, skcb->addr.sa);
569 /* swap SRC and DST flags, leave other untouched */
570 if (skcb->flags & J1939_ECU_LOCAL_SRC)
571 tmp |= J1939_ECU_LOCAL_DST;
572 if (skcb->flags & J1939_ECU_LOCAL_DST)
573 tmp |= J1939_ECU_LOCAL_SRC;
574 skcb->flags &= ~(J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
579 sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv,
580 const struct j1939_sk_buff_cb *re_skcb,
585 struct j1939_sk_buff_cb *skcb;
587 skb = alloc_skb(sizeof(struct can_frame) + sizeof(struct can_skb_priv),
590 return ERR_PTR(-ENOMEM);
592 skb->dev = priv->ndev;
593 can_skb_reserve(skb);
594 can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
595 can_skb_prv(skb)->skbcnt = 0;
596 /* reserve CAN header */
597 skb_reserve(skb, offsetof(struct can_frame, data));
599 memcpy(skb->cb, re_skcb, sizeof(skb->cb));
600 skcb = j1939_skb_to_cb(skb);
602 j1939_skbcb_swap(skcb);
605 if (skcb->addr.type == J1939_ETP)
606 skcb->addr.pgn = J1939_ETP_PGN_CTL;
608 skcb->addr.pgn = J1939_TP_PGN_CTL;
610 if (skcb->addr.type == J1939_ETP)
611 skcb->addr.pgn = J1939_ETP_PGN_DAT;
613 skcb->addr.pgn = J1939_TP_PGN_DAT;
619 /* TP transmit packet functions */
620 static int j1939_tp_tx_dat(struct j1939_session *session,
621 const u8 *dat, int len)
623 struct j1939_priv *priv = session->priv;
626 skb = j1939_tp_tx_dat_new(priv, &session->skcb,
631 skb_put_data(skb, dat, len);
632 if (j1939_tp_padding && len < 8)
633 memset(skb_put(skb, 8 - len), 0xff, 8 - len);
635 return j1939_send_one(priv, skb);
638 static int j1939_xtp_do_tx_ctl(struct j1939_priv *priv,
639 const struct j1939_sk_buff_cb *re_skcb,
640 bool swap_src_dst, pgn_t pgn, const u8 *dat)
645 if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
648 skb = j1939_tp_tx_dat_new(priv, re_skcb, true, swap_src_dst);
652 skdat = skb_put(skb, 8);
653 memcpy(skdat, dat, 5);
654 skdat[5] = (pgn >> 0);
655 skdat[6] = (pgn >> 8);
656 skdat[7] = (pgn >> 16);
658 return j1939_send_one(priv, skb);
661 static inline int j1939_tp_tx_ctl(struct j1939_session *session,
662 bool swap_src_dst, const u8 *dat)
664 struct j1939_priv *priv = session->priv;
666 return j1939_xtp_do_tx_ctl(priv, &session->skcb,
668 session->skcb.addr.pgn, dat);
671 static int j1939_xtp_tx_abort(struct j1939_priv *priv,
672 const struct j1939_sk_buff_cb *re_skcb,
674 enum j1939_xtp_abort err,
679 if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
682 memset(dat, 0xff, sizeof(dat));
683 dat[0] = J1939_TP_CMD_ABORT;
685 return j1939_xtp_do_tx_ctl(priv, re_skcb, swap_src_dst, pgn, dat);
688 void j1939_tp_schedule_txtimer(struct j1939_session *session, int msec)
690 j1939_session_get(session);
691 hrtimer_start(&session->txtimer, ms_to_ktime(msec),
692 HRTIMER_MODE_REL_SOFT);
695 static inline void j1939_tp_set_rxtimeout(struct j1939_session *session,
698 j1939_session_rxtimer_cancel(session);
699 j1939_session_get(session);
700 hrtimer_start(&session->rxtimer, ms_to_ktime(msec),
701 HRTIMER_MODE_REL_SOFT);
704 static int j1939_session_tx_rts(struct j1939_session *session)
709 memset(dat, 0xff, sizeof(dat));
711 dat[1] = (session->total_message_size >> 0);
712 dat[2] = (session->total_message_size >> 8);
713 dat[3] = session->pkt.total;
715 if (session->skcb.addr.type == J1939_ETP) {
716 dat[0] = J1939_ETP_CMD_RTS;
717 dat[1] = (session->total_message_size >> 0);
718 dat[2] = (session->total_message_size >> 8);
719 dat[3] = (session->total_message_size >> 16);
720 dat[4] = (session->total_message_size >> 24);
721 } else if (j1939_cb_is_broadcast(&session->skcb)) {
722 dat[0] = J1939_TP_CMD_BAM;
723 /* fake cts for broadcast */
726 dat[0] = J1939_TP_CMD_RTS;
730 if (dat[0] == session->last_txcmd)
734 ret = j1939_tp_tx_ctl(session, false, dat);
738 session->last_txcmd = dat[0];
739 if (dat[0] == J1939_TP_CMD_BAM) {
740 j1939_tp_schedule_txtimer(session, 50);
741 j1939_tp_set_rxtimeout(session, 250);
743 j1939_tp_set_rxtimeout(session, 1250);
746 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
751 static int j1939_session_tx_dpo(struct j1939_session *session)
757 memset(dat, 0xff, sizeof(dat));
759 dat[0] = J1939_ETP_CMD_DPO;
760 session->pkt.dpo = session->pkt.tx_acked;
761 pkt = session->pkt.dpo;
762 dat[1] = session->pkt.last - session->pkt.tx_acked;
765 dat[4] = (pkt >> 16);
767 ret = j1939_tp_tx_ctl(session, false, dat);
771 session->last_txcmd = dat[0];
772 j1939_tp_set_rxtimeout(session, 1250);
773 session->pkt.tx = session->pkt.tx_acked;
775 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
780 static int j1939_session_tx_dat(struct j1939_session *session)
782 struct j1939_priv *priv = session->priv;
783 struct j1939_sk_buff_cb *se_skcb;
784 int offset, pkt_done, pkt_end;
785 unsigned int len, pdelay;
786 struct sk_buff *se_skb;
791 se_skb = j1939_session_skb_get_by_offset(session, session->pkt.tx * 7);
795 se_skcb = j1939_skb_to_cb(se_skb);
796 tpdat = se_skb->data;
799 if (session->skcb.addr.type != J1939_ETP &&
800 j1939_cb_is_broadcast(&session->skcb))
801 pkt_end = session->pkt.total;
803 pkt_end = session->pkt.last;
805 while (session->pkt.tx < pkt_end) {
806 dat[0] = session->pkt.tx - session->pkt.dpo + 1;
807 offset = (session->pkt.tx * 7) - se_skcb->offset;
808 len = se_skb->len - offset;
812 if (offset + len > se_skb->len) {
813 netdev_err_once(priv->ndev,
814 "%s: 0x%p: requested data outside of queued buffer: offset %i, len %i, pkt.tx: %i\n",
815 __func__, session, se_skcb->offset,
816 se_skb->len , session->pkt.tx);
826 memcpy(&dat[1], &tpdat[offset], len);
827 ret = j1939_tp_tx_dat(session, dat, len + 1);
829 /* ENOBUFS == CAN interface TX queue is full */
831 netdev_alert(priv->ndev,
832 "%s: 0x%p: queue data error: %i\n",
833 __func__, session, ret);
837 session->last_txcmd = 0xff;
840 pdelay = j1939_cb_is_broadcast(&session->skcb) ? 50 :
841 j1939_tp_packet_delay;
843 if (session->pkt.tx < session->pkt.total && pdelay) {
844 j1939_tp_schedule_txtimer(session, pdelay);
850 j1939_tp_set_rxtimeout(session, 250);
861 static int j1939_xtp_txnext_transmiter(struct j1939_session *session)
863 struct j1939_priv *priv = session->priv;
866 if (!j1939_tp_im_transmitter(&session->skcb)) {
867 netdev_alert(priv->ndev, "%s: 0x%p: called by not transmitter!\n",
872 switch (session->last_cmd) {
874 ret = j1939_session_tx_rts(session);
877 case J1939_ETP_CMD_CTS:
878 if (session->last_txcmd != J1939_ETP_CMD_DPO) {
879 ret = j1939_session_tx_dpo(session);
885 case J1939_TP_CMD_CTS:
886 case 0xff: /* did some data */
887 case J1939_ETP_CMD_DPO:
888 case J1939_TP_CMD_BAM:
889 ret = j1939_session_tx_dat(session);
893 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
894 __func__, session, session->last_cmd);
900 static int j1939_session_tx_cts(struct j1939_session *session)
902 struct j1939_priv *priv = session->priv;
903 unsigned int pkt, len;
907 if (!j1939_sk_recv_match(priv, &session->skcb))
910 len = session->pkt.total - session->pkt.rx;
911 len = min3(len, session->pkt.block, j1939_tp_block ?: 255);
912 memset(dat, 0xff, sizeof(dat));
914 if (session->skcb.addr.type == J1939_ETP) {
915 pkt = session->pkt.rx + 1;
916 dat[0] = J1939_ETP_CMD_CTS;
920 dat[4] = (pkt >> 16);
922 dat[0] = J1939_TP_CMD_CTS;
924 dat[2] = session->pkt.rx + 1;
927 if (dat[0] == session->last_txcmd)
931 ret = j1939_tp_tx_ctl(session, true, dat);
936 /* only mark cts done when len is set */
937 session->last_txcmd = dat[0];
938 j1939_tp_set_rxtimeout(session, 1250);
940 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
945 static int j1939_session_tx_eoma(struct j1939_session *session)
947 struct j1939_priv *priv = session->priv;
951 if (!j1939_sk_recv_match(priv, &session->skcb))
954 memset(dat, 0xff, sizeof(dat));
956 if (session->skcb.addr.type == J1939_ETP) {
957 dat[0] = J1939_ETP_CMD_EOMA;
958 dat[1] = session->total_message_size >> 0;
959 dat[2] = session->total_message_size >> 8;
960 dat[3] = session->total_message_size >> 16;
961 dat[4] = session->total_message_size >> 24;
963 dat[0] = J1939_TP_CMD_EOMA;
964 dat[1] = session->total_message_size;
965 dat[2] = session->total_message_size >> 8;
966 dat[3] = session->pkt.total;
969 if (dat[0] == session->last_txcmd)
973 ret = j1939_tp_tx_ctl(session, true, dat);
977 session->last_txcmd = dat[0];
979 /* wait for the EOMA packet to come in */
980 j1939_tp_set_rxtimeout(session, 1250);
982 netdev_dbg(session->priv->ndev, "%p: 0x%p\n", __func__, session);
987 static int j1939_xtp_txnext_receiver(struct j1939_session *session)
989 struct j1939_priv *priv = session->priv;
992 if (!j1939_tp_im_receiver(&session->skcb)) {
993 netdev_alert(priv->ndev, "%s: 0x%p: called by not receiver!\n",
998 switch (session->last_cmd) {
999 case J1939_TP_CMD_RTS:
1000 case J1939_ETP_CMD_RTS:
1001 ret = j1939_session_tx_cts(session);
1004 case J1939_ETP_CMD_CTS:
1005 case J1939_TP_CMD_CTS:
1006 case 0xff: /* did some data */
1007 case J1939_ETP_CMD_DPO:
1008 if ((session->skcb.addr.type == J1939_TP &&
1009 j1939_cb_is_broadcast(&session->skcb)))
1012 if (session->pkt.rx >= session->pkt.total) {
1013 ret = j1939_session_tx_eoma(session);
1014 } else if (session->pkt.rx >= session->pkt.last) {
1015 session->last_txcmd = 0;
1016 ret = j1939_session_tx_cts(session);
1020 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
1021 __func__, session, session->last_cmd);
1027 static int j1939_simple_txnext(struct j1939_session *session)
1029 struct j1939_priv *priv = session->priv;
1030 struct sk_buff *se_skb = j1939_session_skb_get(session);
1031 struct sk_buff *skb;
1037 skb = skb_clone(se_skb, GFP_ATOMIC);
1043 can_skb_set_owner(skb, se_skb->sk);
1045 j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS);
1047 ret = j1939_send_one(priv, skb);
1051 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_SCHED);
1052 j1939_sk_queue_activate_next(session);
1058 consume_skb(se_skb);
1063 static bool j1939_session_deactivate_locked(struct j1939_session *session)
1065 bool active = false;
1067 lockdep_assert_held(&session->priv->active_session_list_lock);
1069 if (session->state >= J1939_SESSION_ACTIVE &&
1070 session->state < J1939_SESSION_ACTIVE_MAX) {
1073 list_del_init(&session->active_session_list_entry);
1074 session->state = J1939_SESSION_DONE;
1075 j1939_session_put(session);
1081 static bool j1939_session_deactivate(struct j1939_session *session)
1083 struct j1939_priv *priv = session->priv;
1086 j1939_session_list_lock(priv);
1087 /* This function should be called with a session ref-count of at
1090 WARN_ON_ONCE(kref_read(&session->kref) < 2);
1091 active = j1939_session_deactivate_locked(session);
1092 j1939_session_list_unlock(priv);
1098 j1939_session_deactivate_activate_next(struct j1939_session *session)
1100 if (j1939_session_deactivate(session))
1101 j1939_sk_queue_activate_next(session);
1104 static void __j1939_session_cancel(struct j1939_session *session,
1105 enum j1939_xtp_abort err)
1107 struct j1939_priv *priv = session->priv;
1110 lockdep_assert_held(&session->priv->active_session_list_lock);
1112 session->err = j1939_xtp_abort_to_errno(priv, err);
1113 session->state = J1939_SESSION_WAITING_ABORT;
1114 /* do not send aborts on incoming broadcasts */
1115 if (!j1939_cb_is_broadcast(&session->skcb)) {
1116 j1939_xtp_tx_abort(priv, &session->skcb,
1117 !session->transmission,
1118 err, session->skcb.addr.pgn);
1122 j1939_sk_send_loop_abort(session->sk, session->err);
1124 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1127 static void j1939_session_cancel(struct j1939_session *session,
1128 enum j1939_xtp_abort err)
1130 j1939_session_list_lock(session->priv);
1132 if (session->state >= J1939_SESSION_ACTIVE &&
1133 session->state < J1939_SESSION_WAITING_ABORT) {
1134 j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS);
1135 __j1939_session_cancel(session, err);
1138 j1939_session_list_unlock(session->priv);
1141 static enum hrtimer_restart j1939_tp_txtimer(struct hrtimer *hrtimer)
1143 struct j1939_session *session =
1144 container_of(hrtimer, struct j1939_session, txtimer);
1145 struct j1939_priv *priv = session->priv;
1148 if (session->skcb.addr.type == J1939_SIMPLE) {
1149 ret = j1939_simple_txnext(session);
1151 if (session->transmission)
1152 ret = j1939_xtp_txnext_transmiter(session);
1154 ret = j1939_xtp_txnext_receiver(session);
1159 /* Retry limit is currently arbitrary chosen */
1160 if (session->tx_retry < J1939_XTP_TX_RETRY_LIMIT) {
1161 session->tx_retry++;
1162 j1939_tp_schedule_txtimer(session,
1163 10 + prandom_u32_max(16));
1165 netdev_alert(priv->ndev, "%s: 0x%p: tx retry count reached\n",
1167 session->err = -ENETUNREACH;
1168 j1939_session_rxtimer_cancel(session);
1169 j1939_session_deactivate_activate_next(session);
1173 /* In this case we should get a netdev_event(), all active
1174 * sessions will be cleared by
1175 * j1939_cancel_all_active_sessions(). So handle this as an
1176 * error, but let j1939_cancel_all_active_sessions() do the
1177 * cleanup including propagation of the error to user space.
1181 j1939_session_cancel(session, J1939_XTP_ABORT_ECTS_TOO_BIG);
1184 session->tx_retry = 0;
1187 netdev_alert(priv->ndev, "%s: 0x%p: tx aborted with unknown reason: %i\n",
1188 __func__, session, ret);
1189 if (session->skcb.addr.type != J1939_SIMPLE) {
1190 j1939_session_cancel(session, J1939_XTP_ABORT_OTHER);
1193 j1939_session_rxtimer_cancel(session);
1194 j1939_session_deactivate_activate_next(session);
1198 j1939_session_put(session);
1200 return HRTIMER_NORESTART;
1203 static void j1939_session_completed(struct j1939_session *session)
1205 struct sk_buff *se_skb;
1207 if (!session->transmission) {
1208 se_skb = j1939_session_skb_get(session);
1209 /* distribute among j1939 receivers */
1210 j1939_sk_recv(session->priv, se_skb);
1211 consume_skb(se_skb);
1214 j1939_session_deactivate_activate_next(session);
1217 static enum hrtimer_restart j1939_tp_rxtimer(struct hrtimer *hrtimer)
1219 struct j1939_session *session = container_of(hrtimer,
1220 struct j1939_session,
1222 struct j1939_priv *priv = session->priv;
1224 if (session->state == J1939_SESSION_WAITING_ABORT) {
1225 netdev_alert(priv->ndev, "%s: 0x%p: abort rx timeout. Force session deactivation\n",
1228 j1939_session_deactivate_activate_next(session);
1230 } else if (session->skcb.addr.type == J1939_SIMPLE) {
1231 netdev_alert(priv->ndev, "%s: 0x%p: Timeout. Failed to send simple message.\n",
1234 /* The message is probably stuck in the CAN controller and can
1235 * be send as soon as CAN bus is in working state again.
1237 session->err = -ETIME;
1238 j1939_session_deactivate(session);
1240 netdev_alert(priv->ndev, "%s: 0x%p: rx timeout, send abort\n",
1243 j1939_session_list_lock(session->priv);
1244 if (session->state >= J1939_SESSION_ACTIVE &&
1245 session->state < J1939_SESSION_ACTIVE_MAX) {
1246 j1939_session_get(session);
1247 hrtimer_start(&session->rxtimer,
1248 ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS),
1249 HRTIMER_MODE_REL_SOFT);
1250 __j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
1252 j1939_session_list_unlock(session->priv);
1255 j1939_session_put(session);
1257 return HRTIMER_NORESTART;
1260 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session *session,
1261 const struct sk_buff *skb)
1263 const struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1264 pgn_t pgn = j1939_xtp_ctl_to_pgn(skb->data);
1265 struct j1939_priv *priv = session->priv;
1266 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1267 u8 cmd = skb->data[0];
1269 if (session->skcb.addr.pgn == pgn)
1273 case J1939_TP_CMD_BAM:
1274 abort = J1939_XTP_NO_ABORT;
1277 case J1939_ETP_CMD_RTS:
1279 case J1939_TP_CMD_RTS:
1280 abort = J1939_XTP_ABORT_BUSY;
1283 case J1939_ETP_CMD_CTS:
1285 case J1939_TP_CMD_CTS:
1286 abort = J1939_XTP_ABORT_ECTS_UNXPECTED_PGN;
1289 case J1939_ETP_CMD_DPO:
1290 abort = J1939_XTP_ABORT_BAD_EDPO_PGN;
1293 case J1939_ETP_CMD_EOMA:
1295 case J1939_TP_CMD_EOMA:
1296 abort = J1939_XTP_ABORT_OTHER;
1299 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
1300 abort = J1939_XTP_NO_ABORT;
1308 netdev_warn(priv->ndev, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n",
1309 __func__, session, cmd, pgn, session->skcb.addr.pgn);
1310 if (abort != J1939_XTP_NO_ABORT)
1311 j1939_xtp_tx_abort(priv, skcb, true, abort, pgn);
1316 static void j1939_xtp_rx_abort_one(struct j1939_priv *priv, struct sk_buff *skb,
1317 bool reverse, bool transmitter)
1319 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1320 struct j1939_session *session;
1321 u8 abort = skb->data[1];
1323 session = j1939_session_get_by_addr(priv, &skcb->addr, reverse,
1328 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1331 netdev_info(priv->ndev, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__,
1332 session, j1939_xtp_ctl_to_pgn(skb->data), abort,
1333 j1939_xtp_abort_to_str(abort));
1335 j1939_session_timers_cancel(session);
1336 session->err = j1939_xtp_abort_to_errno(priv, abort);
1338 j1939_sk_send_loop_abort(session->sk, session->err);
1340 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1341 j1939_session_deactivate_activate_next(session);
1344 j1939_session_put(session);
1347 /* abort packets may come in 2 directions */
1349 j1939_xtp_rx_abort(struct j1939_priv *priv, struct sk_buff *skb,
1352 j1939_xtp_rx_abort_one(priv, skb, false, transmitter);
1353 j1939_xtp_rx_abort_one(priv, skb, true, transmitter);
1357 j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
1359 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1363 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1368 if (skcb->addr.type == J1939_ETP)
1369 len = j1939_etp_ctl_to_size(dat);
1371 len = j1939_tp_ctl_to_size(dat);
1373 if (session->total_message_size != len) {
1374 netdev_warn_once(session->priv->ndev,
1375 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
1376 __func__, session, session->total_message_size,
1380 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1382 session->pkt.tx_acked = session->pkt.total;
1383 j1939_session_timers_cancel(session);
1384 /* transmitted without problems */
1385 j1939_session_completed(session);
1389 j1939_xtp_rx_eoma(struct j1939_priv *priv, struct sk_buff *skb,
1392 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1393 struct j1939_session *session;
1395 session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1400 j1939_xtp_rx_eoma_one(session, skb);
1401 j1939_session_put(session);
1405 j1939_xtp_rx_cts_one(struct j1939_session *session, struct sk_buff *skb)
1407 enum j1939_xtp_abort err = J1939_XTP_ABORT_FAULT;
1413 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1416 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1418 if (session->last_cmd == dat[0]) {
1419 err = J1939_XTP_ABORT_DUP_SEQ;
1420 goto out_session_cancel;
1423 if (session->skcb.addr.type == J1939_ETP)
1424 pkt = j1939_etp_ctl_to_packet(dat);
1429 goto out_session_cancel;
1430 else if (dat[1] > session->pkt.block /* 0xff for etp */)
1431 goto out_session_cancel;
1433 /* set packet counters only when not CTS(0) */
1434 session->pkt.tx_acked = pkt - 1;
1435 j1939_session_skb_drop_old(session);
1436 session->pkt.last = session->pkt.tx_acked + dat[1];
1437 if (session->pkt.last > session->pkt.total)
1438 /* safety measure */
1439 session->pkt.last = session->pkt.total;
1440 /* TODO: do not set tx here, do it in txtimer */
1441 session->pkt.tx = session->pkt.tx_acked;
1443 session->last_cmd = dat[0];
1445 j1939_tp_set_rxtimeout(session, 1250);
1446 if (session->transmission) {
1447 if (session->pkt.tx_acked)
1448 j1939_sk_errqueue(session,
1449 J1939_ERRQUEUE_TX_SCHED);
1450 j1939_session_txtimer_cancel(session);
1451 j1939_tp_schedule_txtimer(session, 0);
1455 j1939_tp_set_rxtimeout(session, 550);
1460 j1939_session_timers_cancel(session);
1461 j1939_session_cancel(session, err);
1465 j1939_xtp_rx_cts(struct j1939_priv *priv, struct sk_buff *skb, bool transmitter)
1467 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1468 struct j1939_session *session;
1470 session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1474 j1939_xtp_rx_cts_one(session, skb);
1475 j1939_session_put(session);
1478 static struct j1939_session *j1939_session_new(struct j1939_priv *priv,
1479 struct sk_buff *skb, size_t size)
1481 struct j1939_session *session;
1482 struct j1939_sk_buff_cb *skcb;
1484 session = kzalloc(sizeof(*session), gfp_any());
1488 INIT_LIST_HEAD(&session->active_session_list_entry);
1489 INIT_LIST_HEAD(&session->sk_session_queue_entry);
1490 kref_init(&session->kref);
1492 j1939_priv_get(priv);
1493 session->priv = priv;
1494 session->total_message_size = size;
1495 session->state = J1939_SESSION_NEW;
1497 skb_queue_head_init(&session->skb_queue);
1498 skb_queue_tail(&session->skb_queue, skb);
1500 skcb = j1939_skb_to_cb(skb);
1501 memcpy(&session->skcb, skcb, sizeof(session->skcb));
1503 hrtimer_init(&session->txtimer, CLOCK_MONOTONIC,
1504 HRTIMER_MODE_REL_SOFT);
1505 session->txtimer.function = j1939_tp_txtimer;
1506 hrtimer_init(&session->rxtimer, CLOCK_MONOTONIC,
1507 HRTIMER_MODE_REL_SOFT);
1508 session->rxtimer.function = j1939_tp_rxtimer;
1510 netdev_dbg(priv->ndev, "%s: 0x%p: sa: %02x, da: %02x\n",
1511 __func__, session, skcb->addr.sa, skcb->addr.da);
1517 j1939_session *j1939_session_fresh_new(struct j1939_priv *priv,
1519 const struct j1939_sk_buff_cb *rel_skcb)
1521 struct sk_buff *skb;
1522 struct j1939_sk_buff_cb *skcb;
1523 struct j1939_session *session;
1525 skb = alloc_skb(size + sizeof(struct can_skb_priv), GFP_ATOMIC);
1529 skb->dev = priv->ndev;
1530 can_skb_reserve(skb);
1531 can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
1532 can_skb_prv(skb)->skbcnt = 0;
1533 skcb = j1939_skb_to_cb(skb);
1534 memcpy(skcb, rel_skcb, sizeof(*skcb));
1536 session = j1939_session_new(priv, skb, size);
1542 /* alloc data area */
1544 /* skb is recounted in j1939_session_new() */
1548 int j1939_session_activate(struct j1939_session *session)
1550 struct j1939_priv *priv = session->priv;
1551 struct j1939_session *active = NULL;
1554 j1939_session_list_lock(priv);
1555 if (session->skcb.addr.type != J1939_SIMPLE)
1556 active = j1939_session_get_by_addr_locked(priv,
1557 &priv->active_session_list,
1558 &session->skcb.addr, false,
1559 session->transmission);
1561 j1939_session_put(active);
1564 WARN_ON_ONCE(session->state != J1939_SESSION_NEW);
1565 list_add_tail(&session->active_session_list_entry,
1566 &priv->active_session_list);
1567 j1939_session_get(session);
1568 session->state = J1939_SESSION_ACTIVE;
1570 netdev_dbg(session->priv->ndev, "%s: 0x%p\n",
1573 j1939_session_list_unlock(priv);
1579 j1939_session *j1939_xtp_rx_rts_session_new(struct j1939_priv *priv,
1580 struct sk_buff *skb)
1582 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1583 struct j1939_sk_buff_cb skcb = *j1939_skb_to_cb(skb);
1584 struct j1939_session *session;
1589 netdev_dbg(priv->ndev, "%s\n", __func__);
1592 pgn = j1939_xtp_ctl_to_pgn(dat);
1593 skcb.addr.pgn = pgn;
1595 if (!j1939_sk_recv_match(priv, &skcb))
1598 if (skcb.addr.type == J1939_ETP) {
1599 len = j1939_etp_ctl_to_size(dat);
1600 if (len > J1939_MAX_ETP_PACKET_SIZE)
1601 abort = J1939_XTP_ABORT_FAULT;
1602 else if (len > priv->tp_max_packet_size)
1603 abort = J1939_XTP_ABORT_RESOURCE;
1604 else if (len <= J1939_MAX_TP_PACKET_SIZE)
1605 abort = J1939_XTP_ABORT_FAULT;
1607 len = j1939_tp_ctl_to_size(dat);
1608 if (len > J1939_MAX_TP_PACKET_SIZE)
1609 abort = J1939_XTP_ABORT_FAULT;
1610 else if (len > priv->tp_max_packet_size)
1611 abort = J1939_XTP_ABORT_RESOURCE;
1614 if (abort != J1939_XTP_NO_ABORT) {
1615 j1939_xtp_tx_abort(priv, &skcb, true, abort, pgn);
1619 session = j1939_session_fresh_new(priv, len, &skcb);
1621 j1939_xtp_tx_abort(priv, &skcb, true,
1622 J1939_XTP_ABORT_RESOURCE, pgn);
1626 /* initialize the control buffer: plain copy */
1627 session->pkt.total = (len + 6) / 7;
1628 session->pkt.block = 0xff;
1629 if (skcb.addr.type != J1939_ETP) {
1630 if (dat[3] != session->pkt.total)
1631 netdev_alert(priv->ndev, "%s: 0x%p: strange total, %u != %u\n",
1632 __func__, session, session->pkt.total,
1634 session->pkt.total = dat[3];
1635 session->pkt.block = min(dat[3], dat[4]);
1638 session->pkt.rx = 0;
1639 session->pkt.tx = 0;
1641 session->tskey = priv->rx_tskey++;
1642 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_RTS);
1644 WARN_ON_ONCE(j1939_session_activate(session));
1649 static int j1939_xtp_rx_rts_session_active(struct j1939_session *session,
1650 struct sk_buff *skb)
1652 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1653 struct j1939_priv *priv = session->priv;
1655 if (!session->transmission) {
1656 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1659 /* RTS on active session */
1660 j1939_session_timers_cancel(session);
1661 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1664 if (session->last_cmd != 0) {
1665 /* we received a second rts on the same connection */
1666 netdev_alert(priv->ndev, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n",
1667 __func__, session, skcb->addr.sa, skcb->addr.da,
1670 j1939_session_timers_cancel(session);
1671 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1676 if (session->skcb.addr.sa != skcb->addr.sa ||
1677 session->skcb.addr.da != skcb->addr.da)
1678 netdev_warn(priv->ndev, "%s: 0x%p: session->skcb.addr.sa=0x%02x skcb->addr.sa=0x%02x session->skcb.addr.da=0x%02x skcb->addr.da=0x%02x\n",
1680 session->skcb.addr.sa, skcb->addr.sa,
1681 session->skcb.addr.da, skcb->addr.da);
1682 /* make sure 'sa' & 'da' are correct !
1683 * They may be 'not filled in yet' for sending
1684 * skb's, since they did not pass the Address Claim ever.
1686 session->skcb.addr.sa = skcb->addr.sa;
1687 session->skcb.addr.da = skcb->addr.da;
1689 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1694 static void j1939_xtp_rx_rts(struct j1939_priv *priv, struct sk_buff *skb,
1697 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1698 struct j1939_session *session;
1699 u8 cmd = skb->data[0];
1701 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1706 /* If we're the transmitter and this function is called,
1707 * we received our own RTS. A session has already been
1710 * For some reasons however it might have been destroyed
1711 * already. So don't create a new one here (using
1712 * "j1939_xtp_rx_rts_session_new()") as this will be a
1715 * The reasons the session is already destroyed might
1717 * - user space closed socket was and the session was
1719 * - session was aborted due to external abort message
1723 session = j1939_xtp_rx_rts_session_new(priv, skb);
1725 if (cmd == J1939_TP_CMD_BAM && j1939_sk_recv_match(priv, skcb))
1726 netdev_info(priv->ndev, "%s: failed to create TP BAM session\n",
1731 if (j1939_xtp_rx_rts_session_active(session, skb)) {
1732 j1939_session_put(session);
1736 session->last_cmd = cmd;
1738 if (cmd == J1939_TP_CMD_BAM) {
1739 if (!session->transmission)
1740 j1939_tp_set_rxtimeout(session, 750);
1742 if (!session->transmission) {
1743 j1939_session_txtimer_cancel(session);
1744 j1939_tp_schedule_txtimer(session, 0);
1746 j1939_tp_set_rxtimeout(session, 1250);
1749 j1939_session_put(session);
1752 static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
1753 struct sk_buff *skb)
1755 const u8 *dat = skb->data;
1757 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1760 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1762 /* transmitted without problems */
1763 session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
1764 session->last_cmd = dat[0];
1765 j1939_tp_set_rxtimeout(session, 750);
1767 if (!session->transmission)
1768 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_DPO);
1771 static void j1939_xtp_rx_dpo(struct j1939_priv *priv, struct sk_buff *skb,
1774 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1775 struct j1939_session *session;
1777 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1780 netdev_info(priv->ndev,
1781 "%s: no connection found\n", __func__);
1785 j1939_xtp_rx_dpo_one(session, skb);
1786 j1939_session_put(session);
1789 static void j1939_xtp_rx_dat_one(struct j1939_session *session,
1790 struct sk_buff *skb)
1792 struct j1939_priv *priv = session->priv;
1793 struct j1939_sk_buff_cb *skcb, *se_skcb;
1794 struct sk_buff *se_skb = NULL;
1800 bool remain = false;
1801 bool do_cts_eoma = false;
1804 skcb = j1939_skb_to_cb(skb);
1807 /* makes no sense */
1808 goto out_session_cancel;
1810 switch (session->last_cmd) {
1813 case J1939_ETP_CMD_DPO:
1814 if (skcb->addr.type == J1939_ETP)
1817 case J1939_TP_CMD_BAM:
1819 case J1939_TP_CMD_CTS:
1820 if (skcb->addr.type != J1939_ETP)
1824 netdev_info(priv->ndev, "%s: 0x%p: last %02x\n", __func__,
1825 session, session->last_cmd);
1826 goto out_session_cancel;
1829 packet = (dat[0] - 1 + session->pkt.dpo);
1830 if (packet > session->pkt.total ||
1831 (session->pkt.rx + 1) > session->pkt.total) {
1832 netdev_info(priv->ndev, "%s: 0x%p: should have been completed\n",
1834 goto out_session_cancel;
1837 se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
1839 netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
1841 goto out_session_cancel;
1844 se_skcb = j1939_skb_to_cb(se_skb);
1845 offset = packet * 7 - se_skcb->offset;
1846 nbytes = se_skb->len - offset;
1849 if (nbytes <= 0 || (nbytes + 1) > skb->len) {
1850 netdev_info(priv->ndev, "%s: 0x%p: nbytes %i, len %i\n",
1851 __func__, session, nbytes, skb->len);
1852 goto out_session_cancel;
1855 tpdat = se_skb->data;
1856 if (!session->transmission) {
1857 memcpy(&tpdat[offset], &dat[1], nbytes);
1861 err = memcmp(&tpdat[offset], &dat[1], nbytes);
1863 netdev_err_once(priv->ndev,
1864 "%s: 0x%p: Data of RX-looped back packet (%*ph) doesn't match TX data (%*ph)!\n",
1867 nbytes, &tpdat[offset]);
1870 if (packet == session->pkt.rx)
1873 if (se_skcb->addr.type != J1939_ETP &&
1874 j1939_cb_is_broadcast(&session->skcb)) {
1875 if (session->pkt.rx >= session->pkt.total)
1880 /* never final, an EOMA must follow */
1881 if (session->pkt.rx >= session->pkt.last)
1886 j1939_session_timers_cancel(session);
1887 j1939_session_completed(session);
1888 } else if (remain) {
1889 if (!session->transmission)
1890 j1939_tp_set_rxtimeout(session, 750);
1891 } else if (do_cts_eoma) {
1892 j1939_tp_set_rxtimeout(session, 1250);
1893 if (!session->transmission)
1894 j1939_tp_schedule_txtimer(session, 0);
1896 j1939_tp_set_rxtimeout(session, 750);
1898 session->last_cmd = 0xff;
1899 consume_skb(se_skb);
1900 j1939_session_put(session);
1906 j1939_session_timers_cancel(session);
1907 j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
1908 j1939_session_put(session);
1911 static void j1939_xtp_rx_dat(struct j1939_priv *priv, struct sk_buff *skb)
1913 struct j1939_sk_buff_cb *skcb;
1914 struct j1939_session *session;
1916 skcb = j1939_skb_to_cb(skb);
1918 if (j1939_tp_im_transmitter(skcb)) {
1919 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1922 netdev_info(priv->ndev, "%s: no tx connection found\n",
1925 j1939_xtp_rx_dat_one(session, skb);
1928 if (j1939_tp_im_receiver(skcb)) {
1929 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1932 netdev_info(priv->ndev, "%s: no rx connection found\n",
1935 j1939_xtp_rx_dat_one(session, skb);
1938 if (j1939_cb_is_broadcast(skcb)) {
1939 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1942 j1939_xtp_rx_dat_one(session, skb);
1946 /* j1939 main intf */
1947 struct j1939_session *j1939_tp_send(struct j1939_priv *priv,
1948 struct sk_buff *skb, size_t size)
1950 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1951 struct j1939_session *session;
1954 if (skcb->addr.pgn == J1939_TP_PGN_DAT ||
1955 skcb->addr.pgn == J1939_TP_PGN_CTL ||
1956 skcb->addr.pgn == J1939_ETP_PGN_DAT ||
1957 skcb->addr.pgn == J1939_ETP_PGN_CTL)
1958 /* avoid conflict */
1959 return ERR_PTR(-EDOM);
1961 if (size > priv->tp_max_packet_size)
1962 return ERR_PTR(-EMSGSIZE);
1965 skcb->addr.type = J1939_SIMPLE;
1966 else if (size > J1939_MAX_TP_PACKET_SIZE)
1967 skcb->addr.type = J1939_ETP;
1969 skcb->addr.type = J1939_TP;
1971 if (skcb->addr.type == J1939_ETP &&
1972 j1939_cb_is_broadcast(skcb))
1973 return ERR_PTR(-EDESTADDRREQ);
1975 /* fill in addresses from names */
1976 ret = j1939_ac_fixup(priv, skb);
1978 return ERR_PTR(ret);
1980 /* fix DST flags, it may be used there soon */
1981 if (j1939_address_is_unicast(skcb->addr.da) &&
1982 priv->ents[skcb->addr.da].nusers)
1983 skcb->flags |= J1939_ECU_LOCAL_DST;
1985 /* src is always local, I'm sending ... */
1986 skcb->flags |= J1939_ECU_LOCAL_SRC;
1988 /* prepare new session */
1989 session = j1939_session_new(priv, skb, size);
1991 return ERR_PTR(-ENOMEM);
1993 /* skb is recounted in j1939_session_new() */
1995 session->sk = skb->sk;
1996 session->transmission = true;
1997 session->pkt.total = (size + 6) / 7;
1998 session->pkt.block = skcb->addr.type == J1939_ETP ? 255 :
1999 min(j1939_tp_block ?: 255, session->pkt.total);
2001 if (j1939_cb_is_broadcast(&session->skcb))
2002 /* set the end-packet for broadcast */
2003 session->pkt.last = session->pkt.total;
2005 skcb->tskey = session->sk->sk_tskey++;
2006 session->tskey = skcb->tskey;
2011 static void j1939_tp_cmd_recv(struct j1939_priv *priv, struct sk_buff *skb)
2013 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2014 int extd = J1939_TP;
2015 u8 cmd = skb->data[0];
2018 case J1939_ETP_CMD_RTS:
2021 case J1939_TP_CMD_BAM:
2023 case J1939_TP_CMD_RTS:
2024 if (skcb->addr.type != extd)
2027 if (cmd == J1939_TP_CMD_RTS && j1939_cb_is_broadcast(skcb)) {
2028 netdev_alert(priv->ndev, "%s: rts without destination (%02x)\n",
2029 __func__, skcb->addr.sa);
2033 if (j1939_tp_im_transmitter(skcb))
2034 j1939_xtp_rx_rts(priv, skb, true);
2036 if (j1939_tp_im_receiver(skcb) || j1939_cb_is_broadcast(skcb))
2037 j1939_xtp_rx_rts(priv, skb, false);
2041 case J1939_ETP_CMD_CTS:
2044 case J1939_TP_CMD_CTS:
2045 if (skcb->addr.type != extd)
2048 if (j1939_tp_im_transmitter(skcb))
2049 j1939_xtp_rx_cts(priv, skb, false);
2051 if (j1939_tp_im_receiver(skcb))
2052 j1939_xtp_rx_cts(priv, skb, true);
2056 case J1939_ETP_CMD_DPO:
2057 if (skcb->addr.type != J1939_ETP)
2060 if (j1939_tp_im_transmitter(skcb))
2061 j1939_xtp_rx_dpo(priv, skb, true);
2063 if (j1939_tp_im_receiver(skcb))
2064 j1939_xtp_rx_dpo(priv, skb, false);
2068 case J1939_ETP_CMD_EOMA:
2071 case J1939_TP_CMD_EOMA:
2072 if (skcb->addr.type != extd)
2075 if (j1939_tp_im_transmitter(skcb))
2076 j1939_xtp_rx_eoma(priv, skb, false);
2078 if (j1939_tp_im_receiver(skcb))
2079 j1939_xtp_rx_eoma(priv, skb, true);
2083 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
2084 if (j1939_tp_im_transmitter(skcb))
2085 j1939_xtp_rx_abort(priv, skb, true);
2087 if (j1939_tp_im_receiver(skcb))
2088 j1939_xtp_rx_abort(priv, skb, false);
2096 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb)
2098 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2100 if (!j1939_tp_im_involved_anydir(skcb) && !j1939_cb_is_broadcast(skcb))
2103 switch (skcb->addr.pgn) {
2104 case J1939_ETP_PGN_DAT:
2105 skcb->addr.type = J1939_ETP;
2107 case J1939_TP_PGN_DAT:
2108 j1939_xtp_rx_dat(priv, skb);
2111 case J1939_ETP_PGN_CTL:
2112 skcb->addr.type = J1939_ETP;
2114 case J1939_TP_PGN_CTL:
2116 return 0; /* Don't care. Nothing to extract here */
2118 j1939_tp_cmd_recv(priv, skb);
2121 return 0; /* no problem */
2123 return 1; /* "I processed the message" */
2126 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb)
2128 struct j1939_session *session;
2133 if (skb->sk->sk_family != AF_CAN ||
2134 skb->sk->sk_protocol != CAN_J1939)
2137 j1939_session_list_lock(priv);
2138 session = j1939_session_get_simple(priv, skb);
2139 j1939_session_list_unlock(priv);
2141 netdev_warn(priv->ndev,
2142 "%s: Received already invalidated message\n",
2147 j1939_session_timers_cancel(session);
2148 j1939_session_deactivate(session);
2149 j1939_session_put(session);
2152 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk)
2154 struct j1939_session *session, *saved;
2156 netdev_dbg(priv->ndev, "%s, sk: %p\n", __func__, sk);
2157 j1939_session_list_lock(priv);
2158 list_for_each_entry_safe(session, saved,
2159 &priv->active_session_list,
2160 active_session_list_entry) {
2161 if (!sk || sk == session->sk) {
2162 if (hrtimer_try_to_cancel(&session->txtimer) == 1)
2163 j1939_session_put(session);
2164 if (hrtimer_try_to_cancel(&session->rxtimer) == 1)
2165 j1939_session_put(session);
2167 session->err = ESHUTDOWN;
2168 j1939_session_deactivate_locked(session);
2171 j1939_session_list_unlock(priv);
2175 void j1939_tp_init(struct j1939_priv *priv)
2177 spin_lock_init(&priv->active_session_list_lock);
2178 INIT_LIST_HEAD(&priv->active_session_list);
2179 priv->tp_max_packet_size = J1939_MAX_ETP_PACKET_SIZE;