Skip to content

Commit faba586

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: raw: instantly reject disabled CAN frames
For real CAN interfaces the CAN_CTRLMODE_FD and CAN_CTRLMODE_XL control modes indicate whether an interface can handle those CAN FD/XL frames. In the case a CAN XL interface is configured in CANXL-only mode with disabled error-signalling neither CAN CC nor CAN FD frames can be sent. The checks are now performed on CAN_RAW sockets to give an instant feedback to the user when writing unsupported CAN frames to the interface or when the CAN interface is in read-only mode. Fixes: 1a620a7 ("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-4-socketcan@hartkopp.net [mkl: fix dev reference leak] Link: https://lore.kernel.org/all/0636c732-2e71-4633-8005-dfa85e1da445@hartkopp.net Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 166e873 commit faba586

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

net/can/raw.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#include <linux/if_arp.h>
5050
#include <linux/skbuff.h>
5151
#include <linux/can.h>
52+
#include <linux/can/can-ml.h>
5253
#include <linux/can/core.h>
53-
#include <linux/can/dev.h> /* for can_is_canxl_dev_mtu() */
5454
#include <linux/can/skb.h>
5555
#include <linux/can/raw.h>
5656
#include <net/sock.h>
@@ -892,20 +892,21 @@ static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
892892
}
893893
}
894894

895-
static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
895+
static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb,
896+
struct net_device *dev)
896897
{
897-
/* Classical CAN -> no checks for flags and device capabilities */
898-
if (can_is_can_skb(skb))
898+
/* Classical CAN */
899+
if (can_is_can_skb(skb) && can_cap_enabled(dev, CAN_CAP_CC))
899900
return CAN_MTU;
900901

901-
/* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
902+
/* CAN FD */
902903
if (ro->fd_frames && can_is_canfd_skb(skb) &&
903-
(mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
904+
can_cap_enabled(dev, CAN_CAP_FD))
904905
return CANFD_MTU;
905906

906-
/* CAN XL -> needs to be enabled and a CAN XL device */
907+
/* CAN XL */
907908
if (ro->xl_frames && can_is_canxl_skb(skb) &&
908-
can_is_canxl_dev_mtu(mtu))
909+
can_cap_enabled(dev, CAN_CAP_XL))
909910
return CANXL_MTU;
910911

911912
return 0;
@@ -944,6 +945,12 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
944945
if (!dev)
945946
return -ENXIO;
946947

948+
/* no sending on a CAN device in read-only mode */
949+
if (can_cap_enabled(dev, CAN_CAP_RO)) {
950+
err = -EACCES;
951+
goto put_dev;
952+
}
953+
947954
skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
948955
msg->msg_flags & MSG_DONTWAIT, &err);
949956
if (!skb)
@@ -961,7 +968,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
961968
err = -EINVAL;
962969

963970
/* check for valid CAN (CC/FD/XL) frame content */
964-
txmtu = raw_check_txframe(ro, skb, READ_ONCE(dev->mtu));
971+
txmtu = raw_check_txframe(ro, skb, dev);
965972
if (!txmtu)
966973
goto free_skb;
967974

0 commit comments

Comments
 (0)