Skip to contentSkip to conversation

can: skb: enforce CANFD_FDF check in can_is_canfd_skb()

RFC PATCH Superseded Oliver Hartkopp 2 participants 6 messages Sep 8, 2025 – Sep 9, 2025

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

RFC PATCH v3 1/2 can: skb: enforce CANFD_FDF check in can_is_canfd_skb()

The check in can_is_canfd_skb() is about a length check of skb->len and the CAN FD data length. As a skb length of CANFD_MTU can potentially be created with a CAN XL frame with a data length of 60, the length check of the CAN FD data length is used to detect CAN XL frames via its CANXL_XLF flag which exceeds valid CAN FD data length values.

To make sure the CANFD_FDF flag can be safely used as a marker for CAN FD frame skbs the bit was set in can_send() and is now also set in raw_check_txframe() to re-use the indroduced can_is_canfd_skb_set_fdf() function. In the RX path alloc_canfd_skb() sets the CANFD_FDF flag.

The enforced CANFD_FDF check in can_is_canfd_skb() clears up the potential uncertainty when using the skb->len check with the CANFD_MTU.

---
 include/linux/can/skb.h | 25 +++++++++++++++++++++++--
 net/can/af_can.c        |  7 +------
 net/can/raw.c           |  2 +-
 3 files changed, 25 insertions(+), 9 deletions(-)
