/* Since ISO 15765-2:2016 the CAN isotp protocol supports more than 4095 *byteperISOPDUastheFF_DLcantakefull32bitvalues(4Gbyte). *Wewouldneedsomegoodconcepttohandlethisbetweenuserspaceand *kernelspace.Fornowsetthestaticbuffertosomethingabout8kbyte *tobeabletotestthisnewfunctionality.
*/ #define DEFAULT_MAX_PDU_SIZE 8300
/* maximum PDU size before ISO 15765-2:2016 extension was 4095 */ #define MAX_12BIT_PDU_SIZE 4095
/* limit the isotp pdu size from the optional module parameter to 1MByte */ #define MAX_PDU_SIZE (1025 * 1024U)
/* N_PCI type values in bits 7-4 of N_PCI bytes */ #define N_PCI_SF 0x00 /* single frame */ #define N_PCI_FF 0x10 /* first frame */ #define N_PCI_CF 0x20 /* consecutive frame */ #define N_PCI_FC 0x30 /* flow control */
#define N_PCI_SZ 1/* size of the PCI byte #1 */ #define SF_PCI_SZ4 1/* size of SingleFrame PCI including 4 bit SF_DL */ #define SF_PCI_SZ8 2/* size of SingleFrame PCI including 8 bit SF_DL */ #define FF_PCI_SZ12 2/* size of FirstFrame PCI including 12 bit FF_DL */ #define FF_PCI_SZ32 6/* size of FirstFrame PCI including 32 bit FF_DL */ #define FC_CONTENT_SZ 3/* flow control content size in byte (FS/BS/STmin) */
/* check for length optimization and return 1/true when the check fails */ staticint check_optimized(struct canfd_frame *cf, int start_index)
{ /* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the *paddingwouldstartatthispoint.E.g.ifthepaddingwould *startatcf.data[7]cf->lenhastobe7tobeoptimal. *Note:Thedata[]indexstartswithzero.
*/ if (cf->len <= CAN_MAX_DLEN) return (cf->len != start_index);
/* This relation is also valid in the non-linear DLC range, where *weneedtotakecareoftheminimalnextpossibleCAN_DL. *Thecorrectcheckwouldbe(padlen(cf->len)!=padlen(start_index)). *Butascf->lencanonlytakediscretevaluesfrom12,..,64atthis *pointthepadlen(cf->len)isalwaysequaltocf->len.
*/ return (cf->len != padlen(start_index));
}
/* check padding and return 1/true when the check fails */ staticint check_pad(struct isotp_sock *so, struct canfd_frame *cf, int start_index, u8 content)
{ int i;
/* no RX_PADDING value => check length of optimized frame length */ if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) { if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) return check_optimized(cf, start_index);
/* no valid test against empty value => ignore frame */ return1;
}
/* check datalength of correctly padded CAN frame */ if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
cf->len != padlen(cf->len)) return1;
/* check padding content */ if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) { for (i = start_index; i < cf->len; i++) if (cf->data[i] != content) return1;
} return0;
}
case ISOTP_FC_WT: /* start timer to wait for next FC frame */
hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
HRTIMER_MODE_REL_SOFT); break;
case ISOTP_FC_OVFLW: /* overflow on receiver side - report 'message too long' */
sk->sk_err = EMSGSIZE; if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
fallthrough;
/* drop if timestamp gap is less than force_rx_stmin nano secs */ if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) { if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
so->force_rx_stmin) return0;
so->lastrxcf_tstamp = skb->tstamp;
}
hrtimer_cancel(&so->rxtimer);
/* CFs are never longer than the FF */ if (cf->len > so->rx.ll_dl) return1;
/* CFs have usually the LL_DL length */ if (cf->len < so->rx.ll_dl) { /* this is only allowed for the last CF */ if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ) return1;
}
if ((cf->data[ae] & 0x0F) != so->rx.sn) { /* wrong sn detected - report 'illegal byte sequence' */
sk->sk_err = EILSEQ; if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
/* Strictly receive only frames with the configured MTU size *=>clearseparationofCAN2.0/CANFDtransportchannels
*/ if (skb->len != so->ll.mtu) return;
cf = (struct canfd_frame *)skb->data;
/* if enabled: check reception of my configured extended address */ if (ae && cf->data[0] != so->opt.rx_ext_address) return;
n_pci_type = cf->data[ae] & 0xF0;
/* Make sure the state changes and data structures stay consistent at *CANframereceptiontime.Thislockingisnotneededinrealworld *usecasesbuttheinconsistencycanbetriggeredwithsyzkaller.
*/
spin_lock(&so->rx_lock);
staticvoid isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so, int ae, int off)
{ int pcilen = N_PCI_SZ + ae + off; int space = so->tx.ll_dl - pcilen; int num = min_t(int, so->tx.len - so->tx.idx, space); int i;
cf->can_id = so->txid;
cf->len = num + pcilen;
if (num < space) { if (so->opt.flags & CAN_ISOTP_TX_PADDING) { /* user requested padding */
cf->len = padlen(cf->len);
memset(cf->data, so->opt.txpad_content, cf->len);
} elseif (cf->len > CAN_MAX_DLEN) { /* mandatory padding for CAN FD frames */
cf->len = padlen(cf->len);
memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
cf->len);
}
}
for (i = 0; i < num; i++)
cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
/* only handle my own local echo CF/SF skb's (no FF!) */ if (skb->sk != sk || so->cfecho != *(u32 *)cf->data) return;
/* cancel local echo timeout */
hrtimer_cancel(&so->txtimer);
/* local echo skb with consecutive frame has been consumed */
so->cfecho = 0;
if (so->tx.idx >= so->tx.len) { /* we are done */
so->tx.state = ISOTP_IDLE;
wake_up_interruptible(&so->wait); return;
}
if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { /* stop and wait for FC with timeout */
so->tx.state = ISOTP_WAIT_FC;
hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
HRTIMER_MODE_REL_SOFT); return;
}
/* no gap between data frames needed => use burst mode */ if (!so->tx_gap) { /* enable echo timeout handling */
hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
HRTIMER_MODE_REL_SOFT);
isotp_send_cframe(so); return;
}
/* start timer to send next consecutive frame with correct delay */
hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
}
if (!so->bound || so->tx.state == ISOTP_SHUTDOWN) return -EADDRNOTAVAIL;
while (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE) { /* we do not support multiple buffers - for now */ if (msg->msg_flags & MSG_DONTWAIT) return -EAGAIN;
if (so->tx.state == ISOTP_SHUTDOWN) return -EADDRNOTAVAIL;
/* wait for complete transmission of current pdu */
err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); if (err) goto err_event_drop;
}
/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
/* does the given data fit into a single frame for SF_BROADCAST? */ if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
(size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
err = -EINVAL; goto err_out_drop;
}
/* cfecho should have been zero'ed by init / former isotp_rcv_echo() */ if (so->cfecho)
pr_notice_once("can-isotp: uninit cfecho %08X\n", so->cfecho);
/* check for single frame transmission depending on TX_DL */ if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) { /* The message size generally fits into a SingleFrame - good. * *SF_DLESCoffsetoptimization: * *WhenTX_DLisgreater8butthemessagewouldstillfit *intoa8byteCANframe,wecanomittheoffset. *Thispreventsaprotocolcausedlengthextensionfrom *CAN_DL=8toCAN_DL=12duetotheSF_SLESChandling.
*/ if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
off = 0;
isotp_fill_dataframe(cf, so, ae, off);
/* place single frame N_PCI w/o length in appropriate index */
cf->data[ae] = N_PCI_SF;
/* place SF_DL size value depending on the SF_DL ESC offset */ if (off)
cf->data[SF_PCI_SZ4 + ae] = size; else
cf->data[ae] |= size;
/* set CF echo tag for isotp_rcv_echo() (SF-mode) */
so->cfecho = *(u32 *)cf->data;
} else { /* send first frame */
isotp_create_fframe(cf, so, ae);
if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) { /* set timer for FC-less operation (STmin = 0) */ if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
so->tx_gap = ktime_set(0, so->force_tx_stmin); else
so->tx_gap = ktime_set(0, so->frame_txtime);
/* disable wait for FCs due to activated block size */
so->txfc.bs = 0;
/* set CF echo tag for isotp_rcv_echo() (CF-mode) */
so->cfecho = *(u32 *)cf->data;
} else { /* standard flow control check */
so->tx.state = ISOTP_WAIT_FIRST_FC;
/* start timeout for FC */
hrtimer_sec = ISOTP_FC_TIMEOUT;
/* no CF echo tag for isotp_rcv_echo() (FF-mode) */
so->cfecho = 0;
}
}
/* no transmission -> no timeout monitoring */
hrtimer_cancel(&so->txtimer);
/* reset consecutive frame echo tag */
so->cfecho = 0;
goto err_out_drop;
}
if (wait_tx_done) { /* wait for complete transmission of current pdu */
err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); if (err) goto err_event_drop;
err = sock_error(sk); if (err) return err;
}
return size;
err_event_drop: /* got signal: force tx state machine to be idle */
so->tx.state = ISOTP_IDLE;
hrtimer_cancel(&so->txfrtimer);
hrtimer_cancel(&so->txtimer);
err_out_drop: /* drop this PDU and unlock a potential wait queue */
so->tx.state = ISOTP_IDLE;
wake_up_interruptible(&so->wait);
return err;
}
staticint isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, int flags)
{ struct sock *sk = sock->sk; struct sk_buff *skb; struct isotp_sock *so = isotp_sk(sk); int ret = 0;
/* remove current filters & unregister */ if (so->bound) { if (so->ifindex) { struct net_device *dev;
dev = dev_get_by_index(net, so->ifindex); if (dev) { if (isotp_register_rxid(so))
can_rx_unregister(net, dev, so->rxid,
SINGLE_MASK(so->rxid),
isotp_rcv, sk);
staticint isotp_setsockopt_locked(struct socket *sock, int level, int optname,
sockptr_t optval, unsignedint optlen)
{ struct sock *sk = sock->sk; struct isotp_sock *so = isotp_sk(sk); int ret = 0;
if (so->bound) return -EISCONN;
switch (optname) { case CAN_ISOTP_OPTS: if (optlen != sizeof(struct can_isotp_options)) return -EINVAL;
if (copy_from_sockptr(&so->opt, optval, optlen)) return -EFAULT;
/* no separate rx_ext_address is given => use ext_address */ if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
so->opt.rx_ext_address = so->opt.ext_address;
/* these broadcast flags are not allowed together */ if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) { /* CAN_ISOTP_SF_BROADCAST is prioritized */
so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
/* give user feedback on wrong config attempt */
ret = -EINVAL;
}
/* check for frame_txtime changes (0 => no changes) */ if (so->opt.frame_txtime) { if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
so->frame_txtime = 0; else
so->frame_txtime = so->opt.frame_txtime;
} break;
case CAN_ISOTP_RECV_FC: if (optlen != sizeof(struct can_isotp_fc_options)) return -EINVAL;
if (copy_from_sockptr(&so->rxfc, optval, optlen)) return -EFAULT; break;
case CAN_ISOTP_TX_STMIN: if (optlen != sizeof(u32)) return -EINVAL;
if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen)) return -EFAULT; break;
case CAN_ISOTP_RX_STMIN: if (optlen != sizeof(u32)) return -EINVAL;
if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen)) return -EFAULT; break;
case CAN_ISOTP_LL_OPTS: if (optlen == sizeof(struct can_isotp_ll_options)) { struct can_isotp_ll_options ll;
if (copy_from_sockptr(&ll, optval, optlen)) return -EFAULT;
/* check for correct ISO 11898-1 DLC data length */ if (ll.tx_dl != padlen(ll.tx_dl)) return -EINVAL;
if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU) return -EINVAL;
/* Check for false positives due to TX state */ if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE))
mask &= ~(EPOLLOUT | EPOLLWRNORM);
return mask;
}
staticint isotp_sock_no_ioctlcmd(struct socket *sock, unsignedint cmd, unsignedlong arg)
{ /* no ioctls for socket layer -> hand it down to NIC layer */ return -ENOIOCTLCMD;
}
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.28Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.