Skip to contentSkip to conversation

can: raw: fix build without CONFIG_CAN_DEV

net-next Oliver Hartkopp 2 participants 6 messages Nov 27, 2025

Subsystems (4)
lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

The feature to instantly reject unsupported CAN frames makes use of CAN netdevice specific flags which are only accessible when the CAN device driver infrastructure is built.

Therefore check for CONFIG_CAN_DEV and fall back to MTU testing when the CAN device driver infrastructure is absent.

Fixes 1a620a723853 ("can: raw: instantly reject unsupported CAN frames")
---
 net/can/raw.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
net/can/raw.c +18 -14
index 223630f0f9e9..9d5c43df06dd 100644
@@ -890,62 +890,66 @@ 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 inline bool raw_dev_cc_enabled(struct net_device *dev,
-				      struct can_priv *priv)
+static bool raw_dev_cc_enabled(struct net_device *dev)
 {
+#ifdef CONFIG_CAN_DEV
+	struct can_priv *priv = safe_candev_priv(dev);
+
 	/* The CANXL-only mode disables error-signalling on the CAN bus
 	 * which is needed to send CAN CC/FD frames
 	 */
 	if (priv)
 		return !can_dev_in_xl_only_mode(priv);
-
+#endif
 	/* virtual CAN interfaces always support CAN CC */
 	return true;
 }
 
-static inline bool raw_dev_fd_enabled(struct net_device *dev,
-				      struct can_priv *priv)
+static bool raw_dev_fd_enabled(struct net_device *dev)
 {
+#ifdef CONFIG_CAN_DEV
+	struct can_priv *priv = safe_candev_priv(dev);
+
 	/* check FD ctrlmode on real CAN interfaces */
 	if (priv)
 		return (priv->ctrlmode & CAN_CTRLMODE_FD);
-
+#endif
 	/* check MTU for virtual CAN FD interfaces */
 	return (READ_ONCE(dev->mtu) >= CANFD_MTU);
 }
 
-static inline bool raw_dev_xl_enabled(struct net_device *dev,
-				      struct can_priv *priv)
+static bool raw_dev_xl_enabled(struct net_device *dev)
 {
+#ifdef CONFIG_CAN_DEV
+	struct can_priv *priv = safe_candev_priv(dev);
+
 	/* check XL ctrlmode on real CAN interfaces */
 	if (priv)
 		return (priv->ctrlmode & CAN_CTRLMODE_XL);
-
+#endif
 	/* check MTU for virtual CAN XL interfaces */
 	return can_is_canxl_dev_mtu(READ_ONCE(dev->mtu));
 }
 
 static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,
 				      struct net_device *dev)
 {
-	struct can_priv *priv = safe_candev_priv(dev);
-
 	/* Classical CAN */
-	if (can_is_can_skb(skb) && raw_dev_cc_enabled(dev, priv))
+	if (can_is_can_skb(skb) && raw_dev_cc_enabled(dev))
 		return CAN_MTU;
 
 	/* CAN FD */
 	if (ro->fd_frames && can_is_canfd_skb(skb) &&
-	    raw_dev_fd_enabled(dev, priv))
+	    raw_dev_fd_enabled(dev))
 		return CANFD_MTU;
 
 	/* CAN XL */
 	if (ro->xl_frames && can_is_canxl_skb(skb) &&
-	    raw_dev_xl_enabled(dev, priv))
+	    raw_dev_xl_enabled(dev))
 		return CANXL_MTU;
 
 	return 0;
 }
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 10 lines · Oliver Hartkopp

The feature to instantly reject unsupported CAN frames makes use of CAN netdevice specific flags which are only accessible when the CAN device driver infrastructure is built.

Therefore check for CONFIG_CAN_DEV and fall back to MTU testing when the CAN device driver infrastructure is absent.

Fixes 1a620a723853 ("can: raw: instantly reject unsupported CAN frames")

That's not sufficient. We can build the CAN_DEV as a module but compile CAN_RAW into the kernel.

This patch does the same as your's but only with a single ifdef in the header file.

include/linux/can/dev.h +7 -0
index 52c8be5c160e..14d58461430e 100644
@@ -111,7 +111,14 @@ 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 */
+#ifdef CONFIG_CAN_DEV
 struct can_priv *safe_candev_priv(struct net_device *dev);
+#else
+static inline struct can_priv *safe_candev_priv(struct net_device *dev)
+{
+        return NULL;
+}
+#endif
 
 int open_candev(struct net_device *dev);
 void close_candev(struct net_device *dev);

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author maintainer
#2 lore raw
Quoted 8 lines · Marc Kleine-Budde
Quoted 7 lines · Oliver Hartkopp

