xref: /linux/net/can/j1939/transport.c (revision 3d8b92472ae7ba91d759cadb4670bd492ef97d04)
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 *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 	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) - 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, skcb->offset, se_skb->len , session->pkt.tx);
812 			ret = -EOVERFLOW;
813 			goto out_free;
814 		}
815 
816 		if (!len) {
817 			ret = -ENOBUFS;
818 			break;
819 		}
820 
821 		memcpy(&dat[1], &tpdat[offset], len);
822 		ret = j1939_tp_tx_dat(session, dat, len + 1);
823 		if (ret < 0) {
824 			/* ENOBUS == CAN interface TX queue is full */
825 			if (ret != -ENOBUFS)
826 				netdev_alert(priv->ndev,
827 					     "%s: 0x%p: queue data error: %i\n",
828 					     __func__, session, ret);
829 			break;
830 		}
831 
832 		session->last_txcmd = 0xff;
833 		pkt_done++;
834 		session->pkt.tx++;
835 		pdelay = j1939_cb_is_broadcast(&session->skcb) ? 50 :
836 			j1939_tp_packet_delay;
837 
838 		if (session->pkt.tx < session->pkt.total && pdelay) {
839 			j1939_tp_schedule_txtimer(session, pdelay);
840 			break;
841 		}
842 	}
843 
844 	if (pkt_done)
845 		j1939_tp_set_rxtimeout(session, 250);
846 
847  out_free:
848 	if (ret)
849 		kfree_skb(se_skb);
850 	else
851 		consume_skb(se_skb);
852 
853 	return ret;
854 }
855 
856 static int j1939_xtp_txnext_transmiter(struct j1939_session *session)
857 {
858 	struct j1939_priv *priv = session->priv;
859 	int ret = 0;
860 
861 	if (!j1939_tp_im_transmitter(&session->skcb)) {
862 		netdev_alert(priv->ndev, "%s: 0x%p: called by not transmitter!\n",
863 			     __func__, session);
864 		return -EINVAL;
865 	}
866 
867 	switch (session->last_cmd) {
868 	case 0:
869 		ret = j1939_session_tx_rts(session);
870 		break;
871 
872 	case J1939_ETP_CMD_CTS:
873 		if (session->last_txcmd != J1939_ETP_CMD_DPO) {
874 			ret = j1939_session_tx_dpo(session);
875 			if (ret)
876 				return ret;
877 		}
878 
879 		fallthrough;
880 	case J1939_TP_CMD_CTS:
881 	case 0xff: /* did some data */
882 	case J1939_ETP_CMD_DPO:
883 	case J1939_TP_CMD_BAM:
884 		ret = j1939_session_tx_dat(session);
885 
886 		break;
887 	default:
888 		netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
889 			     __func__, session, session->last_cmd);
890 	}
891 
892 	return ret;
893 }
894 
895 static int j1939_session_tx_cts(struct j1939_session *session)
896 {
897 	struct j1939_priv *priv = session->priv;
898 	unsigned int pkt, len;
899 	int ret;
900 	u8 dat[8];
901 
902 	if (!j1939_sk_recv_match(priv, &session->skcb))
903 		return -ENOENT;
904 
905 	len = session->pkt.total - session->pkt.rx;
906 	len = min3(len, session->pkt.block, j1939_tp_block ?: 255);
907 	memset(dat, 0xff, sizeof(dat));
908 
909 	if (session->skcb.addr.type == J1939_ETP) {
910 		pkt = session->pkt.rx + 1;
911 		dat[0] = J1939_ETP_CMD_CTS;
912 		dat[1] = len;
913 		dat[2] = (pkt >> 0);
914 		dat[3] = (pkt >> 8);
915 		dat[4] = (pkt >> 16);
916 	} else {
917 		dat[0] = J1939_TP_CMD_CTS;
918 		dat[1] = len;
919 		dat[2] = session->pkt.rx + 1;
920 	}
921 
922 	if (dat[0] == session->last_txcmd)
923 		/* done already */
924 		return 0;
925 
926 	ret = j1939_tp_tx_ctl(session, true, dat);
927 	if (ret < 0)
928 		return ret;
929 
930 	if (len)
931 		/* only mark cts done when len is set */
932 		session->last_txcmd = dat[0];
933 	j1939_tp_set_rxtimeout(session, 1250);
934 
935 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
936 
937 	return 0;
938 }
939 
940 static int j1939_session_tx_eoma(struct j1939_session *session)
941 {
942 	struct j1939_priv *priv = session->priv;
943 	u8 dat[8];
944 	int ret;
945 
946 	if (!j1939_sk_recv_match(priv, &session->skcb))
947 		return -ENOENT;
948 
949 	memset(dat, 0xff, sizeof(dat));
950 
951 	if (session->skcb.addr.type == J1939_ETP) {
952 		dat[0] = J1939_ETP_CMD_EOMA;
953 		dat[1] = session->total_message_size >> 0;
954 		dat[2] = session->total_message_size >> 8;
955 		dat[3] = session->total_message_size >> 16;
956 		dat[4] = session->total_message_size >> 24;
957 	} else {
958 		dat[0] = J1939_TP_CMD_EOMA;
959 		dat[1] = session->total_message_size;
960 		dat[2] = session->total_message_size >> 8;
961 		dat[3] = session->pkt.total;
962 	}
963 
964 	if (dat[0] == session->last_txcmd)
965 		/* done already */
966 		return 0;
967 
968 	ret = j1939_tp_tx_ctl(session, true, dat);
969 	if (ret < 0)
970 		return ret;
971 
972 	session->last_txcmd = dat[0];
973 
974 	/* wait for the EOMA packet to come in */
975 	j1939_tp_set_rxtimeout(session, 1250);
976 
977 	netdev_dbg(session->priv->ndev, "%p: 0x%p\n", __func__, session);
978 
979 	return 0;
980 }
981 
982 static int j1939_xtp_txnext_receiver(struct j1939_session *session)
983 {
984 	struct j1939_priv *priv = session->priv;
985 	int ret = 0;
986 
987 	if (!j1939_tp_im_receiver(&session->skcb)) {
988 		netdev_alert(priv->ndev, "%s: 0x%p: called by not receiver!\n",
989 			     __func__, session);
990 		return -EINVAL;
991 	}
992 
993 	switch (session->last_cmd) {
994 	case J1939_TP_CMD_RTS:
995 	case J1939_ETP_CMD_RTS:
996 		ret = j1939_session_tx_cts(session);
997 		break;
998 
999 	case J1939_ETP_CMD_CTS:
1000 	case J1939_TP_CMD_CTS:
1001 	case 0xff: /* did some data */
1002 	case J1939_ETP_CMD_DPO:
1003 		if ((session->skcb.addr.type == J1939_TP &&
1004 		     j1939_cb_is_broadcast(&session->skcb)))
1005 			break;
1006 
1007 		if (session->pkt.rx >= session->pkt.total) {
1008 			ret = j1939_session_tx_eoma(session);
1009 		} else if (session->pkt.rx >= session->pkt.last) {
1010 			session->last_txcmd = 0;
1011 			ret = j1939_session_tx_cts(session);
1012 		}
1013 		break;
1014 	default:
1015 		netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n",
1016 			     __func__, session, session->last_cmd);
1017 	}
1018 
1019 	return ret;
1020 }
1021 
1022 static int j1939_simple_txnext(struct j1939_session *session)
1023 {
1024 	struct j1939_priv *priv = session->priv;
1025 	struct sk_buff *se_skb = j1939_session_skb_get(session);
1026 	struct sk_buff *skb;
1027 	int ret;
1028 
1029 	if (!se_skb)
1030 		return 0;
1031 
1032 	skb = skb_clone(se_skb, GFP_ATOMIC);
1033 	if (!skb) {
1034 		ret = -ENOMEM;
1035 		goto out_free;
1036 	}
1037 
1038 	can_skb_set_owner(skb, se_skb->sk);
1039 
1040 	j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS);
1041 
1042 	ret = j1939_send_one(priv, skb);
1043 	if (ret)
1044 		goto out_free;
1045 
1046 	j1939_sk_errqueue(session, J1939_ERRQUEUE_SCHED);
1047 	j1939_sk_queue_activate_next(session);
1048 
1049  out_free:
1050 	if (ret)
1051 		kfree_skb(se_skb);
1052 	else
1053 		consume_skb(se_skb);
1054 
1055 	return ret;
1056 }
1057 
1058 static bool j1939_session_deactivate_locked(struct j1939_session *session)
1059 {
1060 	bool active = false;
1061 
1062 	lockdep_assert_held(&session->priv->active_session_list_lock);
1063 
1064 	if (session->state >= J1939_SESSION_ACTIVE &&
1065 	    session->state < J1939_SESSION_ACTIVE_MAX) {
1066 		active = true;
1067 
1068 		list_del_init(&session->active_session_list_entry);
1069 		session->state = J1939_SESSION_DONE;
1070 		j1939_session_put(session);
1071 	}
1072 
1073 	return active;
1074 }
1075 
1076 static bool j1939_session_deactivate(struct j1939_session *session)
1077 {
1078 	struct j1939_priv *priv = session->priv;
1079 	bool active;
1080 
1081 	j1939_session_list_lock(priv);
1082 	/* This function should be called with a session ref-count of at
1083 	 * least 2.
1084 	 */
1085 	WARN_ON_ONCE(kref_read(&session->kref) < 2);
1086 	active = j1939_session_deactivate_locked(session);
1087 	j1939_session_list_unlock(priv);
1088 
1089 	return active;
1090 }
1091 
1092 static void
1093 j1939_session_deactivate_activate_next(struct j1939_session *session)
1094 {
1095 	if (j1939_session_deactivate(session))
1096 		j1939_sk_queue_activate_next(session);
1097 }
1098 
1099 static void __j1939_session_cancel(struct j1939_session *session,
1100 				 enum j1939_xtp_abort err)
1101 {
1102 	struct j1939_priv *priv = session->priv;
1103 
1104 	WARN_ON_ONCE(!err);
1105 	lockdep_assert_held(&session->priv->active_session_list_lock);
1106 
1107 	session->err = j1939_xtp_abort_to_errno(priv, err);
1108 	session->state = J1939_SESSION_WAITING_ABORT;
1109 	/* do not send aborts on incoming broadcasts */
1110 	if (!j1939_cb_is_broadcast(&session->skcb)) {
1111 		j1939_xtp_tx_abort(priv, &session->skcb,
1112 				   !session->transmission,
1113 				   err, session->skcb.addr.pgn);
1114 	}
1115 
1116 	if (session->sk)
1117 		j1939_sk_send_loop_abort(session->sk, session->err);
1118 }
1119 
1120 static void j1939_session_cancel(struct j1939_session *session,
1121 				 enum j1939_xtp_abort err)
1122 {
1123 	j1939_session_list_lock(session->priv);
1124 
1125 	if (session->state >= J1939_SESSION_ACTIVE &&
1126 	    session->state < J1939_SESSION_WAITING_ABORT) {
1127 		j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS);
1128 		__j1939_session_cancel(session, err);
1129 	}
1130 
1131 	j1939_session_list_unlock(session->priv);
1132 }
1133 
1134 static enum hrtimer_restart j1939_tp_txtimer(struct hrtimer *hrtimer)
1135 {
1136 	struct j1939_session *session =
1137 		container_of(hrtimer, struct j1939_session, txtimer);
1138 	struct j1939_priv *priv = session->priv;
1139 	int ret = 0;
1140 
1141 	if (session->skcb.addr.type == J1939_SIMPLE) {
1142 		ret = j1939_simple_txnext(session);
1143 	} else {
1144 		if (session->transmission)
1145 			ret = j1939_xtp_txnext_transmiter(session);
1146 		else
1147 			ret = j1939_xtp_txnext_receiver(session);
1148 	}
1149 
1150 	switch (ret) {
1151 	case -ENOBUFS:
1152 		/* Retry limit is currently arbitrary chosen */
1153 		if (session->tx_retry < J1939_XTP_TX_RETRY_LIMIT) {
1154 			session->tx_retry++;
1155 			j1939_tp_schedule_txtimer(session,
1156 						  10 + prandom_u32_max(16));
1157 		} else {
1158 			netdev_alert(priv->ndev, "%s: 0x%p: tx retry count reached\n",
1159 				     __func__, session);
1160 			session->err = -ENETUNREACH;
1161 			j1939_session_rxtimer_cancel(session);
1162 			j1939_session_deactivate_activate_next(session);
1163 		}
1164 		break;
1165 	case -ENETDOWN:
1166 		/* In this case we should get a netdev_event(), all active
1167 		 * sessions will be cleared by
1168 		 * j1939_cancel_all_active_sessions(). So handle this as an
1169 		 * error, but let j1939_cancel_all_active_sessions() do the
1170 		 * cleanup including propagation of the error to user space.
1171 		 */
1172 		break;
1173 	case -EOVERFLOW:
1174 		j1939_session_cancel(session, J1939_XTP_ABORT_ECTS_TOO_BIG);
1175 		break;
1176 	case 0:
1177 		session->tx_retry = 0;
1178 		break;
1179 	default:
1180 		netdev_alert(priv->ndev, "%s: 0x%p: tx aborted with unknown reason: %i\n",
1181 			     __func__, session, ret);
1182 		if (session->skcb.addr.type != J1939_SIMPLE) {
1183 			j1939_session_cancel(session, J1939_XTP_ABORT_OTHER);
1184 		} else {
1185 			session->err = ret;
1186 			j1939_session_rxtimer_cancel(session);
1187 			j1939_session_deactivate_activate_next(session);
1188 		}
1189 	}
1190 
1191 	j1939_session_put(session);
1192 
1193 	return HRTIMER_NORESTART;
1194 }
1195 
1196 static void j1939_session_completed(struct j1939_session *session)
1197 {
1198 	struct sk_buff *skb;
1199 
1200 	if (!session->transmission) {
1201 		skb = j1939_session_skb_get(session);
1202 		/* distribute among j1939 receivers */
1203 		j1939_sk_recv(session->priv, skb);
1204 		consume_skb(skb);
1205 	}
1206 
1207 	j1939_session_deactivate_activate_next(session);
1208 }
1209 
1210 static enum hrtimer_restart j1939_tp_rxtimer(struct hrtimer *hrtimer)
1211 {
1212 	struct j1939_session *session = container_of(hrtimer,
1213 						     struct j1939_session,
1214 						     rxtimer);
1215 	struct j1939_priv *priv = session->priv;
1216 
1217 	if (session->state == J1939_SESSION_WAITING_ABORT) {
1218 		netdev_alert(priv->ndev, "%s: 0x%p: abort rx timeout. Force session deactivation\n",
1219 			     __func__, session);
1220 
1221 		j1939_session_deactivate_activate_next(session);
1222 
1223 	} else if (session->skcb.addr.type == J1939_SIMPLE) {
1224 		netdev_alert(priv->ndev, "%s: 0x%p: Timeout. Failed to send simple message.\n",
1225 			     __func__, session);
1226 
1227 		/* The message is probably stuck in the CAN controller and can
1228 		 * be send as soon as CAN bus is in working state again.
1229 		 */
1230 		session->err = -ETIME;
1231 		j1939_session_deactivate(session);
1232 	} else {
1233 		netdev_alert(priv->ndev, "%s: 0x%p: rx timeout, send abort\n",
1234 			     __func__, session);
1235 
1236 		j1939_session_list_lock(session->priv);
1237 		if (session->state >= J1939_SESSION_ACTIVE &&
1238 		    session->state < J1939_SESSION_ACTIVE_MAX) {
1239 			j1939_session_get(session);
1240 			hrtimer_start(&session->rxtimer,
1241 				      ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS),
1242 				      HRTIMER_MODE_REL_SOFT);
1243 			__j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT);
1244 		}
1245 		j1939_session_list_unlock(session->priv);
1246 	}
1247 
1248 	j1939_session_put(session);
1249 
1250 	return HRTIMER_NORESTART;
1251 }
1252 
1253 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session *session,
1254 				     const struct sk_buff *skb)
1255 {
1256 	const struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1257 	pgn_t pgn = j1939_xtp_ctl_to_pgn(skb->data);
1258 	struct j1939_priv *priv = session->priv;
1259 	enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1260 	u8 cmd = skb->data[0];
1261 
1262 	if (session->skcb.addr.pgn == pgn)
1263 		return false;
1264 
1265 	switch (cmd) {
1266 	case J1939_TP_CMD_BAM:
1267 		abort = J1939_XTP_NO_ABORT;
1268 		break;
1269 
1270 	case J1939_ETP_CMD_RTS:
1271 	case J1939_TP_CMD_RTS: /* fall through */
1272 		abort = J1939_XTP_ABORT_BUSY;
1273 		break;
1274 
1275 	case J1939_ETP_CMD_CTS:
1276 	case J1939_TP_CMD_CTS: /* fall through */
1277 		abort = J1939_XTP_ABORT_ECTS_UNXPECTED_PGN;
1278 		break;
1279 
1280 	case J1939_ETP_CMD_DPO:
1281 		abort = J1939_XTP_ABORT_BAD_EDPO_PGN;
1282 		break;
1283 
1284 	case J1939_ETP_CMD_EOMA:
1285 	case J1939_TP_CMD_EOMA: /* fall through */
1286 		abort = J1939_XTP_ABORT_OTHER;
1287 		break;
1288 
1289 	case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
1290 		abort = J1939_XTP_NO_ABORT;
1291 		break;
1292 
1293 	default:
1294 		WARN_ON_ONCE(1);
1295 		break;
1296 	}
1297 
1298 	netdev_warn(priv->ndev, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n",
1299 		    __func__, session, cmd, pgn, session->skcb.addr.pgn);
1300 	if (abort != J1939_XTP_NO_ABORT)
1301 		j1939_xtp_tx_abort(priv, skcb, true, abort, pgn);
1302 
1303 	return true;
1304 }
1305 
1306 static void j1939_xtp_rx_abort_one(struct j1939_priv *priv, struct sk_buff *skb,
1307 				   bool reverse, bool transmitter)
1308 {
1309 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1310 	struct j1939_session *session;
1311 	u8 abort = skb->data[1];
1312 
1313 	session = j1939_session_get_by_addr(priv, &skcb->addr, reverse,
1314 					    transmitter);
1315 	if (!session)
1316 		return;
1317 
1318 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1319 		goto abort_put;
1320 
1321 	netdev_info(priv->ndev, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__,
1322 		    session, j1939_xtp_ctl_to_pgn(skb->data), abort,
1323 		    j1939_xtp_abort_to_str(abort));
1324 
1325 	j1939_session_timers_cancel(session);
1326 	session->err = j1939_xtp_abort_to_errno(priv, abort);
1327 	if (session->sk)
1328 		j1939_sk_send_loop_abort(session->sk, session->err);
1329 	j1939_session_deactivate_activate_next(session);
1330 
1331 abort_put:
1332 	j1939_session_put(session);
1333 }
1334 
1335 /* abort packets may come in 2 directions */
1336 static void
1337 j1939_xtp_rx_abort(struct j1939_priv *priv, struct sk_buff *skb,
1338 		   bool transmitter)
1339 {
1340 	j1939_xtp_rx_abort_one(priv, skb, false, transmitter);
1341 	j1939_xtp_rx_abort_one(priv, skb, true, transmitter);
1342 }
1343 
1344 static void
1345 j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb)
1346 {
1347 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1348 	const u8 *dat;
1349 	int len;
1350 
1351 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1352 		return;
1353 
1354 	dat = skb->data;
1355 
1356 	if (skcb->addr.type == J1939_ETP)
1357 		len = j1939_etp_ctl_to_size(dat);
1358 	else
1359 		len = j1939_tp_ctl_to_size(dat);
1360 
1361 	if (session->total_message_size != len) {
1362 		netdev_warn_once(session->priv->ndev,
1363 				 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n",
1364 				 __func__, session, session->total_message_size,
1365 				 len);
1366 	}
1367 
1368 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1369 
1370 	session->pkt.tx_acked = session->pkt.total;
1371 	j1939_session_timers_cancel(session);
1372 	/* transmitted without problems */
1373 	j1939_session_completed(session);
1374 }
1375 
1376 static void
1377 j1939_xtp_rx_eoma(struct j1939_priv *priv, struct sk_buff *skb,
1378 		  bool transmitter)
1379 {
1380 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1381 	struct j1939_session *session;
1382 
1383 	session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1384 					    transmitter);
1385 	if (!session)
1386 		return;
1387 
1388 	j1939_xtp_rx_eoma_one(session, skb);
1389 	j1939_session_put(session);
1390 }
1391 
1392 static void
1393 j1939_xtp_rx_cts_one(struct j1939_session *session, struct sk_buff *skb)
1394 {
1395 	enum j1939_xtp_abort err = J1939_XTP_ABORT_FAULT;
1396 	unsigned int pkt;
1397 	const u8 *dat;
1398 
1399 	dat = skb->data;
1400 
1401 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1402 		return;
1403 
1404 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1405 
1406 	if (session->last_cmd == dat[0]) {
1407 		err = J1939_XTP_ABORT_DUP_SEQ;
1408 		goto out_session_cancel;
1409 	}
1410 
1411 	if (session->skcb.addr.type == J1939_ETP)
1412 		pkt = j1939_etp_ctl_to_packet(dat);
1413 	else
1414 		pkt = dat[2];
1415 
1416 	if (!pkt)
1417 		goto out_session_cancel;
1418 	else if (dat[1] > session->pkt.block /* 0xff for etp */)
1419 		goto out_session_cancel;
1420 
1421 	/* set packet counters only when not CTS(0) */
1422 	session->pkt.tx_acked = pkt - 1;
1423 	j1939_session_skb_drop_old(session);
1424 	session->pkt.last = session->pkt.tx_acked + dat[1];
1425 	if (session->pkt.last > session->pkt.total)
1426 		/* safety measure */
1427 		session->pkt.last = session->pkt.total;
1428 	/* TODO: do not set tx here, do it in txtimer */
1429 	session->pkt.tx = session->pkt.tx_acked;
1430 
1431 	session->last_cmd = dat[0];
1432 	if (dat[1]) {
1433 		j1939_tp_set_rxtimeout(session, 1250);
1434 		if (session->transmission) {
1435 			if (session->pkt.tx_acked)
1436 				j1939_sk_errqueue(session,
1437 						  J1939_ERRQUEUE_SCHED);
1438 			j1939_session_txtimer_cancel(session);
1439 			j1939_tp_schedule_txtimer(session, 0);
1440 		}
1441 	} else {
1442 		/* CTS(0) */
1443 		j1939_tp_set_rxtimeout(session, 550);
1444 	}
1445 	return;
1446 
1447  out_session_cancel:
1448 	j1939_session_timers_cancel(session);
1449 	j1939_session_cancel(session, err);
1450 }
1451 
1452 static void
1453 j1939_xtp_rx_cts(struct j1939_priv *priv, struct sk_buff *skb, bool transmitter)
1454 {
1455 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1456 	struct j1939_session *session;
1457 
1458 	session = j1939_session_get_by_addr(priv, &skcb->addr, true,
1459 					    transmitter);
1460 	if (!session)
1461 		return;
1462 	j1939_xtp_rx_cts_one(session, skb);
1463 	j1939_session_put(session);
1464 }
1465 
1466 static struct j1939_session *j1939_session_new(struct j1939_priv *priv,
1467 					       struct sk_buff *skb, size_t size)
1468 {
1469 	struct j1939_session *session;
1470 	struct j1939_sk_buff_cb *skcb;
1471 
1472 	session = kzalloc(sizeof(*session), gfp_any());
1473 	if (!session)
1474 		return NULL;
1475 
1476 	INIT_LIST_HEAD(&session->active_session_list_entry);
1477 	INIT_LIST_HEAD(&session->sk_session_queue_entry);
1478 	kref_init(&session->kref);
1479 
1480 	j1939_priv_get(priv);
1481 	session->priv = priv;
1482 	session->total_message_size = size;
1483 	session->state = J1939_SESSION_NEW;
1484 
1485 	skb_queue_head_init(&session->skb_queue);
1486 	skb_queue_tail(&session->skb_queue, skb);
1487 
1488 	skcb = j1939_skb_to_cb(skb);
1489 	memcpy(&session->skcb, skcb, sizeof(session->skcb));
1490 
1491 	hrtimer_init(&session->txtimer, CLOCK_MONOTONIC,
1492 		     HRTIMER_MODE_REL_SOFT);
1493 	session->txtimer.function = j1939_tp_txtimer;
1494 	hrtimer_init(&session->rxtimer, CLOCK_MONOTONIC,
1495 		     HRTIMER_MODE_REL_SOFT);
1496 	session->rxtimer.function = j1939_tp_rxtimer;
1497 
1498 	netdev_dbg(priv->ndev, "%s: 0x%p: sa: %02x, da: %02x\n",
1499 		   __func__, session, skcb->addr.sa, skcb->addr.da);
1500 
1501 	return session;
1502 }
1503 
1504 static struct
1505 j1939_session *j1939_session_fresh_new(struct j1939_priv *priv,
1506 				       int size,
1507 				       const struct j1939_sk_buff_cb *rel_skcb)
1508 {
1509 	struct sk_buff *skb;
1510 	struct j1939_sk_buff_cb *skcb;
1511 	struct j1939_session *session;
1512 
1513 	skb = alloc_skb(size + sizeof(struct can_skb_priv), GFP_ATOMIC);
1514 	if (unlikely(!skb))
1515 		return NULL;
1516 
1517 	skb->dev = priv->ndev;
1518 	can_skb_reserve(skb);
1519 	can_skb_prv(skb)->ifindex = priv->ndev->ifindex;
1520 	can_skb_prv(skb)->skbcnt = 0;
1521 	skcb = j1939_skb_to_cb(skb);
1522 	memcpy(skcb, rel_skcb, sizeof(*skcb));
1523 
1524 	session = j1939_session_new(priv, skb, size);
1525 	if (!session) {
1526 		kfree_skb(skb);
1527 		return NULL;
1528 	}
1529 
1530 	/* alloc data area */
1531 	skb_put(skb, size);
1532 	/* skb is recounted in j1939_session_new() */
1533 	return session;
1534 }
1535 
1536 int j1939_session_activate(struct j1939_session *session)
1537 {
1538 	struct j1939_priv *priv = session->priv;
1539 	struct j1939_session *active = NULL;
1540 	int ret = 0;
1541 
1542 	j1939_session_list_lock(priv);
1543 	if (session->skcb.addr.type != J1939_SIMPLE)
1544 		active = j1939_session_get_by_addr_locked(priv,
1545 							  &priv->active_session_list,
1546 							  &session->skcb.addr, false,
1547 							  session->transmission);
1548 	if (active) {
1549 		j1939_session_put(active);
1550 		ret = -EAGAIN;
1551 	} else {
1552 		WARN_ON_ONCE(session->state != J1939_SESSION_NEW);
1553 		list_add_tail(&session->active_session_list_entry,
1554 			      &priv->active_session_list);
1555 		j1939_session_get(session);
1556 		session->state = J1939_SESSION_ACTIVE;
1557 
1558 		netdev_dbg(session->priv->ndev, "%s: 0x%p\n",
1559 			   __func__, session);
1560 	}
1561 	j1939_session_list_unlock(priv);
1562 
1563 	return ret;
1564 }
1565 
1566 static struct
1567 j1939_session *j1939_xtp_rx_rts_session_new(struct j1939_priv *priv,
1568 					    struct sk_buff *skb)
1569 {
1570 	enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT;
1571 	struct j1939_sk_buff_cb skcb = *j1939_skb_to_cb(skb);
1572 	struct j1939_session *session;
1573 	const u8 *dat;
1574 	pgn_t pgn;
1575 	int len;
1576 
1577 	netdev_dbg(priv->ndev, "%s\n", __func__);
1578 
1579 	dat = skb->data;
1580 	pgn = j1939_xtp_ctl_to_pgn(dat);
1581 	skcb.addr.pgn = pgn;
1582 
1583 	if (!j1939_sk_recv_match(priv, &skcb))
1584 		return NULL;
1585 
1586 	if (skcb.addr.type == J1939_ETP) {
1587 		len = j1939_etp_ctl_to_size(dat);
1588 		if (len > J1939_MAX_ETP_PACKET_SIZE)
1589 			abort = J1939_XTP_ABORT_FAULT;
1590 		else if (len > priv->tp_max_packet_size)
1591 			abort = J1939_XTP_ABORT_RESOURCE;
1592 		else if (len <= J1939_MAX_TP_PACKET_SIZE)
1593 			abort = J1939_XTP_ABORT_FAULT;
1594 	} else {
1595 		len = j1939_tp_ctl_to_size(dat);
1596 		if (len > J1939_MAX_TP_PACKET_SIZE)
1597 			abort = J1939_XTP_ABORT_FAULT;
1598 		else if (len > priv->tp_max_packet_size)
1599 			abort = J1939_XTP_ABORT_RESOURCE;
1600 	}
1601 
1602 	if (abort != J1939_XTP_NO_ABORT) {
1603 		j1939_xtp_tx_abort(priv, &skcb, true, abort, pgn);
1604 		return NULL;
1605 	}
1606 
1607 	session = j1939_session_fresh_new(priv, len, &skcb);
1608 	if (!session) {
1609 		j1939_xtp_tx_abort(priv, &skcb, true,
1610 				   J1939_XTP_ABORT_RESOURCE, pgn);
1611 		return NULL;
1612 	}
1613 
1614 	/* initialize the control buffer: plain copy */
1615 	session->pkt.total = (len + 6) / 7;
1616 	session->pkt.block = 0xff;
1617 	if (skcb.addr.type != J1939_ETP) {
1618 		if (dat[3] != session->pkt.total)
1619 			netdev_alert(priv->ndev, "%s: 0x%p: strange total, %u != %u\n",
1620 				     __func__, session, session->pkt.total,
1621 				     dat[3]);
1622 		session->pkt.total = dat[3];
1623 		session->pkt.block = min(dat[3], dat[4]);
1624 	}
1625 
1626 	session->pkt.rx = 0;
1627 	session->pkt.tx = 0;
1628 
1629 	WARN_ON_ONCE(j1939_session_activate(session));
1630 
1631 	return session;
1632 }
1633 
1634 static int j1939_xtp_rx_rts_session_active(struct j1939_session *session,
1635 					   struct sk_buff *skb)
1636 {
1637 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1638 	struct j1939_priv *priv = session->priv;
1639 
1640 	if (!session->transmission) {
1641 		if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1642 			return -EBUSY;
1643 
1644 		/* RTS on active session */
1645 		j1939_session_timers_cancel(session);
1646 		j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1647 	}
1648 
1649 	if (session->last_cmd != 0) {
1650 		/* we received a second rts on the same connection */
1651 		netdev_alert(priv->ndev, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n",
1652 			     __func__, session, skcb->addr.sa, skcb->addr.da,
1653 			     session->last_cmd);
1654 
1655 		j1939_session_timers_cancel(session);
1656 		j1939_session_cancel(session, J1939_XTP_ABORT_BUSY);
1657 
1658 		return -EBUSY;
1659 	}
1660 
1661 	if (session->skcb.addr.sa != skcb->addr.sa ||
1662 	    session->skcb.addr.da != skcb->addr.da)
1663 		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",
1664 			    __func__, session,
1665 			    session->skcb.addr.sa, skcb->addr.sa,
1666 			    session->skcb.addr.da, skcb->addr.da);
1667 	/* make sure 'sa' & 'da' are correct !
1668 	 * They may be 'not filled in yet' for sending
1669 	 * skb's, since they did not pass the Address Claim ever.
1670 	 */
1671 	session->skcb.addr.sa = skcb->addr.sa;
1672 	session->skcb.addr.da = skcb->addr.da;
1673 
1674 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1675 
1676 	return 0;
1677 }
1678 
1679 static void j1939_xtp_rx_rts(struct j1939_priv *priv, struct sk_buff *skb,
1680 			     bool transmitter)
1681 {
1682 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1683 	struct j1939_session *session;
1684 	u8 cmd = skb->data[0];
1685 
1686 	session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1687 					    transmitter);
1688 
1689 	if (!session) {
1690 		if (transmitter) {
1691 			/* If we're the transmitter and this function is called,
1692 			 * we received our own RTS. A session has already been
1693 			 * created.
1694 			 *
1695 			 * For some reasons however it might have been destroyed
1696 			 * already. So don't create a new one here (using
1697 			 * "j1939_xtp_rx_rts_session_new()") as this will be a
1698 			 * receiver session.
1699 			 *
1700 			 * The reasons the session is already destroyed might
1701 			 * be:
1702 			 * - user space closed socket was and the session was
1703 			 *   aborted
1704 			 * - session was aborted due to external abort message
1705 			 */
1706 			return;
1707 		}
1708 		session = j1939_xtp_rx_rts_session_new(priv, skb);
1709 		if (!session) {
1710 			if (cmd == J1939_TP_CMD_BAM && j1939_sk_recv_match(priv, skcb))
1711 				netdev_info(priv->ndev, "%s: failed to create TP BAM session\n",
1712 					    __func__);
1713 			return;
1714 		}
1715 	} else {
1716 		if (j1939_xtp_rx_rts_session_active(session, skb)) {
1717 			j1939_session_put(session);
1718 			return;
1719 		}
1720 	}
1721 	session->last_cmd = cmd;
1722 
1723 	if (cmd == J1939_TP_CMD_BAM) {
1724 		if (!session->transmission)
1725 			j1939_tp_set_rxtimeout(session, 750);
1726 	} else {
1727 		if (!session->transmission) {
1728 			j1939_session_txtimer_cancel(session);
1729 			j1939_tp_schedule_txtimer(session, 0);
1730 		}
1731 		j1939_tp_set_rxtimeout(session, 1250);
1732 	}
1733 
1734 	j1939_session_put(session);
1735 }
1736 
1737 static void j1939_xtp_rx_dpo_one(struct j1939_session *session,
1738 				 struct sk_buff *skb)
1739 {
1740 	const u8 *dat = skb->data;
1741 
1742 	if (j1939_xtp_rx_cmd_bad_pgn(session, skb))
1743 		return;
1744 
1745 	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
1746 
1747 	/* transmitted without problems */
1748 	session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data);
1749 	session->last_cmd = dat[0];
1750 	j1939_tp_set_rxtimeout(session, 750);
1751 }
1752 
1753 static void j1939_xtp_rx_dpo(struct j1939_priv *priv, struct sk_buff *skb,
1754 			     bool transmitter)
1755 {
1756 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1757 	struct j1939_session *session;
1758 
1759 	session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1760 					    transmitter);
1761 	if (!session) {
1762 		netdev_info(priv->ndev,
1763 			    "%s: no connection found\n", __func__);
1764 		return;
1765 	}
1766 
1767 	j1939_xtp_rx_dpo_one(session, skb);
1768 	j1939_session_put(session);
1769 }
1770 
1771 static void j1939_xtp_rx_dat_one(struct j1939_session *session,
1772 				 struct sk_buff *skb)
1773 {
1774 	struct j1939_priv *priv = session->priv;
1775 	struct j1939_sk_buff_cb *skcb;
1776 	struct sk_buff *se_skb = NULL;
1777 	const u8 *dat;
1778 	u8 *tpdat;
1779 	int offset;
1780 	int nbytes;
1781 	bool final = false;
1782 	bool remain = false;
1783 	bool do_cts_eoma = false;
1784 	int packet;
1785 
1786 	skcb = j1939_skb_to_cb(skb);
1787 	dat = skb->data;
1788 	if (skb->len <= 1)
1789 		/* makes no sense */
1790 		goto out_session_cancel;
1791 
1792 	switch (session->last_cmd) {
1793 	case 0xff:
1794 		break;
1795 	case J1939_ETP_CMD_DPO:
1796 		if (skcb->addr.type == J1939_ETP)
1797 			break;
1798 		fallthrough;
1799 	case J1939_TP_CMD_BAM:
1800 	case J1939_TP_CMD_CTS: /* fall through */
1801 		if (skcb->addr.type != J1939_ETP)
1802 			break;
1803 		fallthrough;
1804 	default:
1805 		netdev_info(priv->ndev, "%s: 0x%p: last %02x\n", __func__,
1806 			    session, session->last_cmd);
1807 		goto out_session_cancel;
1808 	}
1809 
1810 	packet = (dat[0] - 1 + session->pkt.dpo);
1811 	if (packet > session->pkt.total ||
1812 	    (session->pkt.rx + 1) > session->pkt.total) {
1813 		netdev_info(priv->ndev, "%s: 0x%p: should have been completed\n",
1814 			    __func__, session);
1815 		goto out_session_cancel;
1816 	}
1817 
1818 	se_skb = j1939_session_skb_get_by_offset(session, packet * 7);
1819 	if (!se_skb) {
1820 		netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__,
1821 			    session);
1822 		goto out_session_cancel;
1823 	}
1824 
1825 	skcb = j1939_skb_to_cb(se_skb);
1826 	offset = packet * 7 - skcb->offset;
1827 	nbytes = se_skb->len - offset;
1828 	if (nbytes > 7)
1829 		nbytes = 7;
1830 	if (nbytes <= 0 || (nbytes + 1) > skb->len) {
1831 		netdev_info(priv->ndev, "%s: 0x%p: nbytes %i, len %i\n",
1832 			    __func__, session, nbytes, skb->len);
1833 		goto out_session_cancel;
1834 	}
1835 
1836 	tpdat = se_skb->data;
1837 	if (!session->transmission) {
1838 		memcpy(&tpdat[offset], &dat[1], nbytes);
1839 	} else {
1840 		int err;
1841 
1842 		err = memcmp(&tpdat[offset], &dat[1], nbytes);
1843 		if (err)
1844 			netdev_err_once(priv->ndev,
1845 					"%s: 0x%p: Data of RX-looped back packet (%*ph) doesn't match TX data (%*ph)!\n",
1846 					__func__, session,
1847 					nbytes, &dat[1],
1848 					nbytes, &tpdat[offset]);
1849 	}
1850 
1851 	if (packet == session->pkt.rx)
1852 		session->pkt.rx++;
1853 
1854 	if (skcb->addr.type != J1939_ETP &&
1855 	    j1939_cb_is_broadcast(&session->skcb)) {
1856 		if (session->pkt.rx >= session->pkt.total)
1857 			final = true;
1858 		else
1859 			remain = true;
1860 	} else {
1861 		/* never final, an EOMA must follow */
1862 		if (session->pkt.rx >= session->pkt.last)
1863 			do_cts_eoma = true;
1864 	}
1865 
1866 	if (final) {
1867 		j1939_session_timers_cancel(session);
1868 		j1939_session_completed(session);
1869 	} else if (remain) {
1870 		if (!session->transmission)
1871 			j1939_tp_set_rxtimeout(session, 750);
1872 	} else if (do_cts_eoma) {
1873 		j1939_tp_set_rxtimeout(session, 1250);
1874 		if (!session->transmission)
1875 			j1939_tp_schedule_txtimer(session, 0);
1876 	} else {
1877 		j1939_tp_set_rxtimeout(session, 750);
1878 	}
1879 	session->last_cmd = 0xff;
1880 	consume_skb(se_skb);
1881 	j1939_session_put(session);
1882 
1883 	return;
1884 
1885  out_session_cancel:
1886 	kfree_skb(se_skb);
1887 	j1939_session_timers_cancel(session);
1888 	j1939_session_cancel(session, J1939_XTP_ABORT_FAULT);
1889 	j1939_session_put(session);
1890 }
1891 
1892 static void j1939_xtp_rx_dat(struct j1939_priv *priv, struct sk_buff *skb)
1893 {
1894 	struct j1939_sk_buff_cb *skcb;
1895 	struct j1939_session *session;
1896 
1897 	skcb = j1939_skb_to_cb(skb);
1898 
1899 	if (j1939_tp_im_transmitter(skcb)) {
1900 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1901 						    true);
1902 		if (!session)
1903 			netdev_info(priv->ndev, "%s: no tx connection found\n",
1904 				    __func__);
1905 		else
1906 			j1939_xtp_rx_dat_one(session, skb);
1907 	}
1908 
1909 	if (j1939_tp_im_receiver(skcb)) {
1910 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1911 						    false);
1912 		if (!session)
1913 			netdev_info(priv->ndev, "%s: no rx connection found\n",
1914 				    __func__);
1915 		else
1916 			j1939_xtp_rx_dat_one(session, skb);
1917 	}
1918 
1919 	if (j1939_cb_is_broadcast(skcb)) {
1920 		session = j1939_session_get_by_addr(priv, &skcb->addr, false,
1921 						    false);
1922 		if (session)
1923 			j1939_xtp_rx_dat_one(session, skb);
1924 	}
1925 }
1926 
1927 /* j1939 main intf */
1928 struct j1939_session *j1939_tp_send(struct j1939_priv *priv,
1929 				    struct sk_buff *skb, size_t size)
1930 {
1931 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1932 	struct j1939_session *session;
1933 	int ret;
1934 
1935 	if (skcb->addr.pgn == J1939_TP_PGN_DAT ||
1936 	    skcb->addr.pgn == J1939_TP_PGN_CTL ||
1937 	    skcb->addr.pgn == J1939_ETP_PGN_DAT ||
1938 	    skcb->addr.pgn == J1939_ETP_PGN_CTL)
1939 		/* avoid conflict */
1940 		return ERR_PTR(-EDOM);
1941 
1942 	if (size > priv->tp_max_packet_size)
1943 		return ERR_PTR(-EMSGSIZE);
1944 
1945 	if (size <= 8)
1946 		skcb->addr.type = J1939_SIMPLE;
1947 	else if (size > J1939_MAX_TP_PACKET_SIZE)
1948 		skcb->addr.type = J1939_ETP;
1949 	else
1950 		skcb->addr.type = J1939_TP;
1951 
1952 	if (skcb->addr.type == J1939_ETP &&
1953 	    j1939_cb_is_broadcast(skcb))
1954 		return ERR_PTR(-EDESTADDRREQ);
1955 
1956 	/* fill in addresses from names */
1957 	ret = j1939_ac_fixup(priv, skb);
1958 	if (unlikely(ret))
1959 		return ERR_PTR(ret);
1960 
1961 	/* fix DST flags, it may be used there soon */
1962 	if (j1939_address_is_unicast(skcb->addr.da) &&
1963 	    priv->ents[skcb->addr.da].nusers)
1964 		skcb->flags |= J1939_ECU_LOCAL_DST;
1965 
1966 	/* src is always local, I'm sending ... */
1967 	skcb->flags |= J1939_ECU_LOCAL_SRC;
1968 
1969 	/* prepare new session */
1970 	session = j1939_session_new(priv, skb, size);
1971 	if (!session)
1972 		return ERR_PTR(-ENOMEM);
1973 
1974 	/* skb is recounted in j1939_session_new() */
1975 	sock_hold(skb->sk);
1976 	session->sk = skb->sk;
1977 	session->transmission = true;
1978 	session->pkt.total = (size + 6) / 7;
1979 	session->pkt.block = skcb->addr.type == J1939_ETP ? 255 :
1980 		min(j1939_tp_block ?: 255, session->pkt.total);
1981 
1982 	if (j1939_cb_is_broadcast(&session->skcb))
1983 		/* set the end-packet for broadcast */
1984 		session->pkt.last = session->pkt.total;
1985 
1986 	skcb->tskey = session->sk->sk_tskey++;
1987 	session->tskey = skcb->tskey;
1988 
1989 	return session;
1990 }
1991 
1992 static void j1939_tp_cmd_recv(struct j1939_priv *priv, struct sk_buff *skb)
1993 {
1994 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
1995 	int extd = J1939_TP;
1996 	u8 cmd = skb->data[0];
1997 
1998 	switch (cmd) {
1999 	case J1939_ETP_CMD_RTS:
2000 		extd = J1939_ETP;
2001 		fallthrough;
2002 	case J1939_TP_CMD_BAM:
2003 	case J1939_TP_CMD_RTS: /* fall through */
2004 		if (skcb->addr.type != extd)
2005 			return;
2006 
2007 		if (cmd == J1939_TP_CMD_RTS && j1939_cb_is_broadcast(skcb)) {
2008 			netdev_alert(priv->ndev, "%s: rts without destination (%02x)\n",
2009 				     __func__, skcb->addr.sa);
2010 			return;
2011 		}
2012 
2013 		if (j1939_tp_im_transmitter(skcb))
2014 			j1939_xtp_rx_rts(priv, skb, true);
2015 
2016 		if (j1939_tp_im_receiver(skcb) || j1939_cb_is_broadcast(skcb))
2017 			j1939_xtp_rx_rts(priv, skb, false);
2018 
2019 		break;
2020 
2021 	case J1939_ETP_CMD_CTS:
2022 		extd = J1939_ETP;
2023 		fallthrough;
2024 	case J1939_TP_CMD_CTS:
2025 		if (skcb->addr.type != extd)
2026 			return;
2027 
2028 		if (j1939_tp_im_transmitter(skcb))
2029 			j1939_xtp_rx_cts(priv, skb, false);
2030 
2031 		if (j1939_tp_im_receiver(skcb))
2032 			j1939_xtp_rx_cts(priv, skb, true);
2033 
2034 		break;
2035 
2036 	case J1939_ETP_CMD_DPO:
2037 		if (skcb->addr.type != J1939_ETP)
2038 			return;
2039 
2040 		if (j1939_tp_im_transmitter(skcb))
2041 			j1939_xtp_rx_dpo(priv, skb, true);
2042 
2043 		if (j1939_tp_im_receiver(skcb))
2044 			j1939_xtp_rx_dpo(priv, skb, false);
2045 
2046 		break;
2047 
2048 	case J1939_ETP_CMD_EOMA:
2049 		extd = J1939_ETP;
2050 		fallthrough;
2051 	case J1939_TP_CMD_EOMA:
2052 		if (skcb->addr.type != extd)
2053 			return;
2054 
2055 		if (j1939_tp_im_transmitter(skcb))
2056 			j1939_xtp_rx_eoma(priv, skb, false);
2057 
2058 		if (j1939_tp_im_receiver(skcb))
2059 			j1939_xtp_rx_eoma(priv, skb, true);
2060 
2061 		break;
2062 
2063 	case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */
2064 		if (j1939_tp_im_transmitter(skcb))
2065 			j1939_xtp_rx_abort(priv, skb, true);
2066 
2067 		if (j1939_tp_im_receiver(skcb))
2068 			j1939_xtp_rx_abort(priv, skb, false);
2069 
2070 		break;
2071 	default:
2072 		return;
2073 	}
2074 }
2075 
2076 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb)
2077 {
2078 	struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb);
2079 
2080 	if (!j1939_tp_im_involved_anydir(skcb) && !j1939_cb_is_broadcast(skcb))
2081 		return 0;
2082 
2083 	switch (skcb->addr.pgn) {
2084 	case J1939_ETP_PGN_DAT:
2085 		skcb->addr.type = J1939_ETP;
2086 		fallthrough;
2087 	case J1939_TP_PGN_DAT:
2088 		j1939_xtp_rx_dat(priv, skb);
2089 		break;
2090 
2091 	case J1939_ETP_PGN_CTL:
2092 		skcb->addr.type = J1939_ETP;
2093 		fallthrough;
2094 	case J1939_TP_PGN_CTL:
2095 		if (skb->len < 8)
2096 			return 0; /* Don't care. Nothing to extract here */
2097 
2098 		j1939_tp_cmd_recv(priv, skb);
2099 		break;
2100 	default:
2101 		return 0; /* no problem */
2102 	}
2103 	return 1; /* "I processed the message" */
2104 }
2105 
2106 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb)
2107 {
2108 	struct j1939_session *session;
2109 
2110 	if (!skb->sk)
2111 		return;
2112 
2113 	if (skb->sk->sk_family != AF_CAN ||
2114 	    skb->sk->sk_protocol != CAN_J1939)
2115 		return;
2116 
2117 	j1939_session_list_lock(priv);
2118 	session = j1939_session_get_simple(priv, skb);
2119 	j1939_session_list_unlock(priv);
2120 	if (!session) {
2121 		netdev_warn(priv->ndev,
2122 			    "%s: Received already invalidated message\n",
2123 			    __func__);
2124 		return;
2125 	}
2126 
2127 	j1939_session_timers_cancel(session);
2128 	j1939_session_deactivate(session);
2129 	j1939_session_put(session);
2130 }
2131 
2132 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk)
2133 {
2134 	struct j1939_session *session, *saved;
2135 
2136 	netdev_dbg(priv->ndev, "%s, sk: %p\n", __func__, sk);
2137 	j1939_session_list_lock(priv);
2138 	list_for_each_entry_safe(session, saved,
2139 				 &priv->active_session_list,
2140 				 active_session_list_entry) {
2141 		if (!sk || sk == session->sk) {
2142 			if (hrtimer_try_to_cancel(&session->txtimer) == 1)
2143 				j1939_session_put(session);
2144 			if (hrtimer_try_to_cancel(&session->rxtimer) == 1)
2145 				j1939_session_put(session);
2146 
2147 			session->err = ESHUTDOWN;
2148 			j1939_session_deactivate_locked(session);
2149 		}
2150 	}
2151 	j1939_session_list_unlock(priv);
2152 	return NOTIFY_DONE;
2153 }
2154 
2155 void j1939_tp_init(struct j1939_priv *priv)
2156 {
2157 	spin_lock_init(&priv->active_session_list_lock);
2158 	INIT_LIST_HEAD(&priv->active_session_list);
2159 	priv->tp_max_packet_size = J1939_MAX_ETP_PACKET_SIZE;
2160 }
2161