]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
can: propagate CAN device capabilities via ml_priv
authorOliver Hartkopp <socketcan@hartkopp.net>
Fri, 9 Jan 2026 14:41:34 +0000 (15:41 +0100)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Thu, 15 Jan 2026 08:52:04 +0000 (09:52 +0100)
Commit 1a620a723853 ("can: raw: instantly reject unsupported CAN frames")
caused a sequence of dependency and linker fixes.

Instead of accessing CAN device internal data structures which caused the
dependency problems this patch introduces capability information into the
CAN specific ml_priv data which is accessible from both sides.

With this change the CAN network layer can check the required features and
the decoupling of the driver layer and network layer is restored.

Fixes: 1a620a723853 ("can: raw: instantly reject unsupported CAN frames")
Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Vincent Mailhol <mailhol@kernel.org>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://patch.msgid.link/20260109144135.8495-3-socketcan@hartkopp.net
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/dev/dev.c
drivers/net/can/dev/netlink.c
drivers/net/can/vcan.c
drivers/net/can/vxcan.c
include/linux/can/can-ml.h
include/linux/can/dev.h

index 091f30e94c610cdfb6b808177be73538220627e4..7ab9578f5b8909c64cafb4c6d508562f91c98718 100644 (file)
@@ -375,6 +375,32 @@ void can_set_default_mtu(struct net_device *dev)
        }
 }
 
+void can_set_cap_info(struct net_device *dev)
+{
+       struct can_priv *priv = netdev_priv(dev);
+       u32 can_cap;
+
+       if (can_dev_in_xl_only_mode(priv)) {
+               /* XL only mode => no CC/FD capability */
+               can_cap = CAN_CAP_XL;
+       } else {
+               /* mixed mode => CC + FD/XL capability */
+               can_cap = CAN_CAP_CC;
+
+               if (priv->ctrlmode & CAN_CTRLMODE_FD)
+                       can_cap |= CAN_CAP_FD;
+
+               if (priv->ctrlmode & CAN_CTRLMODE_XL)
+                       can_cap |= CAN_CAP_XL;
+       }
+
+       if (priv->ctrlmode & (CAN_CTRLMODE_LISTENONLY |
+                             CAN_CTRLMODE_RESTRICTED))
+               can_cap |= CAN_CAP_RO;
+
+       can_set_cap(dev, can_cap);
+}
+
 /* helper to define static CAN controller features at device creation time */
 int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
 {
@@ -390,6 +416,7 @@ int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
 
        /* override MTU which was set by default in can_setup()? */
        can_set_default_mtu(dev);
+       can_set_cap_info(dev);
 
        return 0;
 }
index d6b0e686fb114fa35c811ad924c7b404a330ac09..0498198a469658e20f31ed29ec90107841a09068 100644 (file)
@@ -377,6 +377,7 @@ static int can_ctrlmode_changelink(struct net_device *dev,
        }
 
        can_set_default_mtu(dev);
+       can_set_cap_info(dev);
 
        return 0;
 }
index fdc662aea2798125b3aa373f09958363b427ced2..76e6b7b5c6a11935baaf2554d8684f8703a0d2bf 100644 (file)
@@ -130,6 +130,19 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
        return NETDEV_TX_OK;
 }
 
+static void vcan_set_cap_info(struct net_device *dev)
+{
+       u32 can_cap = CAN_CAP_CC;
+
+       if (dev->mtu > CAN_MTU)
+               can_cap |= CAN_CAP_FD;
+
+       if (dev->mtu >= CANXL_MIN_MTU)
+               can_cap |= CAN_CAP_XL;
+
+       can_set_cap(dev, can_cap);
+}
+
 static int vcan_change_mtu(struct net_device *dev, int new_mtu)
 {
        /* Do not allow changing the MTU while running */
@@ -141,6 +154,7 @@ static int vcan_change_mtu(struct net_device *dev, int new_mtu)
                return -EINVAL;
 
        WRITE_ONCE(dev->mtu, new_mtu);
+       vcan_set_cap_info(dev);
        return 0;
 }
 
@@ -162,6 +176,7 @@ static void vcan_setup(struct net_device *dev)
        dev->tx_queue_len       = 0;
        dev->flags              = IFF_NOARP;
        can_set_ml_priv(dev, netdev_priv(dev));
