1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
4 * This implementation does not provide ISO-TP specific return values to the
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
19 * Copyright (c) 2020 Volkswagen Group Electronic Research
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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.
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.
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.
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
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/interrupt.h>
59 #include <linux/hrtimer.h>
60 #include <linux/wait.h>
61 #include <linux/uio.h>
62 #include <linux/net.h>
63 #include <linux/netdevice.h>
64 #include <linux/socket.h>
65 #include <linux/if_arp.h>
66 #include <linux/skbuff.h>
67 #include <linux/can.h>
68 #include <linux/can/core.h>
69 #include <linux/can/skb.h>
70 #include <linux/can/isotp.h>
71 #include <linux/slab.h>
73 #include <net/net_namespace.h>
75 MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol");
76 MODULE_LICENSE("Dual BSD/GPL");
77 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
78 MODULE_ALIAS("can-proto-6");
80 #define ISOTP_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.tp)
82 #define SINGLE_MASK(id) (((id) & CAN_EFF_FLAG) ? \
83 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
84 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
86 /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
87 * take full 32 bit values (4 Gbyte). We would need some good concept to handle
88 * this between user space and kernel space. For now increase the static buffer
89 * to something about 8 kbyte to be able to test this new functionality.
91 #define MAX_MSG_LENGTH 8200
93 /* N_PCI type values in bits 7-4 of N_PCI bytes */
94 #define N_PCI_SF 0x00 /* single frame */
95 #define N_PCI_FF 0x10 /* first frame */
96 #define N_PCI_CF 0x20 /* consecutive frame */
97 #define N_PCI_FC 0x30 /* flow control */
99 #define N_PCI_SZ 1 /* size of the PCI byte #1 */
100 #define SF_PCI_SZ4 1 /* size of SingleFrame PCI including 4 bit SF_DL */
101 #define SF_PCI_SZ8 2 /* size of SingleFrame PCI including 8 bit SF_DL */
102 #define FF_PCI_SZ12 2 /* size of FirstFrame PCI including 12 bit FF_DL */
103 #define FF_PCI_SZ32 6 /* size of FirstFrame PCI including 32 bit FF_DL */
104 #define FC_CONTENT_SZ 3 /* flow control content size in byte (FS/BS/STmin) */
106 #define ISOTP_CHECK_PADDING (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA)
108 /* Flow Status given in FC frame */
109 #define ISOTP_FC_CTS 0 /* clear to send */
110 #define ISOTP_FC_WT 1 /* wait */
111 #define ISOTP_FC_OVFLW 2 /* overflow */
128 u8 buf[MAX_MSG_LENGTH + 1];
138 ktime_t lastrxcf_tstamp;
139 struct hrtimer rxtimer, txtimer;
140 struct can_isotp_options opt;
141 struct can_isotp_fc_options rxfc, txfc;
142 struct can_isotp_ll_options ll;
146 struct list_head notifier;
147 wait_queue_head_t wait;
150 static LIST_HEAD(isotp_notifier_list);
151 static DEFINE_SPINLOCK(isotp_notifier_lock);
152 static struct isotp_sock *isotp_busy_notifier;
154 static inline struct isotp_sock *isotp_sk(const struct sock *sk)
156 return (struct isotp_sock *)sk;
159 static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
161 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
163 struct sock *sk = &so->sk;
165 if (so->rx.state == ISOTP_WAIT_DATA) {
166 /* we did not get new data frames in time */
168 /* report 'connection timed out' */
169 sk->sk_err = ETIMEDOUT;
170 if (!sock_flag(sk, SOCK_DEAD))
174 so->rx.state = ISOTP_IDLE;
177 return HRTIMER_NORESTART;
180 static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
182 struct net_device *dev;
183 struct sk_buff *nskb;
184 struct canfd_frame *ncf;
185 struct isotp_sock *so = isotp_sk(sk);
188 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
192 dev = dev_get_by_index(sock_net(sk), so->ifindex);
198 can_skb_reserve(nskb);
199 can_skb_prv(nskb)->ifindex = dev->ifindex;
200 can_skb_prv(nskb)->skbcnt = 0;
203 can_skb_set_owner(nskb, sk);
204 ncf = (struct canfd_frame *)nskb->data;
205 skb_put_zero(nskb, so->ll.mtu);
207 /* create & send flow control reply */
208 ncf->can_id = so->txid;
210 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
211 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
212 ncf->len = CAN_MAX_DLEN;
214 ncf->len = ae + FC_CONTENT_SZ;
217 ncf->data[ae] = N_PCI_FC | flowstatus;
218 ncf->data[ae + 1] = so->rxfc.bs;
219 ncf->data[ae + 2] = so->rxfc.stmin;
222 ncf->data[0] = so->opt.ext_address;
224 ncf->flags = so->ll.tx_flags;
226 can_send_ret = can_send(nskb, 1);
228 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
229 __func__, ERR_PTR(can_send_ret));
233 /* reset blocksize counter */
236 /* reset last CF frame rx timestamp for rx stmin enforcement */
237 so->lastrxcf_tstamp = ktime_set(0, 0);
239 /* start rx timeout watchdog */
240 hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
244 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
246 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
248 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
250 memset(addr, 0, sizeof(*addr));
251 addr->can_family = AF_CAN;
252 addr->can_ifindex = skb->dev->ifindex;
254 if (sock_queue_rcv_skb(sk, skb) < 0)
258 static u8 padlen(u8 datalen)
260 static const u8 plen[] = {
261 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */
262 12, 12, 12, 12, /* 9 - 12 */
263 16, 16, 16, 16, /* 13 - 16 */
264 20, 20, 20, 20, /* 17 - 20 */
265 24, 24, 24, 24, /* 21 - 24 */
266 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */
267 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */
268 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */
274 return plen[datalen];
277 /* check for length optimization and return 1/true when the check fails */
278 static int check_optimized(struct canfd_frame *cf, int start_index)
280 /* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
281 * padding would start at this point. E.g. if the padding would
282 * start at cf.data[7] cf->len has to be 7 to be optimal.
283 * Note: The data[] index starts with zero.
285 if (cf->len <= CAN_MAX_DLEN)
286 return (cf->len != start_index);
288 /* This relation is also valid in the non-linear DLC range, where
289 * we need to take care of the minimal next possible CAN_DL.
290 * The correct check would be (padlen(cf->len) != padlen(start_index)).
291 * But as cf->len can only take discrete values from 12, .., 64 at this
292 * point the padlen(cf->len) is always equal to cf->len.
294 return (cf->len != padlen(start_index));
297 /* check padding and return 1/true when the check fails */
298 static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
299 int start_index, u8 content)
303 /* no RX_PADDING value => check length of optimized frame length */
304 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
305 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
306 return check_optimized(cf, start_index);
308 /* no valid test against empty value => ignore frame */
312 /* check datalength of correctly padded CAN frame */
313 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
314 cf->len != padlen(cf->len))
317 /* check padding content */
318 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
319 for (i = start_index; i < cf->len; i++)
320 if (cf->data[i] != content)
326 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
328 struct sock *sk = &so->sk;
330 if (so->tx.state != ISOTP_WAIT_FC &&
331 so->tx.state != ISOTP_WAIT_FIRST_FC)
334 hrtimer_cancel(&so->txtimer);
336 if ((cf->len < ae + FC_CONTENT_SZ) ||
337 ((so->opt.flags & ISOTP_CHECK_PADDING) &&
338 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
339 /* malformed PDU - report 'not a data message' */
340 sk->sk_err = EBADMSG;
341 if (!sock_flag(sk, SOCK_DEAD))
344 so->tx.state = ISOTP_IDLE;
345 wake_up_interruptible(&so->wait);
349 /* get communication parameters only from the first FC frame */
350 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
351 so->txfc.bs = cf->data[ae + 1];
352 so->txfc.stmin = cf->data[ae + 2];
354 /* fix wrong STmin values according spec */
355 if (so->txfc.stmin > 0x7F &&
356 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
357 so->txfc.stmin = 0x7F;
359 so->tx_gap = ktime_set(0, 0);
360 /* add transmission time for CAN frame N_As */
361 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
362 /* add waiting time for consecutive frames N_Cs */
363 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
364 so->tx_gap = ktime_add_ns(so->tx_gap,
366 else if (so->txfc.stmin < 0x80)
367 so->tx_gap = ktime_add_ns(so->tx_gap,
368 so->txfc.stmin * 1000000);
370 so->tx_gap = ktime_add_ns(so->tx_gap,
371 (so->txfc.stmin - 0xF0)
373 so->tx.state = ISOTP_WAIT_FC;
376 switch (cf->data[ae] & 0x0F) {
379 so->tx.state = ISOTP_SENDING;
380 /* start cyclic timer for sending CF frame */
381 hrtimer_start(&so->txtimer, so->tx_gap,
382 HRTIMER_MODE_REL_SOFT);
386 /* start timer to wait for next FC frame */
387 hrtimer_start(&so->txtimer, ktime_set(1, 0),
388 HRTIMER_MODE_REL_SOFT);
392 /* overflow on receiver side - report 'message too long' */
393 sk->sk_err = EMSGSIZE;
394 if (!sock_flag(sk, SOCK_DEAD))
399 /* stop this tx job */
400 so->tx.state = ISOTP_IDLE;
401 wake_up_interruptible(&so->wait);
406 static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
407 struct sk_buff *skb, int len)
409 struct isotp_sock *so = isotp_sk(sk);
410 struct sk_buff *nskb;
412 hrtimer_cancel(&so->rxtimer);
413 so->rx.state = ISOTP_IDLE;
415 if (!len || len > cf->len - pcilen)
418 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
419 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
420 /* malformed PDU - report 'not a data message' */
421 sk->sk_err = EBADMSG;
422 if (!sock_flag(sk, SOCK_DEAD))
427 nskb = alloc_skb(len, gfp_any());
431 memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
433 nskb->tstamp = skb->tstamp;
434 nskb->dev = skb->dev;
435 isotp_rcv_skb(nskb, sk);
439 static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
441 struct isotp_sock *so = isotp_sk(sk);
446 hrtimer_cancel(&so->rxtimer);
447 so->rx.state = ISOTP_IDLE;
449 /* get the used sender LL_DL from the (first) CAN frame data length */
450 so->rx.ll_dl = padlen(cf->len);
452 /* the first frame has to use the entire frame up to LL_DL length */
453 if (cf->len != so->rx.ll_dl)
457 so->rx.len = (cf->data[ae] & 0x0F) << 8;
458 so->rx.len += cf->data[ae + 1];
460 /* Check for FF_DL escape sequence supporting 32 bit PDU length */
462 ff_pci_sz = FF_PCI_SZ12;
464 /* FF_DL = 0 => get real length from next 4 bytes */
465 so->rx.len = cf->data[ae + 2] << 24;
466 so->rx.len += cf->data[ae + 3] << 16;
467 so->rx.len += cf->data[ae + 4] << 8;
468 so->rx.len += cf->data[ae + 5];
469 ff_pci_sz = FF_PCI_SZ32;
472 /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
473 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
475 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
478 if (so->rx.len > MAX_MSG_LENGTH) {
479 /* send FC frame with overflow status */
480 isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
484 /* copy the first received data bytes */
486 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
487 so->rx.buf[so->rx.idx++] = cf->data[i];
489 /* initial setup for this pdu reception */
491 so->rx.state = ISOTP_WAIT_DATA;
493 /* no creation of flow control frames */
494 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
497 /* send our first FC frame */
498 isotp_send_fc(sk, ae, ISOTP_FC_CTS);
502 static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
505 struct isotp_sock *so = isotp_sk(sk);
506 struct sk_buff *nskb;
509 if (so->rx.state != ISOTP_WAIT_DATA)
512 /* drop if timestamp gap is less than force_rx_stmin nano secs */
513 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
514 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
518 so->lastrxcf_tstamp = skb->tstamp;
521 hrtimer_cancel(&so->rxtimer);
523 /* CFs are never longer than the FF */
524 if (cf->len > so->rx.ll_dl)
527 /* CFs have usually the LL_DL length */
528 if (cf->len < so->rx.ll_dl) {
529 /* this is only allowed for the last CF */
530 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
534 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
535 /* wrong sn detected - report 'illegal byte sequence' */
537 if (!sock_flag(sk, SOCK_DEAD))
541 so->rx.state = ISOTP_IDLE;
547 for (i = ae + N_PCI_SZ; i < cf->len; i++) {
548 so->rx.buf[so->rx.idx++] = cf->data[i];
549 if (so->rx.idx >= so->rx.len)
553 if (so->rx.idx >= so->rx.len) {
555 so->rx.state = ISOTP_IDLE;
557 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
558 check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
559 /* malformed PDU - report 'not a data message' */
560 sk->sk_err = EBADMSG;
561 if (!sock_flag(sk, SOCK_DEAD))
566 nskb = alloc_skb(so->rx.len, gfp_any());
570 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
573 nskb->tstamp = skb->tstamp;
574 nskb->dev = skb->dev;
575 isotp_rcv_skb(nskb, sk);
579 /* perform blocksize handling, if enabled */
580 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
581 /* start rx timeout watchdog */
582 hrtimer_start(&so->rxtimer, ktime_set(1, 0),
583 HRTIMER_MODE_REL_SOFT);
587 /* no creation of flow control frames */
588 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
591 /* we reached the specified blocksize so->rxfc.bs */
592 isotp_send_fc(sk, ae, ISOTP_FC_CTS);
596 static void isotp_rcv(struct sk_buff *skb, void *data)
598 struct sock *sk = (struct sock *)data;
599 struct isotp_sock *so = isotp_sk(sk);
600 struct canfd_frame *cf;
601 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
602 u8 n_pci_type, sf_dl;
604 /* Strictly receive only frames with the configured MTU size
605 * => clear separation of CAN2.0 / CAN FD transport channels
607 if (skb->len != so->ll.mtu)
610 cf = (struct canfd_frame *)skb->data;
612 /* if enabled: check reception of my configured extended address */
613 if (ae && cf->data[0] != so->opt.rx_ext_address)
616 n_pci_type = cf->data[ae] & 0xF0;
618 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
619 /* check rx/tx path half duplex expectations */
620 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
621 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
625 switch (n_pci_type) {
627 /* tx path: flow control frame containing the FC parameters */
628 isotp_rcv_fc(so, cf, ae);
632 /* rx path: single frame
634 * As we do not have a rx.ll_dl configuration, we can only test
635 * if the CAN frames payload length matches the LL_DL == 8
636 * requirements - no matter if it's CAN 2.0 or CAN FD
639 /* get the SF_DL from the N_PCI byte */
640 sf_dl = cf->data[ae] & 0x0F;
642 if (cf->len <= CAN_MAX_DLEN) {
643 isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl);
645 if (skb->len == CANFD_MTU) {
646 /* We have a CAN FD frame and CAN_DL is greater than 8:
647 * Only frames with the SF_DL == 0 ESC value are valid.
649 * If so take care of the increased SF PCI size
650 * (SF_PCI_SZ8) to point to the message content behind
651 * the extended SF PCI info and get the real SF_DL
652 * length value from the formerly first data byte.
655 isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb,
656 cf->data[SF_PCI_SZ4 + ae]);
662 /* rx path: first frame */
663 isotp_rcv_ff(sk, cf, ae);
667 /* rx path: consecutive frame */
668 isotp_rcv_cf(sk, cf, ae, skb);
673 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
676 int pcilen = N_PCI_SZ + ae + off;
677 int space = so->tx.ll_dl - pcilen;
678 int num = min_t(int, so->tx.len - so->tx.idx, space);
681 cf->can_id = so->txid;
682 cf->len = num + pcilen;
685 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
686 /* user requested padding */
687 cf->len = padlen(cf->len);
688 memset(cf->data, so->opt.txpad_content, cf->len);
689 } else if (cf->len > CAN_MAX_DLEN) {
690 /* mandatory padding for CAN FD frames */
691 cf->len = padlen(cf->len);
692 memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
697 for (i = 0; i < num; i++)
698 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
701 cf->data[0] = so->opt.ext_address;
704 static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
710 cf->can_id = so->txid;
711 cf->len = so->tx.ll_dl;
713 cf->data[0] = so->opt.ext_address;
715 /* create N_PCI bytes with 12/32 bit FF_DL data length */
716 if (so->tx.len > 4095) {
717 /* use 32 bit FF_DL notation */
718 cf->data[ae] = N_PCI_FF;
719 cf->data[ae + 1] = 0;
720 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
721 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
722 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
723 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
724 ff_pci_sz = FF_PCI_SZ32;
726 /* use 12 bit FF_DL notation */
727 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
728 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
729 ff_pci_sz = FF_PCI_SZ12;
732 /* add first data bytes depending on ae */
733 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
734 cf->data[i] = so->tx.buf[so->tx.idx++];
737 so->tx.state = ISOTP_WAIT_FIRST_FC;
740 static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)
742 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
744 struct sock *sk = &so->sk;
746 struct net_device *dev;
747 struct canfd_frame *cf;
748 enum hrtimer_restart restart = HRTIMER_NORESTART;
750 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
752 switch (so->tx.state) {
754 case ISOTP_WAIT_FIRST_FC:
756 /* we did not get any flow control frame in time */
758 /* report 'communication error on send' */
760 if (!sock_flag(sk, SOCK_DEAD))
764 so->tx.state = ISOTP_IDLE;
765 wake_up_interruptible(&so->wait);
770 /* push out the next segmented pdu */
771 dev = dev_get_by_index(sock_net(sk), so->ifindex);
776 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv),
783 can_skb_reserve(skb);
784 can_skb_prv(skb)->ifindex = dev->ifindex;
785 can_skb_prv(skb)->skbcnt = 0;
787 cf = (struct canfd_frame *)skb->data;
788 skb_put_zero(skb, so->ll.mtu);
790 /* create consecutive frame */
791 isotp_fill_dataframe(cf, so, ae, 0);
793 /* place consecutive frame N_PCI in appropriate index */
794 cf->data[ae] = N_PCI_CF | so->tx.sn++;
798 cf->flags = so->ll.tx_flags;
801 can_skb_set_owner(skb, sk);
803 can_send_ret = can_send(skb, 1);
805 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
806 __func__, ERR_PTR(can_send_ret));
807 if (can_send_ret == -ENOBUFS)
808 pr_notice_once("can-isotp: tx queue is full, increasing txqueuelen may prevent this error\n");
810 if (so->tx.idx >= so->tx.len) {
812 so->tx.state = ISOTP_IDLE;
814 wake_up_interruptible(&so->wait);
818 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
819 /* stop and wait for FC */
820 so->tx.state = ISOTP_WAIT_FC;
822 hrtimer_set_expires(&so->txtimer,
823 ktime_add(ktime_get(),
825 restart = HRTIMER_RESTART;
829 /* no gap between data frames needed => use burst mode */
833 /* start timer to send next data frame with correct delay */
835 hrtimer_set_expires(&so->txtimer,
836 ktime_add(ktime_get(), so->tx_gap));
837 restart = HRTIMER_RESTART;
847 static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
849 struct sock *sk = sock->sk;
850 struct isotp_sock *so = isotp_sk(sk);
851 u32 old_state = so->tx.state;
853 struct net_device *dev;
854 struct canfd_frame *cf;
855 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
856 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
861 return -EADDRNOTAVAIL;
863 /* we do not support multiple buffers - for now */
864 if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE ||
865 wq_has_sleeper(&so->wait)) {
866 if (msg->msg_flags & MSG_DONTWAIT) {
871 /* wait for complete transmission of current pdu */
872 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
877 if (!size || size > MAX_MSG_LENGTH) {
882 /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
883 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
885 /* does the given data fit into a single frame for SF_BROADCAST? */
886 if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) &&
887 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
892 err = memcpy_from_msg(so->tx.buf, msg, size);
896 dev = dev_get_by_index(sock_net(sk), so->ifindex);
902 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
903 msg->msg_flags & MSG_DONTWAIT, &err);
909 can_skb_reserve(skb);
910 can_skb_prv(skb)->ifindex = dev->ifindex;
911 can_skb_prv(skb)->skbcnt = 0;
916 cf = (struct canfd_frame *)skb->data;
917 skb_put_zero(skb, so->ll.mtu);
919 /* check for single frame transmission depending on TX_DL */
920 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
921 /* The message size generally fits into a SingleFrame - good.
923 * SF_DL ESC offset optimization:
925 * When TX_DL is greater 8 but the message would still fit
926 * into a 8 byte CAN frame, we can omit the offset.
927 * This prevents a protocol caused length extension from
928 * CAN_DL = 8 to CAN_DL = 12 due to the SF_SL ESC handling.
930 if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
933 isotp_fill_dataframe(cf, so, ae, off);
935 /* place single frame N_PCI w/o length in appropriate index */
936 cf->data[ae] = N_PCI_SF;
938 /* place SF_DL size value depending on the SF_DL ESC offset */
940 cf->data[SF_PCI_SZ4 + ae] = size;
942 cf->data[ae] |= size;
944 so->tx.state = ISOTP_IDLE;
945 wake_up_interruptible(&so->wait);
947 /* don't enable wait queue for a single frame transmission */
950 /* send first frame and wait for FC */
952 isotp_create_fframe(cf, so, ae);
954 /* start timeout for FC */
955 hrtimer_start(&so->txtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
958 /* send the first or only CAN frame */
959 cf->flags = so->ll.tx_flags;
963 err = can_send(skb, 1);
966 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
967 __func__, ERR_PTR(err));
972 /* wait for complete transmission of current pdu */
973 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
982 so->tx.state = old_state;
983 if (so->tx.state == ISOTP_IDLE)
984 wake_up_interruptible(&so->wait);
989 static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
992 struct sock *sk = sock->sk;
997 noblock = flags & MSG_DONTWAIT;
998 flags &= ~MSG_DONTWAIT;
1000 skb = skb_recv_datagram(sk, flags, noblock, &err);
1004 if (size < skb->len)
1005 msg->msg_flags |= MSG_TRUNC;
1009 err = memcpy_to_msg(msg, skb->data, size);
1011 skb_free_datagram(sk, skb);
1015 sock_recv_timestamp(msg, sk, skb);
1017 if (msg->msg_name) {
1018 __sockaddr_check_size(ISOTP_MIN_NAMELEN);
1019 msg->msg_namelen = ISOTP_MIN_NAMELEN;
1020 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1023 skb_free_datagram(sk, skb);
1028 static int isotp_release(struct socket *sock)
1030 struct sock *sk = sock->sk;
1031 struct isotp_sock *so;
1040 /* wait for complete transmission of current pdu */
1041 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1043 spin_lock(&isotp_notifier_lock);
1044 while (isotp_busy_notifier == so) {
1045 spin_unlock(&isotp_notifier_lock);
1046 schedule_timeout_uninterruptible(1);
1047 spin_lock(&isotp_notifier_lock);
1049 list_del(&so->notifier);
1050 spin_unlock(&isotp_notifier_lock);
1054 /* remove current filters & unregister */
1055 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) {
1057 struct net_device *dev;
1059 dev = dev_get_by_index(net, so->ifindex);
1061 can_rx_unregister(net, dev, so->rxid,
1062 SINGLE_MASK(so->rxid),
1070 hrtimer_cancel(&so->txtimer);
1071 hrtimer_cancel(&so->rxtimer);
1085 static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
1087 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1088 struct sock *sk = sock->sk;
1089 struct isotp_sock *so = isotp_sk(sk);
1090 struct net *net = sock_net(sk);
1092 struct net_device *dev;
1094 int notify_enetdown = 0;
1097 if (len < ISOTP_MIN_NAMELEN)
1100 if (addr->can_addr.tp.tx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG))
1101 return -EADDRNOTAVAIL;
1103 if (!addr->can_ifindex)
1108 /* do not register frame reception for functional addressing */
1109 if (so->opt.flags & CAN_ISOTP_SF_BROADCAST)
1112 /* do not validate rx address for functional addressing */
1114 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) {
1115 err = -EADDRNOTAVAIL;
1119 if (addr->can_addr.tp.rx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) {
1120 err = -EADDRNOTAVAIL;
1125 if (so->bound && addr->can_ifindex == so->ifindex &&
1126 addr->can_addr.tp.rx_id == so->rxid &&
1127 addr->can_addr.tp.tx_id == so->txid)
1130 dev = dev_get_by_index(net, addr->can_ifindex);
1135 if (dev->type != ARPHRD_CAN) {
1140 if (dev->mtu < so->ll.mtu) {
1145 if (!(dev->flags & IFF_UP))
1146 notify_enetdown = 1;
1148 ifindex = dev->ifindex;
1151 can_rx_register(net, dev, addr->can_addr.tp.rx_id,
1152 SINGLE_MASK(addr->can_addr.tp.rx_id),
1153 isotp_rcv, sk, "isotp", sk);
1157 if (so->bound && do_rx_reg) {
1158 /* unregister old filter */
1160 dev = dev_get_by_index(net, so->ifindex);
1162 can_rx_unregister(net, dev, so->rxid,
1163 SINGLE_MASK(so->rxid),
1170 /* switch to new settings */
1171 so->ifindex = ifindex;
1172 so->rxid = addr->can_addr.tp.rx_id;
1173 so->txid = addr->can_addr.tp.tx_id;
1179 if (notify_enetdown) {
1180 sk->sk_err = ENETDOWN;
1181 if (!sock_flag(sk, SOCK_DEAD))
1182 sk_error_report(sk);
1188 static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
1190 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1191 struct sock *sk = sock->sk;
1192 struct isotp_sock *so = isotp_sk(sk);
1197 memset(addr, 0, ISOTP_MIN_NAMELEN);
1198 addr->can_family = AF_CAN;
1199 addr->can_ifindex = so->ifindex;
1200 addr->can_addr.tp.rx_id = so->rxid;
1201 addr->can_addr.tp.tx_id = so->txid;
1203 return ISOTP_MIN_NAMELEN;
1206 static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,
1207 sockptr_t optval, unsigned int optlen)
1209 struct sock *sk = sock->sk;
1210 struct isotp_sock *so = isotp_sk(sk);
1217 case CAN_ISOTP_OPTS:
1218 if (optlen != sizeof(struct can_isotp_options))
1221 if (copy_from_sockptr(&so->opt, optval, optlen))
1224 /* no separate rx_ext_address is given => use ext_address */
1225 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1226 so->opt.rx_ext_address = so->opt.ext_address;
1229 case CAN_ISOTP_RECV_FC:
1230 if (optlen != sizeof(struct can_isotp_fc_options))
1233 if (copy_from_sockptr(&so->rxfc, optval, optlen))
1237 case CAN_ISOTP_TX_STMIN:
1238 if (optlen != sizeof(u32))
1241 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1245 case CAN_ISOTP_RX_STMIN:
1246 if (optlen != sizeof(u32))
1249 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1253 case CAN_ISOTP_LL_OPTS:
1254 if (optlen == sizeof(struct can_isotp_ll_options)) {
1255 struct can_isotp_ll_options ll;
1257 if (copy_from_sockptr(&ll, optval, optlen))
1260 /* check for correct ISO 11898-1 DLC data length */
1261 if (ll.tx_dl != padlen(ll.tx_dl))
1264 if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU)
1267 if (ll.mtu == CAN_MTU &&
1268 (ll.tx_dl > CAN_MAX_DLEN || ll.tx_flags != 0))
1271 memcpy(&so->ll, &ll, sizeof(ll));
1273 /* set ll_dl for tx path to similar place as for rx */
1274 so->tx.ll_dl = ll.tx_dl;
1287 static int isotp_setsockopt(struct socket *sock, int level, int optname,
1288 sockptr_t optval, unsigned int optlen)
1291 struct sock *sk = sock->sk;
1294 if (level != SOL_CAN_ISOTP)
1298 ret = isotp_setsockopt_locked(sock, level, optname, optval, optlen);
1303 static int isotp_getsockopt(struct socket *sock, int level, int optname,
1304 char __user *optval, int __user *optlen)
1306 struct sock *sk = sock->sk;
1307 struct isotp_sock *so = isotp_sk(sk);
1311 if (level != SOL_CAN_ISOTP)
1313 if (get_user(len, optlen))
1319 case CAN_ISOTP_OPTS:
1320 len = min_t(int, len, sizeof(struct can_isotp_options));
1324 case CAN_ISOTP_RECV_FC:
1325 len = min_t(int, len, sizeof(struct can_isotp_fc_options));
1329 case CAN_ISOTP_TX_STMIN:
1330 len = min_t(int, len, sizeof(u32));
1331 val = &so->force_tx_stmin;
1334 case CAN_ISOTP_RX_STMIN:
1335 len = min_t(int, len, sizeof(u32));
1336 val = &so->force_rx_stmin;
1339 case CAN_ISOTP_LL_OPTS:
1340 len = min_t(int, len, sizeof(struct can_isotp_ll_options));
1345 return -ENOPROTOOPT;
1348 if (put_user(len, optlen))
1350 if (copy_to_user(optval, val, len))
1355 static void isotp_notify(struct isotp_sock *so, unsigned long msg,
1356 struct net_device *dev)
1358 struct sock *sk = &so->sk;
1360 if (!net_eq(dev_net(dev), sock_net(sk)))
1363 if (so->ifindex != dev->ifindex)
1367 case NETDEV_UNREGISTER:
1369 /* remove current filters & unregister */
1370 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST)))
1371 can_rx_unregister(dev_net(dev), dev, so->rxid,
1372 SINGLE_MASK(so->rxid),
1379 sk->sk_err = ENODEV;
1380 if (!sock_flag(sk, SOCK_DEAD))
1381 sk_error_report(sk);
1385 sk->sk_err = ENETDOWN;
1386 if (!sock_flag(sk, SOCK_DEAD))
1387 sk_error_report(sk);
1392 static int isotp_notifier(struct notifier_block *nb, unsigned long msg,
1395 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1397 if (dev->type != ARPHRD_CAN)
1399 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
1401 if (unlikely(isotp_busy_notifier)) /* Check for reentrant bug. */
1404 spin_lock(&isotp_notifier_lock);
1405 list_for_each_entry(isotp_busy_notifier, &isotp_notifier_list, notifier) {
1406 spin_unlock(&isotp_notifier_lock);
1407 isotp_notify(isotp_busy_notifier, msg, dev);
1408 spin_lock(&isotp_notifier_lock);
1410 isotp_busy_notifier = NULL;
1411 spin_unlock(&isotp_notifier_lock);
1415 static int isotp_init(struct sock *sk)
1417 struct isotp_sock *so = isotp_sk(sk);
1422 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1423 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1424 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1425 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1426 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1427 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1428 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1429 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1430 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1431 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1432 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1433 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1435 /* set ll_dl for tx path to similar place as for rx */
1436 so->tx.ll_dl = so->ll.tx_dl;
1438 so->rx.state = ISOTP_IDLE;
1439 so->tx.state = ISOTP_IDLE;
1441 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1442 so->rxtimer.function = isotp_rx_timer_handler;
1443 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1444 so->txtimer.function = isotp_tx_timer_handler;
1446 init_waitqueue_head(&so->wait);
1448 spin_lock(&isotp_notifier_lock);
1449 list_add_tail(&so->notifier, &isotp_notifier_list);
1450 spin_unlock(&isotp_notifier_lock);
1455 static int isotp_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1458 /* no ioctls for socket layer -> hand it down to NIC layer */
1459 return -ENOIOCTLCMD;
1462 static const struct proto_ops isotp_ops = {
1464 .release = isotp_release,
1466 .connect = sock_no_connect,
1467 .socketpair = sock_no_socketpair,
1468 .accept = sock_no_accept,
1469 .getname = isotp_getname,
1470 .poll = datagram_poll,
1471 .ioctl = isotp_sock_no_ioctlcmd,
1472 .gettstamp = sock_gettstamp,
1473 .listen = sock_no_listen,
1474 .shutdown = sock_no_shutdown,
1475 .setsockopt = isotp_setsockopt,
1476 .getsockopt = isotp_getsockopt,
1477 .sendmsg = isotp_sendmsg,
1478 .recvmsg = isotp_recvmsg,
1479 .mmap = sock_no_mmap,
1480 .sendpage = sock_no_sendpage,
1483 static struct proto isotp_proto __read_mostly = {
1484 .name = "CAN_ISOTP",
1485 .owner = THIS_MODULE,
1486 .obj_size = sizeof(struct isotp_sock),
1490 static const struct can_proto isotp_can_proto = {
1492 .protocol = CAN_ISOTP,
1494 .prot = &isotp_proto,
1497 static struct notifier_block canisotp_notifier = {
1498 .notifier_call = isotp_notifier
1501 static __init int isotp_module_init(void)
1505 pr_info("can: isotp protocol\n");
1507 err = can_proto_register(&isotp_can_proto);
1509 pr_err("can: registration of isotp protocol failed %pe\n", ERR_PTR(err));
1511 register_netdevice_notifier(&canisotp_notifier);
1516 static __exit void isotp_module_exit(void)
1518 can_proto_unregister(&isotp_can_proto);
1519 unregister_netdevice_notifier(&canisotp_notifier);
1522 module_init(isotp_module_init);
1523 module_exit(isotp_module_exit);