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
)
264 j1939_sk_errqueue(session
, J1939_ERRQUEUE_ABORT
);
266 j1939_sk_errqueue(session
, J1939_ERRQUEUE_ACK
);
268 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
270 WARN_ON_ONCE(!list_empty(&session
->sk_session_queue_entry
));
271 WARN_ON_ONCE(!list_empty(&session
->active_session_list_entry
));
273 skb_queue_purge(&session
->skb_queue
);
274 __j1939_session_drop(session
);
275 j1939_priv_put(session
->priv
);
279 static void __j1939_session_release(struct kref
*kref
)
281 struct j1939_session
*session
= container_of(kref
, struct j1939_session
,
284 j1939_session_destroy(session
);
287 void j1939_session_put(struct j1939_session
*session
)
289 kref_put(&session
->kref
, __j1939_session_release
);
292 static void j1939_session_txtimer_cancel(struct j1939_session
*session
)
294 if (hrtimer_cancel(&session
->txtimer
))
295 j1939_session_put(session
);
298 static void j1939_session_rxtimer_cancel(struct j1939_session
*session
)
300 if (hrtimer_cancel(&session
->rxtimer
))
301 j1939_session_put(session
);
304 void j1939_session_timers_cancel(struct j1939_session
*session
)
306 j1939_session_txtimer_cancel(session
);
307 j1939_session_rxtimer_cancel(session
);
310 static inline bool j1939_cb_is_broadcast(const struct j1939_sk_buff_cb
*skcb
)
312 return (!skcb
->addr
.dst_name
&& (skcb
->addr
.da
== 0xff));
315 static void j1939_session_skb_drop_old(struct j1939_session
*session
)
317 struct sk_buff
*do_skb
;
318 struct j1939_sk_buff_cb
*do_skcb
;
319 unsigned int offset_start
;
322 if (skb_queue_len(&session
->skb_queue
) < 2)
325 offset_start
= session
->pkt
.tx_acked
* 7;
327 spin_lock_irqsave(&session
->skb_queue
.lock
, flags
);
328 do_skb
= skb_peek(&session
->skb_queue
);
329 do_skcb
= j1939_skb_to_cb(do_skb
);
331 if ((do_skcb
->offset
+ do_skb
->len
) < offset_start
) {
332 __skb_unlink(do_skb
, &session
->skb_queue
);
335 spin_unlock_irqrestore(&session
->skb_queue
.lock
, flags
);
338 void j1939_session_skb_queue(struct j1939_session
*session
,
341 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
342 struct j1939_priv
*priv
= session
->priv
;
344 j1939_ac_fixup(priv
, skb
);
346 if (j1939_address_is_unicast(skcb
->addr
.da
) &&
347 priv
->ents
[skcb
->addr
.da
].nusers
)
348 skcb
->flags
|= J1939_ECU_LOCAL_DST
;
350 skcb
->flags
|= J1939_ECU_LOCAL_SRC
;
352 skb_queue_tail(&session
->skb_queue
, skb
);
355 static struct sk_buff
*j1939_session_skb_find(struct j1939_session
*session
)
357 struct j1939_priv
*priv
= session
->priv
;
358 struct sk_buff
*skb
= NULL
;
359 struct sk_buff
*do_skb
;
360 struct j1939_sk_buff_cb
*do_skcb
;
361 unsigned int offset_start
;
364 offset_start
= session
->pkt
.dpo
* 7;
366 spin_lock_irqsave(&session
->skb_queue
.lock
, flags
);
367 skb_queue_walk(&session
->skb_queue
, do_skb
) {
368 do_skcb
= j1939_skb_to_cb(do_skb
);
370 if (offset_start
>= do_skcb
->offset
&&
371 offset_start
< (do_skcb
->offset
+ do_skb
->len
)) {
375 spin_unlock_irqrestore(&session
->skb_queue
.lock
, flags
);
378 netdev_dbg(priv
->ndev
, "%s: 0x%p: no skb found for start: %i, queue size: %i\n",
379 __func__
, session
, offset_start
,
380 skb_queue_len(&session
->skb_queue
));
385 /* see if we are receiver
386 * returns 0 for broadcasts, although we will receive them
388 static inline int j1939_tp_im_receiver(const struct j1939_sk_buff_cb
*skcb
)
390 return skcb
->flags
& J1939_ECU_LOCAL_DST
;
393 /* see if we are sender */
394 static inline int j1939_tp_im_transmitter(const struct j1939_sk_buff_cb
*skcb
)
396 return skcb
->flags
& J1939_ECU_LOCAL_SRC
;
399 /* see if we are involved as either receiver or transmitter */
400 static int j1939_tp_im_involved(const struct j1939_sk_buff_cb
*skcb
, bool swap
)
403 return j1939_tp_im_receiver(skcb
);
405 return j1939_tp_im_transmitter(skcb
);
408 static int j1939_tp_im_involved_anydir(struct j1939_sk_buff_cb
*skcb
)
410 return skcb
->flags
& (J1939_ECU_LOCAL_SRC
| J1939_ECU_LOCAL_DST
);
413 /* extract pgn from flow-ctl message */
414 static inline pgn_t
j1939_xtp_ctl_to_pgn(const u8
*dat
)
418 pgn
= (dat
[7] << 16) | (dat
[6] << 8) | (dat
[5] << 0);
419 if (j1939_pgn_is_pdu1(pgn
))
424 static inline unsigned int j1939_tp_ctl_to_size(const u8
*dat
)
426 return (dat
[2] << 8) + (dat
[1] << 0);
429 static inline unsigned int j1939_etp_ctl_to_packet(const u8
*dat
)
431 return (dat
[4] << 16) | (dat
[3] << 8) | (dat
[2] << 0);
434 static inline unsigned int j1939_etp_ctl_to_size(const u8
*dat
)
436 return (dat
[4] << 24) | (dat
[3] << 16) |
437 (dat
[2] << 8) | (dat
[1] << 0);
440 /* find existing session:
441 * reverse: swap cb's src & dst
442 * there is no problem with matching broadcasts, since
443 * broadcasts (no dst, no da) would never call this
444 * with reverse == true
446 static bool j1939_session_match(struct j1939_addr
*se_addr
,
447 struct j1939_addr
*sk_addr
, bool reverse
)
449 if (se_addr
->type
!= sk_addr
->type
)
453 if (se_addr
->src_name
) {
454 if (se_addr
->src_name
!= sk_addr
->dst_name
)
456 } else if (se_addr
->sa
!= sk_addr
->da
) {
460 if (se_addr
->dst_name
) {
461 if (se_addr
->dst_name
!= sk_addr
->src_name
)
463 } else if (se_addr
->da
!= sk_addr
->sa
) {
467 if (se_addr
->src_name
) {
468 if (se_addr
->src_name
!= sk_addr
->src_name
)
470 } else if (se_addr
->sa
!= sk_addr
->sa
) {
474 if (se_addr
->dst_name
) {
475 if (se_addr
->dst_name
!= sk_addr
->dst_name
)
477 } else if (se_addr
->da
!= sk_addr
->da
) {
486 j1939_session
*j1939_session_get_by_addr_locked(struct j1939_priv
*priv
,
487 struct list_head
*root
,
488 struct j1939_addr
*addr
,
489 bool reverse
, bool transmitter
)
491 struct j1939_session
*session
;
493 lockdep_assert_held(&priv
->active_session_list_lock
);
495 list_for_each_entry(session
, root
, active_session_list_entry
) {
496 j1939_session_get(session
);
497 if (j1939_session_match(&session
->skcb
.addr
, addr
, reverse
) &&
498 session
->transmission
== transmitter
)
500 j1939_session_put(session
);
507 j1939_session
*j1939_session_get_simple(struct j1939_priv
*priv
,
510 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
511 struct j1939_session
*session
;
513 lockdep_assert_held(&priv
->active_session_list_lock
);
515 list_for_each_entry(session
, &priv
->active_session_list
,
516 active_session_list_entry
) {
517 j1939_session_get(session
);
518 if (session
->skcb
.addr
.type
== J1939_SIMPLE
&&
519 session
->tskey
== skcb
->tskey
&& session
->sk
== skb
->sk
)
521 j1939_session_put(session
);
528 j1939_session
*j1939_session_get_by_addr(struct j1939_priv
*priv
,
529 struct j1939_addr
*addr
,
530 bool reverse
, bool transmitter
)
532 struct j1939_session
*session
;
534 j1939_session_list_lock(priv
);
535 session
= j1939_session_get_by_addr_locked(priv
,
536 &priv
->active_session_list
,
537 addr
, reverse
, transmitter
);
538 j1939_session_list_unlock(priv
);
543 static void j1939_skbcb_swap(struct j1939_sk_buff_cb
*skcb
)
547 swap(skcb
->addr
.dst_name
, skcb
->addr
.src_name
);
548 swap(skcb
->addr
.da
, skcb
->addr
.sa
);
550 /* swap SRC and DST flags, leave other untouched */
551 if (skcb
->flags
& J1939_ECU_LOCAL_SRC
)
552 tmp
|= J1939_ECU_LOCAL_DST
;
553 if (skcb
->flags
& J1939_ECU_LOCAL_DST
)
554 tmp
|= J1939_ECU_LOCAL_SRC
;
555 skcb
->flags
&= ~(J1939_ECU_LOCAL_SRC
| J1939_ECU_LOCAL_DST
);
560 sk_buff
*j1939_tp_tx_dat_new(struct j1939_priv
*priv
,
561 const struct j1939_sk_buff_cb
*re_skcb
,
566 struct j1939_sk_buff_cb
*skcb
;
568 skb
= alloc_skb(sizeof(struct can_frame
) + sizeof(struct can_skb_priv
),
571 return ERR_PTR(-ENOMEM
);
573 skb
->dev
= priv
->ndev
;
574 can_skb_reserve(skb
);
575 can_skb_prv(skb
)->ifindex
= priv
->ndev
->ifindex
;
576 /* reserve CAN header */
577 skb_reserve(skb
, offsetof(struct can_frame
, data
));
579 memcpy(skb
->cb
, re_skcb
, sizeof(skb
->cb
));
580 skcb
= j1939_skb_to_cb(skb
);
582 j1939_skbcb_swap(skcb
);
585 if (skcb
->addr
.type
== J1939_ETP
)
586 skcb
->addr
.pgn
= J1939_ETP_PGN_CTL
;
588 skcb
->addr
.pgn
= J1939_TP_PGN_CTL
;
590 if (skcb
->addr
.type
== J1939_ETP
)
591 skcb
->addr
.pgn
= J1939_ETP_PGN_DAT
;
593 skcb
->addr
.pgn
= J1939_TP_PGN_DAT
;
599 /* TP transmit packet functions */
600 static int j1939_tp_tx_dat(struct j1939_session
*session
,
601 const u8
*dat
, int len
)
603 struct j1939_priv
*priv
= session
->priv
;
606 skb
= j1939_tp_tx_dat_new(priv
, &session
->skcb
,
611 skb_put_data(skb
, dat
, len
);
612 if (j1939_tp_padding
&& len
< 8)
613 memset(skb_put(skb
, 8 - len
), 0xff, 8 - len
);
615 return j1939_send_one(priv
, skb
);
618 static int j1939_xtp_do_tx_ctl(struct j1939_priv
*priv
,
619 const struct j1939_sk_buff_cb
*re_skcb
,
620 bool swap_src_dst
, pgn_t pgn
, const u8
*dat
)
625 if (!j1939_tp_im_involved(re_skcb
, swap_src_dst
))
628 skb
= j1939_tp_tx_dat_new(priv
, re_skcb
, true, swap_src_dst
);
632 skdat
= skb_put(skb
, 8);
633 memcpy(skdat
, dat
, 5);
634 skdat
[5] = (pgn
>> 0);
635 skdat
[6] = (pgn
>> 8);
636 skdat
[7] = (pgn
>> 16);
638 return j1939_send_one(priv
, skb
);
641 static inline int j1939_tp_tx_ctl(struct j1939_session
*session
,
642 bool swap_src_dst
, const u8
*dat
)
644 struct j1939_priv
*priv
= session
->priv
;
646 return j1939_xtp_do_tx_ctl(priv
, &session
->skcb
,
648 session
->skcb
.addr
.pgn
, dat
);
651 static int j1939_xtp_tx_abort(struct j1939_priv
*priv
,
652 const struct j1939_sk_buff_cb
*re_skcb
,
654 enum j1939_xtp_abort err
,
659 if (!j1939_tp_im_involved(re_skcb
, swap_src_dst
))
662 memset(dat
, 0xff, sizeof(dat
));
663 dat
[0] = J1939_TP_CMD_ABORT
;
665 return j1939_xtp_do_tx_ctl(priv
, re_skcb
, swap_src_dst
, pgn
, dat
);
668 void j1939_tp_schedule_txtimer(struct j1939_session
*session
, int msec
)
670 j1939_session_get(session
);
671 hrtimer_start(&session
->txtimer
, ms_to_ktime(msec
),
672 HRTIMER_MODE_REL_SOFT
);
675 static inline void j1939_tp_set_rxtimeout(struct j1939_session
*session
,
678 j1939_session_rxtimer_cancel(session
);
679 j1939_session_get(session
);
680 hrtimer_start(&session
->rxtimer
, ms_to_ktime(msec
),
681 HRTIMER_MODE_REL_SOFT
);
684 static int j1939_session_tx_rts(struct j1939_session
*session
)
689 memset(dat
, 0xff, sizeof(dat
));
691 dat
[1] = (session
->total_message_size
>> 0);
692 dat
[2] = (session
->total_message_size
>> 8);
693 dat
[3] = session
->pkt
.total
;
695 if (session
->skcb
.addr
.type
== J1939_ETP
) {
696 dat
[0] = J1939_ETP_CMD_RTS
;
697 dat
[1] = (session
->total_message_size
>> 0);
698 dat
[2] = (session
->total_message_size
>> 8);
699 dat
[3] = (session
->total_message_size
>> 16);
700 dat
[4] = (session
->total_message_size
>> 24);
701 } else if (j1939_cb_is_broadcast(&session
->skcb
)) {
702 dat
[0] = J1939_TP_CMD_BAM
;
703 /* fake cts for broadcast */
706 dat
[0] = J1939_TP_CMD_RTS
;
710 if (dat
[0] == session
->last_txcmd
)
714 ret
= j1939_tp_tx_ctl(session
, false, dat
);
718 session
->last_txcmd
= dat
[0];
719 if (dat
[0] == J1939_TP_CMD_BAM
)
720 j1939_tp_schedule_txtimer(session
, 50);
722 j1939_tp_set_rxtimeout(session
, 1250);
724 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
729 static int j1939_session_tx_dpo(struct j1939_session
*session
)
735 memset(dat
, 0xff, sizeof(dat
));
737 dat
[0] = J1939_ETP_CMD_DPO
;
738 session
->pkt
.dpo
= session
->pkt
.tx_acked
;
739 pkt
= session
->pkt
.dpo
;
740 dat
[1] = session
->pkt
.last
- session
->pkt
.tx_acked
;
743 dat
[4] = (pkt
>> 16);
745 ret
= j1939_tp_tx_ctl(session
, false, dat
);
749 session
->last_txcmd
= dat
[0];
750 j1939_tp_set_rxtimeout(session
, 1250);
751 session
->pkt
.tx
= session
->pkt
.tx_acked
;
753 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
758 static int j1939_session_tx_dat(struct j1939_session
*session
)
760 struct j1939_priv
*priv
= session
->priv
;
761 struct j1939_sk_buff_cb
*skcb
;
762 int offset
, pkt_done
, pkt_end
;
763 unsigned int len
, pdelay
;
764 struct sk_buff
*se_skb
;
769 se_skb
= j1939_session_skb_find(session
);
773 skcb
= j1939_skb_to_cb(se_skb
);
774 tpdat
= se_skb
->data
;
777 if (session
->skcb
.addr
.type
!= J1939_ETP
&&
778 j1939_cb_is_broadcast(&session
->skcb
))
779 pkt_end
= session
->pkt
.total
;
781 pkt_end
= session
->pkt
.last
;
783 while (session
->pkt
.tx
< pkt_end
) {
784 dat
[0] = session
->pkt
.tx
- session
->pkt
.dpo
+ 1;
785 offset
= (session
->pkt
.tx
* 7) - skcb
->offset
;
786 len
= se_skb
->len
- offset
;
790 memcpy(&dat
[1], &tpdat
[offset
], len
);
791 ret
= j1939_tp_tx_dat(session
, dat
, len
+ 1);
793 /* ENOBUS == CAN interface TX queue is full */
795 netdev_alert(priv
->ndev
,
796 "%s: 0x%p: queue data error: %i\n",
797 __func__
, session
, ret
);
801 session
->last_txcmd
= 0xff;
804 pdelay
= j1939_cb_is_broadcast(&session
->skcb
) ? 50 :
805 j1939_tp_packet_delay
;
807 if (session
->pkt
.tx
< session
->pkt
.total
&& pdelay
) {
808 j1939_tp_schedule_txtimer(session
, pdelay
);
814 j1939_tp_set_rxtimeout(session
, 250);
819 static int j1939_xtp_txnext_transmiter(struct j1939_session
*session
)
821 struct j1939_priv
*priv
= session
->priv
;
824 if (!j1939_tp_im_transmitter(&session
->skcb
)) {
825 netdev_alert(priv
->ndev
, "%s: 0x%p: called by not transmitter!\n",
830 switch (session
->last_cmd
) {
832 ret
= j1939_session_tx_rts(session
);
835 case J1939_ETP_CMD_CTS
:
836 if (session
->last_txcmd
!= J1939_ETP_CMD_DPO
) {
837 ret
= j1939_session_tx_dpo(session
);
843 case J1939_TP_CMD_CTS
:
844 case 0xff: /* did some data */
845 case J1939_ETP_CMD_DPO
:
846 case J1939_TP_CMD_BAM
:
847 ret
= j1939_session_tx_dat(session
);
851 netdev_alert(priv
->ndev
, "%s: 0x%p: unexpected last_cmd: %x\n",
852 __func__
, session
, session
->last_cmd
);
858 static int j1939_session_tx_cts(struct j1939_session
*session
)
860 struct j1939_priv
*priv
= session
->priv
;
861 unsigned int pkt
, len
;
865 if (!j1939_sk_recv_match(priv
, &session
->skcb
))
868 len
= session
->pkt
.total
- session
->pkt
.rx
;
869 len
= min3(len
, session
->pkt
.block
, j1939_tp_block
?: 255);
870 memset(dat
, 0xff, sizeof(dat
));
872 if (session
->skcb
.addr
.type
== J1939_ETP
) {
873 pkt
= session
->pkt
.rx
+ 1;
874 dat
[0] = J1939_ETP_CMD_CTS
;
878 dat
[4] = (pkt
>> 16);
880 dat
[0] = J1939_TP_CMD_CTS
;
882 dat
[2] = session
->pkt
.rx
+ 1;
885 if (dat
[0] == session
->last_txcmd
)
889 ret
= j1939_tp_tx_ctl(session
, true, dat
);
894 /* only mark cts done when len is set */
895 session
->last_txcmd
= dat
[0];
896 j1939_tp_set_rxtimeout(session
, 1250);
898 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
903 static int j1939_session_tx_eoma(struct j1939_session
*session
)
905 struct j1939_priv
*priv
= session
->priv
;
909 if (!j1939_sk_recv_match(priv
, &session
->skcb
))
912 memset(dat
, 0xff, sizeof(dat
));
914 if (session
->skcb
.addr
.type
== J1939_ETP
) {
915 dat
[0] = J1939_ETP_CMD_EOMA
;
916 dat
[1] = session
->total_message_size
>> 0;
917 dat
[2] = session
->total_message_size
>> 8;
918 dat
[3] = session
->total_message_size
>> 16;
919 dat
[4] = session
->total_message_size
>> 24;
921 dat
[0] = J1939_TP_CMD_EOMA
;
922 dat
[1] = session
->total_message_size
;
923 dat
[2] = session
->total_message_size
>> 8;
924 dat
[3] = session
->pkt
.total
;
927 if (dat
[0] == session
->last_txcmd
)
931 ret
= j1939_tp_tx_ctl(session
, true, dat
);
935 session
->last_txcmd
= dat
[0];
937 /* wait for the EOMA packet to come in */
938 j1939_tp_set_rxtimeout(session
, 1250);
940 netdev_dbg(session
->priv
->ndev
, "%p: 0x%p\n", __func__
, session
);
945 static int j1939_xtp_txnext_receiver(struct j1939_session
*session
)
947 struct j1939_priv
*priv
= session
->priv
;
950 if (!j1939_tp_im_receiver(&session
->skcb
)) {
951 netdev_alert(priv
->ndev
, "%s: 0x%p: called by not receiver!\n",
956 switch (session
->last_cmd
) {
957 case J1939_TP_CMD_RTS
:
958 case J1939_ETP_CMD_RTS
:
959 ret
= j1939_session_tx_cts(session
);
962 case J1939_ETP_CMD_CTS
:
963 case J1939_TP_CMD_CTS
:
964 case 0xff: /* did some data */
965 case J1939_ETP_CMD_DPO
:
966 if ((session
->skcb
.addr
.type
== J1939_TP
&&
967 j1939_cb_is_broadcast(&session
->skcb
)))
970 if (session
->pkt
.rx
>= session
->pkt
.total
) {
971 ret
= j1939_session_tx_eoma(session
);
972 } else if (session
->pkt
.rx
>= session
->pkt
.last
) {
973 session
->last_txcmd
= 0;
974 ret
= j1939_session_tx_cts(session
);
978 netdev_alert(priv
->ndev
, "%s: 0x%p: unexpected last_cmd: %x\n",
979 __func__
, session
, session
->last_cmd
);
985 static int j1939_simple_txnext(struct j1939_session
*session
)
987 struct j1939_priv
*priv
= session
->priv
;
988 struct sk_buff
*se_skb
= j1939_session_skb_find(session
);
995 skb
= skb_clone(se_skb
, GFP_ATOMIC
);
999 can_skb_set_owner(skb
, se_skb
->sk
);
1001 j1939_tp_set_rxtimeout(session
, J1939_SIMPLE_ECHO_TIMEOUT_MS
);
1003 ret
= j1939_send_one(priv
, skb
);
1007 j1939_sk_errqueue(session
, J1939_ERRQUEUE_SCHED
);
1008 j1939_sk_queue_activate_next(session
);
1013 static bool j1939_session_deactivate_locked(struct j1939_session
*session
)
1015 bool active
= false;
1017 lockdep_assert_held(&session
->priv
->active_session_list_lock
);
1019 if (session
->state
>= J1939_SESSION_ACTIVE
&&
1020 session
->state
< J1939_SESSION_ACTIVE_MAX
) {
1023 list_del_init(&session
->active_session_list_entry
);
1024 session
->state
= J1939_SESSION_DONE
;
1025 j1939_session_put(session
);
1031 static bool j1939_session_deactivate(struct j1939_session
*session
)
1035 j1939_session_list_lock(session
->priv
);
1036 active
= j1939_session_deactivate_locked(session
);
1037 j1939_session_list_unlock(session
->priv
);
1043 j1939_session_deactivate_activate_next(struct j1939_session
*session
)
1045 if (j1939_session_deactivate(session
))
1046 j1939_sk_queue_activate_next(session
);
1049 static void __j1939_session_cancel(struct j1939_session
*session
,
1050 enum j1939_xtp_abort err
)
1052 struct j1939_priv
*priv
= session
->priv
;
1055 lockdep_assert_held(&session
->priv
->active_session_list_lock
);
1057 session
->err
= j1939_xtp_abort_to_errno(priv
, err
);
1058 /* do not send aborts on incoming broadcasts */
1059 if (!j1939_cb_is_broadcast(&session
->skcb
)) {
1060 session
->state
= J1939_SESSION_WAITING_ABORT
;
1061 j1939_xtp_tx_abort(priv
, &session
->skcb
,
1062 !session
->transmission
,
1063 err
, session
->skcb
.addr
.pgn
);
1067 j1939_sk_send_loop_abort(session
->sk
, session
->err
);
1070 static void j1939_session_cancel(struct j1939_session
*session
,
1071 enum j1939_xtp_abort err
)
1073 j1939_session_list_lock(session
->priv
);
1075 if (session
->state
>= J1939_SESSION_ACTIVE
&&
1076 session
->state
< J1939_SESSION_WAITING_ABORT
) {
1077 j1939_tp_set_rxtimeout(session
, J1939_XTP_ABORT_TIMEOUT_MS
);
1078 __j1939_session_cancel(session
, err
);
1081 j1939_session_list_unlock(session
->priv
);
1084 static enum hrtimer_restart
j1939_tp_txtimer(struct hrtimer
*hrtimer
)
1086 struct j1939_session
*session
=
1087 container_of(hrtimer
, struct j1939_session
, txtimer
);
1088 struct j1939_priv
*priv
= session
->priv
;
1091 if (session
->skcb
.addr
.type
== J1939_SIMPLE
) {
1092 ret
= j1939_simple_txnext(session
);
1094 if (session
->transmission
)
1095 ret
= j1939_xtp_txnext_transmiter(session
);
1097 ret
= j1939_xtp_txnext_receiver(session
);
1102 /* Retry limit is currently arbitrary chosen */
1103 if (session
->tx_retry
< J1939_XTP_TX_RETRY_LIMIT
) {
1104 session
->tx_retry
++;
1105 j1939_tp_schedule_txtimer(session
,
1106 10 + prandom_u32_max(16));
1108 netdev_alert(priv
->ndev
, "%s: 0x%p: tx retry count reached\n",
1110 session
->err
= -ENETUNREACH
;
1111 j1939_session_rxtimer_cancel(session
);
1112 j1939_session_deactivate_activate_next(session
);
1116 /* In this case we should get a netdev_event(), all active
1117 * sessions will be cleared by
1118 * j1939_cancel_all_active_sessions(). So handle this as an
1119 * error, but let j1939_cancel_all_active_sessions() do the
1120 * cleanup including propagation of the error to user space.
1124 session
->tx_retry
= 0;
1127 netdev_alert(priv
->ndev
, "%s: 0x%p: tx aborted with unknown reason: %i\n",
1128 __func__
, session
, ret
);
1129 if (session
->skcb
.addr
.type
!= J1939_SIMPLE
) {
1130 j1939_session_cancel(session
, J1939_XTP_ABORT_OTHER
);
1133 j1939_session_rxtimer_cancel(session
);
1134 j1939_session_deactivate_activate_next(session
);
1138 j1939_session_put(session
);
1140 return HRTIMER_NORESTART
;
1143 static void j1939_session_completed(struct j1939_session
*session
)
1145 struct sk_buff
*skb
;
1147 if (!session
->transmission
) {
1148 skb
= j1939_session_skb_find(session
);
1149 /* distribute among j1939 receivers */
1150 j1939_sk_recv(session
->priv
, skb
);
1153 j1939_session_deactivate_activate_next(session
);
1156 static enum hrtimer_restart
j1939_tp_rxtimer(struct hrtimer
*hrtimer
)
1158 struct j1939_session
*session
= container_of(hrtimer
,
1159 struct j1939_session
,
1161 struct j1939_priv
*priv
= session
->priv
;
1163 if (session
->state
== J1939_SESSION_WAITING_ABORT
) {
1164 netdev_alert(priv
->ndev
, "%s: 0x%p: abort rx timeout. Force session deactivation\n",
1167 j1939_session_deactivate_activate_next(session
);
1169 } else if (session
->skcb
.addr
.type
== J1939_SIMPLE
) {
1170 netdev_alert(priv
->ndev
, "%s: 0x%p: Timeout. Failed to send simple message.\n",
1173 /* The message is probably stuck in the CAN controller and can
1174 * be send as soon as CAN bus is in working state again.
1176 session
->err
= -ETIME
;
1177 j1939_session_deactivate(session
);
1179 netdev_alert(priv
->ndev
, "%s: 0x%p: rx timeout, send abort\n",
1182 j1939_session_list_lock(session
->priv
);
1183 if (session
->state
>= J1939_SESSION_ACTIVE
&&
1184 session
->state
< J1939_SESSION_ACTIVE_MAX
) {
1185 j1939_session_get(session
);
1186 hrtimer_start(&session
->rxtimer
,
1187 ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS
),
1188 HRTIMER_MODE_REL_SOFT
);
1189 __j1939_session_cancel(session
, J1939_XTP_ABORT_TIMEOUT
);
1191 j1939_session_list_unlock(session
->priv
);
1194 j1939_session_put(session
);
1196 return HRTIMER_NORESTART
;
1199 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session
*session
,
1200 const struct sk_buff
*skb
)
1202 const struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1203 pgn_t pgn
= j1939_xtp_ctl_to_pgn(skb
->data
);
1204 struct j1939_priv
*priv
= session
->priv
;
1205 enum j1939_xtp_abort abort
= J1939_XTP_NO_ABORT
;
1206 u8 cmd
= skb
->data
[0];
1208 if (session
->skcb
.addr
.pgn
== pgn
)
1212 case J1939_TP_CMD_BAM
:
1213 abort
= J1939_XTP_NO_ABORT
;
1216 case J1939_ETP_CMD_RTS
:
1217 case J1939_TP_CMD_RTS
: /* fall through */
1218 abort
= J1939_XTP_ABORT_BUSY
;
1221 case J1939_ETP_CMD_CTS
:
1222 case J1939_TP_CMD_CTS
: /* fall through */
1223 abort
= J1939_XTP_ABORT_ECTS_UNXPECTED_PGN
;
1226 case J1939_ETP_CMD_DPO
:
1227 abort
= J1939_XTP_ABORT_BAD_EDPO_PGN
;
1230 case J1939_ETP_CMD_EOMA
:
1231 case J1939_TP_CMD_EOMA
: /* fall through */
1232 abort
= J1939_XTP_ABORT_OTHER
;
1235 case J1939_ETP_CMD_ABORT
: /* && J1939_TP_CMD_ABORT */
1236 abort
= J1939_XTP_NO_ABORT
;
1244 netdev_warn(priv
->ndev
, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n",
1245 __func__
, session
, cmd
, pgn
, session
->skcb
.addr
.pgn
);
1246 if (abort
!= J1939_XTP_NO_ABORT
)
1247 j1939_xtp_tx_abort(priv
, skcb
, true, abort
, pgn
);
1252 static void j1939_xtp_rx_abort_one(struct j1939_priv
*priv
, struct sk_buff
*skb
,
1253 bool reverse
, bool transmitter
)
1255 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1256 struct j1939_session
*session
;
1257 u8 abort
= skb
->data
[1];
1259 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, reverse
,
1264 if (j1939_xtp_rx_cmd_bad_pgn(session
, skb
))
1267 netdev_info(priv
->ndev
, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__
,
1268 session
, j1939_xtp_ctl_to_pgn(skb
->data
), abort
,
1269 j1939_xtp_abort_to_str(abort
));
1271 j1939_session_timers_cancel(session
);
1272 session
->err
= j1939_xtp_abort_to_errno(priv
, abort
);
1274 j1939_sk_send_loop_abort(session
->sk
, session
->err
);
1275 j1939_session_deactivate_activate_next(session
);
1278 j1939_session_put(session
);
1281 /* abort packets may come in 2 directions */
1283 j1939_xtp_rx_abort(struct j1939_priv
*priv
, struct sk_buff
*skb
,
1286 j1939_xtp_rx_abort_one(priv
, skb
, false, transmitter
);
1287 j1939_xtp_rx_abort_one(priv
, skb
, true, transmitter
);
1291 j1939_xtp_rx_eoma_one(struct j1939_session
*session
, struct sk_buff
*skb
)
1293 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1297 if (j1939_xtp_rx_cmd_bad_pgn(session
, skb
))
1302 if (skcb
->addr
.type
== J1939_ETP
)
1303 len
= j1939_etp_ctl_to_size(dat
);
1305 len
= j1939_tp_ctl_to_size(dat
);
1307 if (session
->total_message_size
!= len
) {
1308 netdev_warn_once(session
->priv
->ndev
,
1309 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
1310 __func__
, session
, session
->total_message_size
,
1314 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
1316 session
->pkt
.tx_acked
= session
->pkt
.total
;
1317 j1939_session_timers_cancel(session
);
1318 /* transmitted without problems */
1319 j1939_session_completed(session
);
1323 j1939_xtp_rx_eoma(struct j1939_priv
*priv
, struct sk_buff
*skb
,
1326 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1327 struct j1939_session
*session
;
1329 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, true,
1334 j1939_xtp_rx_eoma_one(session
, skb
);
1335 j1939_session_put(session
);
1339 j1939_xtp_rx_cts_one(struct j1939_session
*session
, struct sk_buff
*skb
)
1341 enum j1939_xtp_abort err
= J1939_XTP_ABORT_FAULT
;
1347 if (j1939_xtp_rx_cmd_bad_pgn(session
, skb
))
1350 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
1352 if (session
->last_cmd
== dat
[0]) {
1353 err
= J1939_XTP_ABORT_DUP_SEQ
;
1354 goto out_session_cancel
;
1357 if (session
->skcb
.addr
.type
== J1939_ETP
)
1358 pkt
= j1939_etp_ctl_to_packet(dat
);
1363 goto out_session_cancel
;
1364 else if (dat
[1] > session
->pkt
.block
/* 0xff for etp */)
1365 goto out_session_cancel
;
1367 /* set packet counters only when not CTS(0) */
1368 session
->pkt
.tx_acked
= pkt
- 1;
1369 j1939_session_skb_drop_old(session
);
1370 session
->pkt
.last
= session
->pkt
.tx_acked
+ dat
[1];
1371 if (session
->pkt
.last
> session
->pkt
.total
)
1372 /* safety measure */
1373 session
->pkt
.last
= session
->pkt
.total
;
1374 /* TODO: do not set tx here, do it in txtimer */
1375 session
->pkt
.tx
= session
->pkt
.tx_acked
;
1377 session
->last_cmd
= dat
[0];
1379 j1939_tp_set_rxtimeout(session
, 1250);
1380 if (session
->transmission
) {
1381 if (session
->pkt
.tx_acked
)
1382 j1939_sk_errqueue(session
,
1383 J1939_ERRQUEUE_SCHED
);
1384 j1939_session_txtimer_cancel(session
);
1385 j1939_tp_schedule_txtimer(session
, 0);
1389 j1939_tp_set_rxtimeout(session
, 550);
1394 j1939_session_timers_cancel(session
);
1395 j1939_session_cancel(session
, err
);
1399 j1939_xtp_rx_cts(struct j1939_priv
*priv
, struct sk_buff
*skb
, bool transmitter
)
1401 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1402 struct j1939_session
*session
;
1404 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, true,
1408 j1939_xtp_rx_cts_one(session
, skb
);
1409 j1939_session_put(session
);
1412 static struct j1939_session
*j1939_session_new(struct j1939_priv
*priv
,
1413 struct sk_buff
*skb
, size_t size
)
1415 struct j1939_session
*session
;
1416 struct j1939_sk_buff_cb
*skcb
;
1418 session
= kzalloc(sizeof(*session
), gfp_any());
1422 INIT_LIST_HEAD(&session
->active_session_list_entry
);
1423 INIT_LIST_HEAD(&session
->sk_session_queue_entry
);
1424 kref_init(&session
->kref
);
1426 j1939_priv_get(priv
);
1427 session
->priv
= priv
;
1428 session
->total_message_size
= size
;
1429 session
->state
= J1939_SESSION_NEW
;
1431 skb_queue_head_init(&session
->skb_queue
);
1432 skb_queue_tail(&session
->skb_queue
, skb
);
1434 skcb
= j1939_skb_to_cb(skb
);
1435 memcpy(&session
->skcb
, skcb
, sizeof(session
->skcb
));
1437 hrtimer_init(&session
->txtimer
, CLOCK_MONOTONIC
,
1438 HRTIMER_MODE_REL_SOFT
);
1439 session
->txtimer
.function
= j1939_tp_txtimer
;
1440 hrtimer_init(&session
->rxtimer
, CLOCK_MONOTONIC
,
1441 HRTIMER_MODE_REL_SOFT
);
1442 session
->rxtimer
.function
= j1939_tp_rxtimer
;
1444 netdev_dbg(priv
->ndev
, "%s: 0x%p: sa: %02x, da: %02x\n",
1445 __func__
, session
, skcb
->addr
.sa
, skcb
->addr
.da
);
1451 j1939_session
*j1939_session_fresh_new(struct j1939_priv
*priv
,
1453 const struct j1939_sk_buff_cb
*rel_skcb
)
1455 struct sk_buff
*skb
;
1456 struct j1939_sk_buff_cb
*skcb
;
1457 struct j1939_session
*session
;
1459 skb
= alloc_skb(size
+ sizeof(struct can_skb_priv
), GFP_ATOMIC
);
1463 skb
->dev
= priv
->ndev
;
1464 can_skb_reserve(skb
);
1465 can_skb_prv(skb
)->ifindex
= priv
->ndev
->ifindex
;
1466 skcb
= j1939_skb_to_cb(skb
);
1467 memcpy(skcb
, rel_skcb
, sizeof(*skcb
));
1469 session
= j1939_session_new(priv
, skb
, size
);
1475 /* alloc data area */
1477 /* skb is recounted in j1939_session_new() */
1481 int j1939_session_activate(struct j1939_session
*session
)
1483 struct j1939_priv
*priv
= session
->priv
;
1484 struct j1939_session
*active
= NULL
;
1487 j1939_session_list_lock(priv
);
1488 if (session
->skcb
.addr
.type
!= J1939_SIMPLE
)
1489 active
= j1939_session_get_by_addr_locked(priv
,
1490 &priv
->active_session_list
,
1491 &session
->skcb
.addr
, false,
1492 session
->transmission
);
1494 j1939_session_put(active
);
1497 WARN_ON_ONCE(session
->state
!= J1939_SESSION_NEW
);
1498 list_add_tail(&session
->active_session_list_entry
,
1499 &priv
->active_session_list
);
1500 j1939_session_get(session
);
1501 session
->state
= J1939_SESSION_ACTIVE
;
1503 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n",
1506 j1939_session_list_unlock(priv
);
1512 j1939_session
*j1939_xtp_rx_rts_session_new(struct j1939_priv
*priv
,
1513 struct sk_buff
*skb
)
1515 enum j1939_xtp_abort abort
= J1939_XTP_NO_ABORT
;
1516 struct j1939_sk_buff_cb skcb
= *j1939_skb_to_cb(skb
);
1517 struct j1939_session
*session
;
1522 netdev_dbg(priv
->ndev
, "%s\n", __func__
);
1525 pgn
= j1939_xtp_ctl_to_pgn(dat
);
1526 skcb
.addr
.pgn
= pgn
;
1528 if (!j1939_sk_recv_match(priv
, &skcb
))
1531 if (skcb
.addr
.type
== J1939_ETP
) {
1532 len
= j1939_etp_ctl_to_size(dat
);
1533 if (len
> J1939_MAX_ETP_PACKET_SIZE
)
1534 abort
= J1939_XTP_ABORT_FAULT
;
1535 else if (len
> priv
->tp_max_packet_size
)
1536 abort
= J1939_XTP_ABORT_RESOURCE
;
1537 else if (len
<= J1939_MAX_TP_PACKET_SIZE
)
1538 abort
= J1939_XTP_ABORT_FAULT
;
1540 len
= j1939_tp_ctl_to_size(dat
);
1541 if (len
> J1939_MAX_TP_PACKET_SIZE
)
1542 abort
= J1939_XTP_ABORT_FAULT
;
1543 else if (len
> priv
->tp_max_packet_size
)
1544 abort
= J1939_XTP_ABORT_RESOURCE
;
1547 if (abort
!= J1939_XTP_NO_ABORT
) {
1548 j1939_xtp_tx_abort(priv
, &skcb
, true, abort
, pgn
);
1552 session
= j1939_session_fresh_new(priv
, len
, &skcb
);
1554 j1939_xtp_tx_abort(priv
, &skcb
, true,
1555 J1939_XTP_ABORT_RESOURCE
, pgn
);
1559 /* initialize the control buffer: plain copy */
1560 session
->pkt
.total
= (len
+ 6) / 7;
1561 session
->pkt
.block
= 0xff;
1562 if (skcb
.addr
.type
!= J1939_ETP
) {
1563 if (dat
[3] != session
->pkt
.total
)
1564 netdev_alert(priv
->ndev
, "%s: 0x%p: strange total, %u != %u\n",
1565 __func__
, session
, session
->pkt
.total
,
1567 session
->pkt
.total
= dat
[3];
1568 session
->pkt
.block
= min(dat
[3], dat
[4]);
1571 session
->pkt
.rx
= 0;
1572 session
->pkt
.tx
= 0;
1574 WARN_ON_ONCE(j1939_session_activate(session
));
1579 static int j1939_xtp_rx_rts_session_active(struct j1939_session
*session
,
1580 struct sk_buff
*skb
)
1582 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1583 struct j1939_priv
*priv
= session
->priv
;
1585 if (!session
->transmission
) {
1586 if (j1939_xtp_rx_cmd_bad_pgn(session
, skb
))
1589 /* RTS on active session */
1590 j1939_session_timers_cancel(session
);
1591 j1939_session_cancel(session
, J1939_XTP_ABORT_BUSY
);
1594 if (session
->last_cmd
!= 0) {
1595 /* we received a second rts on the same connection */
1596 netdev_alert(priv
->ndev
, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n",
1597 __func__
, session
, skcb
->addr
.sa
, skcb
->addr
.da
,
1600 j1939_session_timers_cancel(session
);
1601 j1939_session_cancel(session
, J1939_XTP_ABORT_BUSY
);
1606 if (session
->skcb
.addr
.sa
!= skcb
->addr
.sa
||
1607 session
->skcb
.addr
.da
!= skcb
->addr
.da
)
1608 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",
1610 session
->skcb
.addr
.sa
, skcb
->addr
.sa
,
1611 session
->skcb
.addr
.da
, skcb
->addr
.da
);
1612 /* make sure 'sa' & 'da' are correct !
1613 * They may be 'not filled in yet' for sending
1614 * skb's, since they did not pass the Address Claim ever.
1616 session
->skcb
.addr
.sa
= skcb
->addr
.sa
;
1617 session
->skcb
.addr
.da
= skcb
->addr
.da
;
1619 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
1624 static void j1939_xtp_rx_rts(struct j1939_priv
*priv
, struct sk_buff
*skb
,
1627 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1628 struct j1939_session
*session
;
1629 u8 cmd
= skb
->data
[0];
1631 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, false,
1636 /* If we're the transmitter and this function is called,
1637 * we received our own RTS. A session has already been
1640 * For some reasons however it might have been destroyed
1641 * already. So don't create a new one here (using
1642 * "j1939_xtp_rx_rts_session_new()") as this will be a
1645 * The reasons the session is already destroyed might
1647 * - user space closed socket was and the session was
1649 * - session was aborted due to external abort message
1653 session
= j1939_xtp_rx_rts_session_new(priv
, skb
);
1657 if (j1939_xtp_rx_rts_session_active(session
, skb
)) {
1658 j1939_session_put(session
);
1662 session
->last_cmd
= cmd
;
1664 j1939_tp_set_rxtimeout(session
, 1250);
1666 if (cmd
!= J1939_TP_CMD_BAM
&& !session
->transmission
) {
1667 j1939_session_txtimer_cancel(session
);
1668 j1939_tp_schedule_txtimer(session
, 0);
1671 j1939_session_put(session
);
1674 static void j1939_xtp_rx_dpo_one(struct j1939_session
*session
,
1675 struct sk_buff
*skb
)
1677 const u8
*dat
= skb
->data
;
1679 if (j1939_xtp_rx_cmd_bad_pgn(session
, skb
))
1682 netdev_dbg(session
->priv
->ndev
, "%s: 0x%p\n", __func__
, session
);
1684 /* transmitted without problems */
1685 session
->pkt
.dpo
= j1939_etp_ctl_to_packet(skb
->data
);
1686 session
->last_cmd
= dat
[0];
1687 j1939_tp_set_rxtimeout(session
, 750);
1690 static void j1939_xtp_rx_dpo(struct j1939_priv
*priv
, struct sk_buff
*skb
,
1693 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1694 struct j1939_session
*session
;
1696 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, false,
1699 netdev_info(priv
->ndev
,
1700 "%s: no connection found\n", __func__
);
1704 j1939_xtp_rx_dpo_one(session
, skb
);
1705 j1939_session_put(session
);
1708 static void j1939_xtp_rx_dat_one(struct j1939_session
*session
,
1709 struct sk_buff
*skb
)
1711 struct j1939_priv
*priv
= session
->priv
;
1712 struct j1939_sk_buff_cb
*skcb
;
1713 struct sk_buff
*se_skb
;
1719 bool do_cts_eoma
= false;
1722 skcb
= j1939_skb_to_cb(skb
);
1725 /* makes no sense */
1726 goto out_session_cancel
;
1728 switch (session
->last_cmd
) {
1731 case J1939_ETP_CMD_DPO
:
1732 if (skcb
->addr
.type
== J1939_ETP
)
1735 case J1939_TP_CMD_BAM
: /* fall through */
1736 case J1939_TP_CMD_CTS
: /* fall through */
1737 if (skcb
->addr
.type
!= J1939_ETP
)
1741 netdev_info(priv
->ndev
, "%s: 0x%p: last %02x\n", __func__
,
1742 session
, session
->last_cmd
);
1743 goto out_session_cancel
;
1746 packet
= (dat
[0] - 1 + session
->pkt
.dpo
);
1747 if (packet
> session
->pkt
.total
||
1748 (session
->pkt
.rx
+ 1) > session
->pkt
.total
) {
1749 netdev_info(priv
->ndev
, "%s: 0x%p: should have been completed\n",
1751 goto out_session_cancel
;
1753 se_skb
= j1939_session_skb_find(session
);
1755 netdev_warn(priv
->ndev
, "%s: 0x%p: no skb found\n", __func__
,
1757 goto out_session_cancel
;
1760 skcb
= j1939_skb_to_cb(se_skb
);
1761 offset
= packet
* 7 - skcb
->offset
;
1762 nbytes
= se_skb
->len
- offset
;
1765 if (nbytes
<= 0 || (nbytes
+ 1) > skb
->len
) {
1766 netdev_info(priv
->ndev
, "%s: 0x%p: nbytes %i, len %i\n",
1767 __func__
, session
, nbytes
, skb
->len
);
1768 goto out_session_cancel
;
1771 tpdat
= se_skb
->data
;
1772 memcpy(&tpdat
[offset
], &dat
[1], nbytes
);
1773 if (packet
== session
->pkt
.rx
)
1776 if (skcb
->addr
.type
!= J1939_ETP
&&
1777 j1939_cb_is_broadcast(&session
->skcb
)) {
1778 if (session
->pkt
.rx
>= session
->pkt
.total
)
1781 /* never final, an EOMA must follow */
1782 if (session
->pkt
.rx
>= session
->pkt
.last
)
1787 j1939_session_completed(session
);
1788 } else if (do_cts_eoma
) {
1789 j1939_tp_set_rxtimeout(session
, 1250);
1790 if (!session
->transmission
)
1791 j1939_tp_schedule_txtimer(session
, 0);
1793 j1939_tp_set_rxtimeout(session
, 250);
1795 session
->last_cmd
= 0xff;
1796 j1939_session_put(session
);
1801 j1939_session_timers_cancel(session
);
1802 j1939_session_cancel(session
, J1939_XTP_ABORT_FAULT
);
1803 j1939_session_put(session
);
1806 static void j1939_xtp_rx_dat(struct j1939_priv
*priv
, struct sk_buff
*skb
)
1808 struct j1939_sk_buff_cb
*skcb
;
1809 struct j1939_session
*session
;
1811 skcb
= j1939_skb_to_cb(skb
);
1813 if (j1939_tp_im_transmitter(skcb
)) {
1814 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, false,
1817 netdev_info(priv
->ndev
, "%s: no tx connection found\n",
1820 j1939_xtp_rx_dat_one(session
, skb
);
1823 if (j1939_tp_im_receiver(skcb
)) {
1824 session
= j1939_session_get_by_addr(priv
, &skcb
->addr
, false,
1827 netdev_info(priv
->ndev
, "%s: no rx connection found\n",
1830 j1939_xtp_rx_dat_one(session
, skb
);
1834 /* j1939 main intf */
1835 struct j1939_session
*j1939_tp_send(struct j1939_priv
*priv
,
1836 struct sk_buff
*skb
, size_t size
)
1838 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1839 struct j1939_session
*session
;
1842 if (skcb
->addr
.pgn
== J1939_TP_PGN_DAT
||
1843 skcb
->addr
.pgn
== J1939_TP_PGN_CTL
||
1844 skcb
->addr
.pgn
== J1939_ETP_PGN_DAT
||
1845 skcb
->addr
.pgn
== J1939_ETP_PGN_CTL
)
1846 /* avoid conflict */
1847 return ERR_PTR(-EDOM
);
1849 if (size
> priv
->tp_max_packet_size
)
1850 return ERR_PTR(-EMSGSIZE
);
1853 skcb
->addr
.type
= J1939_SIMPLE
;
1854 else if (size
> J1939_MAX_TP_PACKET_SIZE
)
1855 skcb
->addr
.type
= J1939_ETP
;
1857 skcb
->addr
.type
= J1939_TP
;
1859 if (skcb
->addr
.type
== J1939_ETP
&&
1860 j1939_cb_is_broadcast(skcb
))
1861 return ERR_PTR(-EDESTADDRREQ
);
1863 /* fill in addresses from names */
1864 ret
= j1939_ac_fixup(priv
, skb
);
1866 return ERR_PTR(ret
);
1868 /* fix DST flags, it may be used there soon */
1869 if (j1939_address_is_unicast(skcb
->addr
.da
) &&
1870 priv
->ents
[skcb
->addr
.da
].nusers
)
1871 skcb
->flags
|= J1939_ECU_LOCAL_DST
;
1873 /* src is always local, I'm sending ... */
1874 skcb
->flags
|= J1939_ECU_LOCAL_SRC
;
1876 /* prepare new session */
1877 session
= j1939_session_new(priv
, skb
, size
);
1879 return ERR_PTR(-ENOMEM
);
1881 /* skb is recounted in j1939_session_new() */
1883 session
->sk
= skb
->sk
;
1884 session
->transmission
= true;
1885 session
->pkt
.total
= (size
+ 6) / 7;
1886 session
->pkt
.block
= skcb
->addr
.type
== J1939_ETP
? 255 :
1887 min(j1939_tp_block
?: 255, session
->pkt
.total
);
1889 if (j1939_cb_is_broadcast(&session
->skcb
))
1890 /* set the end-packet for broadcast */
1891 session
->pkt
.last
= session
->pkt
.total
;
1893 skcb
->tskey
= session
->sk
->sk_tskey
++;
1894 session
->tskey
= skcb
->tskey
;
1899 static void j1939_tp_cmd_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
)
1901 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1902 int extd
= J1939_TP
;
1903 u8 cmd
= skb
->data
[0];
1906 case J1939_ETP_CMD_RTS
:
1909 case J1939_TP_CMD_BAM
: /* fall through */
1910 case J1939_TP_CMD_RTS
: /* fall through */
1911 if (skcb
->addr
.type
!= extd
)
1914 if (cmd
== J1939_TP_CMD_RTS
&& j1939_cb_is_broadcast(skcb
)) {
1915 netdev_alert(priv
->ndev
, "%s: rts without destination (%02x)\n",
1916 __func__
, skcb
->addr
.sa
);
1920 if (j1939_tp_im_transmitter(skcb
))
1921 j1939_xtp_rx_rts(priv
, skb
, true);
1923 if (j1939_tp_im_receiver(skcb
))
1924 j1939_xtp_rx_rts(priv
, skb
, false);
1928 case J1939_ETP_CMD_CTS
:
1931 case J1939_TP_CMD_CTS
:
1932 if (skcb
->addr
.type
!= extd
)
1935 if (j1939_tp_im_transmitter(skcb
))
1936 j1939_xtp_rx_cts(priv
, skb
, false);
1938 if (j1939_tp_im_receiver(skcb
))
1939 j1939_xtp_rx_cts(priv
, skb
, true);
1943 case J1939_ETP_CMD_DPO
:
1944 if (skcb
->addr
.type
!= J1939_ETP
)
1947 if (j1939_tp_im_transmitter(skcb
))
1948 j1939_xtp_rx_dpo(priv
, skb
, true);
1950 if (j1939_tp_im_receiver(skcb
))
1951 j1939_xtp_rx_dpo(priv
, skb
, false);
1955 case J1939_ETP_CMD_EOMA
:
1958 case J1939_TP_CMD_EOMA
:
1959 if (skcb
->addr
.type
!= extd
)
1962 if (j1939_tp_im_transmitter(skcb
))
1963 j1939_xtp_rx_eoma(priv
, skb
, false);
1965 if (j1939_tp_im_receiver(skcb
))
1966 j1939_xtp_rx_eoma(priv
, skb
, true);
1970 case J1939_ETP_CMD_ABORT
: /* && J1939_TP_CMD_ABORT */
1971 if (j1939_tp_im_transmitter(skcb
))
1972 j1939_xtp_rx_abort(priv
, skb
, true);
1974 if (j1939_tp_im_receiver(skcb
))
1975 j1939_xtp_rx_abort(priv
, skb
, false);
1983 int j1939_tp_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
)
1985 struct j1939_sk_buff_cb
*skcb
= j1939_skb_to_cb(skb
);
1987 if (!j1939_tp_im_involved_anydir(skcb
))
1990 switch (skcb
->addr
.pgn
) {
1991 case J1939_ETP_PGN_DAT
:
1992 skcb
->addr
.type
= J1939_ETP
;
1994 case J1939_TP_PGN_DAT
:
1995 j1939_xtp_rx_dat(priv
, skb
);
1998 case J1939_ETP_PGN_CTL
:
1999 skcb
->addr
.type
= J1939_ETP
;
2001 case J1939_TP_PGN_CTL
:
2003 return 0; /* Don't care. Nothing to extract here */
2005 j1939_tp_cmd_recv(priv
, skb
);
2008 return 0; /* no problem */
2010 return 1; /* "I processed the message" */
2013 void j1939_simple_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
)
2015 struct j1939_session
*session
;
2020 j1939_session_list_lock(priv
);
2021 session
= j1939_session_get_simple(priv
, skb
);
2022 j1939_session_list_unlock(priv
);
2024 netdev_warn(priv
->ndev
,
2025 "%s: Received already invalidated message\n",
2030 j1939_session_timers_cancel(session
);
2031 j1939_session_deactivate(session
);
2032 j1939_session_put(session
);
2035 int j1939_cancel_active_session(struct j1939_priv
*priv
, struct sock
*sk
)
2037 struct j1939_session
*session
, *saved
;
2039 netdev_dbg(priv
->ndev
, "%s, sk: %p\n", __func__
, sk
);
2040 j1939_session_list_lock(priv
);
2041 list_for_each_entry_safe(session
, saved
,
2042 &priv
->active_session_list
,
2043 active_session_list_entry
) {
2044 if (!sk
|| sk
== session
->sk
) {
2045 if (hrtimer_try_to_cancel(&session
->txtimer
) == 1)
2046 j1939_session_put(session
);
2047 if (hrtimer_try_to_cancel(&session
->rxtimer
) == 1)
2048 j1939_session_put(session
);
2050 session
->err
= ESHUTDOWN
;
2051 j1939_session_deactivate_locked(session
);
2054 j1939_session_list_unlock(priv
);
2058 void j1939_tp_init(struct j1939_priv
*priv
)
2060 spin_lock_init(&priv
->active_session_list_lock
);
2061 INIT_LIST_HEAD(&priv
->active_session_list
);
2062 priv
->tp_max_packet_size
= J1939_MAX_ETP_PACKET_SIZE
;