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