include/linux/can/skb.h +23 -2
index 1abc25a8d144..38d036b43280 100644
@@ -111,12 +111,33 @@ static inline bool can_is_can_skb(const struct sk_buff *skb)
 
 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
 {
 	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-	/* the CAN specific type of skb is identified by its data length */
-	return (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN);
+	if (skb->len != CANFD_MTU || cfd->len > CANFD_MAX_DLEN)
+		return false;
+
+	return cfd->flags & CANFD_FDF;
+}
+
+static inline bool can_is_canfd_skb_set_fdf(const struct sk_buff *skb)
+{
+	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+	/* The CAN specific type of skb is identified by its data length.
+	 * A CAN XL frame skb might have a skb->len of CANFD_MTU but the
+	 * skb would have the CANXL_XLF bit set (0x80 = 128) in the
+	 * cfd->len field position which would intentionally break the
+	 * CAN FD length check here. So we can surely tag it as CAN FD.
+	 */
+	if (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN) {
+		/* set CAN FD flag for CAN FD frames by default */
+		cfd->flags |= CANFD_FDF;
+		return true;
+	}
+
+	return false;
 }
 
 static inline bool can_is_canxl_skb(const struct sk_buff *skb)
 {
 	const struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
net/can/af_can.c +1 -6
index b2387a46794a..0caf75a9e27f 100644
@@ -207,17 +207,12 @@ int can_send(struct sk_buff *skb, int loop)
 
 	if (can_is_canxl_skb(skb)) {
 		skb->protocol = htons(ETH_P_CANXL);
 	} else if (can_is_can_skb(skb)) {
 		skb->protocol = htons(ETH_P_CAN);
-	} else if (can_is_canfd_skb(skb)) {
-		struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
-
+	} else if (can_is_canfd_skb_set_fdf(skb)) {
 		skb->protocol = htons(ETH_P_CANFD);
-
-		/* set CAN FD flag for CAN FD frames by default */
-		cfd->flags |= CANFD_FDF;
 	} else {
 		goto inval_skb;
 	}
 
 	/* Make sure the CAN frame can pass the selected CAN netdevice. */
net/can/raw.c +1 -1
index 76b867d21def..f48b1f3fd6e8 100644
@@ -886,11 +886,11 @@ static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,
 	/* Classical CAN -> no checks for flags and device capabilities */
 	if (can_is_can_skb(skb))
 		return CAN_MTU;
 
 	/* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-	if (ro->fd_frames && can_is_canfd_skb(skb) &&
+	if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
 	    (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
 		return CANFD_MTU;
 
 	/* CAN XL -> needs to be enabled and a CAN XL device */
 	if (ro->xl_frames && can_is_canxl_skb(skb) &&
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH v3 2/2 can: support XL only content on real CAN XL interfaces

The CAN XL devices can be configured as CAN XL only with 'xl on fd off' which is currently not supported as the CAN XL sockopt on the CAN_RAW socket implicitly enables CAN FD support.

This patch removes this XL/FD connection for real CAN XL interfaces and rejects CAN FD content on CAN XL interfaces with 'fd off'.

---
 include/linux/can/dev.h | 12 ++++++++++++
 net/can/raw.c           | 32 ++++++++++++++------------------
 2 files changed, 26 insertions(+), 18 deletions(-)
include/linux/can/dev.h +12 -0
index 9a92cbe5b2cb..9fa139cc793e 100644
@@ -183,10 +183,22 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
 void free_candev(struct net_device *dev);
 
 /* a candev safe wrapper around netdev_priv */
 struct can_priv *safe_candev_priv(struct net_device *dev);
 
+static inline bool can_dev_ctrlmode_fd_on(struct net_device *dev)
+{
+	struct can_priv *priv = safe_candev_priv(dev);
+
+	/* check ctrlmode on real CAN interfaces */
+	if (priv)
+		return (priv->ctrlmode & CAN_CTRLMODE_FD);
+
+	/* virtual CAN FD/XL interfaces always support CAN FD */
+	return true;
+}
+
 int open_candev(struct net_device *dev);
 void close_candev(struct net_device *dev);
 int can_change_mtu(struct net_device *dev, int new_mtu);
 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
net/can/raw.c +14 -18
index f48b1f3fd6e8..6cd1f9cb050d 100644
@@ -558,11 +558,10 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 	struct raw_sock *ro = raw_sk(sk);
 	struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
 	struct can_filter sfilter;         /* single filter */
 	struct net_device *dev = NULL;
 	can_err_mask_t err_mask = 0;
-	int fd_frames;
 	int count = 0;
 	int err = 0;
 
 	if (level != SOL_CAN_RAW)
 		return -EINVAL;
@@ -698,33 +697,25 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 			return -EFAULT;
 
 		break;
 
 	case CAN_RAW_FD_FRAMES:
-		if (optlen != sizeof(fd_frames))
+		if (optlen != sizeof(ro->fd_frames))
 			return -EINVAL;
 
-		if (copy_from_sockptr(&fd_frames, optval, optlen))
+		if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
 			return -EFAULT;
 
-		/* Enabling CAN XL includes CAN FD */
-		if (ro->xl_frames && !fd_frames)
-			return -EINVAL;
-
-		ro->fd_frames = fd_frames;
 		break;
 
 	case CAN_RAW_XL_FRAMES:
 		if (optlen != sizeof(ro->xl_frames))
 			return -EINVAL;
 
 		if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
 			return -EFAULT;
 
-		/* Enabling CAN XL includes CAN FD */
-		if (ro->xl_frames)
-			ro->fd_frames = ro->xl_frames;
 		break;
 
 	case CAN_RAW_XL_VCID_OPTS:
 		if (optlen != sizeof(ro->raw_vcid_opts))
 			return -EINVAL;
@@ -879,24 +870,29 @@ static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
 		cxl->prio &= CANXL_PRIO_MASK;
 		cxl->prio |= ro->tx_vcid_shifted;
 	}
 }
 
-static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
+static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, struct net_device *dev)
 {
 	/* Classical CAN -> no checks for flags and device capabilities */
 	if (can_is_can_skb(skb))
 		return CAN_MTU;
 
-	/* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-	if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
-	    (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
-		return CANFD_MTU;
+	/* CAN FD -> needs to be enabled in a CAN FD or CAN XL device */
+	if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb)) {
+		/* real/virtual CAN FD interface */
+		if (dev->mtu == CANFD_MTU)
+			return CANFD_MTU;
+		if (can_is_canxl_dev_mtu(dev->mtu) &&
+		    can_dev_ctrlmode_fd_on(dev))
+			return CANFD_MTU;
+	}
 
 	/* CAN XL -> needs to be enabled and a CAN XL device */
 	if (ro->xl_frames && can_is_canxl_skb(skb) &&
-	    can_is_canxl_dev_mtu(mtu))
+	    can_is_canxl_dev_mtu(dev->mtu))
 		return CANXL_MTU;
 
 	return 0;
 }
 
@@ -948,11 +944,11 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		goto free_skb;
 
 	err = -EINVAL;
 
 	/* check for valid CAN (CC/FD/XL) frame content */