+       vcan_set_cap_info(dev);
 
        /* set flags according to driver capabilities */
        if (echo)
index b2c19f8c5f8e5101b8be343401afe9a4f388c4da..f14c6f02b662717a3616c84cb8c6077b2e50a755 100644 (file)
@@ -125,6 +125,19 @@ static int vxcan_get_iflink(const struct net_device *dev)
        return iflink;
 }
 
+static void vxcan_set_cap_info(struct net_device *dev)
+{
+       u32 can_cap = CAN_CAP_CC;
+
+       if (dev->mtu > CAN_MTU)
+               can_cap |= CAN_CAP_FD;
+
+       if (dev->mtu >= CANXL_MIN_MTU)
+               can_cap |= CAN_CAP_XL;
+
+       can_set_cap(dev, can_cap);
+}
+
 static int vxcan_change_mtu(struct net_device *dev, int new_mtu)
 {
        /* Do not allow changing the MTU while running */
@@ -136,6 +149,7 @@ static int vxcan_change_mtu(struct net_device *dev, int new_mtu)
                return -EINVAL;
 
        WRITE_ONCE(dev->mtu, new_mtu);
+       vxcan_set_cap_info(dev);
        return 0;
 }
 
@@ -167,6 +181,7 @@ static void vxcan_setup(struct net_device *dev)
 
        can_ml = netdev_priv(dev) + ALIGN(sizeof(struct vxcan_priv), NETDEV_ALIGN);
        can_set_ml_priv(dev, can_ml);
+       vxcan_set_cap_info(dev);
 }
 
 /* forward declaration for rtnl_create_link() */
index 8afa92d15a664397a836d08987252ca13f65fdd3..1e99fda2b3803b68a2bf7761e8ff2cd407abe9ce 100644 (file)
 #include <linux/list.h>
 #include <linux/netdevice.h>
 
+/* exposed CAN device capabilities for network layer */
+#define CAN_CAP_CC BIT(0) /* CAN CC aka Classical CAN */
+#define CAN_CAP_FD BIT(1) /* CAN FD */
+#define CAN_CAP_XL BIT(2) /* CAN XL */
+#define CAN_CAP_RO BIT(3) /* read-only mode (LISTEN/RESTRICTED) */
+
 #define CAN_SFF_RCV_ARRAY_SZ (1 << CAN_SFF_ID_BITS)
 #define CAN_EFF_RCV_HASH_BITS 10
 #define CAN_EFF_RCV_ARRAY_SZ (1 << CAN_EFF_RCV_HASH_BITS)
@@ -64,6 +70,7 @@ struct can_ml_priv {
 #ifdef CAN_J1939
        struct j1939_priv *j1939_priv;
 #endif
+       u32 can_cap;
 };
 
 static inline struct can_ml_priv *can_get_ml_priv(struct net_device *dev)
@@ -77,4 +84,21 @@ static inline void can_set_ml_priv(struct net_device *dev,
        netdev_set_ml_priv(dev, ml_priv, ML_PRIV_CAN);
 }
 
+static inline bool can_cap_enabled(struct net_device *dev, u32 cap)
+{
+       struct can_ml_priv *can_ml = can_get_ml_priv(dev);
+
+       if (!can_ml)
+               return false;
+
+       return (can_ml->can_cap & cap);
+}
+
+static inline void can_set_cap(struct net_device *dev, u32 cap)
+{
+       struct can_ml_priv *can_ml = can_get_ml_priv(dev);
+
+       can_ml->can_cap = cap;
+}
+
 #endif /* CAN_ML_H */
index 52c8be5c160e47796c1e0d7a77f8dc17aaee810d..6d0710d6f571db528386e312a4cc7e7b922db230 100644 (file)
@@ -116,6 +116,7 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
 int open_candev(struct net_device *dev);
 void close_candev(struct net_device *dev);
 void can_set_default_mtu(struct net_device *dev);
+void can_set_cap_info(struct net_device *dev);
 int __must_check can_set_static_ctrlmode(struct net_device *dev,
                                         u32 static_mode);
 int can_hwtstamp_get(struct net_device *netdev,