xref: /linux/net/can/j1939/transport.c (revision 9052e9c95d908d6c3d7570aadc8898e1d871c8bb)
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>
10 
11 #include <linux/can/skb.h>
12 
13 #include "j1939-priv.h"
14 
15 #define J1939_XTP_TX_RETRY_LIMIT 100
16 
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
21 
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
27 
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
33 
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.
39 	 *
40 	 * EALREADY:
41 	 * Operation already in progress
42 	 */
43 
44 	J1939_XTP_ABORT_RESOURCE = 2,
45 	/* System resources were needed for another task so this
46 	 * connection managed session was terminated.
47 	 *
48 	 * EMSGSIZE:
49 	 * The socket type requires that message be sent atomically,
50 	 * and the size of the message to be sent made this
51 	 * impossible.
52 	 */
53 
54 	J1939_XTP_ABORT_TIMEOUT = 3,
55 	/* A timeout occurred and this is the connection abort to
56 	 * close the session.
57 	 *
58 	 * EHOSTUNREACH:
59 	 * The destination host cannot be reached (probably because
60 	 * the host is down or a remote router cannot reach it).
61 	 */
62 
63 	J1939_XTP_ABORT_GENERIC = 4,
64 	/* CTS messages received when data transfer is in progress
65 	 *
66 	 * EBADMSG:
67 	 * Not a data message
68 	 */
69 
70 	J1939_XTP_ABORT_FAULT = 5,
71 	/* Maximal retransmit request limit reached
72 	 *
73 	 * ENOTRECOVERABLE:
74 	 * State not recoverable
75 	 */
76 
77 	J1939_XTP_ABORT_UNEXPECTED_DATA = 6,
78 	/* Unexpected data transfer packet
79 	 *
80 	 * ENOTCONN:
81 	 * Transport endpoint is not connected
82 	 */
83 
84 	J1939_XTP_ABORT_BAD_SEQ = 7,
85 	/* Bad sequence number (and software is not able to recover)
86 	 *
87 	 * EILSEQ:
88 	 * Illegal byte sequence
89 	 */
90 
91 	J1939_XTP_ABORT_DUP_SEQ = 8,
92 	/* Duplicate sequence number (and software is not able to
93 	 * recover)
94 	 */
95 
96 	J1939_XTP_ABORT_EDPO_UNEXPECTED = 9,
97 	/* Unexpected EDPO packet (ETP) or Message size > 1785 bytes
98 	 * (TP)
99 	 */
100 
101 	J1939_XTP_ABORT_BAD_EDPO_PGN = 10,
102 	/* Unexpected EDPO PGN (PGN in EDPO is bad) */
103 
104 	J1939_XTP_ABORT_EDPO_OUTOF_CTS = 11,
105 	/* EDPO number of packets is greater than CTS */
106 
107 	J1939_XTP_ABORT_BAD_EDPO_OFFSET = 12,
108 	/* Bad EDPO offset */
109 
110 	J1939_XTP_ABORT_OTHER_DEPRECATED = 13,
111 	/* Deprecated. Use 250 instead (Any other reason)  */
112 
113 	J1939_XTP_ABORT_ECTS_UNXPECTED_PGN = 14,
114 	/* Unexpected ECTS PGN (PGN in ECTS is bad) */
115 
116 	J1939_XTP_ABORT_ECTS_TOO_BIG = 15,
117 	/* ECTS requested packets exceeds message size */
118 
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)
122 	 */
123 };
124 
125 static unsigned int j1939_tp_block = 255;
126 static unsigned int j1939_tp_packet_delay;
127 static unsigned int j1939_tp_padding = 1;
128 
129 /* helpers */
130 static const char *j1939_xtp_abort_to_str(enum j1939_xtp_abort abort)
131 {
132 	switch (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)";
165 	default:
166 		return "<unknown>";
167 	}
168 }
169 
170 static int j1939_xtp_abort_to_errno(struct j1939_priv *priv,
171 				    enum j1939_xtp_abort abort)
172 {
173 	int err;
174 
175 	switch (abort) {
176 	case J1939_XTP_NO_ABORT:
177 		WARN_ON_ONCE(abort == J1939_XTP_NO_ABORT);
178 		err = 0;
179 		break;
180 	case J1939_XTP_ABORT_BUSY:
181 		err = EALREADY;
182 		break;
183 	case J1939_XTP_ABORT_RESOURCE:
184 		err = EMSGSIZE;
185 		break;
186 	case J1939_XTP_ABORT_TIMEOUT:
187 		err = EHOSTUNREACH;
188 		break;
189 	case J1939_XTP_ABORT_GENERIC:
190 		err = EBADMSG;
191 		break;
192 	case J1939_XTP_ABORT_FAULT:
193 		err = ENOTRECOVERABLE;
194 		break;
195 	case J1939_XTP_ABORT_UNEXPECTED_DATA:
196 		err = ENOTCONN;
197 		break;
198 	case J1939_XTP_ABORT_BAD_SEQ:
199 		err = EILSEQ;
200 		break;
201 	case J1939_XTP_ABORT_DUP_SEQ:
202 		err = EPROTO;
203 		break;
204 	case J1939_XTP_ABORT_EDPO_UNEXPECTED:
205 		err = EPROTO;
206 		break;
207 	case J1939_XTP_ABORT_BAD_EDPO_PGN:
208 		err = EPROTO;
209 		break;
210 	case J1939_XTP_ABORT_EDPO_OUTOF_CTS:
211 		err = EPROTO;
212 		break;
213 	case J1939_XTP_ABORT_BAD_EDPO_OFFSET:
214 		err = EPROTO;
215 		break;
216 	case J1939_XTP_ABORT_OTHER_DEPRECATED:
217 		err = EPROTO;
218 		break;
219 	case J1939_XTP_ABORT_ECTS_UNXPECTED_PGN:
220 		err = EPROTO;
221 		break;
222 	case J1939_XTP_ABORT_ECTS_TOO_BIG:
223 		err = EPROTO;
224 		break;
225 	case J1939_XTP_ABORT_OTHER:
226 		err = EPROTO;
227 		break;
228 	default:
229 		netdev_warn(priv->ndev, "Unknown abort code %i", abort);
230 		err = EPROTO;
231 	}
232 
233 	return err;
234 }
235 
236 static inline void j1939_session_list_lock(struct j1939_priv *priv)
237 {
238 	spin_lock_bh(&priv->active_session_list_lock);
239 }
240 
241 static inline void j1939_session_list_unlock(struct j1939_priv *priv)
242 {
243 	spin_unlock_bh(&priv->active_session_list_lock);
244 }
245 
246 void j1939_session_get(struct j1939_session *session)
247 {
248 	kref_get(&session->kref);
249 }
250 
251 /* session completion functions */
252 static void __j1939_session_drop(struct j1939_session *session)
253 {
254 	if (!session->transmission)
255 		return;
256 
257 	j1939_sock_pending_del(session->sk);
258 	sock_put(session->sk);
259 }
260 
261 static void j1939_session_destroy(struct j1939_session *session)
262 {
263 	if (session->transmission) {
264 		if (session->err)
265 			j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
266 		else
267 			j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ACK);
268 	} else if (session->err) {
269 			j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
270 	}
271 
272 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
273 
274 	WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
275 	WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
276 
277 	skb_queue_purge(&session->skb_queue);
278 	__j1939_session_drop(session);
279 	j1939_priv_put(session->priv);
280 	kfree(session);
281 }
282 
283 static void __j1939_session_release(struct kref *kref)
284 {
285 	struct j1939_session *session = container_of(kref, struct j1939_session,
286 						     kref);
287 
288 	j1939_session_destroy(session);
289 }
290 
291 void j1939_session_put(struct j1939_session *session)
292 {
293 	kref_put(&session->kref, __j1939_session_release);
294 }
295 
296 static void j1939_session_txtimer_cancel(struct j1939_session *session)
297 {
298 	if (hrtimer_cancel(&session->txtimer))
299 		j1939_session_put(session);
300 }
301 
302 static void j1939_session_rxtimer_cancel(struct j1939_session *session)
303 {
304 	if (hrtimer_cancel(&session->rxtimer))
305 		j1939_session_put(session);
306 }
307 
308 void j1939_session_timers_cancel(struct j1939_session *session)
309 {
310 	j1939_session_txtimer_cancel(session);
311 	j1939_session_rxtimer_cancel(session);
312 }
313 
314 static inline bool j1939_cb_is_broadcast(const struct j1939_sk_buff_cb *skcb)
315 {
316 	return (!skcb->addr.dst_name && (skcb->addr.da == 0xff));
317 }
318 
319 static void j1939_session_skb_drop_old(struct j1939_session *session)
320 {
321 	struct sk_buff *do_skb;
322 	struct j1939_sk_buff_cb *do_skcb;
323 	unsigned int offset_start;
324 	unsigned long flags;
325 
326 	if (skb_queue_len(&session->skb_queue) < 2)
327 		return;
328 
329 	offset_start = session->pkt.tx_acked * 7;
330 
331 	spin_lock_irqsave(&session->skb_queue.lock, flags);
332 	do_skb = skb_peek(&session->skb_queue);
333 	do_skcb = j1939_skb_to_cb(do_skb);
334 
335 	if ((do_skcb->offset + do_skb->len) < offset_start) {
336 		__skb_unlink(do_skb, &session->skb_queue);
337 		/* drop ref taken in j1939_session_skb_queue() */
338 		skb_unref(do_skb);
339 
340 		kfree_skb(do_skb);
341 	}
342 	spin_unlock_irqrestore(&session->skb_queue.lock, flags);
343 }
344 
345 void j1939_session_skb_queue(struct j1939_session *session,
346 			     struct sk_buff *skb)
347 {
348 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
349 	struct j1939_priv *priv = session->priv;
350 
351 	j1939_ac_fixup(priv, skb);
352 
353 	if (j1939_address_is_unicast(skcb->addr.da) &&
354 	    priv->ents[skcb->addr.da].nusers)
355 		skcb->flags |= J1939_ECU_LOCAL_DST;
356 
357 	skcb->flags |= J1939_ECU_LOCAL_SRC;
358 
359 	skb_get(skb);
360 	skb_queue_tail(&session->skb_queue, skb);
361 }
362 
363 static struct
364 sk_buff *j1939_session_skb_get_by_offset(struct j1939_session *session,
365 					 unsigned int offset_start)
366 {
367 	struct j1939_priv *priv = session->priv;
368 	struct j1939_sk_buff_cb *do_skcb;
369 	struct sk_buff *skb = NULL;
370 	struct sk_buff *do_skb;
371 	unsigned long flags;
372 
373 	spin_lock_irqsave(&session->skb_queue.lock, flags);
374 	skb_queue_walk(&session->skb_queue, do_skb) {
375 		do_skcb = j1939_skb_to_cb(do_skb);
376 
377 		if (offset_start >= do_skcb->offset &&
378 		    offset_start < (do_skcb->offset + do_skb->len)) {
379 			skb = do_skb;
380 		}
381 	}
382 
383 	if (skb)
384 		skb_get(skb);
385 
386 	spin_unlock_irqrestore(&session->skb_queue.lock, flags);
387 
388 	if (!skb)
389 		netdev_dbg(priv->ndev, "%s: 0x%p: no skb found for start: %i, queue size: %i\n",
390 			   __func__, session, offset_start,
391 			   skb_queue_len(&session->skb_queue));
392 
393 	return skb;
394 }
395 
396 static struct sk_buff *j1939_session_skb_get(struct j1939_session *session)
397 {
398 	unsigned int offset_start;
399 
400 	offset_start = session->pkt.dpo * 7;
401 	return j1939_session_skb_get_by_offset(session, offset_start);
402 }
403 
404 /* see if we are receiver
405  * returns 0 for broadcasts, although we will receive them
406  */
407 static inline int j1939_tp_im_receiver(const struct j1939_sk_buff_cb *skcb)
408 {
409 	return skcb->flags & J1939_ECU_LOCAL_DST;
410 }
411 
412 /* see if we are sender */
413 static inline int j1939_tp_im_transmitter(const struct j1939_sk_buff_cb *skcb)
414 {
415 	return skcb->flags & J1939_ECU_LOCAL_SRC;
416 }
417 
418 /* see if we are involved as either receiver or transmitter */
419 static int j1939_tp_im_involved(const struct j1939_sk_buff_cb *skcb, bool swap)
420 {
421 	if (swap)
422 		return j1939_tp_im_receiver(skcb);
423 	else
424 		return j1939_tp_im_transmitter(skcb);
425 }
426 
427 static int j1939_tp_im_involved_anydir(struct j1939_sk_buff_cb *skcb)
428 {
429 	return skcb->flags & (J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
430 }
431 
432 /* extract pgn from flow-ctl message */
433 static inline pgn_t j1939_xtp_ctl_to_pgn(const u8 *dat)
434 {
435 	pgn_t pgn;
436 
437 	pgn = (dat[7] << 16) | (dat[6] << 8) | (dat[5] << 0);
438 	if (j1939_pgn_is_pdu1(pgn))
439 		pgn &= 0xffff00;
440 	return pgn;
441 }
442 
443 static inline unsigned int j1939_tp_ctl_to_size(const u8 *dat)
444 {
445 	return (dat[2] << 8) + (dat[1] << 0);
446 }
447 
448 static inline unsigned int j1939_etp_ctl_to_packet(const u8 *dat)
449 {
450 	return (dat[4] << 16) | (dat[3] << 8) | (dat[2] << 0);
451 }
452 
453 static inline unsigned int j1939_etp_ctl_to_size(const u8 *dat)
454 {
455 	return (dat[4] << 24) | (dat[3] << 16) |
456 		(dat[2] << 8) | (dat[1] << 0);
457 }
458 
459 /* find existing session:
460  * reverse: swap cb's src & dst
461  * there is no problem with matching broadcasts, since
462  * broadcasts (no dst, no da) would never call this
463  * with reverse == true
464  */
465 static bool j1939_session_match(struct j1939_addr *se_addr,
466 				struct j1939_addr *sk_addr, bool reverse)
467 {
468 	if (se_addr->type != sk_addr->type)
469 		return false;
470 
471 	if (reverse) {
472 		if (se_addr->src_name) {
473 			if (se_addr->src_name != sk_addr->dst_name)
474 				return false;
475 		} else if (se_addr->sa != sk_addr->da) {
476 			return false;
477 		}
478 
479 		if (se_addr->dst_name) {
480 			if (se_addr->dst_name != sk_addr->src_name)
481 				return false;
482 		} else if (se_addr->da != sk_addr->sa) {
483 			return false;
484 		}
485 	} else {
486 		if (se_addr->src_name) {
487 			if (se_addr->src_name != sk_addr->src_name)
488 				return false;
489 		} else if (se_addr->sa != sk_addr->sa) {
490 			return false;
491 		}
492 
493 		if (se_addr->dst_name) {
494 			if (se_addr->dst_name != sk_addr->dst_name)
495 				return false;
496 		} else if (se_addr->da != sk_addr->da) {
497 			return false;
498 		}
499 	}
500 
501 	return true;
502 }
503 
504 static struct
505 j1939_session *j1939_session_get_by_addr_locked(struct j1939_priv *priv,
506 						struct list_head *root,
507 						struct j1939_addr *addr,
508 						bool reverse, bool transmitter)
509 {
510 	struct j1939_session *session;
511 
512 	lockdep_assert_held(&priv->active_session_list_lock);
513 
514 	list_for_each_entry(session, root, active_session_list_entry) {
515 		j1939_session_get(session);
516 		if (j1939_session_match(&session->skcb.addr, addr, reverse) &&
517 		    session->transmission == transmitter)
518 			return session;
519 		j1939_session_put(session);
520 	}
521 
522 	return NULL;
523 }
524 
525 static struct
526 j1939_session *j1939_session_get_simple(struct j1939_priv *priv,
527 					struct sk_buff *skb)
528 {
529 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
530 	struct j1939_session *session;
531 
532 	lockdep_assert_held(&priv->active_session_list_lock);
533 
534 	list_for_each_entry(session, &priv->active_session_list,
535 			    active_session_list_entry) {
536 		j1939_session_get(session);
537 		if (session->skcb.addr.type == J1939_SIMPLE &&
538 		    session->tskey == skcb->tskey && session->sk == skb->sk)
539 			return session;
540 		j1939_session_put(session);
541 	}
542 
543 	return NULL;
544 }
545 
546 static struct
547 j1939_session *j1939_session_get_by_addr(struct j1939_priv *priv,
548 					 struct j1939_addr *addr,
549 					 bool reverse, bool transmitter)
550 {
551 	struct j1939_session *session;
552 
553 	j1939_session_list_lock(priv);
554 	session = j1939_session_get_by_addr_locked(priv,
555 						   &priv->active_session_list,
556 						   addr, reverse, transmitter);
557 	j1939_session_list_unlock(priv);
558 
559 	return session;
560 }
561 
562 static void j1939_skbcb_swap(struct j1939_sk_buff_cb *skcb)
563 {
564 	u8 tmp = 0;
565 
566 	swap(skcb->addr.dst_name, skcb->addr.src_name);
567 	swap(skcb->addr.da, skcb->addr.sa);
568 
569 	/* swap SRC and DST flags, leave other untouched */
570 	if (skcb->flags & J1939_ECU_LOCAL_SRC)
571 		tmp |= J1939_ECU_LOCAL_DST;
572 	if (skcb->flags & J1939_ECU_LOCAL_DST)
573 		tmp |= J1939_ECU_LOCAL_SRC;
574 	skcb->flags &= ~(J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST);
575 	skcb->flags |= tmp;
576 }
577 
578 static struct
579 sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv,
580 			     const struct j1939_sk_buff_cb *re_skcb,
581 			     bool ctl,
582 			     bool swap_src_dst)
583 {
584 	struct sk_buff *skb;
585 	struct j1939_sk_buff_cb *skcb;
586 
587 	skb = alloc_skb(sizeof(struct can_frame) + sizeof(struct can_skb_priv),
588 			GFP_ATOMIC);
589 	if (unlikely(!skb))
590 		return ERR_PTR(-ENOMEM);
591 
592 	skb->dev = priv->ndev;
593 	can_skb_reserve(skb);
594 	can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
595 	can_skb_prv(skb)->skbcnt = 0;
596 	/* reserve CAN header */
597 	skb_reserve(skb, offsetof(struct can_frame, data));
598 
599 	memcpy(skb->cb, re_skcb, sizeof(skb->cb));
600 	skcb = j1939_skb_to_cb(skb);
601 	if (swap_src_dst)
602 		j1939_skbcb_swap(skcb);
603 
604 	if (ctl) {
605 		if (skcb->addr.type == J1939_ETP)
606 			skcb->addr.pgn = J1939_ETP_PGN_CTL;
607 		else
608 			skcb->addr.pgn = J1939_TP_PGN_CTL;
609 	} else {
610 		if (skcb->addr.type == J1939_ETP)
611 			skcb->addr.pgn = J1939_ETP_PGN_DAT;
612 		else
613 			skcb->addr.pgn = J1939_TP_PGN_DAT;
614 	}
615 
616 	return skb;
617 }
618 
619 /* TP transmit packet functions */
620 static int j1939_tp_tx_dat(struct j1939_session *session,
621 			   const u8 *dat, int len)
622 {
623 	struct j1939_priv *priv = session->priv;
624 	struct sk_buff *skb;
625 
626 	skb = j1939_tp_tx_dat_new(priv, &session->skcb,
627 				  false, false);
628 	if (IS_ERR(skb))
629 		return PTR_ERR(skb);
630 
631 	skb_put_data(skb, dat, len);
632 	if (j1939_tp_padding && len < 8)
633 		memset(skb_put(skb, 8 - len), 0xff, 8 - len);
634 
635 	return j1939_send_one(priv, skb);
636 }
637 
638 static int j1939_xtp_do_tx_ctl(struct j1939_priv *priv,
639 			       const struct j1939_sk_buff_cb *re_skcb,
640 			       bool swap_src_dst, pgn_t pgn, const u8 *dat)
641 {
642 	struct sk_buff *skb;
643 	u8 *skdat;
644 
645 	if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
646 		return 0;
647 
648 	skb = j1939_tp_tx_dat_new(priv, re_skcb, true, swap_src_dst);
649 	if (IS_ERR(skb))
650 		return PTR_ERR(skb);
651 
652 	skdat = skb_put(skb, 8);
653 	memcpy(skdat, dat, 5);
654 	skdat[5] = (pgn >> 0);
655 	skdat[6] = (pgn >> 8);
656 	skdat[7] = (pgn >> 16);
657 
658 	return j1939_send_one(priv, skb);
659 }
660 
661 static inline int j1939_tp_tx_ctl(struct j1939_session *session,
662 				  bool swap_src_dst, const u8 *dat)
663 {
664 	struct j1939_priv *priv = session->priv;
665 
666 	return j1939_xtp_do_tx_ctl(priv, &session->skcb,
667 				   swap_src_dst,
668 				   session->skcb.addr.pgn, dat);
669 }
670 
671 static int j1939_xtp_tx_abort(struct j1939_priv *priv,
672 			      const struct j1939_sk_buff_cb *re_skcb,
673 			      bool swap_src_dst,
674 			      enum j1939_xtp_abort err,
675 			      pgn_t pgn)
676 {
677 	u8 dat[5];
678 
679 	if (!j1939_tp_im_involved(re_skcb, swap_src_dst))
680 		return 0;
681 
682 	memset(dat, 0xff, sizeof(dat));
683 	dat[0] = J1939_TP_CMD_ABORT;
684 	dat[1] = err;
685 	return j1939_xtp_do_tx_ctl(priv, re_skcb, swap_src_dst, pgn, dat);
686 }
687 
688 void j1939_tp_schedule_txtimer(struct j1939_session *session, int msec)
689 {
690 	j1939_session_get(session);
691 	hrtimer_start(&session->txtimer, ms_to_ktime(msec),
692 		      HRTIMER_MODE_REL_SOFT);
693 }
694 
695 static inline void j1939_tp_set_rxtimeout(struct j1939_session *session,
696 					  int msec)
697 {
698 	j1939_session_rxtimer_cancel(session);
699 	j1939_session_get(session);
700 	hrtimer_start(&session->rxtimer, ms_to_ktime(msec),
701 		      HRTIMER_MODE_REL_SOFT);
702 }
703 
704 static int j1939_session_tx_rts(struct j1939_session *session)
705 {
706 	u8 dat[8];
707 	int ret;
708 
709 	memset(dat, 0xff, sizeof(dat));
710 
711 	dat[1] = (session->total_message_size >> 0);
712 	dat[2] = (session->total_message_size >> 8);
713 	dat[3] = session->pkt.total;
714 
715 	if (session->skcb.addr.type == J1939_ETP) {
716 		dat[0] = J1939_ETP_CMD_RTS;
717 		dat[1] = (session->total_message_size >> 0);
718 		dat[2] = (session->total_message_size >> 8);
719 		dat[3] = (session->total_message_size >> 16);
720 		dat[4] = (session->total_message_size >> 24);
721 	} else if (j1939_cb_is_broadcast(&session->skcb)) {
722 		dat[0] = J1939_TP_CMD_BAM;
723 		/* fake cts for broadcast */
724 		session->pkt.tx = 0;
725 	} else {
726 		dat[0] = J1939_TP_CMD_RTS;
727 		dat[4] = dat[3];
728 	}
729 
730 	if (dat[0] == session->last_txcmd)
731 		/* done already */
732 		return 0;
733 
734 	ret = j1939_tp_tx_ctl(session, false, dat);
735 	if (ret < 0)
736 		return ret;
737 
738 	session->last_txcmd = dat[0];
739 	if (dat[0] == J1939_TP_CMD_BAM) {
740 		j1939_tp_schedule_txtimer(session, 50);
741 		j1939_tp_set_rxtimeout(session, 250);
742 	} else {
743 		j1939_tp_set_rxtimeout(session, 1250);
744 	}
745 
746 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
747 
748 	return 0;
749 }
750 
751 static int j1939_session_tx_dpo(struct j1939_session *session)
752 {
753 	unsigned int pkt;
754 	u8 dat[8];
755 	int ret;
756 
757 	memset(dat, 0xff, sizeof(dat));
758 
759 	dat[0] = J1939_ETP_CMD_DPO;
760 	session->pkt.dpo = session->pkt.tx_acked;
761 	pkt = session->pkt.dpo;
762 	dat[1] = session->pkt.last - session->pkt.tx_acked;
763 	dat[2] = (pkt >> 0);
764 	dat[3] = (pkt >> 8);
765 	dat[4] = (pkt >> 16);
766 
767 	ret = j1939_tp_tx_ctl(session, false, dat);
768 	if (ret < 0)
769 		return ret;
770 
771 	session->last_txcmd = dat[0];
772 	j1939_tp_set_rxtimeout(session, 1250);
773 	session->pkt.tx = session->pkt.tx_acked;
774 
775 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
776 
777 	return 0;
778 }
779 
780 static int j1939_session_tx_dat(struct j1939_session *session)
781 {
782 	struct j1939_priv *priv = session->priv;
783 	struct j1939_sk_buff_cb *se_skcb;
784 	int offset, pkt_done, pkt_end;
785 	unsigned int len, pdelay;
786 	struct sk_buff *se_skb;
787 	const u8 *tpdat;
788 	int ret = 0;
789 	u8 dat[8];
790 
791 	se_skb = j1939_session_skb_get_by_offset(session, session->pkt.tx * 7);
792 	if (!se_skb)
793 		return -ENOBUFS;
794 
795 	se_skcb = j1939_skb_to_cb(se_skb);
796 	tpdat = se_skb->data;
797 	ret = 0;
798 	pkt_done = 0;
799 	if (session->skcb.addr.type != J1939_ETP &&
800 	    j1939_cb_is_broadcast(&session->skcb))
801 		pkt_end = session->pkt.total;
802 	else
803 		pkt_end = session->pkt.last;
804 
805 	while (session->pkt.tx < pkt_end) {
806 		dat[0] = session->pkt.tx - session->pkt.dpo + 1;
807 		offset = (session->pkt.tx * 7) - se_skcb->offset;
808 		len =  se_skb->len - offset;
809 		if (len > 7)
810 			len = 7;
811 
812 		if (offset + len > se_skb->len) {
813 			netdev_err_once(priv->ndev,
814 					"%s: 0x%p: requested data outside of queued buffer: offset %i, len %i, pkt.tx: %i\n",
815 					__func__, session, se_skcb->offset,
816 					se_skb->len , session->pkt.tx);
817 			ret = -EOVERFLOW;
818 			goto out_free;
819 		}
820 
821 		if (!len) {
822 			ret = -ENOBUFS;
823 			break;
824 		}
825 
826 		memcpy(&dat[1], &tpdat[offset], len);
827 		ret = j1939_tp_tx_dat(session, dat, len + 1);
828 		if (ret < 0) {
829 			/* ENOBUFS == CAN interface TX queue is full */
830 			if (ret != -ENOBUFS)
831 				netdev_alert(priv->ndev,
832 					     "%s: 0x%p: queue data error: %i\n",
833 					     __func__, session, ret);
834 			break;
835 		}
836 
837 		session->last_txcmd = 0xff;
838 		pkt_done++;
839 		session->pkt.tx++;
840 		pdelay = j1939_cb_is_broadcast(&session->skcb) ? 50 :
841 			j1939_tp_packet_delay;
842 
843 		if (session->pkt.tx < session->pkt.total && pdelay) {
844 			j1939_tp_schedule_txtimer(session, pdelay);
845 			break;
846 		}
847 	}
848 
849 	if (pkt_done)
850 		j1939_tp_set_rxtimeout(session, 250);
851 
852  out_free:
853 	if (ret)
854 		kfree_skb(se_skb);
855 	else
856 		consume_skb(se_skb);
857 
858 	return ret;
859 }
860 
861 static int j1939_xtp_txnext_transmiter(struct j1939_session *session)
862 {
863 	struct j1939_priv *priv = session->priv;
864 	int ret = 0;
865 
866 	if (!j1939_tp_im_transmitter(&session->skcb)) {
867 		netdev_alert(priv->ndev, "%s: 0x%p: called by not transmitter!\n",
868 			     __func__, session);
869 		return -EINVAL;
870 	}
871 
872 	switch (session->last_cmd) {
873 	case 0:
874 		ret = j1939_session_tx_rts(session);
875 		break;
876 
877 	case J1939_ETP_CMD_CTS:
878 		if (session->last_txcmd != J1939_ETP_CMD_DPO) {
879 			ret = j1939_session_tx_dpo(session);
880 			if (ret)
881 				return ret;
882 		}
883 
884 		fallthrough;
885 	case J1939_TP_CMD_CTS:
886 	case 0xff: /* did some data */
887 	case J1939_ETP_CMD_DPO:
888 	case J1939_TP_CMD_BAM:
889 		ret = j1939_session_tx_dat(session);
890 
891 		break;
892 	default:
893 		netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
894 			     __func__, session, session->last_cmd);
895 	}
896 
897 	return ret;
898 }
899 
900 static int j1939_session_tx_cts(struct j1939_session *session)
901 {
902 	struct j1939_priv *priv = session->priv;
903 	unsigned int pkt, len;
904 	int ret;
905 	u8 dat[8];
906 
907 	if (!j1939_sk_recv_match(priv, &session->skcb))
908 		return -ENOENT;
909 
910 	len = session->pkt.total - session->pkt.rx;
911 	len = min3(len, session->pkt.block, j1939_tp_block ?: 255);
912 	memset(dat, 0xff, sizeof(dat));
913 
914 	if (session->skcb.addr.type == J1939_ETP) {
915 		pkt = session->pkt.rx + 1;
916 		dat[0] = J1939_ETP_CMD_CTS;
917 		dat[1] = len;
918 		dat[2] = (pkt >> 0);
919 		dat[3] = (pkt >> 8);
920 		dat[4] = (pkt >> 16);
921 	} else {
922 		dat[0] = J1939_TP_CMD_CTS;
923 		dat[1] = len;
924 		dat[2] = session->pkt.rx + 1;
925 	}
926 
927 	if (dat[0] == session->last_txcmd)
928 		/* done already */
929 		return 0;
930 
931 	ret = j1939_tp_tx_ctl(session, true, dat);
932 	if (ret < 0)
933 		return ret;
934 
935 	if (len)
936 		/* only mark cts done when len is set */
937 		session->last_txcmd = dat[0];
938 	j1939_tp_set_rxtimeout(session, 1250);
939 
940 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
941 
942 	return 0;
943 }
944 
945 static int j1939_session_tx_eoma(struct j1939_session *session)
946 {
947 	struct j1939_priv *priv = session->priv;
948 	u8 dat[8];
949 	int ret;
950 
951 	if (!j1939_sk_recv_match(priv, &session->skcb))
952 		return -ENOENT;
953 
954 	memset(dat, 0xff, sizeof(dat));
955 
956 	if (session->skcb.addr.type == J1939_ETP) {
957 		dat[0] = J1939_ETP_CMD_EOMA;
958 		dat[1] = session->total_message_size >> 0;
959 		dat[2] = session->total_message_size >> 8;
960 		dat[3] = session->total_message_size >> 16;
961 		dat[4] = session->total_message_size >> 24;
962 	} else {
963 		dat[0] = J1939_TP_CMD_EOMA;
964 		dat[1] = session->total_message_size;
965 		dat[2] = session->total_message_size >> 8;
966 		dat[3] = session->pkt.total;
967 	}
968 
969 	if (dat[0] == session->last_txcmd)
970 		/* done already */
971 		return 0;
972 
973 	ret = j1939_tp_tx_ctl(session, true, dat);
974 	if (ret < 0)
975 		return ret;
976 
977 	session->last_txcmd = dat[0];
978 
979 	/* wait for the EOMA packet to come in */
980 	j1939_tp_set_rxtimeout(session, 1250);
981 
982 	netdev_dbg(session->priv->ndev, "%p: 0x%p\n", __func__, session);
983 
984 	return 0;
985 }
986 
987 static int j1939_xtp_txnext_receiver(struct j1939_session *session)
988 {
989 	struct j1939_priv *priv = session->priv;
990 	int ret = 0;
991 
992 	if (!j1939_tp_im_receiver(&session->skcb)) {
993 		netdev_alert(priv->ndev, "%s: 0x%p: called by not receiver!\n",
994 			     __func__, session);
995 		return -EINVAL;
996 	}
997 
998 	switch (session->last_cmd) {
999 	case J1939_TP_CMD_RTS:
1000 	case J1939_ETP_CMD_RTS:
1001 		ret = j1939_session_tx_cts(session);
1002 		break;
1003 
1004 	case J1939_ETP_CMD_CTS:
1005 	case J1939_TP_CMD_CTS:
1006 	case 0xff: /* did some data */
1007 	case J1939_ETP_CMD_DPO:
1008 		if ((session->skcb.addr.type == J1939_TP &&
1009 		     j1939_cb_is_broadcast(&session->skcb)))
1010 			break;
1011 
1012 		if (session->pkt.rx >= session->pkt.total) {
1013 			ret = j1939_session_tx_eoma(session);
1014 		} else if (session->pkt.rx >= session->pkt.last) {
1015 			session->last_txcmd = 0;
1016 			ret = j1939_session_tx_cts(session);
1017 		}
1018 		break;
1019 	default:
1020 		netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
1021 			     __func__, session, session->last_cmd);
1022 	}
1023 
1024 	return ret;
1025 }
1026 
1027 static int j1939_simple_txnext(struct j1939_session *session)
1028 {
1029 	struct j1939_priv *priv = session->priv;
1030 	struct sk_buff *se_skb = j1939_session_skb_get(session);
1031 	struct sk_buff *skb;
1032 	int ret;
1033 
1034 	if (!se_skb)
1035 		return 0;
1036 
1037 	skb = skb_clone(se_skb, GFP_ATOMIC);
1038 	if (!skb) {
1039 		ret = -ENOMEM;
1040 		goto out_free;
1041 	}
1042 
1043 	can_skb_set_owner(skb, se_skb->sk);
1044 
1045 	j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS);
1046 
1047 	ret = j1939_send_one(priv, skb);
1048 	if (ret)
1049 		goto out_free;
1050 
1051 	j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_SCHED);
1052 	j1939_sk_queue_activate_next(session);
1053 
1054  out_free:
1055 	if (ret)
1056 		kfree_skb(se_skb);
1057 	else
1058 		consume_skb(se_skb);
1059 
1060 	return ret;
1061 }
1062 
1063 static bool j1939_session_deactivate_locked(struct j1939_session *session)
1064 {
1065 	bool active = false;
1066 
1067 	lockdep_assert_held(&session->priv->active_session_list_lock);
1068 
1069 	if (session->state >= J1939_SESSION_ACTIVE &&
1070 	    session->state < J1939_SESSION_ACTIVE_MAX) {
1071 		active = true;
1072 
1073 		list_del_init(&session->active_session_list_entry);
1074 		session->state = J1939_SESSION_DONE;
1075 		j1939_session_put(session);
1076 	}
1077 
1078 	return active;
1079 }
1080 
1081 static bool j1939_session_deactivate(struct j1939_session *session)
1082 {
1083 	struct j1939_priv *priv = session->priv;
1084 	bool active;
1085 
1086 	j1939_session_list_lock(priv);
1087 	/* This function should be called with a session ref-count of at
1088 	 * least 2.
1089 	 */
1090 	WARN_ON_ONCE(kref_read(&session->kref) < 2);
1091 	active = j1939_session_deactivate_locked(session);
1092 	j1939_session_list_unlock(priv);
1093 
1094 	return active;
1095 }
1096 
1097 static void
1098 j1939_session_deactivate_activate_next(struct j1939_session *session)
1099 {
1100 	if (j1939_session_deactivate(session))
1101 		j1939_sk_queue_activate_next(session);
1102 }
1103 
1104 static void __j1939_session_cancel(struct j1939_session *session,
1105 				   enum j1939_xtp_abort err)
1106 {
1107 	struct j1939_priv *priv = session->priv;
1108 
1109 	WARN_ON_ONCE(!err);
1110 	lockdep_assert_held(&session->priv->active_session_list_lock);
1111 
1112 	session->err = j1939_xtp_abort_to_errno(priv, err);
1113 	session->state = J1939_SESSION_WAITING_ABORT;
1114 	/* do not send aborts on incoming broadcasts */
1115 	if (!j1939_cb_is_broadcast(&session->skcb)) {
1116 		j1939_xtp_tx_abort(priv, &session->skcb,
1117 				   !session->transmission,
1118 				   err, session->skcb.addr.pgn);
1119 	}
1120 
1121 	if (session->sk)
1122 		j1939_sk_send_loop_abort(session->sk, session->err);
1123 	else
1124 		j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1125 }
1126 
1127 static void j1939_session_cancel(struct j1939_session *session,
1128 				 enum j1939_xtp_abort err)
1129 {
1130 	j1939_session_list_lock(session->priv);
1131 
1132 	if (session->state >= J1939_SESSION_ACTIVE &&
1133 	    session->state < J1939_SESSION_WAITING_ABORT) {
1134 		j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS);
1135 		__j1939_session_cancel(session, err);
1136 	}
1137 
1138 	j1939_session_list_unlock(session->priv);
1139 }
1140 
1141 static enum hrtimer_restart j1939_tp_txtimer(struct hrtimer *hrtimer)
1142 {
1143 	struct j1939_session *session =
1144 		container_of(hrtimer, struct j1939_session, txtimer);
1145 	struct j1939_priv *priv = session->priv;
1146 	int ret = 0;
1147 
1148 	if (session->skcb.addr.type == J1939_SIMPLE) {
1149 		ret = j1939_simple_txnext(session);
1150 	} else {
1151 		if (session->transmission)
1152 			ret = j1939_xtp_txnext_transmiter(session);
1153 		else
1154 			ret = j1939_xtp_txnext_receiver(session);
1155 	}
1156 
1157 	switch (ret) {
1158 	case -ENOBUFS:
1159 		/* Retry limit is currently arbitrary chosen */
1160 		if (session->tx_retry < J1939_XTP_TX_RETRY_LIMIT) {
1161 			session->tx_retry++;
1162 			j1939_tp_schedule_txtimer(session,
1163 						  10 + prandom_u32_max(16));
1164 		} else {
1165 			netdev_alert(priv->ndev, "%s: 0x%p: tx retry count reached\n",
1166 				     __func__, session);
1167 			session->err = -ENETUNREACH;
1168 			j1939_session_rxtimer_cancel(session);
1169 			j1939_session_deactivate_activate_next(session);
1170 		}
1171 		break;
1172 	case -ENETDOWN:
1173 		/* In this case we should get a netdev_event(), all active
1174 		 * sessions will be cleared by
1175 		 * j1939_cancel_all_active_sessions(). So handle this as an
1176 		 * error, but let j1939_cancel_all_active_sessions() do the
1177 		 * cleanup including propagation of the error to user space.
1178 		 */
1179 		break;
1180 	case -EOVERFLOW:
1181 		j1939_session_cancel(session, J1939_XTP_ABORT_ECTS_TOO_BIG);
1182 		break;
1183 	case 0:
1184 		session->tx_retry = 0;
1185 		break;
1186 	default:
1187 		netdev_alert(priv->ndev, "%s: 0x%p: tx aborted with unknown reason: %i\n",
1188 			     __func__, session, ret);
1189 		if (session->skcb.addr.type != J1939_SIMPLE) {
1190 			j1939_session_cancel(session, J1939_XTP_ABORT_OTHER);
1191 		} else {
1192 			session->err = ret;
1193 			j1939_session_rxtimer_cancel(session);
1194 			j1939_session_deactivate_activate_next(session);
1195 		}
1196 	}
1197 
1198 	j1939_session_put(session);
1199 
1200 	return HRTIMER_NORESTART;
1201 }
1202 
1203 static void j1939_session_completed(struct j1939_session *session)
1204 {
1205 	struct sk_buff *se_skb;
1206 
1207 	if (!session->transmission) {
1208 		se_skb = j1939_session_skb_get(session);
1209 		/* distribute among j1939 receivers */
1210 		j1939_sk_recv(session->priv, se_skb);
1211 		consume_skb(se_skb);
1212 	}
1213 
1214 	j1939_session_deactivate_activate_next(session);
1215 }
1216 
1217 static enum hrtimer_restart j1939_tp_rxtimer(struct hrtimer *hrtimer)
1218 {
1219 	struct j1939_session *session = container_of(hrtimer,
1220 						     struct j1939_session,
1221 						     rxtimer);
1222 	struct j1939_priv *priv = session->priv;
1223 
1224 	if (session->state == J1939_SESSION_WAITING_ABORT) {
1225 		netdev_alert(priv->ndev, "%s: 0x%p: abort rx timeout. Force session deactivation\n",
1226 			     __func__, session);
1227 
1228 		j1939_session_deactivate_activate_next(session);
1229 
1230 	} else if (session->skcb.addr.type == J1939_SIMPLE) {
1231 		netdev_alert(priv->ndev, "%s: 0x%p: Timeout. Failed to send simple message.\n",
1232 			     __func__, session);
1233 
1234 		/* The message is probably stuck in the CAN controller and can
1235 		 * be send as soon as CAN bus is in working state again.
1236 		 */
1237 		session->err = -ETIME;
1238 		j1939_session_deactivate(session);
1239 	} else {
1240 		netdev_alert(priv->ndev, "%s: 0x%p: rx timeout, send abort\n",
1241 			     __func__, session);
1242 
1243 		j1939_session_list_lock(session->priv);
1244 		if (session->state >= J1939_SESSION_ACTIVE &&
1245 		    session->state < J1939_SESSION_ACTIVE_MAX) {
1246 			j1939_session_get(session);
1247 			hrtimer_start(&session->rxtimer,
1248 				      ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS),
1249 				      HRTIMER_MODE_REL_SOFT);
1250 			__j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
1251 		}
1252 		j1939_session_list_unlock(session->priv);
1253 	}
1254 
1255 	j1939_session_put(session);
1256 
1257 	return HRTIMER_NORESTART;
1258 }
1259 
1260 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session *session,
1261 				     const struct sk_buff *skb)
1262 {
1263 	const struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1264 	pgn_t pgn = j1939_xtp_ctl_to_pgn(skb->data);
1265 	struct j1939_priv *priv = session->priv;
1266 	enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1267 	u8 cmd = skb->data[0];
1268 
1269 	if (session->skcb.addr.pgn == pgn)
1270 		return false;
1271 
1272 	switch (cmd) {
1273 	case J1939_TP_CMD_BAM:
1274 		abort = J1939_XTP_NO_ABORT;
1275 		break;
1276 
1277 	case J1939_ETP_CMD_RTS:
1278 		fallthrough;
1279 	case J1939_TP_CMD_RTS:
1280 		abort = J1939_XTP_ABORT_BUSY;
1281 		break;
1282 
1283 	case J1939_ETP_CMD_CTS:
1284 		fallthrough;
1285 	case J1939_TP_CMD_CTS:
1286 		abort = J1939_XTP_ABORT_ECTS_UNXPECTED_PGN;
1287 		break;
1288 
1289 	case J1939_ETP_CMD_DPO:
1290 		abort = J1939_XTP_ABORT_BAD_EDPO_PGN;
1291 		break;
1292 
1293 	case J1939_ETP_CMD_EOMA:
1294 		fallthrough;
1295 	case J1939_TP_CMD_EOMA:
1296 		abort = J1939_XTP_ABORT_OTHER;
1297 		break;
1298 
1299 	case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
1300 		abort = J1939_XTP_NO_ABORT;
1301 		break;
1302 
1303 	default:
1304 		WARN_ON_ONCE(1);
1305 		break;
1306 	}
1307 
1308 	netdev_warn(priv->ndev, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n",
1309 		    __func__, session, cmd, pgn, session->skcb.addr.pgn);
1310 	if (abort != J1939_XTP_NO_ABORT)
1311 		j1939_xtp_tx_abort(priv, skcb, true, abort, pgn);
1312 
1313 	return true;
1314 }
1315 
1316 static void j1939_xtp_rx_abort_one(struct j1939_priv *priv, struct sk_buff *skb,
1317 				   bool reverse, bool transmitter)
1318 {
1319 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1320 	struct j1939_session *session;
1321 	u8 abort = skb->data[1];
1322 
1323 	session = j1939_session_get_by_addr(priv, &skcb->addr, reverse,
1324 					    transmitter);
1325 	if (!session)
1326 		return;
1327 
1328 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1329 		goto abort_put;
1330 
1331 	netdev_info(priv->ndev, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__,
1332 		    session, j1939_xtp_ctl_to_pgn(skb->data), abort,
1333 		    j1939_xtp_abort_to_str(abort));
1334 
1335 	j1939_session_timers_cancel(session);
1336 	session->err = j1939_xtp_abort_to_errno(priv, abort);
1337 	if (session->sk)
1338 		j1939_sk_send_loop_abort(session->sk, session->err);
1339 	else
1340 		j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
1341 	j1939_session_deactivate_activate_next(session);
1342 
1343 abort_put:
1344 	j1939_session_put(session);
1345 }
1346 
1347 /* abort packets may come in 2 directions */
1348 static void
1349 j1939_xtp_rx_abort(struct j1939_priv *priv, struct sk_buff *skb,
1350 		   bool transmitter)
1351 {
1352 	j1939_xtp_rx_abort_one(priv, skb, false, transmitter);
1353 	j1939_xtp_rx_abort_one(priv, skb, true, transmitter);
1354 }
1355 
1356 static void
1357 j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
1358 {
1359 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1360 	const u8 *dat;
1361 	int len;
1362 
1363 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1364 		return;
1365 
1366 	dat = skb->data;
1367 
1368 	if (skcb->addr.type == J1939_ETP)
1369 		len = j1939_etp_ctl_to_size(dat);
1370 	else
1371 		len = j1939_tp_ctl_to_size(dat);
1372 
1373 	if (session->total_message_size != len) {
1374 		netdev_warn_once(session->priv->ndev,
1375 				 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
1376 				 __func__, session, session->total_message_size,
1377 				 len);
1378 	}
1379 
1380 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1381 
1382 	session->pkt.tx_acked = session->pkt.total;
1383 	j1939_session_timers_cancel(session);
1384 	/* transmitted without problems */
1385 	j1939_session_completed(session);
1386 }
1387 
1388 static void
1389 j1939_xtp_rx_eoma(struct j1939_priv *priv, struct sk_buff *skb,
1390 		  bool transmitter)
1391 {
1392 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1393 	struct j1939_session *session;
1394 
1395 	session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1396 					    transmitter);
1397 	if (!session)
1398 		return;
1399 
1400 	j1939_xtp_rx_eoma_one(session, skb);
1401 	j1939_session_put(session);
1402 }
1403 
1404 static void
1405 j1939_xtp_rx_cts_one(struct j1939_session *session, struct sk_buff *skb)
1406 {
1407 	enum j1939_xtp_abort err = J1939_XTP_ABORT_FAULT;
1408 	unsigned int pkt;
1409 	const u8 *dat;
1410 
1411 	dat = skb->data;
1412 
1413 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1414 		return;
1415 
1416 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1417 
1418 	if (session->last_cmd == dat[0]) {
1419 		err = J1939_XTP_ABORT_DUP_SEQ;
1420 		goto out_session_cancel;
1421 	}
1422 
1423 	if (session->skcb.addr.type == J1939_ETP)
1424 		pkt = j1939_etp_ctl_to_packet(dat);
1425 	else
1426 		pkt = dat[2];
1427 
1428 	if (!pkt)
1429 		goto out_session_cancel;
1430 	else if (dat[1] > session->pkt.block /* 0xff for etp */)
1431 		goto out_session_cancel;
1432 
1433 	/* set packet counters only when not CTS(0) */
1434 	session->pkt.tx_acked = pkt - 1;
1435 	j1939_session_skb_drop_old(session);
1436 	session->pkt.last = session->pkt.tx_acked + dat[1];
1437 	if (session->pkt.last > session->pkt.total)
1438 		/* safety measure */
1439 		session->pkt.last = session->pkt.total;
1440 	/* TODO: do not set tx here, do it in txtimer */
1441 	session->pkt.tx = session->pkt.tx_acked;
1442 
1443 	session->last_cmd = dat[0];
1444 	if (dat[1]) {
1445 		j1939_tp_set_rxtimeout(session, 1250);
1446 		if (session->transmission) {
1447 			if (session->pkt.tx_acked)
1448 				j1939_sk_errqueue(session,
1449 						  J1939_ERRQUEUE_TX_SCHED);
1450 			j1939_session_txtimer_cancel(session);
1451 			j1939_tp_schedule_txtimer(session, 0);
1452 		}
1453 	} else {
1454 		/* CTS(0) */
1455 		j1939_tp_set_rxtimeout(session, 550);
1456 	}
1457 	return;
1458 
1459  out_session_cancel:
1460 	j1939_session_timers_cancel(session);
1461 	j1939_session_cancel(session, err);
1462 }
1463 
1464 static void
1465 j1939_xtp_rx_cts(struct j1939_priv *priv, struct sk_buff *skb, bool transmitter)
1466 {
1467 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1468 	struct j1939_session *session;
1469 
1470 	session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1471 					    transmitter);
1472 	if (!session)
1473 		return;
1474 	j1939_xtp_rx_cts_one(session, skb);
1475 	j1939_session_put(session);
1476 }
1477 
1478 static struct j1939_session *j1939_session_new(struct j1939_priv *priv,
1479 					       struct sk_buff *skb, size_t size)
1480 {
1481 	struct j1939_session *session;
1482 	struct j1939_sk_buff_cb *skcb;
1483 
1484 	session = kzalloc(sizeof(*session), gfp_any());
1485 	if (!session)
1486 		return NULL;
1487 
1488 	INIT_LIST_HEAD(&session->active_session_list_entry);
1489 	INIT_LIST_HEAD(&session->sk_session_queue_entry);
1490 	kref_init(&session->kref);
1491 
1492 	j1939_priv_get(priv);
1493 	session->priv = priv;
1494 	session->total_message_size = size;
1495 	session->state = J1939_SESSION_NEW;
1496 
1497 	skb_queue_head_init(&session->skb_queue);
1498 	skb_queue_tail(&session->skb_queue, skb);
1499 
1500 	skcb = j1939_skb_to_cb(skb);
1501 	memcpy(&session->skcb, skcb, sizeof(session->skcb));
1502 
1503 	hrtimer_init(&session->txtimer, CLOCK_MONOTONIC,
1504 		     HRTIMER_MODE_REL_SOFT);
1505 	session->txtimer.function = j1939_tp_txtimer;
1506 	hrtimer_init(&session->rxtimer, CLOCK_MONOTONIC,
1507 		     HRTIMER_MODE_REL_SOFT);
1508 	session->rxtimer.function = j1939_tp_rxtimer;
1509 
1510 	netdev_dbg(priv->ndev, "%s: 0x%p: sa: %02x, da: %02x\n",
1511 		   __func__, session, skcb->addr.sa, skcb->addr.da);
1512 
1513 	return session;
1514 }
1515 
1516 static struct
1517 j1939_session *j1939_session_fresh_new(struct j1939_priv *priv,
1518 				       int size,
1519 				       const struct j1939_sk_buff_cb *rel_skcb)
1520 {
1521 	struct sk_buff *skb;
1522 	struct j1939_sk_buff_cb *skcb;
1523 	struct j1939_session *session;
1524 
1525 	skb = alloc_skb(size + sizeof(struct can_skb_priv), GFP_ATOMIC);
1526 	if (unlikely(!skb))
1527 		return NULL;
1528 
1529 	skb->dev = priv->ndev;
1530 	can_skb_reserve(skb);
1531 	can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
1532 	can_skb_prv(skb)->skbcnt = 0;
1533 	skcb = j1939_skb_to_cb(skb);
1534 	memcpy(skcb, rel_skcb, sizeof(*skcb));
1535 
1536 	session = j1939_session_new(priv, skb, size);
1537 	if (!session) {
1538 		kfree_skb(skb);
1539 		return NULL;
1540 	}
1541 
1542 	/* alloc data area */
1543 	skb_put(skb, size);
1544 	/* skb is recounted in j1939_session_new() */
1545 	return session;
1546 }
1547 
1548 int j1939_session_activate(struct j1939_session *session)
1549 {
1550 	struct j1939_priv *priv = session->priv;
1551 	struct j1939_session *active = NULL;
1552 	int ret = 0;
1553 
1554 	j1939_session_list_lock(priv);
1555 	if (session->skcb.addr.type != J1939_SIMPLE)
1556 		active = j1939_session_get_by_addr_locked(priv,
1557 							  &priv->active_session_list,
1558 							  &session->skcb.addr, false,
1559 							  session->transmission);
1560 	if (active) {
1561 		j1939_session_put(active);
1562 		ret = -EAGAIN;
1563 	} else {
1564 		WARN_ON_ONCE(session->state != J1939_SESSION_NEW);
1565 		list_add_tail(&session->active_session_list_entry,
1566 			      &priv->active_session_list);
1567 		j1939_session_get(session);
1568 		session->state = J1939_SESSION_ACTIVE;
1569 
1570 		netdev_dbg(session->priv->ndev, "%s: 0x%p\n",
1571 			   __func__, session);
1572 	}
1573 	j1939_session_list_unlock(priv);
1574 
1575 	return ret;
1576 }
1577 
1578 static struct
1579 j1939_session *j1939_xtp_rx_rts_session_new(struct j1939_priv *priv,
1580 					    struct sk_buff *skb)
1581 {
1582 	enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1583 	struct j1939_sk_buff_cb skcb = *j1939_skb_to_cb(skb);
1584 	struct j1939_session *session;
1585 	const u8 *dat;
1586 	pgn_t pgn;
1587 	int len;
1588 
1589 	netdev_dbg(priv->ndev, "%s\n", __func__);
1590 
1591 	dat = skb->data;
1592 	pgn = j1939_xtp_ctl_to_pgn(dat);
1593 	skcb.addr.pgn = pgn;
1594 
1595 	if (!j1939_sk_recv_match(priv, &skcb))
1596 		return NULL;
1597 
1598 	if (skcb.addr.type == J1939_ETP) {
1599 		len = j1939_etp_ctl_to_size(dat);
1600 		if (len > J1939_MAX_ETP_PACKET_SIZE)
1601 			abort = J1939_XTP_ABORT_FAULT;
1602 		else if (len > priv->tp_max_packet_size)
1603 			abort = J1939_XTP_ABORT_RESOURCE;
1604 		else if (len <= J1939_MAX_TP_PACKET_SIZE)
1605 			abort = J1939_XTP_ABORT_FAULT;
1606 	} else {
1607 		len = j1939_tp_ctl_to_size(dat);
1608 		if (len > J1939_MAX_TP_PACKET_SIZE)
1609 			abort = J1939_XTP_ABORT_FAULT;
1610 		else if (len > priv->tp_max_packet_size)
1611 			abort = J1939_XTP_ABORT_RESOURCE;
1612 	}
1613 
1614 	if (abort != J1939_XTP_NO_ABORT) {
1615 		j1939_xtp_tx_abort(priv, &skcb, true, abort, pgn);
1616 		return NULL;
1617 	}
1618 
1619 	session = j1939_session_fresh_new(priv, len, &skcb);
1620 	if (!session) {
1621 		j1939_xtp_tx_abort(priv, &skcb, true,
1622 				   J1939_XTP_ABORT_RESOURCE, pgn);
1623 		return NULL;
1624 	}
1625 
1626 	/* initialize the control buffer: plain copy */
1627 	session->pkt.total = (len + 6) / 7;
1628 	session->pkt.block = 0xff;
1629 	if (skcb.addr.type != J1939_ETP) {
1630 		if (dat[3] != session->pkt.total)
1631 			netdev_alert(priv->ndev, "%s: 0x%p: strange total, %u != %u\n",
1632 				     __func__, session, session->pkt.total,
1633 				     dat[3]);
1634 		session->pkt.total = dat[3];
1635 		session->pkt.block = min(dat[3], dat[4]);
1636 	}
1637 
1638 	session->pkt.rx = 0;
1639 	session->pkt.tx = 0;
1640 
1641 	session->tskey = priv->rx_tskey++;
1642 	j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_RTS);
1643 
1644 	WARN_ON_ONCE(j1939_session_activate(session));
1645 
1646 	return session;
1647 }
1648 
1649 static int j1939_xtp_rx_rts_session_active(struct j1939_session *session,
1650 					   struct sk_buff *skb)
1651 {
1652 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1653 	struct j1939_priv *priv = session->priv;
1654 
1655 	if (!session->transmission) {
1656 		if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1657 			return -EBUSY;
1658 
1659 		/* RTS on active session */
1660 		j1939_session_timers_cancel(session);
1661 		j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1662 	}
1663 
1664 	if (session->last_cmd != 0) {
1665 		/* we received a second rts on the same connection */
1666 		netdev_alert(priv->ndev, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n",
1667 			     __func__, session, skcb->addr.sa, skcb->addr.da,
1668 			     session->last_cmd);
1669 
1670 		j1939_session_timers_cancel(session);
1671 		j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1672 
1673 		return -EBUSY;
1674 	}
1675 
1676 	if (session->skcb.addr.sa != skcb->addr.sa ||
1677 	    session->skcb.addr.da != skcb->addr.da)
1678 		netdev_warn(priv->ndev, "%s: 0x%p: session->skcb.addr.sa=0x%02x skcb->addr.sa=0x%02x session->skcb.addr.da=0x%02x skcb->addr.da=0x%02x\n",
1679 			    __func__, session,
1680 			    session->skcb.addr.sa, skcb->addr.sa,
1681 			    session->skcb.addr.da, skcb->addr.da);
1682 	/* make sure 'sa' & 'da' are correct !
1683 	 * They may be 'not filled in yet' for sending
1684 	 * skb's, since they did not pass the Address Claim ever.
1685 	 */
1686 	session->skcb.addr.sa = skcb->addr.sa;
1687 	session->skcb.addr.da = skcb->addr.da;
1688 
1689 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1690 
1691 	return 0;
1692 }
1693 
1694 static void j1939_xtp_rx_rts(struct j1939_priv *priv, struct sk_buff *skb,
1695 			     bool transmitter)
1696 {
1697 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1698 	struct j1939_session *session;
1699 	u8 cmd = skb->data[0];
1700 
1701 	session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1702 					    transmitter);
1703 
1704 	if (!session) {
1705 		if (transmitter) {
1706 			/* If we're the transmitter and this function is called,
1707 			 * we received our own RTS. A session has already been
1708 			 * created.
1709 			 *
1710 			 * For some reasons however it might have been destroyed
1711 			 * already. So don't create a new one here (using
1712 			 * "j1939_xtp_rx_rts_session_new()") as this will be a
1713 			 * receiver session.
1714 			 *
1715 			 * The reasons the session is already destroyed might
1716 			 * be:
1717 			 * - user space closed socket was and the session was
1718 			 *   aborted
1719 			 * - session was aborted due to external abort message
1720 			 */
1721 			return;
1722 		}
1723 		session = j1939_xtp_rx_rts_session_new(priv, skb);
1724 		if (!session) {
1725 			if (cmd == J1939_TP_CMD_BAM && j1939_sk_recv_match(priv, skcb))
1726 				netdev_info(priv->ndev, "%s: failed to create TP BAM session\n",
1727 					    __func__);
1728 			return;
1729 		}
1730 	} else {
1731 		if (j1939_xtp_rx_rts_session_active(session, skb)) {
1732 			j1939_session_put(session);
1733 			return;
1734 		}
1735 	}
1736 	session->last_cmd = cmd;
1737 
1738 	if (cmd == J1939_TP_CMD_BAM) {
1739 		if (!session->transmission)
1740 			j1939_tp_set_rxtimeout(session, 750);
1741 	} else {
1742 		if (!session->transmission) {
1743 			j1939_session_txtimer_cancel(session);
1744 			j1939_tp_schedule_txtimer(session, 0);
1745 		}
1746 		j1939_tp_set_rxtimeout(session, 1250);
1747 	}
1748 
1749 	j1939_session_put(session);
1750 }
1751 
1752 static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
1753 				 struct sk_buff *skb)
1754 {
1755 	const u8 *dat = skb->data;
1756 
1757 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1758 		return;
1759 
1760 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1761 
1762 	/* transmitted without problems */
1763 	session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
1764 	session->last_cmd = dat[0];
1765 	j1939_tp_set_rxtimeout(session, 750);
1766 
1767 	if (!session->transmission)
1768 		j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_DPO);
1769 }
1770 
1771 static void j1939_xtp_rx_dpo(struct j1939_priv *priv, struct sk_buff *skb,
1772 			     bool transmitter)
1773 {
1774 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1775 	struct j1939_session *session;
1776 
1777 	session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1778 					    transmitter);
1779 	if (!session) {
1780 		netdev_info(priv->ndev,
1781 			    "%s: no connection found\n", __func__);
1782 		return;
1783 	}
1784 
1785 	j1939_xtp_rx_dpo_one(session, skb);
1786 	j1939_session_put(session);
1787 }
1788 
1789 static void j1939_xtp_rx_dat_one(struct j1939_session *session,
1790 				 struct sk_buff *skb)
1791 {
1792 	struct j1939_priv *priv = session->priv;
1793 	struct j1939_sk_buff_cb *skcb, *se_skcb;
1794 	struct sk_buff *se_skb = NULL;
1795 	const u8 *dat;
1796 	u8 *tpdat;
1797 	int offset;
1798 	int nbytes;
1799 	bool final = false;
1800 	bool remain = false;
1801 	bool do_cts_eoma = false;
1802 	int packet;
1803 
1804 	skcb = j1939_skb_to_cb(skb);
1805 	dat = skb->data;
1806 	if (skb->len <= 1)
1807 		/* makes no sense */
1808 		goto out_session_cancel;
1809 
1810 	switch (session->last_cmd) {
1811 	case 0xff:
1812 		break;
1813 	case J1939_ETP_CMD_DPO:
1814 		if (skcb->addr.type == J1939_ETP)
1815 			break;
1816 		fallthrough;
1817 	case J1939_TP_CMD_BAM:
1818 		fallthrough;
1819 	case J1939_TP_CMD_CTS:
1820 		if (skcb->addr.type != J1939_ETP)
1821 			break;
1822 		fallthrough;
1823 	default:
1824 		netdev_info(priv->ndev, "%s: 0x%p: last %02x\n", __func__,
1825 			    session, session->last_cmd);
1826 		goto out_session_cancel;
1827 	}
1828 
1829 	packet = (dat[0] - 1 + session->pkt.dpo);
1830 	if (packet > session->pkt.total ||
1831 	    (session->pkt.rx + 1) > session->pkt.total) {
1832 		netdev_info(priv->ndev, "%s: 0x%p: should have been completed\n",
1833 			    __func__, session);
1834 		goto out_session_cancel;
1835 	}
1836 
1837 	se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
1838 	if (!se_skb) {
1839 		netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
1840 			    session);
1841 		goto out_session_cancel;
1842 	}
1843 
1844 	se_skcb = j1939_skb_to_cb(se_skb);
1845 	offset = packet * 7 - se_skcb->offset;
1846 	nbytes = se_skb->len - offset;
1847 	if (nbytes > 7)
1848 		nbytes = 7;
1849 	if (nbytes <= 0 || (nbytes + 1) > skb->len) {
1850 		netdev_info(priv->ndev, "%s: 0x%p: nbytes %i, len %i\n",
1851 			    __func__, session, nbytes, skb->len);
1852 		goto out_session_cancel;
1853 	}
1854 
1855 	tpdat = se_skb->data;
1856 	if (!session->transmission) {
1857 		memcpy(&tpdat[offset], &dat[1], nbytes);
1858 	} else {
1859 		int err;
1860 
1861 		err = memcmp(&tpdat[offset], &dat[1], nbytes);
1862 		if (err)
1863 			netdev_err_once(priv->ndev,
1864 					"%s: 0x%p: Data of RX-looped back packet (%*ph) doesn't match TX data (%*ph)!\n",
1865 					__func__, session,
1866 					nbytes, &dat[1],
1867 					nbytes, &tpdat[offset]);
1868 	}
1869 
1870 	if (packet == session->pkt.rx)
1871 		session->pkt.rx++;
1872 
1873 	if (se_skcb->addr.type != J1939_ETP &&
1874 	    j1939_cb_is_broadcast(&session->skcb)) {
1875 		if (session->pkt.rx >= session->pkt.total)
1876 			final = true;
1877 		else
1878 			remain = true;
1879 	} else {
1880 		/* never final, an EOMA must follow */
1881 		if (session->pkt.rx >= session->pkt.last)
1882 			do_cts_eoma = true;
1883 	}
1884 
1885 	if (final) {
1886 		j1939_session_timers_cancel(session);
1887 		j1939_session_completed(session);
1888 	} else if (remain) {
1889 		if (!session->transmission)
1890 			j1939_tp_set_rxtimeout(session, 750);
1891 	} else if (do_cts_eoma) {
1892 		j1939_tp_set_rxtimeout(session, 1250);
1893 		if (!session->transmission)
1894 			j1939_tp_schedule_txtimer(session, 0);
1895 	} else {
1896 		j1939_tp_set_rxtimeout(session, 750);
1897 	}
1898 	session->last_cmd = 0xff;
1899 	consume_skb(se_skb);
1900 	j1939_session_put(session);
1901 
1902 	return;
1903 
1904  out_session_cancel:
1905 	kfree_skb(se_skb);
1906 	j1939_session_timers_cancel(session);
1907 	j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
1908 	j1939_session_put(session);
1909 }
1910 
1911 static void j1939_xtp_rx_dat(struct j1939_priv *priv, struct sk_buff *skb)
1912 {
1913 	struct j1939_sk_buff_cb *skcb;
1914 	struct j1939_session *session;
1915 
1916 	skcb = j1939_skb_to_cb(skb);
1917 
1918 	if (j1939_tp_im_transmitter(skcb)) {
1919 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1920 						    true);
1921 		if (!session)
1922 			netdev_info(priv->ndev, "%s: no tx connection found\n",
1923 				    __func__);
1924 		else
1925 			j1939_xtp_rx_dat_one(session, skb);
1926 	}
1927 
1928 	if (j1939_tp_im_receiver(skcb)) {
1929 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1930 						    false);
1931 		if (!session)
1932 			netdev_info(priv->ndev, "%s: no rx connection found\n",
1933 				    __func__);
1934 		else
1935 			j1939_xtp_rx_dat_one(session, skb);
1936 	}
1937 
1938 	if (j1939_cb_is_broadcast(skcb)) {
1939 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1940 						    false);
1941 		if (session)
1942 			j1939_xtp_rx_dat_one(session, skb);
1943 	}
1944 }
1945 
1946 /* j1939 main intf */
1947 struct j1939_session *j1939_tp_send(struct j1939_priv *priv,
1948 				    struct sk_buff *skb, size_t size)
1949 {
1950 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1951 	struct j1939_session *session;
1952 	int ret;
1953 
1954 	if (skcb->addr.pgn == J1939_TP_PGN_DAT ||
1955 	    skcb->addr.pgn == J1939_TP_PGN_CTL ||
1956 	    skcb->addr.pgn == J1939_ETP_PGN_DAT ||
1957 	    skcb->addr.pgn == J1939_ETP_PGN_CTL)
1958 		/* avoid conflict */
1959 		return ERR_PTR(-EDOM);
1960 
1961 	if (size > priv->tp_max_packet_size)
1962 		return ERR_PTR(-EMSGSIZE);
1963 
1964 	if (size <= 8)
1965 		skcb->addr.type = J1939_SIMPLE;
1966 	else if (size > J1939_MAX_TP_PACKET_SIZE)
1967 		skcb->addr.type = J1939_ETP;
1968 	else
1969 		skcb->addr.type = J1939_TP;
1970 
1971 	if (skcb->addr.type == J1939_ETP &&
1972 	    j1939_cb_is_broadcast(skcb))
1973 		return ERR_PTR(-EDESTADDRREQ);
1974 
1975 	/* fill in addresses from names */
1976 	ret = j1939_ac_fixup(priv, skb);
1977 	if (unlikely(ret))
1978 		return ERR_PTR(ret);
1979 
1980 	/* fix DST flags, it may be used there soon */
1981 	if (j1939_address_is_unicast(skcb->addr.da) &&
1982 	    priv->ents[skcb->addr.da].nusers)
1983 		skcb->flags |= J1939_ECU_LOCAL_DST;
1984 
1985 	/* src is always local, I'm sending ... */
1986 	skcb->flags |= J1939_ECU_LOCAL_SRC;
1987 
1988 	/* prepare new session */
1989 	session = j1939_session_new(priv, skb, size);
1990 	if (!session)
1991 		return ERR_PTR(-ENOMEM);
1992 
1993 	/* skb is recounted in j1939_session_new() */
1994 	sock_hold(skb->sk);
1995 	session->sk = skb->sk;
1996 	session->transmission = true;
1997 	session->pkt.total = (size + 6) / 7;
1998 	session->pkt.block = skcb->addr.type == J1939_ETP ? 255 :
1999 		min(j1939_tp_block ?: 255, session->pkt.total);
2000 
2001 	if (j1939_cb_is_broadcast(&session->skcb))
2002 		/* set the end-packet for broadcast */
2003 		session->pkt.last = session->pkt.total;
2004 
2005 	skcb->tskey = session->sk->sk_tskey++;
2006 	session->tskey = skcb->tskey;
2007 
2008 	return session;
2009 }
2010 
2011 static void j1939_tp_cmd_recv(struct j1939_priv *priv, struct sk_buff *skb)
2012 {
2013 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2014 	int extd = J1939_TP;
2015 	u8 cmd = skb->data[0];
2016 
2017 	switch (cmd) {
2018 	case J1939_ETP_CMD_RTS:
2019 		extd = J1939_ETP;
2020 		fallthrough;
2021 	case J1939_TP_CMD_BAM:
2022 		fallthrough;
2023 	case J1939_TP_CMD_RTS:
2024 		if (skcb->addr.type != extd)
2025 			return;
2026 
2027 		if (cmd == J1939_TP_CMD_RTS && j1939_cb_is_broadcast(skcb)) {
2028 			netdev_alert(priv->ndev, "%s: rts without destination (%02x)\n",
2029 				     __func__, skcb->addr.sa);
2030 			return;
2031 		}
2032 
2033 		if (j1939_tp_im_transmitter(skcb))
2034 			j1939_xtp_rx_rts(priv, skb, true);
2035 
2036 		if (j1939_tp_im_receiver(skcb) || j1939_cb_is_broadcast(skcb))
2037 			j1939_xtp_rx_rts(priv, skb, false);
2038 
2039 		break;
2040 
2041 	case J1939_ETP_CMD_CTS:
2042 		extd = J1939_ETP;
2043 		fallthrough;
2044 	case J1939_TP_CMD_CTS:
2045 		if (skcb->addr.type != extd)
2046 			return;
2047 
2048 		if (j1939_tp_im_transmitter(skcb))
2049 			j1939_xtp_rx_cts(priv, skb, false);
2050 
2051 		if (j1939_tp_im_receiver(skcb))
2052 			j1939_xtp_rx_cts(priv, skb, true);
2053 
2054 		break;
2055 
2056 	case J1939_ETP_CMD_DPO:
2057 		if (skcb->addr.type != J1939_ETP)
2058 			return;
2059 
2060 		if (j1939_tp_im_transmitter(skcb))
2061 			j1939_xtp_rx_dpo(priv, skb, true);
2062 
2063 		if (j1939_tp_im_receiver(skcb))
2064 			j1939_xtp_rx_dpo(priv, skb, false);
2065 
2066 		break;
2067 
2068 	case J1939_ETP_CMD_EOMA:
2069 		extd = J1939_ETP;
2070 		fallthrough;
2071 	case J1939_TP_CMD_EOMA:
2072 		if (skcb->addr.type != extd)
2073 			return;
2074 
2075 		if (j1939_tp_im_transmitter(skcb))
2076 			j1939_xtp_rx_eoma(priv, skb, false);
2077 
2078 		if (j1939_tp_im_receiver(skcb))
2079 			j1939_xtp_rx_eoma(priv, skb, true);
2080 
2081 		break;
2082 
2083 	case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
2084 		if (j1939_tp_im_transmitter(skcb))
2085 			j1939_xtp_rx_abort(priv, skb, true);
2086 
2087 		if (j1939_tp_im_receiver(skcb))
2088 			j1939_xtp_rx_abort(priv, skb, false);
2089 
2090 		break;
2091 	default:
2092 		return;
2093 	}
2094 }
2095 
2096 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb)
2097 {
2098 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2099 
2100 	if (!j1939_tp_im_involved_anydir(skcb) && !j1939_cb_is_broadcast(skcb))
2101 		return 0;
2102 
2103 	switch (skcb->addr.pgn) {
2104 	case J1939_ETP_PGN_DAT:
2105 		skcb->addr.type = J1939_ETP;
2106 		fallthrough;
2107 	case J1939_TP_PGN_DAT:
2108 		j1939_xtp_rx_dat(priv, skb);
2109 		break;
2110 
2111 	case J1939_ETP_PGN_CTL:
2112 		skcb->addr.type = J1939_ETP;
2113 		fallthrough;
2114 	case J1939_TP_PGN_CTL:
2115 		if (skb->len < 8)
2116 			return 0; /* Don't care. Nothing to extract here */
2117 
2118 		j1939_tp_cmd_recv(priv, skb);
2119 		break;
2120 	default:
2121 		return 0; /* no problem */
2122 	}
2123 	return 1; /* "I processed the message" */
2124 }
2125 
2126 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb)
2127 {
2128 	struct j1939_session *session;
2129 
2130 	if (!skb->sk)
2131 		return;
2132 
2133 	if (skb->sk->sk_family != AF_CAN ||
2134 	    skb->sk->sk_protocol != CAN_J1939)
2135 		return;
2136 
2137 	j1939_session_list_lock(priv);
2138 	session = j1939_session_get_simple(priv, skb);
2139 	j1939_session_list_unlock(priv);
2140 	if (!session) {
2141 		netdev_warn(priv->ndev,
2142 			    "%s: Received already invalidated message\n",
2143 			    __func__);
2144 		return;
2145 	}
2146 
2147 	j1939_session_timers_cancel(session);
2148 	j1939_session_deactivate(session);
2149 	j1939_session_put(session);
2150 }
2151 
2152 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk)
2153 {
2154 	struct j1939_session *session, *saved;
2155 
2156 	netdev_dbg(priv->ndev, "%s, sk: %p\n", __func__, sk);
2157 	j1939_session_list_lock(priv);
2158 	list_for_each_entry_safe(session, saved,
2159 				 &priv->active_session_list,
2160 				 active_session_list_entry) {
2161 		if (!sk || sk == session->sk) {
2162 			if (hrtimer_try_to_cancel(&session->txtimer) == 1)
2163 				j1939_session_put(session);
2164 			if (hrtimer_try_to_cancel(&session->rxtimer) == 1)
2165 				j1939_session_put(session);
2166 
2167 			session->err = ESHUTDOWN;
2168 			j1939_session_deactivate_locked(session);
2169 		}
2170 	}
2171 	j1939_session_list_unlock(priv);
2172 	return NOTIFY_DONE;
2173 }
2174 
2175 void j1939_tp_init(struct j1939_priv *priv)
2176 {
2177 	spin_lock_init(&priv->active_session_list_lock);
2178 	INIT_LIST_HEAD(&priv->active_session_list);
2179 	priv->tp_max_packet_size = J1939_MAX_ETP_PACKET_SIZE;
2180 }
2181