-	txmtu = raw_check_txframe(ro, skb, dev->mtu);
+	txmtu = raw_check_txframe(ro, skb, dev);
 	if (!txmtu)
 		goto free_skb;
 
 	/* only CANXL: clear/forward/set VCID value */
 	if (txmtu == CANXL_MTU)
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [RFC PATCH v3 2/2] can: support XL only content on real CAN XL interfaces

Quoted 78 lines · Oliver Hartkopp

The CAN XL devices can be configured as CAN XL only with 'xl on fd off' which is currently not supported as the CAN XL sockopt on the CAN_RAW socket implicitly enables CAN FD support.

This patch removes this XL/FD connection for real CAN XL interfaces and rejects CAN FD content on CAN XL interfaces with 'fd off'.

---
 include/linux/can/dev.h | 12 ++++++++++++
 net/can/raw.c           | 32 ++++++++++++++------------------
 2 files changed, 26 insertions(+), 18 deletions(-)
include/linux/can/dev.h +12 -0
index 9a92cbe5b2cb..9fa139cc793e 100644
@@ -183,10 +183,22 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
 void free_candev(struct net_device *dev);
 
 /* a candev safe wrapper around netdev_priv */
 struct can_priv *safe_candev_priv(struct net_device *dev);
 
+static inline bool can_dev_ctrlmode_fd_on(struct net_device *dev)
+{
+       struct can_priv *priv = safe_candev_priv(dev);
+
+       /* check ctrlmode on real CAN interfaces */
+       if (priv)
+               return (priv->ctrlmode & CAN_CTRLMODE_FD);
+
+       /* virtual CAN FD/XL interfaces always support CAN FD */
+       return true;
+}
+
 int open_candev(struct net_device *dev);
 void close_candev(struct net_device *dev);
 int can_change_mtu(struct net_device *dev, int new_mtu);
 int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
 int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
net/can/raw.c +2 -8
index f48b1f3fd6e8..6cd1f9cb050d 100644
@@ -558,11 +558,10 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
        struct raw_sock *ro = raw_sk(sk);
        struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
        struct can_filter sfilter;         /* single filter */
        struct net_device *dev = NULL;
        can_err_mask_t err_mask = 0;
-       int fd_frames;
        int count = 0;
        int err = 0;
 
        if (level != SOL_CAN_RAW)
                return -EINVAL;
@@ -698,33 +697,25 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
                        return -EFAULT;
 
                break;
 
        case CAN_RAW_FD_FRAMES:
-               if (optlen != sizeof(fd_frames))
+               if (optlen != sizeof(ro->fd_frames))
                        return -EINVAL;
 
-               if (copy_from_sockptr(&fd_frames, optval, optlen))
+               if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
                        return -EFAULT;
 
-               /* Enabling CAN XL includes CAN FD */
-               if (ro->xl_frames && !fd_frames)
-                       return -EINVAL;
-
-               ro->fd_frames = fd_frames;
                break;
 
        case CAN_RAW_XL_FRAMES:

net/can/raw.c static int raw_setsockopt(struct socket *sock, int level, int optname, · line 717

Quoted 10 lines
net/can/raw.c +0 -3
                if (optlen != sizeof(ro->xl_frames))
                        return -EINVAL;
 
                if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
                        return -EFAULT;
 
-               /* Enabling CAN XL includes CAN FD */
-               if (ro->xl_frames)
-                       ro->fd_frames = ro->xl_frames;
                break;

I think this is a UAPI breaking change. Any previous code which would have only set CAN_RAW_XL_FRAMES to implicitly also set CAN_RAW_FD_FRAMES would now break. I think it is an acceptable compromise, but, at least, we should make it crystal clear in the patch description that this is an UAPI breakage and clarify what the impacts are for the end user.

Quoted 52 lines
net/can/raw.c +12 -7
        case CAN_RAW_XL_VCID_OPTS:
                if (optlen != sizeof(ro->raw_vcid_opts))
                        return -EINVAL;
@@ -879,24 +870,29 @@ static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
                cxl->prio &= CANXL_PRIO_MASK;
                cxl->prio |= ro->tx_vcid_shifted;
        }
 }
 
