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)
265 if (session->transmission) {
267 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
269 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ACK);
270 } else if (session->err) {
271 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
274 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
276 WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
277 WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
279 while ((skb = skb_dequeue(&session->skb_queue)) != NULL) {
280 /* drop ref taken in j1939_session_skb_queue() */
284 __j1939_session_drop(session);
285 j1939_priv_put(session->priv);
289 static void __j1939_session_release(struct kref *kref)
291 struct j1939_session *session = container_of(kref, struct j1939_session,
294 j1939_session_destroy(session);
297 void j1939_session_put(struct j1939_session *session)
299 kref_put(&session->kref, __j1939_session_release);
302 static void j1939_session_txtimer_cancel(struct j1939_session *session)
304 if (hrtimer_cancel(&session->txtimer))
305 j1939_session_put(session);
308 static void j1939_session_rxtimer_cancel(struct j1939_session *session)
310 if (hrtimer_cancel(&session->rxtimer))
311 j1939_session_put(session);
314 void j1939_session_timers_cancel(struct j1939_session *session)
316 j1939_session_txtimer_cancel(session);
317 j1939_session_rxtimer_cancel(session);
320 static inline bool j1939_cb_is_broadcast(const struct j1939_sk_buff_cb *skcb)
322 return (!skcb->addr.dst_name && (skcb->addr.da == 0xff));
325 static void j1939_session_skb_drop_old(struct j1939_session *session)
327 struct sk_buff *do_skb;
328 struct j1939_sk_buff_cb *do_skcb;
329 unsigned int offset_start;
332 if (skb_queue_len(&session->skb_queue) < 2)
335 offset_start = session->pkt.tx_acked * 7;
337 spin_lock_irqsave(&session->skb_queue.lock, flags);
338 do_skb = skb_peek(&session->skb_queue);
339 do_skcb = j1939_skb_to_cb(do_skb);
341 if ((do_skcb->offset + do_skb->len) < offset_start) {
342 __skb_unlink(do_skb, &session->skb_queue);
343 /* drop ref taken in j1939_session_skb_queue() */
345 spin_unlock_irqrestore(&session->skb_queue.lock, flags);
349 spin_unlock_irqrestore(&session->skb_queue.lock, flags);
353 void j1939_session_skb_queue(struct j1939_session *session,
356 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
357 struct j1939_priv *priv = session->priv;
359 j1939_ac_fixup(priv, skb);
361 if (j1939_address_is_unicast(skcb->addr.da) &&
362 priv->ents[skcb->addr.da].nusers)
363 skcb->flags |= J1939_ECU_LOCAL_DST;
365 skcb->flags |= J1939_ECU_LOCAL_SRC;
368 skb_queue_tail(&session->skb_queue, skb);
372 sk_buff *j1939_session_skb_get_by_offset(struct j1939_session *session,
373 unsigned int offset_start)
375 struct j1939_priv *priv = session->priv;
376 struct j1939_sk_buff_cb *do_skcb;
377 struct sk_buff *skb = NULL;
378 struct sk_buff *do_skb;
381 spin_lock_irqsave(&session->skb_queue.lock, flags);
382 skb_queue_walk(&session->skb_queue, do_skb) {
383 do_skcb = j1939_skb_to_cb(do_skb);
385 if (offset_start >= do_skcb->offset &&
386 offset_start < (do_skcb->offset + do_skb->len)) {
394 spin_unlock_irqrestore(&session->skb_queue.lock, flags);
397 netdev_dbg(priv->ndev, "%s: 0x%p: no skb found for start: %i, queue size: %i\n",
398 __func__, session, offset_start,
399 skb_queue_len(&session->skb_queue));
404 static struct sk_buff *j1939_session_skb_get(struct j1939_session *session)
406 unsigned int offset_start;
408 offset_start = session->pkt.dpo * 7;
409 return j1939_session_skb_get_by_offset(session, offset_start);
412 /* see if we are receiver
413 * returns 0 for broadcasts, although we will receive them
415 static inline int j1939_tp_im_receiver(const struct j1939_sk_buff_cb *skcb)
417 return skcb->flags & J1939_ECU_LOCAL_DST;
420 /* see if we are sender */
421 static inline int j1939_tp_im_transmitter(const struct j1939_sk_buff_cb *skcb)
423 return skcb->flags & J1939_ECU_LOCAL_SRC;
426 /* see if we are involved as either receiver or transmitter */
427 static int j1939_tp_im_involved(const struct j1939_sk_buff_cb *skcb, bool swap)
430 return j1939_tp_im_receiver(skcb);
432 return j1939_tp_im_transmitter(skcb);
435 static int j1939_tp_im_involved_anydir(struct j1939_sk_buff_cb *skcb)
437 return skcb->flags & (J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
440 /* extract pgn from flow-ctl message */
441 static inline pgn_t j1939_xtp_ctl_to_pgn(const u8 *dat)
445 pgn = (dat[7] << 16) | (dat[6] << 8) | (dat[5] << 0);
446 if (j1939_pgn_is_pdu1(pgn))
451 static inline unsigned int j1939_tp_ctl_to_size(const u8 *dat)
453 return (dat[2] << 8) + (dat[1] << 0);
456 static inline unsigned int j1939_etp_ctl_to_packet(const u8 *dat)
458 return (dat[4] << 16) | (dat[3] << 8) | (dat[2] << 0);
461 static inline unsigned int j1939_etp_ctl_to_size(const u8 *dat)
463 return (dat[4] << 24) | (dat[3] << 16) |
464 (dat[2] << 8) | (dat[1] << 0);
467 /* find existing session:
468 * reverse: swap cb's src & dst
469 * there is no problem with matching broadcasts, since
470 * broadcasts (no dst, no da) would never call this
471 * with reverse == true
473 static bool j1939_session_match(struct j1939_addr *se_addr,
474 struct j1939_addr *sk_addr, bool reverse)
476 if (se_addr->type != sk_addr->type)
480 if (se_addr->src_name) {
481 if (se_addr->src_name != sk_addr->dst_name)
483 } else if (se_addr->sa != sk_addr->da) {
487 if (se_addr->dst_name) {
488 if (se_addr->dst_name != sk_addr->src_name)
490 } else if (se_addr->da != sk_addr->sa) {
494 if (se_addr->src_name) {
495 if (se_addr->src_name != sk_addr->src_name)
497 } else if (se_addr->sa != sk_addr->sa) {
501 if (se_addr->dst_name) {
502 if (se_addr->dst_name != sk_addr->dst_name)
504 } else if (se_addr->da != sk_addr->da) {
513 j1939_session *j1939_session_get_by_addr_locked(struct j1939_priv *priv,
514 struct list_head *root,
515 struct j1939_addr *addr,
516 bool reverse, bool transmitter)
518 struct j1939_session *session;
520 lockdep_assert_held(&priv->active_session_list_lock);
522 list_for_each_entry(session, root, active_session_list_entry) {
523 j1939_session_get(session);
524 if (j1939_session_match(&session->skcb.addr, addr, reverse) &&
525 session->transmission == transmitter)
527 j1939_session_put(session);
534 j1939_session *j1939_session_get_simple(struct j1939_priv *priv,
537 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
538 struct j1939_session *session;
540 lockdep_assert_held(&priv->active_session_list_lock);
542 list_for_each_entry(session, &priv->active_session_list,
543 active_session_list_entry) {
544 j1939_session_get(session);
545 if (session->skcb.addr.type == J1939_SIMPLE &&
546 session->tskey == skcb->tskey && session->sk == skb->sk)
548 j1939_session_put(session);
555 j1939_session *j1939_session_get_by_addr(struct j1939_priv *priv,
556 struct j1939_addr *addr,
557 bool reverse, bool transmitter)
559 struct j1939_session *session;
561 j1939_session_list_lock(priv);
562 session = j1939_session_get_by_addr_locked(priv,
563 &priv->active_session_list,
564 addr, reverse, transmitter);
565 j1939_session_list_unlock(priv);
570 static void j1939_skbcb_swap(struct j1939_sk_buff_cb *skcb)
574 swap(skcb->addr.dst_name, skcb->addr.src_name);
575 swap(skcb->addr.da, skcb->addr.sa);
577 /* swap SRC and DST flags, leave other untouched */
578 if (skcb->flags & J1939_ECU_LOCAL_SRC)
579 tmp |= J1939_ECU_LOCAL_DST;
580 if (skcb->flags & J1939_ECU_LOCAL_DST)
581 tmp |= J1939_ECU_LOCAL_SRC;
582 skcb->flags &= ~(J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
587 sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv,
588 const struct j1939_sk_buff_cb *re_skcb,
593 struct j1939_sk_buff_cb *skcb;
595 skb = alloc_skb(sizeof(struct can_frame) + sizeof(struct can_skb_priv),
598 return ERR_PTR(-ENOMEM);
600 skb->dev = priv->ndev;
601 can_skb_reserve(skb);
602 can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
603 can_skb_prv(skb)->skbcnt = 0;
604 /* reserve CAN header */
605 skb_reserve(skb, offsetof(struct can_frame, data));
607 memcpy(skb->cb, re_skcb, sizeof(skb->cb));
608 skcb = j1939_skb_to_cb(skb);
610 j1939_skbcb_swap(skcb);
613 if (skcb->addr.type == J1939_ETP)
614 skcb->addr.pgn = J1939_ETP_PGN_CTL;
616 skcb->addr.pgn = J1939_TP_PGN_CTL;
618 if (skcb->addr.type == J1939_ETP)
619 skcb->addr.pgn = J1939_ETP_PGN_DAT;
621 skcb->addr.pgn = J1939_TP_PGN_DAT;
627 /* TP transmit packet functions */
628 static int j1939_tp_tx_dat(struct j1939_session *session,
629 const u8 *dat, int len)
631 struct j1939_priv *priv = session->priv;
634 skb = j1939_tp_tx_dat_new(priv, &session->skcb,
639 skb_put_data(skb, dat, len);
640 if (j1939_tp_padding && len < 8)
641 memset(skb_put(skb, 8 - len), 0xff, 8 - len);
643 return j1939_send_one(priv, skb);
646 static int j1939_xtp_do_tx_ctl(struct j1939_priv *priv,
647 const struct j1939_sk_buff_cb *re_skcb,
648 bool swap_src_dst, pgn_t pgn, const u8 *dat)
653 if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
656 skb = j1939_tp_tx_dat_new(priv, re_skcb, true, swap_src_dst);
660 skdat = skb_put(skb, 8);
661 memcpy(skdat, dat, 5);
662 skdat[5] = (pgn >> 0);
663 skdat[6] = (pgn >> 8);
664 skdat[7] = (pgn >> 16);
666 return j1939_send_one(priv, skb);
669 static inline int j1939_tp_tx_ctl(struct j1939_session *session,
670 bool swap_src_dst, const u8 *dat)
672 struct j1939_priv *priv = session->priv;
674 return j1939_xtp_do_tx_ctl(priv, &session->skcb,
676 session->skcb.addr.pgn, dat);
679 static int j1939_xtp_tx_abort(struct j1939_priv *priv,
680 const struct j1939_sk_buff_cb *re_skcb,
682 enum j1939_xtp_abort err,
687 if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
690 memset(dat, 0xff, sizeof(dat));
691 dat[0] = J1939_TP_CMD_ABORT;
693 return j1939_xtp_do_tx_ctl(priv, re_skcb, swap_src_dst, pgn, dat);
696 void j1939_tp_schedule_txtimer(struct j1939_session *session, int msec)
698 j1939_session_get(session);
699 hrtimer_start(&session->txtimer, ms_to_ktime(msec),
700 HRTIMER_MODE_REL_SOFT);
703 static inline void j1939_tp_set_rxtimeout(struct j1939_session *session,
706 j1939_session_rxtimer_cancel(session);
707 j1939_session_get(session);
708 hrtimer_start(&session->rxtimer, ms_to_ktime(msec),
709 HRTIMER_MODE_REL_SOFT);
712 static int j1939_session_tx_rts(struct j1939_session *session)
717 memset(dat, 0xff, sizeof(dat));
719 dat[1] = (session->total_message_size >> 0);
720 dat[2] = (session->total_message_size >> 8);
721 dat[3] = session->pkt.total;
723 if (session->skcb.addr.type == J1939_ETP) {
724 dat[0] = J1939_ETP_CMD_RTS;
725 dat[1] = (session->total_message_size >> 0);
726 dat[2] = (session->total_message_size >> 8);
727 dat[3] = (session->total_message_size >> 16);
728 dat[4] = (session->total_message_size >> 24);
729 } else if (j1939_cb_is_broadcast(&session->skcb)) {
730 dat[0] = J1939_TP_CMD_BAM;
731 /* fake cts for broadcast */
734 dat[0] = J1939_TP_CMD_RTS;
738 if (dat[0] == session->last_txcmd)
742 ret = j1939_tp_tx_ctl(session, false, dat);
746 session->last_txcmd = dat[0];
747 if (dat[0] == J1939_TP_CMD_BAM) {
748 j1939_tp_schedule_txtimer(session, 50);
749 j1939_tp_set_rxtimeout(session, 250);
751 j1939_tp_set_rxtimeout(session, 1250);
754 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
759 static int j1939_session_tx_dpo(struct j1939_session *session)
765 memset(dat, 0xff, sizeof(dat));
767 dat[0] = J1939_ETP_CMD_DPO;
768 session->pkt.dpo = session->pkt.tx_acked;
769 pkt = session->pkt.dpo;
770 dat[1] = session->pkt.last - session->pkt.tx_acked;
773 dat[4] = (pkt >> 16);
775 ret = j1939_tp_tx_ctl(session, false, dat);
779 session->last_txcmd = dat[0];
780 j1939_tp_set_rxtimeout(session, 1250);
781 session->pkt.tx = session->pkt.tx_acked;
783 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
788 static int j1939_session_tx_dat(struct j1939_session *session)
790 struct j1939_priv *priv = session->priv;
791 struct j1939_sk_buff_cb *se_skcb;
792 int offset, pkt_done, pkt_end;
793 unsigned int len, pdelay;
794 struct sk_buff *se_skb;
799 se_skb = j1939_session_skb_get_by_offset(session, session->pkt.tx * 7);
803 se_skcb = j1939_skb_to_cb(se_skb);
804 tpdat = se_skb->data;
807 if (session->skcb.addr.type != J1939_ETP &&
808 j1939_cb_is_broadcast(&session->skcb))
809 pkt_end = session->pkt.total;
811 pkt_end = session->pkt.last;
813 while (session->pkt.tx < pkt_end) {
814 dat[0] = session->pkt.tx - session->pkt.dpo + 1;
815 offset = (session->pkt.tx * 7) - se_skcb->offset;
816 len = se_skb->len - offset;
820 if (offset + len > se_skb->len) {
821 netdev_err_once(priv->ndev,
822 "%s: 0x%p: requested data outside of queued buffer: offset %i, len %i, pkt.tx: %i\n",
823 __func__, session, se_skcb->offset,
824 se_skb->len , session->pkt.tx);
834 memcpy(&dat[1], &tpdat[offset], len);
835 ret = j1939_tp_tx_dat(session, dat, len + 1);
837 /* ENOBUFS == CAN interface TX queue is full */
839 netdev_alert(priv->ndev,
840 "%s: 0x%p: queue data error: %i\n",
841 __func__, session, ret);
845 session->last_txcmd = 0xff;
848 pdelay = j1939_cb_is_broadcast(&session->skcb) ? 50 :
849 j1939_tp_packet_delay;
851 if (session->pkt.tx < session->pkt.total && pdelay) {
852 j1939_tp_schedule_txtimer(session, pdelay);
858 j1939_tp_set_rxtimeout(session, 250);
869 static int j1939_xtp_txnext_transmiter(struct j1939_session *session)
871 struct j1939_priv *priv = session->priv;
874 if (!j1939_tp_im_transmitter(&session->skcb)) {
875 netdev_alert(priv->ndev, "%s: 0x%p: called by not transmitter!\n",
880 switch (session->last_cmd) {
882 ret = j1939_session_tx_rts(session);
885 case J1939_ETP_CMD_CTS:
886 if (session->last_txcmd != J1939_ETP_CMD_DPO) {
887 ret = j1939_session_tx_dpo(session);
893 case J1939_TP_CMD_CTS:
894 case 0xff: /* did some data */
895 case J1939_ETP_CMD_DPO:
896 case J1939_TP_CMD_BAM:
897 ret = j1939_session_tx_dat(session);
901 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
902 __func__, session, session->last_cmd);
908 static int j1939_session_tx_cts(struct j1939_session *session)
910 struct j1939_priv *priv = session->priv;
911 unsigned int pkt, len;
915 if (!j1939_sk_recv_match(priv, &session->skcb))
918 len = session->pkt.total - session->pkt.rx;
919 len = min3(len, session->pkt.block, j1939_tp_block ?: 255);
920 memset(dat, 0xff, sizeof(dat));
922 if (session->skcb.addr.type == J1939_ETP) {
923 pkt = session->pkt.rx + 1;
924 dat[0] = J1939_ETP_CMD_CTS;
928 dat[4] = (pkt >> 16);
930 dat[0] = J1939_TP_CMD_CTS;
932 dat[2] = session->pkt.rx + 1;
935 if (dat[0] == session->last_txcmd)
939 ret = j1939_tp_tx_ctl(session, true, dat);
944 /* only mark cts done when len is set */
945 session->last_txcmd = dat[0];
946 j1939_tp_set_rxtimeout(session, 1250);
948 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
953 static int j1939_session_tx_eoma(struct j1939_session *session)
955 struct j1939_priv *priv = session->priv;
959 if (!j1939_sk_recv_match(priv, &session->skcb))
962 memset(dat, 0xff, sizeof(dat));
964 if (session->skcb.addr.type == J1939_ETP) {
965 dat[0] = J1939_ETP_CMD_EOMA;
966 dat[1] = session->total_message_size >> 0;
967 dat[2] = session->total_message_size >> 8;
968 dat[3] = session->total_message_size >> 16;
969 dat[4] = session->total_message_size >> 24;
971 dat[0] = J1939_TP_CMD_EOMA;
972 dat[1] = session->total_message_size;
973 dat[2] = session->total_message_size >> 8;
974 dat[3] = session->pkt.total;
977 if (dat[0] == session->last_txcmd)
981 ret = j1939_tp_tx_ctl(session, true, dat);
985 session->last_txcmd = dat[0];
987 /* wait for the EOMA packet to come in */
988 j1939_tp_set_rxtimeout(session, 1250);
990 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
995 static int j1939_xtp_txnext_receiver(struct j1939_session *session)
997 struct j1939_priv *priv = session->priv;
1000 if (!j1939_tp_im_receiver(&session->skcb)) {
1001 netdev_alert(priv->ndev, "%s: 0x%p: called by not receiver!\n",
1006 switch (session->last_cmd) {
1007 case J1939_TP_CMD_RTS:
1008 case J1939_ETP_CMD_RTS:
1009 ret = j1939_session_tx_cts(session);
1012 case J1939_ETP_CMD_CTS:
1013 case J1939_TP_CMD_CTS:
1014 case 0xff: /* did some data */
1015 case J1939_ETP_CMD_DPO:
1016 if ((session->skcb.addr.type == J1939_TP &&
1017 j1939_cb_is_broadcast(&session->skcb)))
1020 if (session->pkt.rx >= session->pkt.total) {
1021 ret = j1939_session_tx_eoma(session);
1022 } else if (session->pkt.rx >= session->pkt.last) {
1023 session->last_txcmd = 0;
1024 ret = j1939_session_tx_cts(session);
1028 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
1029 __func__, session, session->last_cmd);
1035 static int j1939_simple_txnext(struct j1939_session *session)
1037 struct j1939_priv *priv = session->priv;
1038 struct sk_buff *se_skb = j1939_session_skb_get(session);
1039 struct sk_buff *skb;
1045 skb = skb_clone(se_skb, GFP_ATOMIC);
1051 can_skb_set_owner(skb, se_skb->sk);
1053 j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS);
1055 ret = j1939_send_one(priv, skb);
1059 j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_SCHED);
1060 j1939_sk_queue_activate_next(session);
1066 consume_skb(se_skb);
1071 static bool j1939_session_deactivate_locked(struct j1939_session *session)
1073 bool active = false;
1075 lockdep_assert_held(&session->priv->active_session_list_lock);
1077 if (session->state >= J1939_SESSION_ACTIVE &&
1078 session->state < J1939_SESSION_ACTIVE_MAX) {
1081 list_del_init(&session->active_session_list_entry);
1082 session->state = J1939_SESSION_DONE;
1083 j1939_session_put(session);
1089 static bool j1939_session_deactivate(struct j1939_session *session)
1091 struct j1939_priv *priv = session->priv;
1094 j1939_session_list_lock(priv);
1095 active = j1939_session_deactivate_locked(session);
1096 j1939_session_list_unlock(priv);
1102 j1939_session_deactivate_activate_next(struct j1939_session *session)
1104 if (j1939_session_deactivate(session))
1105 j1939_sk_queue_activate_next(session);
1108 static void __j1939_session_cancel(struct j1939_session *session,
1109 enum j1939_xtp_abort err)
1111 struct j1939_priv *priv = session->priv;
1114 lockdep_assert_held(&session->priv->active_session_list_lock);
1116 session->err = j1939_xtp_abort_to_errno(priv, err);
1117 session->state = J1939_SESSION_WAITING_ABORT;
1118 /* do not send aborts on incoming broadcasts */
1119 if (!j1939_cb_is_broadcast(&session->skcb)) {
1120 j1939_xtp_tx_abort(priv, &session->skcb,
1121 !session->transmission,
1122 err, session->skcb.addr.pgn);
1126 j1939_sk_send_loop_abort(session->sk, session->err);
1129 static void j1939_session_cancel(struct j1939_session *session,
1130 enum j1939_xtp_abort err)
1132 j1939_session_list_lock(session->priv);
1134 if (session->state >= J1939_SESSION_ACTIVE &&
1135 session->state < J1939_SESSION_WAITING_ABORT) {
1136 j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS);
1137 __j1939_session_cancel(session, err);
1140 j1939_session_list_unlock(session->priv);
1143 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1146 static enum hrtimer_restart j1939_tp_txtimer(struct hrtimer *hrtimer)
1148 struct j1939_session *session =
1149 container_of(hrtimer, struct j1939_session, txtimer);
1150 struct j1939_priv *priv = session->priv;
1153 if (session->skcb.addr.type == J1939_SIMPLE) {
1154 ret = j1939_simple_txnext(session);
1156 if (session->transmission)
1157 ret = j1939_xtp_txnext_transmiter(session);
1159 ret = j1939_xtp_txnext_receiver(session);
1164 /* Retry limit is currently arbitrary chosen */
1165 if (session->tx_retry < J1939_XTP_TX_RETRY_LIMIT) {
1166 session->tx_retry++;
1167 j1939_tp_schedule_txtimer(session,
1168 10 + get_random_u32_below(16));
1170 netdev_alert(priv->ndev, "%s: 0x%p: tx retry count reached\n",
1172 session->err = -ENETUNREACH;
1173 j1939_session_rxtimer_cancel(session);
1174 j1939_session_deactivate_activate_next(session);
1178 /* In this case we should get a netdev_event(), all active
1179 * sessions will be cleared by
1180 * j1939_cancel_all_active_sessions(). So handle this as an
1181 * error, but let j1939_cancel_all_active_sessions() do the
1182 * cleanup including propagation of the error to user space.
1186 j1939_session_cancel(session, J1939_XTP_ABORT_ECTS_TOO_BIG);
1189 session->tx_retry = 0;
1192 netdev_alert(priv->ndev, "%s: 0x%p: tx aborted with unknown reason: %i\n",
1193 __func__, session, ret);
1194 if (session->skcb.addr.type != J1939_SIMPLE) {
1195 j1939_session_cancel(session, J1939_XTP_ABORT_OTHER);
1198 j1939_session_rxtimer_cancel(session);
1199 j1939_session_deactivate_activate_next(session);
1203 j1939_session_put(session);
1205 return HRTIMER_NORESTART;
1208 static void j1939_session_completed(struct j1939_session *session)
1210 struct sk_buff *se_skb;
1212 if (!session->transmission) {
1213 se_skb = j1939_session_skb_get(session);
1214 /* distribute among j1939 receivers */
1215 j1939_sk_recv(session->priv, se_skb);
1216 consume_skb(se_skb);
1219 j1939_session_deactivate_activate_next(session);
1222 static enum hrtimer_restart j1939_tp_rxtimer(struct hrtimer *hrtimer)
1224 struct j1939_session *session = container_of(hrtimer,
1225 struct j1939_session,
1227 struct j1939_priv *priv = session->priv;
1229 if (session->state == J1939_SESSION_WAITING_ABORT) {
1230 netdev_alert(priv->ndev, "%s: 0x%p: abort rx timeout. Force session deactivation\n",
1233 j1939_session_deactivate_activate_next(session);
1235 } else if (session->skcb.addr.type == J1939_SIMPLE) {
1236 netdev_alert(priv->ndev, "%s: 0x%p: Timeout. Failed to send simple message.\n",
1239 /* The message is probably stuck in the CAN controller and can
1240 * be send as soon as CAN bus is in working state again.
1242 session->err = -ETIME;
1243 j1939_session_deactivate(session);
1245 j1939_session_list_lock(session->priv);
1246 if (session->state >= J1939_SESSION_ACTIVE &&
1247 session->state < J1939_SESSION_ACTIVE_MAX) {
1248 netdev_alert(priv->ndev, "%s: 0x%p: rx timeout, send abort\n",
1250 j1939_session_get(session);
1251 hrtimer_start(&session->rxtimer,
1252 ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS),
1253 HRTIMER_MODE_REL_SOFT);
1254 __j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
1256 j1939_session_list_unlock(session->priv);
1259 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1262 j1939_session_put(session);
1264 return HRTIMER_NORESTART;
1267 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session *session,
1268 const struct sk_buff *skb)
1270 const struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1271 pgn_t pgn = j1939_xtp_ctl_to_pgn(skb->data);
1272 struct j1939_priv *priv = session->priv;
1273 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1274 u8 cmd = skb->data[0];
1276 if (session->skcb.addr.pgn == pgn)
1280 case J1939_TP_CMD_BAM:
1281 abort = J1939_XTP_NO_ABORT;
1284 case J1939_ETP_CMD_RTS:
1286 case J1939_TP_CMD_RTS:
1287 abort = J1939_XTP_ABORT_BUSY;
1290 case J1939_ETP_CMD_CTS:
1292 case J1939_TP_CMD_CTS:
1293 abort = J1939_XTP_ABORT_ECTS_UNXPECTED_PGN;
1296 case J1939_ETP_CMD_DPO:
1297 abort = J1939_XTP_ABORT_BAD_EDPO_PGN;
1300 case J1939_ETP_CMD_EOMA:
1302 case J1939_TP_CMD_EOMA:
1303 abort = J1939_XTP_ABORT_OTHER;
1306 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
1307 abort = J1939_XTP_NO_ABORT;
1315 netdev_warn(priv->ndev, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n",
1316 __func__, session, cmd, pgn, session->skcb.addr.pgn);
1317 if (abort != J1939_XTP_NO_ABORT)
1318 j1939_xtp_tx_abort(priv, skcb, true, abort, pgn);
1323 static void j1939_xtp_rx_abort_one(struct j1939_priv *priv, struct sk_buff *skb,
1324 bool reverse, bool transmitter)
1326 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1327 struct j1939_session *session;
1328 u8 abort = skb->data[1];
1330 session = j1939_session_get_by_addr(priv, &skcb->addr, reverse,
1335 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1338 netdev_info(priv->ndev, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__,
1339 session, j1939_xtp_ctl_to_pgn(skb->data), abort,
1340 j1939_xtp_abort_to_str(abort));
1342 j1939_session_timers_cancel(session);
1343 session->err = j1939_xtp_abort_to_errno(priv, abort);
1345 j1939_sk_send_loop_abort(session->sk, session->err);
1347 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1348 j1939_session_deactivate_activate_next(session);
1351 j1939_session_put(session);
1354 /* abort packets may come in 2 directions */
1356 j1939_xtp_rx_abort(struct j1939_priv *priv, struct sk_buff *skb,
1359 j1939_xtp_rx_abort_one(priv, skb, false, transmitter);
1360 j1939_xtp_rx_abort_one(priv, skb, true, transmitter);
1364 j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
1366 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1370 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1375 if (skcb->addr.type == J1939_ETP)
1376 len = j1939_etp_ctl_to_size(dat);
1378 len = j1939_tp_ctl_to_size(dat);
1380 if (session->total_message_size != len) {
1381 netdev_warn_once(session->priv->ndev,
1382 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
1383 __func__, session, session->total_message_size,
1387 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1389 session->pkt.tx_acked = session->pkt.total;
1390 j1939_session_timers_cancel(session);
1391 /* transmitted without problems */
1392 j1939_session_completed(session);
1396 j1939_xtp_rx_eoma(struct j1939_priv *priv, struct sk_buff *skb,
1399 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1400 struct j1939_session *session;
1402 session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1407 j1939_xtp_rx_eoma_one(session, skb);
1408 j1939_session_put(session);
1412 j1939_xtp_rx_cts_one(struct j1939_session *session, struct sk_buff *skb)
1414 enum j1939_xtp_abort err = J1939_XTP_ABORT_FAULT;
1420 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1423 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1425 if (session->last_cmd == dat[0]) {
1426 err = J1939_XTP_ABORT_DUP_SEQ;
1427 goto out_session_cancel;
1430 if (session->skcb.addr.type == J1939_ETP)
1431 pkt = j1939_etp_ctl_to_packet(dat);
1436 goto out_session_cancel;
1437 else if (dat[1] > session->pkt.block /* 0xff for etp */)
1438 goto out_session_cancel;
1440 /* set packet counters only when not CTS(0) */
1441 session->pkt.tx_acked = pkt - 1;
1442 j1939_session_skb_drop_old(session);
1443 session->pkt.last = session->pkt.tx_acked + dat[1];
1444 if (session->pkt.last > session->pkt.total)
1445 /* safety measure */
1446 session->pkt.last = session->pkt.total;
1447 /* TODO: do not set tx here, do it in txtimer */
1448 session->pkt.tx = session->pkt.tx_acked;
1450 session->last_cmd = dat[0];
1452 j1939_tp_set_rxtimeout(session, 1250);
1453 if (session->transmission) {
1454 if (session->pkt.tx_acked)
1455 j1939_sk_errqueue(session,
1456 J1939_ERRQUEUE_TX_SCHED);
1457 j1939_session_txtimer_cancel(session);
1458 j1939_tp_schedule_txtimer(session, 0);
1462 j1939_tp_set_rxtimeout(session, 550);
1467 j1939_session_timers_cancel(session);
1468 j1939_session_cancel(session, err);
1472 j1939_xtp_rx_cts(struct j1939_priv *priv, struct sk_buff *skb, bool transmitter)
1474 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1475 struct j1939_session *session;
1477 session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1481 j1939_xtp_rx_cts_one(session, skb);
1482 j1939_session_put(session);
1485 static struct j1939_session *j1939_session_new(struct j1939_priv *priv,
1486 struct sk_buff *skb, size_t size)
1488 struct j1939_session *session;
1489 struct j1939_sk_buff_cb *skcb;
1491 session = kzalloc(sizeof(*session), gfp_any());
1495 INIT_LIST_HEAD(&session->active_session_list_entry);
1496 INIT_LIST_HEAD(&session->sk_session_queue_entry);
1497 kref_init(&session->kref);
1499 j1939_priv_get(priv);
1500 session->priv = priv;
1501 session->total_message_size = size;
1502 session->state = J1939_SESSION_NEW;
1504 skb_queue_head_init(&session->skb_queue);
1505 skb_queue_tail(&session->skb_queue, skb);
1507 skcb = j1939_skb_to_cb(skb);
1508 memcpy(&session->skcb, skcb, sizeof(session->skcb));
1510 hrtimer_init(&session->txtimer, CLOCK_MONOTONIC,
1511 HRTIMER_MODE_REL_SOFT);
1512 session->txtimer.function = j1939_tp_txtimer;
1513 hrtimer_init(&session->rxtimer, CLOCK_MONOTONIC,
1514 HRTIMER_MODE_REL_SOFT);
1515 session->rxtimer.function = j1939_tp_rxtimer;
1517 netdev_dbg(priv->ndev, "%s: 0x%p: sa: %02x, da: %02x\n",
1518 __func__, session, skcb->addr.sa, skcb->addr.da);
1524 j1939_session *j1939_session_fresh_new(struct j1939_priv *priv,
1526 const struct j1939_sk_buff_cb *rel_skcb)
1528 struct sk_buff *skb;
1529 struct j1939_sk_buff_cb *skcb;
1530 struct j1939_session *session;
1532 skb = alloc_skb(size + sizeof(struct can_skb_priv), GFP_ATOMIC);
1536 skb->dev = priv->ndev;
1537 can_skb_reserve(skb);
1538 can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
1539 can_skb_prv(skb)->skbcnt = 0;
1540 skcb = j1939_skb_to_cb(skb);
1541 memcpy(skcb, rel_skcb, sizeof(*skcb));
1543 session = j1939_session_new(priv, skb, size);
1549 /* alloc data area */
1551 /* skb is recounted in j1939_session_new() */
1555 int j1939_session_activate(struct j1939_session *session)
1557 struct j1939_priv *priv = session->priv;
1558 struct j1939_session *active = NULL;
1561 j1939_session_list_lock(priv);
1562 if (session->skcb.addr.type != J1939_SIMPLE)
1563 active = j1939_session_get_by_addr_locked(priv,
1564 &priv->active_session_list,
1565 &session->skcb.addr, false,
1566 session->transmission);
1568 j1939_session_put(active);
1571 WARN_ON_ONCE(session->state != J1939_SESSION_NEW);
1572 list_add_tail(&session->active_session_list_entry,
1573 &priv->active_session_list);
1574 j1939_session_get(session);
1575 session->state = J1939_SESSION_ACTIVE;
1577 netdev_dbg(session->priv->ndev, "%s: 0x%p\n",
1580 j1939_session_list_unlock(priv);
1586 j1939_session *j1939_xtp_rx_rts_session_new(struct j1939_priv *priv,
1587 struct sk_buff *skb)
1589 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1590 struct j1939_sk_buff_cb skcb = *j1939_skb_to_cb(skb);
1591 struct j1939_session *session;
1596 netdev_dbg(priv->ndev, "%s\n", __func__);
1599 pgn = j1939_xtp_ctl_to_pgn(dat);
1600 skcb.addr.pgn = pgn;
1602 if (!j1939_sk_recv_match(priv, &skcb))
1605 if (skcb.addr.type == J1939_ETP) {
1606 len = j1939_etp_ctl_to_size(dat);
1607 if (len > J1939_MAX_ETP_PACKET_SIZE)
1608 abort = J1939_XTP_ABORT_FAULT;
1609 else if (len > priv->tp_max_packet_size)
1610 abort = J1939_XTP_ABORT_RESOURCE;
1611 else if (len <= J1939_MAX_TP_PACKET_SIZE)
1612 abort = J1939_XTP_ABORT_FAULT;
1614 len = j1939_tp_ctl_to_size(dat);
1615 if (len > J1939_MAX_TP_PACKET_SIZE)
1616 abort = J1939_XTP_ABORT_FAULT;
1617 else if (len > priv->tp_max_packet_size)
1618 abort = J1939_XTP_ABORT_RESOURCE;
1619 else if (len < J1939_MIN_TP_PACKET_SIZE)
1620 abort = J1939_XTP_ABORT_FAULT;
1623 if (abort != J1939_XTP_NO_ABORT) {
1624 j1939_xtp_tx_abort(priv, &skcb, true, abort, pgn);
1628 session = j1939_session_fresh_new(priv, len, &skcb);
1630 j1939_xtp_tx_abort(priv, &skcb, true,
1631 J1939_XTP_ABORT_RESOURCE, pgn);
1635 /* initialize the control buffer: plain copy */
1636 session->pkt.total = (len + 6) / 7;
1637 session->pkt.block = 0xff;
1638 if (skcb.addr.type != J1939_ETP) {
1639 if (dat[3] != session->pkt.total)
1640 netdev_alert(priv->ndev, "%s: 0x%p: strange total, %u != %u\n",
1641 __func__, session, session->pkt.total,
1643 session->pkt.total = dat[3];
1644 session->pkt.block = min(dat[3], dat[4]);
1647 session->pkt.rx = 0;
1648 session->pkt.tx = 0;
1650 session->tskey = priv->rx_tskey++;
1651 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_RTS);
1653 WARN_ON_ONCE(j1939_session_activate(session));
1658 static int j1939_xtp_rx_rts_session_active(struct j1939_session *session,
1659 struct sk_buff *skb)
1661 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1662 struct j1939_priv *priv = session->priv;
1664 if (!session->transmission) {
1665 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1668 /* RTS on active session */
1669 j1939_session_timers_cancel(session);
1670 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1673 if (session->last_cmd != 0) {
1674 /* we received a second rts on the same connection */
1675 netdev_alert(priv->ndev, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n",
1676 __func__, session, skcb->addr.sa, skcb->addr.da,
1679 j1939_session_timers_cancel(session);
1680 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1685 if (session->skcb.addr.sa != skcb->addr.sa ||
1686 session->skcb.addr.da != skcb->addr.da)
1687 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",
1689 session->skcb.addr.sa, skcb->addr.sa,
1690 session->skcb.addr.da, skcb->addr.da);
1691 /* make sure 'sa' & 'da' are correct !
1692 * They may be 'not filled in yet' for sending
1693 * skb's, since they did not pass the Address Claim ever.
1695 session->skcb.addr.sa = skcb->addr.sa;
1696 session->skcb.addr.da = skcb->addr.da;
1698 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1703 static void j1939_xtp_rx_rts(struct j1939_priv *priv, struct sk_buff *skb,
1706 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1707 struct j1939_session *session;
1708 u8 cmd = skb->data[0];
1710 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1715 /* If we're the transmitter and this function is called,
1716 * we received our own RTS. A session has already been
1719 * For some reasons however it might have been destroyed
1720 * already. So don't create a new one here (using
1721 * "j1939_xtp_rx_rts_session_new()") as this will be a
1724 * The reasons the session is already destroyed might
1726 * - user space closed socket was and the session was
1728 * - session was aborted due to external abort message
1732 session = j1939_xtp_rx_rts_session_new(priv, skb);
1734 if (cmd == J1939_TP_CMD_BAM && j1939_sk_recv_match(priv, skcb))
1735 netdev_info(priv->ndev, "%s: failed to create TP BAM session\n",
1740 if (j1939_xtp_rx_rts_session_active(session, skb)) {
1741 j1939_session_put(session);
1745 session->last_cmd = cmd;
1747 if (cmd == J1939_TP_CMD_BAM) {
1748 if (!session->transmission)
1749 j1939_tp_set_rxtimeout(session, 750);
1751 if (!session->transmission) {
1752 j1939_session_txtimer_cancel(session);
1753 j1939_tp_schedule_txtimer(session, 0);
1755 j1939_tp_set_rxtimeout(session, 1250);
1758 j1939_session_put(session);
1761 static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
1762 struct sk_buff *skb)
1764 const u8 *dat = skb->data;
1766 if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1769 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1771 /* transmitted without problems */
1772 session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
1773 session->last_cmd = dat[0];
1774 j1939_tp_set_rxtimeout(session, 750);
1776 if (!session->transmission)
1777 j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_DPO);
1780 static void j1939_xtp_rx_dpo(struct j1939_priv *priv, struct sk_buff *skb,
1783 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1784 struct j1939_session *session;
1786 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1789 netdev_info(priv->ndev,
1790 "%s: no connection found\n", __func__);
1794 j1939_xtp_rx_dpo_one(session, skb);
1795 j1939_session_put(session);
1798 static void j1939_xtp_rx_dat_one(struct j1939_session *session,
1799 struct sk_buff *skb)
1801 enum j1939_xtp_abort abort = J1939_XTP_ABORT_FAULT;
1802 struct j1939_priv *priv = session->priv;
1803 struct j1939_sk_buff_cb *skcb, *se_skcb;
1804 struct sk_buff *se_skb = NULL;
1810 bool remain = false;
1811 bool do_cts_eoma = false;
1814 skcb = j1939_skb_to_cb(skb);
1816 if (skb->len != 8) {
1817 /* makes no sense */
1818 abort = J1939_XTP_ABORT_UNEXPECTED_DATA;
1819 goto out_session_cancel;
1822 switch (session->last_cmd) {
1825 case J1939_ETP_CMD_DPO:
1826 if (skcb->addr.type == J1939_ETP)
1829 case J1939_TP_CMD_BAM:
1831 case J1939_TP_CMD_CTS:
1832 if (skcb->addr.type != J1939_ETP)
1836 netdev_info(priv->ndev, "%s: 0x%p: last %02x\n", __func__,
1837 session, session->last_cmd);
1838 goto out_session_cancel;
1841 packet = (dat[0] - 1 + session->pkt.dpo);
1842 if (packet > session->pkt.total ||
1843 (session->pkt.rx + 1) > session->pkt.total) {
1844 netdev_info(priv->ndev, "%s: 0x%p: should have been completed\n",
1846 goto out_session_cancel;
1849 se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
1851 netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
1853 goto out_session_cancel;
1856 se_skcb = j1939_skb_to_cb(se_skb);
1857 offset = packet * 7 - se_skcb->offset;
1858 nbytes = se_skb->len - offset;
1861 if (nbytes <= 0 || (nbytes + 1) > skb->len) {
1862 netdev_info(priv->ndev, "%s: 0x%p: nbytes %i, len %i\n",
1863 __func__, session, nbytes, skb->len);
1864 goto out_session_cancel;
1867 tpdat = se_skb->data;
1868 if (!session->transmission) {
1869 memcpy(&tpdat[offset], &dat[1], nbytes);
1873 err = memcmp(&tpdat[offset], &dat[1], nbytes);
1875 netdev_err_once(priv->ndev,
1876 "%s: 0x%p: Data of RX-looped back packet (%*ph) doesn't match TX data (%*ph)!\n",
1879 nbytes, &tpdat[offset]);
1882 if (packet == session->pkt.rx)
1885 if (se_skcb->addr.type != J1939_ETP &&
1886 j1939_cb_is_broadcast(&session->skcb)) {
1887 if (session->pkt.rx >= session->pkt.total)
1892 /* never final, an EOMA must follow */
1893 if (session->pkt.rx >= session->pkt.last)
1898 j1939_session_timers_cancel(session);
1899 j1939_session_completed(session);
1900 } else if (remain) {
1901 if (!session->transmission)
1902 j1939_tp_set_rxtimeout(session, 750);
1903 } else if (do_cts_eoma) {
1904 j1939_tp_set_rxtimeout(session, 1250);
1905 if (!session->transmission)
1906 j1939_tp_schedule_txtimer(session, 0);
1908 j1939_tp_set_rxtimeout(session, 750);
1910 session->last_cmd = 0xff;
1911 consume_skb(se_skb);
1912 j1939_session_put(session);
1918 j1939_session_timers_cancel(session);
1919 j1939_session_cancel(session, abort);
1920 j1939_session_put(session);
1923 static void j1939_xtp_rx_dat(struct j1939_priv *priv, struct sk_buff *skb)
1925 struct j1939_sk_buff_cb *skcb;
1926 struct j1939_session *session;
1928 skcb = j1939_skb_to_cb(skb);
1930 if (j1939_tp_im_transmitter(skcb)) {
1931 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1934 netdev_info(priv->ndev, "%s: no tx connection found\n",
1937 j1939_xtp_rx_dat_one(session, skb);
1940 if (j1939_tp_im_receiver(skcb)) {
1941 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1944 netdev_info(priv->ndev, "%s: no rx connection found\n",
1947 j1939_xtp_rx_dat_one(session, skb);
1950 if (j1939_cb_is_broadcast(skcb)) {
1951 session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1954 j1939_xtp_rx_dat_one(session, skb);
1958 /* j1939 main intf */
1959 struct j1939_session *j1939_tp_send(struct j1939_priv *priv,
1960 struct sk_buff *skb, size_t size)
1962 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1963 struct j1939_session *session;
1966 if (skcb->addr.pgn == J1939_TP_PGN_DAT ||
1967 skcb->addr.pgn == J1939_TP_PGN_CTL ||
1968 skcb->addr.pgn == J1939_ETP_PGN_DAT ||
1969 skcb->addr.pgn == J1939_ETP_PGN_CTL)
1970 /* avoid conflict */
1971 return ERR_PTR(-EDOM);
1973 if (size > priv->tp_max_packet_size)
1974 return ERR_PTR(-EMSGSIZE);
1977 skcb->addr.type = J1939_SIMPLE;
1978 else if (size > J1939_MAX_TP_PACKET_SIZE)
1979 skcb->addr.type = J1939_ETP;
1981 skcb->addr.type = J1939_TP;
1983 if (skcb->addr.type == J1939_ETP &&
1984 j1939_cb_is_broadcast(skcb))
1985 return ERR_PTR(-EDESTADDRREQ);
1987 /* fill in addresses from names */
1988 ret = j1939_ac_fixup(priv, skb);
1990 return ERR_PTR(ret);
1992 /* fix DST flags, it may be used there soon */
1993 if (j1939_address_is_unicast(skcb->addr.da) &&
1994 priv->ents[skcb->addr.da].nusers)
1995 skcb->flags |= J1939_ECU_LOCAL_DST;
1997 /* src is always local, I'm sending ... */
1998 skcb->flags |= J1939_ECU_LOCAL_SRC;
2000 /* prepare new session */
2001 session = j1939_session_new(priv, skb, size);
2003 return ERR_PTR(-ENOMEM);
2005 /* skb is recounted in j1939_session_new() */
2007 session->sk = skb->sk;
2008 session->transmission = true;
2009 session->pkt.total = (size + 6) / 7;
2010 session->pkt.block = skcb->addr.type == J1939_ETP ? 255 :
2011 min(j1939_tp_block ?: 255, session->pkt.total);
2013 if (j1939_cb_is_broadcast(&session->skcb))
2014 /* set the end-packet for broadcast */
2015 session->pkt.last = session->pkt.total;
2017 skcb->tskey = atomic_inc_return(&session->sk->sk_tskey) - 1;
2018 session->tskey = skcb->tskey;
2023 static void j1939_tp_cmd_recv(struct j1939_priv *priv, struct sk_buff *skb)
2025 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2026 int extd = J1939_TP;
2027 u8 cmd = skb->data[0];
2030 case J1939_ETP_CMD_RTS:
2033 case J1939_TP_CMD_BAM:
2034 if (cmd == J1939_TP_CMD_BAM && !j1939_cb_is_broadcast(skcb)) {
2035 netdev_err_once(priv->ndev, "%s: BAM to unicast (%02x), ignoring!\n",
2036 __func__, skcb->addr.sa);
2040 case J1939_TP_CMD_RTS:
2041 if (skcb->addr.type != extd)
2044 if (cmd == J1939_TP_CMD_RTS && j1939_cb_is_broadcast(skcb)) {
2045 netdev_alert(priv->ndev, "%s: rts without destination (%02x)\n",
2046 __func__, skcb->addr.sa);
2050 if (j1939_tp_im_transmitter(skcb))
2051 j1939_xtp_rx_rts(priv, skb, true);
2053 if (j1939_tp_im_receiver(skcb) || j1939_cb_is_broadcast(skcb))
2054 j1939_xtp_rx_rts(priv, skb, false);
2058 case J1939_ETP_CMD_CTS:
2061 case J1939_TP_CMD_CTS:
2062 if (skcb->addr.type != extd)
2065 if (j1939_tp_im_transmitter(skcb))
2066 j1939_xtp_rx_cts(priv, skb, false);
2068 if (j1939_tp_im_receiver(skcb))
2069 j1939_xtp_rx_cts(priv, skb, true);
2073 case J1939_ETP_CMD_DPO:
2074 if (skcb->addr.type != J1939_ETP)
2077 if (j1939_tp_im_transmitter(skcb))
2078 j1939_xtp_rx_dpo(priv, skb, true);
2080 if (j1939_tp_im_receiver(skcb))
2081 j1939_xtp_rx_dpo(priv, skb, false);
2085 case J1939_ETP_CMD_EOMA:
2088 case J1939_TP_CMD_EOMA:
2089 if (skcb->addr.type != extd)
2092 if (j1939_tp_im_transmitter(skcb))
2093 j1939_xtp_rx_eoma(priv, skb, false);
2095 if (j1939_tp_im_receiver(skcb))
2096 j1939_xtp_rx_eoma(priv, skb, true);
2100 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
2101 if (j1939_cb_is_broadcast(skcb)) {
2102 netdev_err_once(priv->ndev, "%s: abort to broadcast (%02x), ignoring!\n",
2103 __func__, skcb->addr.sa);
2107 if (j1939_tp_im_transmitter(skcb))
2108 j1939_xtp_rx_abort(priv, skb, true);
2110 if (j1939_tp_im_receiver(skcb))
2111 j1939_xtp_rx_abort(priv, skb, false);
2119 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb)
2121 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2123 if (!j1939_tp_im_involved_anydir(skcb) && !j1939_cb_is_broadcast(skcb))
2126 switch (skcb->addr.pgn) {
2127 case J1939_ETP_PGN_DAT:
2128 skcb->addr.type = J1939_ETP;
2130 case J1939_TP_PGN_DAT:
2131 j1939_xtp_rx_dat(priv, skb);
2134 case J1939_ETP_PGN_CTL:
2135 skcb->addr.type = J1939_ETP;
2137 case J1939_TP_PGN_CTL:
2139 return 0; /* Don't care. Nothing to extract here */
2141 j1939_tp_cmd_recv(priv, skb);
2144 return 0; /* no problem */
2146 return 1; /* "I processed the message" */
2149 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb)
2151 struct j1939_session *session;
2156 if (skb->sk->sk_family != AF_CAN ||
2157 skb->sk->sk_protocol != CAN_J1939)
2160 j1939_session_list_lock(priv);
2161 session = j1939_session_get_simple(priv, skb);
2162 j1939_session_list_unlock(priv);
2164 netdev_warn(priv->ndev,
2165 "%s: Received already invalidated message\n",
2170 j1939_session_timers_cancel(session);
2171 j1939_session_deactivate(session);
2172 j1939_session_put(session);
2175 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk)
2177 struct j1939_session *session, *saved;
2179 netdev_dbg(priv->ndev, "%s, sk: %p\n", __func__, sk);
2180 j1939_session_list_lock(priv);
2181 list_for_each_entry_safe(session, saved,
2182 &priv->active_session_list,
2183 active_session_list_entry) {
2184 if (!sk || sk == session->sk) {
2185 if (hrtimer_try_to_cancel(&session->txtimer) == 1)
2186 j1939_session_put(session);
2187 if (hrtimer_try_to_cancel(&session->rxtimer) == 1)
2188 j1939_session_put(session);
2190 session->err = ESHUTDOWN;
2191 j1939_session_deactivate_locked(session);
2194 j1939_session_list_unlock(priv);
2198 void j1939_tp_init(struct j1939_priv *priv)
2200 spin_lock_init(&priv->active_session_list_lock);
2201 INIT_LIST_HEAD(&priv->active_session_list);
2202 priv->tp_max_packet_size = J1939_MAX_ETP_PACKET_SIZE;