The feature to instantly reject unsupported CAN frames makes use of CAN netdevice specific flags which are only accessible when the CAN device driver infrastructure is built.

Therefore check for CONFIG_CAN_DEV and fall back to MTU testing when the CAN device driver infrastructure is absent.

Quoted 6 lines
Quoted 3 lines
Fixes 1a620a723853 ("can: raw: instantly reject unsupported CAN frames")

That's not sufficient. We can build the CAN_DEV as a module but compile CAN_RAW into the kernel.

Oh, yes that's better.

I had a fight with CONFIG_CAN_DEV with my first patch too. That's why I sent a v2 some seconds ago.

Quoted 10 lines

This patch does the same as your's but only with a single ifdef in the header file.

include/linux/can/dev.h +0 -0
index 52c8be5c160e..14d58461430e 100644
@@ -111,7 +111,14 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  void free_candev(struct net_device *dev);

include/linux/can/dev.h struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max, · line 121

Quoted 10 lines
include/linux/can/dev.h +7 -0
 
  /* a candev safe wrapper around netdev_priv */
+#ifdef CONFIG_CAN_DEV
  struct can_priv *safe_candev_priv(struct net_device *dev);
+#else
+static inline struct can_priv *safe_candev_priv(struct net_device *dev)
+{
+        return NULL;
+}
+#endif

Will send a v3 with your suggestion.

Many thanks,
Oliver

Quoted 6 lines
include/linux/can/dev.h +0 -0
  int open_candev(struct net_device *dev);
  void close_candev(struct net_device *dev);
 

Marc

#3 lore raw
Quoted 4 lines · Oliver Hartkopp
Quoted 2 lines

That's not sufficient. We can build the CAN_DEV as a module but compile CAN_RAW into the kernel.

Oh, yes that's better.

It's nicer, but it will not work if you build CAN_RAW into the kernel and CAN_DEV as a module....Let me think of the right kconfig magic to workaround this...

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author maintainer
#4 lore raw

Hello Marc,

Quoted 9 lines · Marc Kleine-Budde
Quoted 4 lines · Oliver Hartkopp
Quoted 2 lines

That's not sufficient. We can build the CAN_DEV as a module but compile CAN_RAW into the kernel.

Oh, yes that's better.

It's nicer, but it will not work if you build CAN_RAW into the kernel and CAN_DEV as a module....Let me think of the right kconfig magic to workaround this...

No need for it IMO.

I built my kernel with the CAN netlayer stuff into the kernel and disabled the CAN_DEV stuff.

As you defined

static inline struct can_priv *safe_candev_priv(struct net_device *dev) {
	return NULL;
}

in

include/linux/can/dev.h

this function is now inside raw.c which compiles excellent.

Building without CONFIG_CAN_DEV is not a valid use-case anyway.

It would only make sense, if someone wants to create an out-of-tree CAN driver without netlink ...

Best regards,
Oliver

author maintainer
#5 lore raw

Answering myself:

You were right (raw.c built-in, drivers as modules):

ld: net/can/raw.o: in function `raw_check_txframe':
/home/hartko/linux/net/can/raw.c:933:(.text+0xb2c): undefined reference to `safe_candev_priv' make[2]: *** [scripts/Makefile.vmlinux:72: vmlinux.unstripped] Error 1 make[1]: *** [/home/hartko/linux/Makefile:1242: vmlinux] Error 2
make: *** [Makefile:248: __sub-make] Error 2

Maybe its a better task for tomorrow ...

Sorry for the noise.

Best regards,
Oliver

Quoted 37 lines · Oliver Hartkopp

Hello Marc,

Quoted 9 lines · Marc Kleine-Budde
Quoted 4 lines · Oliver Hartkopp
Quoted 2 lines

That's not sufficient. We can build the CAN_DEV as a module but compile CAN_RAW into the kernel.

Oh, yes that's better.

It's nicer, but it will not work if you build CAN_RAW into the kernel and CAN_DEV as a module....Let me think of the right kconfig magic to workaround this...

No need for it IMO.

I built my kernel with the CAN netlayer stuff into the kernel and disabled the CAN_DEV stuff.

As you defined

static inline struct can_priv *safe_candev_priv(struct net_device *dev) {
    return NULL;
}

in

include/linux/can/dev.h

this function is now inside raw.c which compiles excellent.

Building without CONFIG_CAN_DEV is not a valid use-case anyway.

It would only make sense, if someone wants to create an out-of-tree CAN driver without netlink ...

Best regards,
Oliver