-static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
+static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, struct net_device *dev)
 {
        /* Classical CAN -> no checks for flags and device capabilities */
        if (can_is_can_skb(skb))
                return CAN_MTU;
 
-       /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
-           (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
-               return CANFD_MTU;
+       /* CAN FD -> needs to be enabled in a CAN FD or CAN XL device */
+       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb)) {
+               /* real/virtual CAN FD interface */
+               if (dev->mtu == CANFD_MTU)
+                       return CANFD_MTU;
+               if (can_is_canxl_dev_mtu(dev->mtu) &&
+                   can_dev_ctrlmode_fd_on(dev))
+                       return CANFD_MTU;
+       }
 
        /* CAN XL -> needs to be enabled and a CAN XL device */
        if (ro->xl_frames && can_is_canxl_skb(skb) &&
-           can_is_canxl_dev_mtu(mtu))
+           can_is_canxl_dev_mtu(dev->mtu))
                return CANXL_MTU;
 
        return 0;
 }
 
@@ -948,11 +944,11 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
                goto free_skb;
 
        err = -EINVAL;
 
        /* check for valid CAN (CC/FD/XL) frame content */
-       txmtu = raw_check_txframe(ro, skb, dev->mtu);
+       txmtu = raw_check_txframe(ro, skb, dev);
        if (!txmtu)
                goto free_skb;
 
        /* only CANXL: clear/forward/set VCID value */
        if (txmtu == CANXL_MTU)
author maintainer
#3 lore raw

Re: [RFC PATCH v3 2/2] can: support XL only content on real CAN XL interfaces

Quoted 85 lines · Vincent Mailhol
Quoted 84 lines · Oliver Hartkopp

The CAN XL devices can be configured as CAN XL only with 'xl on fd off' which is currently not supported as the CAN XL sockopt on the CAN_RAW socket implicitly enables CAN FD support.

This patch removes this XL/FD connection for real CAN XL interfaces and rejects CAN FD content on CAN XL interfaces with 'fd off'.

---
  include/linux/can/dev.h | 12 ++++++++++++
  net/can/raw.c           | 32 ++++++++++++++------------------
  2 files changed, 26 insertions(+), 18 deletions(-)
include/linux/can/dev.h +12 -0
index 9a92cbe5b2cb..9fa139cc793e 100644
@@ -183,10 +183,22 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  void free_candev(struct net_device *dev);
 
  /* a candev safe wrapper around netdev_priv */
  struct can_priv *safe_candev_priv(struct net_device *dev);
 
+static inline bool can_dev_ctrlmode_fd_on(struct net_device *dev)
+{
+       struct can_priv *priv = safe_candev_priv(dev);
+
+       /* check ctrlmode on real CAN interfaces */
+       if (priv)
+               return (priv->ctrlmode & CAN_CTRLMODE_FD);
+
+       /* virtual CAN FD/XL interfaces always support CAN FD */
+       return true;
+}
+
  int open_candev(struct net_device *dev);
  void close_candev(struct net_device *dev);
  int can_change_mtu(struct net_device *dev, int new_mtu);
  int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
  int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
net/can/raw.c +2 -8
index f48b1f3fd6e8..6cd1f9cb050d 100644
@@ -558,11 +558,10 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
         struct raw_sock *ro = raw_sk(sk);
         struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
         struct can_filter sfilter;         /* single filter */
         struct net_device *dev = NULL;
         can_err_mask_t err_mask = 0;
-       int fd_frames;
         int count = 0;
         int err = 0;
 
         if (level != SOL_CAN_RAW)
                 return -EINVAL;
@@ -698,33 +697,25 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
                         return -EFAULT;
 
                 break;
 
         case CAN_RAW_FD_FRAMES:
-               if (optlen != sizeof(fd_frames))
+               if (optlen != sizeof(ro->fd_frames))
                         return -EINVAL;
 
-               if (copy_from_sockptr(&fd_frames, optval, optlen))
+               if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
                         return -EFAULT;
 
-               /* Enabling CAN XL includes CAN FD */
-               if (ro->xl_frames && !fd_frames)
-                       return -EINVAL;
-
-               ro->fd_frames = fd_frames;
                 break;
 
         case CAN_RAW_XL_FRAMES:
                 if (optlen != sizeof(ro->xl_frames))
                         return -EINVAL;
 
                 if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
                         return -EFAULT;
 
