~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/can/j1939/transport.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /net/can/j1939/transport.c (Version linux-6.12-rc7) and /net/can/j1939/transport.c (Version linux-5.10.229)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php