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