Quoted 12 lines

net/can/raw.c static int raw_setsockopt(struct socket *sock, int level, int optname, · line 717

Quoted 4 lines
net/can/raw.c +0 -3
-               /* Enabling CAN XL includes CAN FD */
-               if (ro->xl_frames)
-                       ro->fd_frames = ro->xl_frames;
                 break;

I think this is a UAPI breaking change. Any previous code which would have only set CAN_RAW_XL_FRAMES to implicitly also set CAN_RAW_FD_FRAMES would now break. I think it is an acceptable compromise, but, at least, we should make it crystal clear in the patch description that this is an UAPI breakage and clarify what the impacts are for the end user.

Yes it is. And after another night I realized that it breaks the rule, that with every extension CC -> FD -> XL the former protocol types were covered.

So I removed this change in setsockopt() in v4.

The other feature to reject FD traffic on CAN XL interface with 'fd off' is now the only topic for that patch.

Best regards,
Oliver

Quoted 52 lines
Quoted 52 lines
net/can/raw.c +12 -7
         case CAN_RAW_XL_VCID_OPTS:
                 if (optlen != sizeof(ro->raw_vcid_opts))
                         return -EINVAL;
@@ -879,24 +870,29 @@ static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
                 cxl->prio &= CANXL_PRIO_MASK;
                 cxl->prio |= ro->tx_vcid_shifted;
         }
  }
 
-static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
+static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, struct net_device *dev)
  {
         /* Classical CAN -> no checks for flags and device capabilities */
         if (can_is_can_skb(skb))
                 return CAN_MTU;
 
-       /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
-           (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
-               return CANFD_MTU;
+       /* CAN FD -> needs to be enabled in a CAN FD or CAN XL device */
+       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb)) {
+               /* real/virtual CAN FD interface */
+               if (dev->mtu == CANFD_MTU)
+                       return CANFD_MTU;
+               if (can_is_canxl_dev_mtu(dev->mtu) &&
+                   can_dev_ctrlmode_fd_on(dev))
+                       return CANFD_MTU;
+       }
 
         /* CAN XL -> needs to be enabled and a CAN XL device */
         if (ro->xl_frames && can_is_canxl_skb(skb) &&
-           can_is_canxl_dev_mtu(mtu))
+           can_is_canxl_dev_mtu(dev->mtu))
                 return CANXL_MTU;
 
         return 0;
  }
 
@@ -948,11 +944,11 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
                 goto free_skb;
 
         err = -EINVAL;
 
         /* check for valid CAN (CC/FD/XL) frame content */
-       txmtu = raw_check_txframe(ro, skb, dev->mtu);
+       txmtu = raw_check_txframe(ro, skb, dev);
         if (!txmtu)
                 goto free_skb;
 
         /* only CANXL: clear/forward/set VCID value */
         if (txmtu == CANXL_MTU)
maintainer
#4 lore raw
In reply to Oliver Hartkopp

net/can/raw.c static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,

Quoted 89 lines · Oliver Hartkopp

The check in can_is_canfd_skb() is about a length check of skb->len and the CAN FD data length. As a skb length of CANFD_MTU can potentially be created with a CAN XL frame with a data length of 60, the length check of the CAN FD data length is used to detect CAN XL frames via its CANXL_XLF flag which exceeds valid CAN FD data length values.

To make sure the CANFD_FDF flag can be safely used as a marker for CAN FD frame skbs the bit was set in can_send() and is now also set in raw_check_txframe() to re-use the indroduced can_is_canfd_skb_set_fdf() function. In the RX path alloc_canfd_skb() sets the CANFD_FDF flag.

The enforced CANFD_FDF check in can_is_canfd_skb() clears up the potential uncertainty when using the skb->len check with the CANFD_MTU.

---
 include/linux/can/skb.h | 25 +++++++++++++++++++++++--
 net/can/af_can.c        |  7 +------
 net/can/raw.c           |  2 +-
 3 files changed, 25 insertions(+), 9 deletions(-)
