blob: 9149e8d8aefc23e040f92fd9cafdc6a404867389 [file] [log] [blame]
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2/* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
3 *
4 * This implementation does not provide ISO-TP specific return values to the
5 * userspace.
6 *
7 * - RX path timeout of data reception leads to -ETIMEDOUT
8 * - RX path SN mismatch leads to -EILSEQ
9 * - RX path data reception with wrong padding leads to -EBADMSG
10 * - TX path flowcontrol reception timeout leads to -ECOMM
11 * - TX path flowcontrol reception overflow leads to -EMSGSIZE
12 * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG
13 * - when a transfer (tx) is on the run the next write() blocks until it's done
14 * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent
15 * - as we have static buffers the check whether the PDU fits into the buffer
16 * is done at FF reception time (no support for sending 'wait frames')
17 * - take care of the tx-queue-len as traffic shaping is still on the TODO list
18 *
19 * Copyright (c) 2020 Volkswagen Group Electronic Research
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of Volkswagen nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * Alternatively, provided that this notice is retained in full, this
35 * software may be distributed under the terms of the GNU General
36 * Public License ("GPL") version 2, in which case the provisions of the
37 * GPL apply INSTEAD OF those given above.
38 *
39 * The provided data structures and external interfaces from this code
40 * are not restricted to be used by modules with a GPL compatible license.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
53 * DAMAGE.
54 */
55
56#include <linux/module.h>
57#include <linux/init.h>
58#include <linux/interrupt.h>
Oliver Hartkopp7c759042022-02-08 21:00:26 +010059#include <linux/spinlock.h>
Oliver Hartkoppe057dd32020-09-28 22:04:04 +020060#include <linux/hrtimer.h>
61#include <linux/wait.h>
62#include <linux/uio.h>
63#include <linux/net.h>
64#include <linux/netdevice.h>
65#include <linux/socket.h>
66#include <linux/if_arp.h>
67#include <linux/skbuff.h>
68#include <linux/can.h>
69#include <linux/can/core.h>
70#include <linux/can/skb.h>
71#include <linux/can/isotp.h>
72#include <linux/slab.h>
73#include <net/sock.h>
74#include <net/net_namespace.h>
75
Oliver Hartkoppe057dd32020-09-28 22:04:04 +020076MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol");
77MODULE_LICENSE("Dual BSD/GPL");
78MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
79MODULE_ALIAS("can-proto-6");
80
Oliver Hartkoppf522d952021-03-25 13:58:49 +010081#define ISOTP_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.tp)
82
Oliver Hartkoppe057dd32020-09-28 22:04:04 +020083#define SINGLE_MASK(id) (((id) & CAN_EFF_FLAG) ? \
84 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
85 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
86
87/* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
88 * take full 32 bit values (4 Gbyte). We would need some good concept to handle
89 * this between user space and kernel space. For now increase the static buffer
90 * to something about 8 kbyte to be able to test this new functionality.
91 */
92#define MAX_MSG_LENGTH 8200
93
94/* N_PCI type values in bits 7-4 of N_PCI bytes */
95#define N_PCI_SF 0x00 /* single frame */
96#define N_PCI_FF 0x10 /* first frame */
97#define N_PCI_CF 0x20 /* consecutive frame */
98#define N_PCI_FC 0x30 /* flow control */
99
100#define N_PCI_SZ 1 /* size of the PCI byte #1 */
101#define SF_PCI_SZ4 1 /* size of SingleFrame PCI including 4 bit SF_DL */
102#define SF_PCI_SZ8 2 /* size of SingleFrame PCI including 8 bit SF_DL */
103#define FF_PCI_SZ12 2 /* size of FirstFrame PCI including 12 bit FF_DL */
104#define FF_PCI_SZ32 6 /* size of FirstFrame PCI including 32 bit FF_DL */
105#define FC_CONTENT_SZ 3 /* flow control content size in byte (FS/BS/STmin) */
106
107#define ISOTP_CHECK_PADDING (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA)
108
109/* Flow Status given in FC frame */
110#define ISOTP_FC_CTS 0 /* clear to send */
111#define ISOTP_FC_WT 1 /* wait */
112#define ISOTP_FC_OVFLW 2 /* overflow */
113
114enum {
115 ISOTP_IDLE = 0,
116 ISOTP_WAIT_FIRST_FC,
117 ISOTP_WAIT_FC,
118 ISOTP_WAIT_DATA,
119 ISOTP_SENDING
120};
121
122struct tpcon {
Marc Kleine-Budde5f33a092022-01-05 14:01:12 +0100123 unsigned int idx;
124 unsigned int len;
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800125 u32 state;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200126 u8 bs;
127 u8 sn;
128 u8 ll_dl;
129 u8 buf[MAX_MSG_LENGTH + 1];
130};
131
132struct isotp_sock {
133 struct sock sk;
134 int bound;
135 int ifindex;
136 canid_t txid;
137 canid_t rxid;
138 ktime_t tx_gap;
139 ktime_t lastrxcf_tstamp;
140 struct hrtimer rxtimer, txtimer;
141 struct can_isotp_options opt;
142 struct can_isotp_fc_options rxfc, txfc;
143 struct can_isotp_ll_options ll;
144 u32 force_tx_stmin;
145 u32 force_rx_stmin;
146 struct tpcon rx, tx;
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900147 struct list_head notifier;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200148 wait_queue_head_t wait;
Oliver Hartkopp7c759042022-02-08 21:00:26 +0100149 spinlock_t rx_lock; /* protect single thread state machine */
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200150};
151
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900152static LIST_HEAD(isotp_notifier_list);
153static DEFINE_SPINLOCK(isotp_notifier_lock);
154static struct isotp_sock *isotp_busy_notifier;
155
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200156static inline struct isotp_sock *isotp_sk(const struct sock *sk)
157{
158 return (struct isotp_sock *)sk;
159}
160
161static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
162{
163 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
164 rxtimer);
165 struct sock *sk = &so->sk;
166
167 if (so->rx.state == ISOTP_WAIT_DATA) {
168 /* we did not get new data frames in time */
169
170 /* report 'connection timed out' */
171 sk->sk_err = ETIMEDOUT;
172 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400173 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200174
175 /* reset rx state */
176 so->rx.state = ISOTP_IDLE;
177 }
178
179 return HRTIMER_NORESTART;
180}
181
182static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
183{
184 struct net_device *dev;
185 struct sk_buff *nskb;
186 struct canfd_frame *ncf;
187 struct isotp_sock *so = isotp_sk(sk);
188 int can_send_ret;
189
190 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
191 if (!nskb)
192 return 1;
193
194 dev = dev_get_by_index(sock_net(sk), so->ifindex);
195 if (!dev) {
196 kfree_skb(nskb);
197 return 1;
198 }
199
200 can_skb_reserve(nskb);
201 can_skb_prv(nskb)->ifindex = dev->ifindex;
202 can_skb_prv(nskb)->skbcnt = 0;
203
204 nskb->dev = dev;
205 can_skb_set_owner(nskb, sk);
206 ncf = (struct canfd_frame *)nskb->data;
Oliver Hartkoppb5f020f2021-03-19 11:06:19 +0100207 skb_put_zero(nskb, so->ll.mtu);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200208
209 /* create & send flow control reply */
210 ncf->can_id = so->txid;
211
212 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
213 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
214 ncf->len = CAN_MAX_DLEN;
215 } else {
216 ncf->len = ae + FC_CONTENT_SZ;
217 }
218
219 ncf->data[ae] = N_PCI_FC | flowstatus;
220 ncf->data[ae + 1] = so->rxfc.bs;
221 ncf->data[ae + 2] = so->rxfc.stmin;
222
223 if (ae)
224 ncf->data[0] = so->opt.ext_address;
225
Marc Kleine-Budded4eb5382021-02-18 21:24:20 +0100226 ncf->flags = so->ll.tx_flags;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200227
228 can_send_ret = can_send(nskb, 1);
229 if (can_send_ret)
Patrick Menschel46d86572021-04-27 05:21:47 +0000230 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
231 __func__, ERR_PTR(can_send_ret));
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200232
233 dev_put(dev);
234
235 /* reset blocksize counter */
236 so->rx.bs = 0;
237
238 /* reset last CF frame rx timestamp for rx stmin enforcement */
239 so->lastrxcf_tstamp = ktime_set(0, 0);
240
241 /* start rx timeout watchdog */
242 hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
243 return 0;
244}
245
246static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
247{
248 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
249
250 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
251
252 memset(addr, 0, sizeof(*addr));
253 addr->can_family = AF_CAN;
254 addr->can_ifindex = skb->dev->ifindex;
255
256 if (sock_queue_rcv_skb(sk, skb) < 0)
257 kfree_skb(skb);
258}
259
260static u8 padlen(u8 datalen)
261{
Colin Ian Kingc3ddac42020-10-20 16:42:03 +0100262 static const u8 plen[] = {
263 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */
264 12, 12, 12, 12, /* 9 - 12 */
265 16, 16, 16, 16, /* 13 - 16 */
266 20, 20, 20, 20, /* 17 - 20 */
267 24, 24, 24, 24, /* 21 - 24 */
268 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */
269 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */
270 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */
271 };
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200272
273 if (datalen > 48)
274 return 64;
275
276 return plen[datalen];
277}
278
279/* check for length optimization and return 1/true when the check fails */
280static int check_optimized(struct canfd_frame *cf, int start_index)
281{
282 /* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
283 * padding would start at this point. E.g. if the padding would
284 * start at cf.data[7] cf->len has to be 7 to be optimal.
285 * Note: The data[] index starts with zero.
286 */
287 if (cf->len <= CAN_MAX_DLEN)
288 return (cf->len != start_index);
289
290 /* This relation is also valid in the non-linear DLC range, where
291 * we need to take care of the minimal next possible CAN_DL.
292 * The correct check would be (padlen(cf->len) != padlen(start_index)).
293 * But as cf->len can only take discrete values from 12, .., 64 at this
294 * point the padlen(cf->len) is always equal to cf->len.
295 */
296 return (cf->len != padlen(start_index));
297}
298
299/* check padding and return 1/true when the check fails */
300static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
301 int start_index, u8 content)
302{
303 int i;
304
305 /* no RX_PADDING value => check length of optimized frame length */
306 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
307 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
308 return check_optimized(cf, start_index);
309
310 /* no valid test against empty value => ignore frame */
311 return 1;
312 }
313
314 /* check datalength of correctly padded CAN frame */
315 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
316 cf->len != padlen(cf->len))
317 return 1;
318
319 /* check padding content */
320 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
321 for (i = start_index; i < cf->len; i++)
322 if (cf->data[i] != content)
323 return 1;
324 }
325 return 0;
326}
327
328static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
329{
330 struct sock *sk = &so->sk;
331
332 if (so->tx.state != ISOTP_WAIT_FC &&
333 so->tx.state != ISOTP_WAIT_FIRST_FC)
334 return 0;
335
336 hrtimer_cancel(&so->txtimer);
337
338 if ((cf->len < ae + FC_CONTENT_SZ) ||
339 ((so->opt.flags & ISOTP_CHECK_PADDING) &&
340 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
341 /* malformed PDU - report 'not a data message' */
342 sk->sk_err = EBADMSG;
343 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400344 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200345
346 so->tx.state = ISOTP_IDLE;
347 wake_up_interruptible(&so->wait);
348 return 1;
349 }
350
351 /* get communication parameters only from the first FC frame */
352 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
353 so->txfc.bs = cf->data[ae + 1];
354 so->txfc.stmin = cf->data[ae + 2];
355
356 /* fix wrong STmin values according spec */
357 if (so->txfc.stmin > 0x7F &&
358 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
359 so->txfc.stmin = 0x7F;
360
361 so->tx_gap = ktime_set(0, 0);
362 /* add transmission time for CAN frame N_As */
363 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
364 /* add waiting time for consecutive frames N_Cs */
365 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
366 so->tx_gap = ktime_add_ns(so->tx_gap,
367 so->force_tx_stmin);
368 else if (so->txfc.stmin < 0x80)
369 so->tx_gap = ktime_add_ns(so->tx_gap,
370 so->txfc.stmin * 1000000);
371 else
372 so->tx_gap = ktime_add_ns(so->tx_gap,
373 (so->txfc.stmin - 0xF0)
374 * 100000);
375 so->tx.state = ISOTP_WAIT_FC;
376 }
377
378 switch (cf->data[ae] & 0x0F) {
379 case ISOTP_FC_CTS:
380 so->tx.bs = 0;
381 so->tx.state = ISOTP_SENDING;
382 /* start cyclic timer for sending CF frame */
383 hrtimer_start(&so->txtimer, so->tx_gap,
384 HRTIMER_MODE_REL_SOFT);
385 break;
386
387 case ISOTP_FC_WT:
388 /* start timer to wait for next FC frame */
389 hrtimer_start(&so->txtimer, ktime_set(1, 0),
390 HRTIMER_MODE_REL_SOFT);
391 break;
392
393 case ISOTP_FC_OVFLW:
394 /* overflow on receiver side - report 'message too long' */
395 sk->sk_err = EMSGSIZE;
396 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400397 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200398 fallthrough;
399
400 default:
401 /* stop this tx job */
402 so->tx.state = ISOTP_IDLE;
403 wake_up_interruptible(&so->wait);
404 }
405 return 0;
406}
407
408static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
409 struct sk_buff *skb, int len)
410{
411 struct isotp_sock *so = isotp_sk(sk);
412 struct sk_buff *nskb;
413
414 hrtimer_cancel(&so->rxtimer);
415 so->rx.state = ISOTP_IDLE;
416
417 if (!len || len > cf->len - pcilen)
418 return 1;
419
420 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
421 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
422 /* malformed PDU - report 'not a data message' */
423 sk->sk_err = EBADMSG;
424 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400425 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200426 return 1;
427 }
428
429 nskb = alloc_skb(len, gfp_any());
430 if (!nskb)
431 return 1;
432
433 memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
434
435 nskb->tstamp = skb->tstamp;
436 nskb->dev = skb->dev;
437 isotp_rcv_skb(nskb, sk);
438 return 0;
439}
440
441static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
442{
443 struct isotp_sock *so = isotp_sk(sk);
444 int i;
445 int off;
446 int ff_pci_sz;
447
448 hrtimer_cancel(&so->rxtimer);
449 so->rx.state = ISOTP_IDLE;
450
451 /* get the used sender LL_DL from the (first) CAN frame data length */
452 so->rx.ll_dl = padlen(cf->len);
453
454 /* the first frame has to use the entire frame up to LL_DL length */
455 if (cf->len != so->rx.ll_dl)
456 return 1;
457
458 /* get the FF_DL */
459 so->rx.len = (cf->data[ae] & 0x0F) << 8;
460 so->rx.len += cf->data[ae + 1];
461
462 /* Check for FF_DL escape sequence supporting 32 bit PDU length */
463 if (so->rx.len) {
464 ff_pci_sz = FF_PCI_SZ12;
465 } else {
466 /* FF_DL = 0 => get real length from next 4 bytes */
467 so->rx.len = cf->data[ae + 2] << 24;
468 so->rx.len += cf->data[ae + 3] << 16;
469 so->rx.len += cf->data[ae + 4] << 8;
470 so->rx.len += cf->data[ae + 5];
471 ff_pci_sz = FF_PCI_SZ32;
472 }
473
474 /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
475 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
476
477 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
478 return 1;
479
480 if (so->rx.len > MAX_MSG_LENGTH) {
481 /* send FC frame with overflow status */
482 isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
483 return 1;
484 }
485
486 /* copy the first received data bytes */
487 so->rx.idx = 0;
488 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
489 so->rx.buf[so->rx.idx++] = cf->data[i];
490
491 /* initial setup for this pdu reception */
492 so->rx.sn = 1;
493 so->rx.state = ISOTP_WAIT_DATA;
494
495 /* no creation of flow control frames */
496 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
497 return 0;
498
499 /* send our first FC frame */
500 isotp_send_fc(sk, ae, ISOTP_FC_CTS);
501 return 0;
502}
503
504static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
505 struct sk_buff *skb)
506{
507 struct isotp_sock *so = isotp_sk(sk);
508 struct sk_buff *nskb;
509 int i;
510
511 if (so->rx.state != ISOTP_WAIT_DATA)
512 return 0;
513
514 /* drop if timestamp gap is less than force_rx_stmin nano secs */
515 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
516 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
517 so->force_rx_stmin)
518 return 0;
519
520 so->lastrxcf_tstamp = skb->tstamp;
521 }
522
523 hrtimer_cancel(&so->rxtimer);
524
525 /* CFs are never longer than the FF */
526 if (cf->len > so->rx.ll_dl)
527 return 1;
528
529 /* CFs have usually the LL_DL length */
530 if (cf->len < so->rx.ll_dl) {
531 /* this is only allowed for the last CF */
532 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
533 return 1;
534 }
535
536 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
537 /* wrong sn detected - report 'illegal byte sequence' */
538 sk->sk_err = EILSEQ;
539 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400540 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200541
542 /* reset rx state */
543 so->rx.state = ISOTP_IDLE;
544 return 1;
545 }
546 so->rx.sn++;
547 so->rx.sn %= 16;
548
549 for (i = ae + N_PCI_SZ; i < cf->len; i++) {
550 so->rx.buf[so->rx.idx++] = cf->data[i];
551 if (so->rx.idx >= so->rx.len)
552 break;
553 }
554
555 if (so->rx.idx >= so->rx.len) {
556 /* we are done */
557 so->rx.state = ISOTP_IDLE;
558
559 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
560 check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
561 /* malformed PDU - report 'not a data message' */
562 sk->sk_err = EBADMSG;
563 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400564 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200565 return 1;
566 }
567
568 nskb = alloc_skb(so->rx.len, gfp_any());
569 if (!nskb)
570 return 1;
571
572 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
573 so->rx.len);
574
575 nskb->tstamp = skb->tstamp;
576 nskb->dev = skb->dev;
577 isotp_rcv_skb(nskb, sk);
578 return 0;
579 }
580
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200581 /* perform blocksize handling, if enabled */
582 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
583 /* start rx timeout watchdog */
584 hrtimer_start(&so->rxtimer, ktime_set(1, 0),
585 HRTIMER_MODE_REL_SOFT);
586 return 0;
587 }
588
Oliver Hartkopp78656ea2020-10-19 14:02:29 +0200589 /* no creation of flow control frames */
590 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
591 return 0;
592
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200593 /* we reached the specified blocksize so->rxfc.bs */
594 isotp_send_fc(sk, ae, ISOTP_FC_CTS);
595 return 0;
596}
597
598static void isotp_rcv(struct sk_buff *skb, void *data)
599{
600 struct sock *sk = (struct sock *)data;
601 struct isotp_sock *so = isotp_sk(sk);
602 struct canfd_frame *cf;
603 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
604 u8 n_pci_type, sf_dl;
605
606 /* Strictly receive only frames with the configured MTU size
607 * => clear separation of CAN2.0 / CAN FD transport channels
608 */
609 if (skb->len != so->ll.mtu)
610 return;
611
612 cf = (struct canfd_frame *)skb->data;
613
614 /* if enabled: check reception of my configured extended address */
615 if (ae && cf->data[0] != so->opt.rx_ext_address)
616 return;
617
618 n_pci_type = cf->data[ae] & 0xF0;
619
Oliver Hartkopp7c759042022-02-08 21:00:26 +0100620 /* Make sure the state changes and data structures stay consistent at
621 * CAN frame reception time. This locking is not needed in real world
622 * use cases but the inconsistency can be triggered with syzkaller.
623 */
624 spin_lock(&so->rx_lock);
625
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200626 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
627 /* check rx/tx path half duplex expectations */
628 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
629 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
Oliver Hartkopp7c759042022-02-08 21:00:26 +0100630 goto out_unlock;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200631 }
632
633 switch (n_pci_type) {
634 case N_PCI_FC:
635 /* tx path: flow control frame containing the FC parameters */
636 isotp_rcv_fc(so, cf, ae);
637 break;
638
639 case N_PCI_SF:
640 /* rx path: single frame
641 *
642 * As we do not have a rx.ll_dl configuration, we can only test
643 * if the CAN frames payload length matches the LL_DL == 8
644 * requirements - no matter if it's CAN 2.0 or CAN FD
645 */
646
647 /* get the SF_DL from the N_PCI byte */
648 sf_dl = cf->data[ae] & 0x0F;
649
650 if (cf->len <= CAN_MAX_DLEN) {
651 isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl);
652 } else {
653 if (skb->len == CANFD_MTU) {
654 /* We have a CAN FD frame and CAN_DL is greater than 8:
655 * Only frames with the SF_DL == 0 ESC value are valid.
656 *
657 * If so take care of the increased SF PCI size
658 * (SF_PCI_SZ8) to point to the message content behind
659 * the extended SF PCI info and get the real SF_DL
660 * length value from the formerly first data byte.
661 */
662 if (sf_dl == 0)
663 isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb,
664 cf->data[SF_PCI_SZ4 + ae]);
665 }
666 }
667 break;
668
669 case N_PCI_FF:
670 /* rx path: first frame */
671 isotp_rcv_ff(sk, cf, ae);
672 break;
673
674 case N_PCI_CF:
675 /* rx path: consecutive frame */
676 isotp_rcv_cf(sk, cf, ae, skb);
677 break;
678 }
Oliver Hartkopp7c759042022-02-08 21:00:26 +0100679
680out_unlock:
681 spin_unlock(&so->rx_lock);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200682}
683
684static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
685 int ae, int off)
686{
687 int pcilen = N_PCI_SZ + ae + off;
688 int space = so->tx.ll_dl - pcilen;
689 int num = min_t(int, so->tx.len - so->tx.idx, space);
690 int i;
691
692 cf->can_id = so->txid;
693 cf->len = num + pcilen;
694
695 if (num < space) {
696 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
697 /* user requested padding */
698 cf->len = padlen(cf->len);
699 memset(cf->data, so->opt.txpad_content, cf->len);
700 } else if (cf->len > CAN_MAX_DLEN) {
701 /* mandatory padding for CAN FD frames */
702 cf->len = padlen(cf->len);
703 memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
704 cf->len);
705 }
706 }
707
708 for (i = 0; i < num; i++)
709 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
710
711 if (ae)
712 cf->data[0] = so->opt.ext_address;
713}
714
715static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
716 int ae)
717{
718 int i;
719 int ff_pci_sz;
720
721 cf->can_id = so->txid;
722 cf->len = so->tx.ll_dl;
723 if (ae)
724 cf->data[0] = so->opt.ext_address;
725
726 /* create N_PCI bytes with 12/32 bit FF_DL data length */
727 if (so->tx.len > 4095) {
728 /* use 32 bit FF_DL notation */
729 cf->data[ae] = N_PCI_FF;
730 cf->data[ae + 1] = 0;
731 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
732 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
733 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
734 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
735 ff_pci_sz = FF_PCI_SZ32;
736 } else {
737 /* use 12 bit FF_DL notation */
738 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
739 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
740 ff_pci_sz = FF_PCI_SZ12;
741 }
742
743 /* add first data bytes depending on ae */
744 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
745 cf->data[i] = so->tx.buf[so->tx.idx++];
746
747 so->tx.sn = 1;
748 so->tx.state = ISOTP_WAIT_FIRST_FC;
749}
750
751static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
752{
753 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
754 txtimer);
755 struct sock *sk = &so->sk;
756 struct sk_buff *skb;
757 struct net_device *dev;
758 struct canfd_frame *cf;
759 enum hrtimer_restart restart = HRTIMER_NORESTART;
760 int can_send_ret;
761 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
762
763 switch (so->tx.state) {
764 case ISOTP_WAIT_FC:
765 case ISOTP_WAIT_FIRST_FC:
766
767 /* we did not get any flow control frame in time */
768
769 /* report 'communication error on send' */
770 sk->sk_err = ECOMM;
771 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400772 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200773
774 /* reset tx state */
775 so->tx.state = ISOTP_IDLE;
776 wake_up_interruptible(&so->wait);
777 break;
778
779 case ISOTP_SENDING:
780
781 /* push out the next segmented pdu */
782 dev = dev_get_by_index(sock_net(sk), so->ifindex);
783 if (!dev)
784 break;
785
786isotp_tx_burst:
787 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv),
Oliver Hartkoppac911bf2020-10-12 09:43:53 +0200788 GFP_ATOMIC);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200789 if (!skb) {
790 dev_put(dev);
791 break;
792 }
793
794 can_skb_reserve(skb);
795 can_skb_prv(skb)->ifindex = dev->ifindex;
796 can_skb_prv(skb)->skbcnt = 0;
797
798 cf = (struct canfd_frame *)skb->data;
Oliver Hartkoppb5f020f2021-03-19 11:06:19 +0100799 skb_put_zero(skb, so->ll.mtu);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200800
801 /* create consecutive frame */
802 isotp_fill_dataframe(cf, so, ae, 0);
803
804 /* place consecutive frame N_PCI in appropriate index */
805 cf->data[ae] = N_PCI_CF | so->tx.sn++;
806 so->tx.sn %= 16;
807 so->tx.bs++;
808
Marc Kleine-Budded4eb5382021-02-18 21:24:20 +0100809 cf->flags = so->ll.tx_flags;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200810
811 skb->dev = dev;
812 can_skb_set_owner(skb, sk);
813
814 can_send_ret = can_send(skb, 1);
Patrick Menschelc69d1902021-04-27 05:21:49 +0000815 if (can_send_ret) {
Patrick Menschel46d86572021-04-27 05:21:47 +0000816 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
817 __func__, ERR_PTR(can_send_ret));
Patrick Menschelc69d1902021-04-27 05:21:49 +0000818 if (can_send_ret == -ENOBUFS)
819 pr_notice_once("can-isotp: tx queue is full, increasing txqueuelen may prevent this error\n");
820 }
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200821 if (so->tx.idx >= so->tx.len) {
822 /* we are done */
823 so->tx.state = ISOTP_IDLE;
824 dev_put(dev);
825 wake_up_interruptible(&so->wait);
826 break;
827 }
828
829 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
830 /* stop and wait for FC */
831 so->tx.state = ISOTP_WAIT_FC;
832 dev_put(dev);
833 hrtimer_set_expires(&so->txtimer,
834 ktime_add(ktime_get(),
835 ktime_set(1, 0)));
836 restart = HRTIMER_RESTART;
837 break;
838 }
839
840 /* no gap between data frames needed => use burst mode */
841 if (!so->tx_gap)
842 goto isotp_tx_burst;
843
844 /* start timer to send next data frame with correct delay */
845 dev_put(dev);
846 hrtimer_set_expires(&so->txtimer,
847 ktime_add(ktime_get(), so->tx_gap));
848 restart = HRTIMER_RESTART;
849 break;
850
851 default:
852 WARN_ON_ONCE(1);
853 }
854
855 return restart;
856}
857
858static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
859{
860 struct sock *sk = sock->sk;
861 struct isotp_sock *so = isotp_sk(sk);
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800862 u32 old_state = so->tx.state;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200863 struct sk_buff *skb;
864 struct net_device *dev;
865 struct canfd_frame *cf;
866 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
867 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
868 int off;
869 int err;
870
871 if (!so->bound)
872 return -EADDRNOTAVAIL;
873
874 /* we do not support multiple buffers - for now */
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800875 if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE ||
876 wq_has_sleeper(&so->wait)) {
877 if (msg->msg_flags & MSG_DONTWAIT) {
878 err = -EAGAIN;
879 goto err_out;
880 }
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200881
882 /* wait for complete transmission of current pdu */
Ziyang Xuan9acf6362021-10-09 15:40:18 +0800883 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
884 if (err)
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800885 goto err_out;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200886 }
887
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800888 if (!size || size > MAX_MSG_LENGTH) {
889 err = -EINVAL;
890 goto err_out;
891 }
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200892
Oliver Hartkopp921ca572020-12-06 15:47:31 +0100893 /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
894 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
895
896 /* does the given data fit into a single frame for SF_BROADCAST? */
897 if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) &&
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800898 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
899 err = -EINVAL;
900 goto err_out;
901 }
Oliver Hartkopp921ca572020-12-06 15:47:31 +0100902
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200903 err = memcpy_from_msg(so->tx.buf, msg, size);
904 if (err < 0)
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800905 goto err_out;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200906
907 dev = dev_get_by_index(sock_net(sk), so->ifindex);
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800908 if (!dev) {
909 err = -ENXIO;
910 goto err_out;
911 }
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200912
913 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
914 msg->msg_flags & MSG_DONTWAIT, &err);
915 if (!skb) {
916 dev_put(dev);
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800917 goto err_out;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200918 }
919
920 can_skb_reserve(skb);
921 can_skb_prv(skb)->ifindex = dev->ifindex;
922 can_skb_prv(skb)->skbcnt = 0;
923
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200924 so->tx.len = size;
925 so->tx.idx = 0;
926
927 cf = (struct canfd_frame *)skb->data;
Oliver Hartkoppb5f020f2021-03-19 11:06:19 +0100928 skb_put_zero(skb, so->ll.mtu);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200929
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200930 /* check for single frame transmission depending on TX_DL */
931 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
932 /* The message size generally fits into a SingleFrame - good.
933 *
934 * SF_DL ESC offset optimization:
935 *
936 * When TX_DL is greater 8 but the message would still fit
937 * into a 8 byte CAN frame, we can omit the offset.
938 * This prevents a protocol caused length extension from
939 * CAN_DL = 8 to CAN_DL = 12 due to the SF_SL ESC handling.
940 */
941 if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
942 off = 0;
943
944 isotp_fill_dataframe(cf, so, ae, off);
945
946 /* place single frame N_PCI w/o length in appropriate index */
947 cf->data[ae] = N_PCI_SF;
948
949 /* place SF_DL size value depending on the SF_DL ESC offset */
950 if (off)
951 cf->data[SF_PCI_SZ4 + ae] = size;
952 else
953 cf->data[ae] |= size;
954
955 so->tx.state = ISOTP_IDLE;
956 wake_up_interruptible(&so->wait);
957
958 /* don't enable wait queue for a single frame transmission */
959 wait_tx_done = 0;
960 } else {
961 /* send first frame and wait for FC */
962
963 isotp_create_fframe(cf, so, ae);
964
965 /* start timeout for FC */
966 hrtimer_start(&so->txtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
967 }
968
969 /* send the first or only CAN frame */
Marc Kleine-Budded4eb5382021-02-18 21:24:20 +0100970 cf->flags = so->ll.tx_flags;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200971
972 skb->dev = dev;
973 skb->sk = sk;
974 err = can_send(skb, 1);
975 dev_put(dev);
976 if (err) {
Patrick Menschel46d86572021-04-27 05:21:47 +0000977 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
978 __func__, ERR_PTR(err));
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800979 goto err_out;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200980 }
981
982 if (wait_tx_done) {
983 /* wait for complete transmission of current pdu */
984 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
Marc Kleine-Budded674a8f2021-05-07 11:18:39 +0200985
986 if (sk->sk_err)
987 return -sk->sk_err;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200988 }
989
990 return size;
Ziyang Xuan43a08c32021-10-09 15:40:30 +0800991
992err_out:
993 so->tx.state = old_state;
994 if (so->tx.state == ISOTP_IDLE)
995 wake_up_interruptible(&so->wait);
996
997 return err;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +0200998}
999
1000static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1001 int flags)
1002{
1003 struct sock *sk = sock->sk;
1004 struct sk_buff *skb;
1005 int err = 0;
1006 int noblock;
1007
1008 noblock = flags & MSG_DONTWAIT;
1009 flags &= ~MSG_DONTWAIT;
1010
1011 skb = skb_recv_datagram(sk, flags, noblock, &err);
1012 if (!skb)
1013 return err;
1014
1015 if (size < skb->len)
1016 msg->msg_flags |= MSG_TRUNC;
1017 else
1018 size = skb->len;
1019
1020 err = memcpy_to_msg(msg, skb->data, size);
1021 if (err < 0) {
1022 skb_free_datagram(sk, skb);
1023 return err;
1024 }
1025
1026 sock_recv_timestamp(msg, sk, skb);
1027
1028 if (msg->msg_name) {
Oliver Hartkoppf522d952021-03-25 13:58:49 +01001029 __sockaddr_check_size(ISOTP_MIN_NAMELEN);
1030 msg->msg_namelen = ISOTP_MIN_NAMELEN;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001031 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1032 }
1033
1034 skb_free_datagram(sk, skb);
1035
1036 return size;
1037}
1038
1039static int isotp_release(struct socket *sock)
1040{
1041 struct sock *sk = sock->sk;
1042 struct isotp_sock *so;
1043 struct net *net;
1044
1045 if (!sk)
1046 return 0;
1047
1048 so = isotp_sk(sk);
1049 net = sock_net(sk);
1050
1051 /* wait for complete transmission of current pdu */
1052 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1053
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001054 spin_lock(&isotp_notifier_lock);
1055 while (isotp_busy_notifier == so) {
1056 spin_unlock(&isotp_notifier_lock);
1057 schedule_timeout_uninterruptible(1);
1058 spin_lock(&isotp_notifier_lock);
1059 }
1060 list_del(&so->notifier);
1061 spin_unlock(&isotp_notifier_lock);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001062
1063 lock_sock(sk);
1064
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001065 /* remove current filters & unregister */
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001066 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) {
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001067 if (so->ifindex) {
1068 struct net_device *dev;
1069
1070 dev = dev_get_by_index(net, so->ifindex);
1071 if (dev) {
1072 can_rx_unregister(net, dev, so->rxid,
1073 SINGLE_MASK(so->rxid),
1074 isotp_rcv, sk);
1075 dev_put(dev);
Oliver Hartkopp14a46962021-06-18 19:37:13 +02001076 synchronize_rcu();
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001077 }
1078 }
1079 }
1080
Oliver Hartkopp14a46962021-06-18 19:37:13 +02001081 hrtimer_cancel(&so->txtimer);
1082 hrtimer_cancel(&so->rxtimer);
1083
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001084 so->ifindex = 0;
1085 so->bound = 0;
1086
1087 sock_orphan(sk);
1088 sock->sk = NULL;
1089
1090 release_sock(sk);
1091 sock_put(sk);
1092
1093 return 0;
1094}
1095
1096static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
1097{
1098 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1099 struct sock *sk = sock->sk;
1100 struct isotp_sock *so = isotp_sk(sk);
1101 struct net *net = sock_net(sk);
1102 int ifindex;
1103 struct net_device *dev;
1104 int err = 0;
1105 int notify_enetdown = 0;
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001106 int do_rx_reg = 1;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001107
Oliver Hartkoppf522d952021-03-25 13:58:49 +01001108 if (len < ISOTP_MIN_NAMELEN)
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001109 return -EINVAL;
1110
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001111 if (addr->can_addr.tp.tx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG))
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001112 return -EADDRNOTAVAIL;
1113
1114 if (!addr->can_ifindex)
1115 return -ENODEV;
1116
1117 lock_sock(sk);
1118
Norbert Slusarek2b17c402021-05-12 00:43:54 +02001119 /* do not register frame reception for functional addressing */
1120 if (so->opt.flags & CAN_ISOTP_SF_BROADCAST)
1121 do_rx_reg = 0;
1122
1123 /* do not validate rx address for functional addressing */
1124 if (do_rx_reg) {
1125 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) {
1126 err = -EADDRNOTAVAIL;
1127 goto out;
1128 }
1129
1130 if (addr->can_addr.tp.rx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) {
1131 err = -EADDRNOTAVAIL;
1132 goto out;
1133 }
1134 }
1135
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001136 if (so->bound && addr->can_ifindex == so->ifindex &&
1137 addr->can_addr.tp.rx_id == so->rxid &&
1138 addr->can_addr.tp.tx_id == so->txid)
1139 goto out;
1140
1141 dev = dev_get_by_index(net, addr->can_ifindex);
1142 if (!dev) {
1143 err = -ENODEV;
1144 goto out;
1145 }
1146 if (dev->type != ARPHRD_CAN) {
1147 dev_put(dev);
1148 err = -ENODEV;
1149 goto out;
1150 }
1151 if (dev->mtu < so->ll.mtu) {
1152 dev_put(dev);
1153 err = -EINVAL;
1154 goto out;
1155 }
1156 if (!(dev->flags & IFF_UP))
1157 notify_enetdown = 1;
1158
1159 ifindex = dev->ifindex;
1160
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001161 if (do_rx_reg)
1162 can_rx_register(net, dev, addr->can_addr.tp.rx_id,
1163 SINGLE_MASK(addr->can_addr.tp.rx_id),
1164 isotp_rcv, sk, "isotp", sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001165
1166 dev_put(dev);
1167
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001168 if (so->bound && do_rx_reg) {
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001169 /* unregister old filter */
1170 if (so->ifindex) {
1171 dev = dev_get_by_index(net, so->ifindex);
1172 if (dev) {
1173 can_rx_unregister(net, dev, so->rxid,
1174 SINGLE_MASK(so->rxid),
1175 isotp_rcv, sk);
1176 dev_put(dev);
1177 }
1178 }
1179 }
1180
1181 /* switch to new settings */
1182 so->ifindex = ifindex;
1183 so->rxid = addr->can_addr.tp.rx_id;
1184 so->txid = addr->can_addr.tp.tx_id;
1185 so->bound = 1;
1186
1187out:
1188 release_sock(sk);
1189
1190 if (notify_enetdown) {
1191 sk->sk_err = ENETDOWN;
1192 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04001193 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001194 }
1195
1196 return err;
1197}
1198
1199static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
1200{
1201 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1202 struct sock *sk = sock->sk;
1203 struct isotp_sock *so = isotp_sk(sk);
1204
1205 if (peer)
1206 return -EOPNOTSUPP;
1207
Oliver Hartkoppf522d952021-03-25 13:58:49 +01001208 memset(addr, 0, ISOTP_MIN_NAMELEN);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001209 addr->can_family = AF_CAN;
1210 addr->can_ifindex = so->ifindex;
1211 addr->can_addr.tp.rx_id = so->rxid;
1212 addr->can_addr.tp.tx_id = so->txid;
1213
Oliver Hartkoppf522d952021-03-25 13:58:49 +01001214 return ISOTP_MIN_NAMELEN;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001215}
1216
Norbert Slusarek2b17c402021-05-12 00:43:54 +02001217static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001218 sockptr_t optval, unsigned int optlen)
1219{
1220 struct sock *sk = sock->sk;
1221 struct isotp_sock *so = isotp_sk(sk);
1222 int ret = 0;
1223
Oliver Hartkopp323a3912020-12-04 14:35:07 +01001224 if (so->bound)
1225 return -EISCONN;
1226
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001227 switch (optname) {
1228 case CAN_ISOTP_OPTS:
1229 if (optlen != sizeof(struct can_isotp_options))
1230 return -EINVAL;
1231
1232 if (copy_from_sockptr(&so->opt, optval, optlen))
1233 return -EFAULT;
1234
1235 /* no separate rx_ext_address is given => use ext_address */
1236 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1237 so->opt.rx_ext_address = so->opt.ext_address;
1238 break;
1239
1240 case CAN_ISOTP_RECV_FC:
1241 if (optlen != sizeof(struct can_isotp_fc_options))
1242 return -EINVAL;
1243
1244 if (copy_from_sockptr(&so->rxfc, optval, optlen))
1245 return -EFAULT;
1246 break;
1247
1248 case CAN_ISOTP_TX_STMIN:
1249 if (optlen != sizeof(u32))
1250 return -EINVAL;
1251
1252 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1253 return -EFAULT;
1254 break;
1255
1256 case CAN_ISOTP_RX_STMIN:
1257 if (optlen != sizeof(u32))
1258 return -EINVAL;
1259
1260 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1261 return -EFAULT;
1262 break;
1263
1264 case CAN_ISOTP_LL_OPTS:
1265 if (optlen == sizeof(struct can_isotp_ll_options)) {
1266 struct can_isotp_ll_options ll;
1267
1268 if (copy_from_sockptr(&ll, optval, optlen))
1269 return -EFAULT;
1270
1271 /* check for correct ISO 11898-1 DLC data length */
1272 if (ll.tx_dl != padlen(ll.tx_dl))
1273 return -EINVAL;
1274
1275 if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU)
1276 return -EINVAL;
1277
Marc Kleine-Buddee4912452021-02-18 21:58:36 +01001278 if (ll.mtu == CAN_MTU &&
1279 (ll.tx_dl > CAN_MAX_DLEN || ll.tx_flags != 0))
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001280 return -EINVAL;
1281
1282 memcpy(&so->ll, &ll, sizeof(ll));
1283
1284 /* set ll_dl for tx path to similar place as for rx */
1285 so->tx.ll_dl = ll.tx_dl;
1286 } else {
1287 return -EINVAL;
1288 }
1289 break;
1290
1291 default:
1292 ret = -ENOPROTOOPT;
1293 }
1294
1295 return ret;
1296}
1297
Norbert Slusarek2b17c402021-05-12 00:43:54 +02001298static int isotp_setsockopt(struct socket *sock, int level, int optname,
1299 sockptr_t optval, unsigned int optlen)
1300
1301{
1302 struct sock *sk = sock->sk;
1303 int ret;
1304
1305 if (level != SOL_CAN_ISOTP)
1306 return -EINVAL;
1307
1308 lock_sock(sk);
1309 ret = isotp_setsockopt_locked(sock, level, optname, optval, optlen);
1310 release_sock(sk);
1311 return ret;
1312}
1313
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001314static int isotp_getsockopt(struct socket *sock, int level, int optname,
1315 char __user *optval, int __user *optlen)
1316{
1317 struct sock *sk = sock->sk;
1318 struct isotp_sock *so = isotp_sk(sk);
1319 int len;
1320 void *val;
1321
1322 if (level != SOL_CAN_ISOTP)
1323 return -EINVAL;
1324 if (get_user(len, optlen))
1325 return -EFAULT;
1326 if (len < 0)
1327 return -EINVAL;
1328
1329 switch (optname) {
1330 case CAN_ISOTP_OPTS:
1331 len = min_t(int, len, sizeof(struct can_isotp_options));
1332 val = &so->opt;
1333 break;
1334
1335 case CAN_ISOTP_RECV_FC:
1336 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
1337 val = &so->rxfc;
1338 break;
1339
1340 case CAN_ISOTP_TX_STMIN:
1341 len = min_t(int, len, sizeof(u32));
1342 val = &so->force_tx_stmin;
1343 break;
1344
1345 case CAN_ISOTP_RX_STMIN:
1346 len = min_t(int, len, sizeof(u32));
1347 val = &so->force_rx_stmin;
1348 break;
1349
1350 case CAN_ISOTP_LL_OPTS:
1351 len = min_t(int, len, sizeof(struct can_isotp_ll_options));
1352 val = &so->ll;
1353 break;
1354
1355 default:
1356 return -ENOPROTOOPT;
1357 }
1358
1359 if (put_user(len, optlen))
1360 return -EFAULT;
1361 if (copy_to_user(optval, val, len))
1362 return -EFAULT;
1363 return 0;
1364}
1365
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001366static void isotp_notify(struct isotp_sock *so, unsigned long msg,
1367 struct net_device *dev)
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001368{
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001369 struct sock *sk = &so->sk;
1370
1371 if (!net_eq(dev_net(dev), sock_net(sk)))
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001372 return;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001373
1374 if (so->ifindex != dev->ifindex)
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001375 return;
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001376
1377 switch (msg) {
1378 case NETDEV_UNREGISTER:
1379 lock_sock(sk);
1380 /* remove current filters & unregister */
Oliver Hartkopp921ca572020-12-06 15:47:31 +01001381 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST)))
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001382 can_rx_unregister(dev_net(dev), dev, so->rxid,
1383 SINGLE_MASK(so->rxid),
1384 isotp_rcv, sk);
1385
1386 so->ifindex = 0;
1387 so->bound = 0;
1388 release_sock(sk);
1389
1390 sk->sk_err = ENODEV;
1391 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04001392 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001393 break;
1394
1395 case NETDEV_DOWN:
1396 sk->sk_err = ENETDOWN;
1397 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04001398 sk_error_report(sk);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001399 break;
1400 }
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001401}
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001402
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001403static int isotp_notifier(struct notifier_block *nb, unsigned long msg,
1404 void *ptr)
1405{
1406 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1407
1408 if (dev->type != ARPHRD_CAN)
1409 return NOTIFY_DONE;
1410 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
1411 return NOTIFY_DONE;
1412 if (unlikely(isotp_busy_notifier)) /* Check for reentrant bug. */
1413 return NOTIFY_DONE;
1414
1415 spin_lock(&isotp_notifier_lock);
1416 list_for_each_entry(isotp_busy_notifier, &isotp_notifier_list, notifier) {
1417 spin_unlock(&isotp_notifier_lock);
1418 isotp_notify(isotp_busy_notifier, msg, dev);
1419 spin_lock(&isotp_notifier_lock);
1420 }
1421 isotp_busy_notifier = NULL;
1422 spin_unlock(&isotp_notifier_lock);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001423 return NOTIFY_DONE;
1424}
1425
1426static int isotp_init(struct sock *sk)
1427{
1428 struct isotp_sock *so = isotp_sk(sk);
1429
1430 so->ifindex = 0;
1431 so->bound = 0;
1432
1433 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1434 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1435 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1436 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1437 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1438 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1439 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1440 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1441 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1442 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1443 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1444 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1445
1446 /* set ll_dl for tx path to similar place as for rx */
1447 so->tx.ll_dl = so->ll.tx_dl;
1448
1449 so->rx.state = ISOTP_IDLE;
1450 so->tx.state = ISOTP_IDLE;
1451
1452 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1453 so->rxtimer.function = isotp_rx_timer_handler;
1454 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1455 so->txtimer.function = isotp_tx_timer_handler;
1456
1457 init_waitqueue_head(&so->wait);
Oliver Hartkopp7c759042022-02-08 21:00:26 +01001458 spin_lock_init(&so->rx_lock);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001459
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001460 spin_lock(&isotp_notifier_lock);
1461 list_add_tail(&so->notifier, &isotp_notifier_list);
1462 spin_unlock(&isotp_notifier_lock);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001463
1464 return 0;
1465}
1466
1467static int isotp_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1468 unsigned long arg)
1469{
1470 /* no ioctls for socket layer -> hand it down to NIC layer */
1471 return -ENOIOCTLCMD;
1472}
1473
1474static const struct proto_ops isotp_ops = {
1475 .family = PF_CAN,
1476 .release = isotp_release,
1477 .bind = isotp_bind,
1478 .connect = sock_no_connect,
1479 .socketpair = sock_no_socketpair,
1480 .accept = sock_no_accept,
1481 .getname = isotp_getname,
1482 .poll = datagram_poll,
1483 .ioctl = isotp_sock_no_ioctlcmd,
1484 .gettstamp = sock_gettstamp,
1485 .listen = sock_no_listen,
1486 .shutdown = sock_no_shutdown,
1487 .setsockopt = isotp_setsockopt,
1488 .getsockopt = isotp_getsockopt,
1489 .sendmsg = isotp_sendmsg,
1490 .recvmsg = isotp_recvmsg,
1491 .mmap = sock_no_mmap,
1492 .sendpage = sock_no_sendpage,
1493};
1494
1495static struct proto isotp_proto __read_mostly = {
1496 .name = "CAN_ISOTP",
1497 .owner = THIS_MODULE,
1498 .obj_size = sizeof(struct isotp_sock),
1499 .init = isotp_init,
1500};
1501
1502static const struct can_proto isotp_can_proto = {
1503 .type = SOCK_DGRAM,
1504 .protocol = CAN_ISOTP,
1505 .ops = &isotp_ops,
1506 .prot = &isotp_proto,
1507};
1508
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001509static struct notifier_block canisotp_notifier = {
1510 .notifier_call = isotp_notifier
1511};
1512
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001513static __init int isotp_module_init(void)
1514{
1515 int err;
1516
Oliver Hartkoppf726f3d2020-10-12 09:43:54 +02001517 pr_info("can: isotp protocol\n");
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001518
1519 err = can_proto_register(&isotp_can_proto);
1520 if (err < 0)
Patrick Menschel6a5ddae2021-04-27 05:21:48 +00001521 pr_err("can: registration of isotp protocol failed %pe\n", ERR_PTR(err));
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001522 else
1523 register_netdevice_notifier(&canisotp_notifier);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001524
1525 return err;
1526}
1527
1528static __exit void isotp_module_exit(void)
1529{
1530 can_proto_unregister(&isotp_can_proto);
Tetsuo Handa8d0caed2021-06-05 19:26:35 +09001531 unregister_netdevice_notifier(&canisotp_notifier);
Oliver Hartkoppe057dd32020-09-28 22:04:04 +02001532}
1533
1534module_init(isotp_module_init);
1535module_exit(isotp_module_exit);