~ [ 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.8.18)


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

~ [ 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