include/linux/can/skb.h +23 -2
index 1abc25a8d144..38d036b43280 100644
@@ -111,12 +111,33 @@ static inline bool can_is_can_skb(const struct sk_buff *skb)
 
 static inline bool can_is_canfd_skb(const struct sk_buff *skb)
 {
        struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-       /* the CAN specific type of skb is identified by its data length */
-       return (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN);
+       if (skb->len != CANFD_MTU || cfd->len > CANFD_MAX_DLEN)
+               return false;
+
+       return cfd->flags & CANFD_FDF;
+}
+
+static inline bool can_is_canfd_skb_set_fdf(const struct sk_buff *skb)
+{
+       struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+       /* The CAN specific type of skb is identified by its data length.
+        * A CAN XL frame skb might have a skb->len of CANFD_MTU but the
+        * skb would have the CANXL_XLF bit set (0x80 = 128) in the
+        * cfd->len field position which would intentionally break the
+        * CAN FD length check here. So we can surely tag it as CAN FD.
+        */
+       if (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN) {
+               /* set CAN FD flag for CAN FD frames by default */
+               cfd->flags |= CANFD_FDF;
+               return true;
+       }
+
+       return false;
 }
 
 static inline bool can_is_canxl_skb(const struct sk_buff *skb)
 {
        const struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
net/can/af_can.c +1 -6
index b2387a46794a..0caf75a9e27f 100644
@@ -207,17 +207,12 @@ int can_send(struct sk_buff *skb, int loop)
 
        if (can_is_canxl_skb(skb)) {
                skb->protocol = htons(ETH_P_CANXL);
        } else if (can_is_can_skb(skb)) {
                skb->protocol = htons(ETH_P_CAN);
-       } else if (can_is_canfd_skb(skb)) {
-               struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
-
+       } else if (can_is_canfd_skb_set_fdf(skb)) {
                skb->protocol = htons(ETH_P_CANFD);
-
-               /* set CAN FD flag for CAN FD frames by default */
-               cfd->flags |= CANFD_FDF;
        } else {
                goto inval_skb;
        }
 
        /* Make sure the CAN frame can pass the selected CAN netdevice. */
net/can/raw.c +0 -0
index 76b867d21def..f48b1f3fd6e8 100644
@@ -886,11 +886,11 @@ static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,

With this change, raw_check_txframe() now has a side effect. It will set the CANFD_FDF under some conditions. This is weird for a function named "check". When I read the code, I expected a check function to not have such side effects.

I would suggest to set the flag in raw_sendmsg(), something like that:

         txmtu = raw_check_txframe(ro, skb, dev->mtu);
         if (!txmtu)
                 goto free_skb;

+ /* set CAN FD flag for CAN FD frames by default */
+ if (txmtu == CANFD_MTU) {
+ struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+ cfd->flags |= CANFD_FDF;
+ }
+

         /* only CANXL: clear/forward/set VCID value */
         if (txmtu == CANXL_MTU)
                 raw_put_canxl_vcid(ro, skb);
Quoted 12 lines
net/can/raw.c +1 -1
        /* Classical CAN -> no checks for flags and device capabilities */
        if (can_is_can_skb(skb))
                return CAN_MTU;
 
        /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-       if (ro->fd_frames && can_is_canfd_skb(skb) &&
+       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
            (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
                return CANFD_MTU;
 
        /* CAN XL -> needs to be enabled and a CAN XL device */
        if (ro->xl_frames && can_is_canxl_skb(skb) &&
author maintainer
#5 lore raw
Quoted 102 lines · Vincent Mailhol

net/can/raw.c static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,

Quoted 89 lines · Oliver Hartkopp

The check in can_is_canfd_skb() is about a length check of skb->len and the CAN FD data length. As a skb length of CANFD_MTU can potentially be created with a CAN XL frame with a data length of 60, the length check of the CAN FD data length is used to detect CAN XL frames via its CANXL_XLF flag which exceeds valid CAN FD data length values.

To make sure the CANFD_FDF flag can be safely used as a marker for CAN FD frame skbs the bit was set in can_send() and is now also set in raw_check_txframe() to re-use the indroduced can_is_canfd_skb_set_fdf() function. In the RX path alloc_canfd_skb() sets the CANFD_FDF flag.

The enforced CANFD_FDF check in can_is_canfd_skb() clears up the potential uncertainty when using the skb->len check with the CANFD_MTU.

---
  include/linux/can/skb.h | 25 +++++++++++++++++++++++--
  net/can/af_can.c        |  7 +------
  net/can/raw.c           |  2 +-
  3 files changed, 25 insertions(+), 9 deletions(-)
include/linux/can/skb.h +23 -2
index 1abc25a8d144..38d036b43280 100644
@@ -111,12 +111,33 @@ static inline bool can_is_can_skb(const struct sk_buff *skb)
 
  static inline bool can_is_canfd_skb(const struct sk_buff *skb)
  {
         struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 
-       /* the CAN specific type of skb is identified by its data length */
-       return (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN);
+       if (skb->len != CANFD_MTU || cfd->len > CANFD_MAX_DLEN)
+               return false;
+
+       return cfd->flags & CANFD_FDF;
+}
+
+static inline bool can_is_canfd_skb_set_fdf(const struct sk_buff *skb)
+{
+       struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+       /* The CAN specific type of skb is identified by its data length.
+        * A CAN XL frame skb might have a skb->len of CANFD_MTU but the
+        * skb would have the CANXL_XLF bit set (0x80 = 128) in the
+        * cfd->len field position which would intentionally break the
+        * CAN FD length check here. So we can surely tag it as CAN FD.
+        */
+       if (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN) {
+               /* set CAN FD flag for CAN FD frames by default */
+               cfd->flags |= CANFD_FDF;
+               return true;
+       }
+
+       return false;
  }
 
  static inline bool can_is_canxl_skb(const struct sk_buff *skb)
  {
         const struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
net/can/af_can.c +1 -6
index b2387a46794a..0caf75a9e27f 100644
@@ -207,17 +207,12 @@ int can_send(struct sk_buff *skb, int loop)
 
         if (can_is_canxl_skb(skb)) {
                 skb->protocol = htons(ETH_P_CANXL);
         } else if (can_is_can_skb(skb)) {
                 skb->protocol = htons(ETH_P_CAN);
-       } else if (can_is_canfd_skb(skb)) {
-               struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
-
+       } else if (can_is_canfd_skb_set_fdf(skb)) {
                 skb->protocol = htons(ETH_P_CANFD);
-
-               /* set CAN FD flag for CAN FD frames by default */
-               cfd->flags |= CANFD_FDF;
         } else {
                 goto inval_skb;
         }
 
         /* Make sure the CAN frame can pass the selected CAN netdevice. */
net/can/raw.c +0 -0
index 76b867d21def..f48b1f3fd6e8 100644
@@ -886,11 +886,11 @@ static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,

With this change, raw_check_txframe() now has a side effect. It will set the CANFD_FDF under some conditions. This is weird for a function named "check". When I read the code, I expected a check function to not have such side effects.

I would suggest to set the flag in raw_sendmsg(), something like that:

          txmtu = raw_check_txframe(ro, skb, dev->mtu);
          if (!txmtu)
                  goto free_skb;

include/linux/can/skb.h static inline bool can_is_can_skb(const struct sk_buff *skb) · line 137

Quoted 7 lines
include/linux/can/skb.h +7 -0
+        /* set CAN FD flag for CAN FD frames by default */
+        if (txmtu == CANFD_MTU) {
+                struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+                cfd->flags |= CANFD_FDF;
+        }
+

Right. I've seen this remark to late for my v4.
Will propose a better solution.

Best regards,
Oliver

Quoted 16 lines
          /* only CANXL: clear/forward/set VCID value */
          if (txmtu == CANXL_MTU)
                  raw_put_canxl_vcid(ro, skb);
Quoted 12 lines
net/can/raw.c +1 -1
         /* Classical CAN -> no checks for flags and device capabilities */
         if (can_is_can_skb(skb))
                 return CAN_MTU;
 
         /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
-       if (ro->fd_frames && can_is_canfd_skb(skb) &&
+       if (ro->fd_frames && can_is_canfd_skb_set_fdf(skb) &&
             (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
                 return CANFD_MTU;
 
         /* CAN XL -> needs to be enabled and a CAN XL device */
         if (ro->xl_frames && can_is_canxl_skb(skb) &&