1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /* raw.c - Raw sockets for protocol family CAN
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/uio.h>
45 #include <linux/net.h>
46 #include <linux/slab.h>
47 #include <linux/netdevice.h>
48 #include <linux/socket.h>
49 #include <linux/if_arp.h>
50 #include <linux/skbuff.h>
51 #include <linux/can.h>
52 #include <linux/can/core.h>
53 #include <linux/can/dev.h> /* for can_is_canxl_dev_mtu() */
54 #include <linux/can/skb.h>
55 #include <linux/can/raw.h>
57 #include <net/net_namespace.h>
59 MODULE_DESCRIPTION("PF_CAN raw protocol");
60 MODULE_LICENSE("Dual BSD/GPL");
61 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
62 MODULE_ALIAS("can-proto-1");
64 #define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
68 /* A raw socket has a list of can_filters attached to it, each receiving
69 * the CAN frames matching that filter. If the filter list is empty,
70 * no CAN frames will be received by the socket. The default after
71 * opening the socket, is to have one filter which receives all frames.
72 * The filter list is allocated dynamically with the exception of the
73 * list containing only one item. This common case is optimized by
74 * storing the single filter in dfilter, to avoid using dynamic memory.
79 const struct sk_buff *skb;
80 unsigned int join_rx_count;
87 struct net_device *dev;
88 struct list_head notifier;
94 int count; /* number of active filters */
95 struct can_filter dfilter; /* default/single filter */
96 struct can_filter *filter; /* pointer to filter(s) */
97 can_err_mask_t err_mask;
98 struct uniqframe __percpu *uniq;
101 static LIST_HEAD(raw_notifier_list);
102 static DEFINE_SPINLOCK(raw_notifier_lock);
103 static struct raw_sock *raw_busy_notifier;
105 /* Return pointer to store the extra msg flags for raw_recvmsg().
106 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
109 static inline unsigned int *raw_flags(struct sk_buff *skb)
111 sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
112 sizeof(unsigned int));
114 /* return pointer after struct sockaddr_can */
115 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
118 static inline struct raw_sock *raw_sk(const struct sock *sk)
120 return (struct raw_sock *)sk;
123 static void raw_rcv(struct sk_buff *oskb, void *data)
125 struct sock *sk = (struct sock *)data;
126 struct raw_sock *ro = raw_sk(sk);
127 struct sockaddr_can *addr;
129 unsigned int *pflags;
131 /* check the received tx sock reference */
132 if (!ro->recv_own_msgs && oskb->sk == sk)
135 /* make sure to not pass oversized frames to the socket */
136 if ((!ro->fd_frames && can_is_canfd_skb(oskb)) ||
137 (!ro->xl_frames && can_is_canxl_skb(oskb)))
140 /* eliminate multiple filter matches for the same skb */
141 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
142 this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
143 if (!ro->join_filters)
146 this_cpu_inc(ro->uniq->join_rx_count);
147 /* drop frame until all enabled filters matched */
148 if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
151 this_cpu_ptr(ro->uniq)->skb = oskb;
152 this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
153 this_cpu_ptr(ro->uniq)->join_rx_count = 1;
154 /* drop first frame to check all enabled filters? */
155 if (ro->join_filters && ro->count > 1)
159 /* clone the given skb to be able to enqueue it into the rcv queue */
160 skb = skb_clone(oskb, GFP_ATOMIC);
164 /* Put the datagram to the queue so that raw_recvmsg() can get
165 * it from there. We need to pass the interface index to
166 * raw_recvmsg(). We pass a whole struct sockaddr_can in
167 * skb->cb containing the interface index.
170 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
171 addr = (struct sockaddr_can *)skb->cb;
172 memset(addr, 0, sizeof(*addr));
173 addr->can_family = AF_CAN;
174 addr->can_ifindex = skb->dev->ifindex;
176 /* add CAN specific message flags for raw_recvmsg() */
177 pflags = raw_flags(skb);
180 *pflags |= MSG_DONTROUTE;
182 *pflags |= MSG_CONFIRM;
184 if (sock_queue_rcv_skb(sk, skb) < 0)
188 static int raw_enable_filters(struct net *net, struct net_device *dev,
189 struct sock *sk, struct can_filter *filter,
195 for (i = 0; i < count; i++) {
196 err = can_rx_register(net, dev, filter[i].can_id,
198 raw_rcv, sk, "raw", sk);
200 /* clean up successfully registered filters */
202 can_rx_unregister(net, dev, filter[i].can_id,
212 static int raw_enable_errfilter(struct net *net, struct net_device *dev,
213 struct sock *sk, can_err_mask_t err_mask)
218 err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
219 raw_rcv, sk, "raw", sk);
224 static void raw_disable_filters(struct net *net, struct net_device *dev,
225 struct sock *sk, struct can_filter *filter,
230 for (i = 0; i < count; i++)
231 can_rx_unregister(net, dev, filter[i].can_id,
232 filter[i].can_mask, raw_rcv, sk);
235 static inline void raw_disable_errfilter(struct net *net,
236 struct net_device *dev,
238 can_err_mask_t err_mask)
242 can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
246 static inline void raw_disable_allfilters(struct net *net,
247 struct net_device *dev,
250 struct raw_sock *ro = raw_sk(sk);
252 raw_disable_filters(net, dev, sk, ro->filter, ro->count);
253 raw_disable_errfilter(net, dev, sk, ro->err_mask);
256 static int raw_enable_allfilters(struct net *net, struct net_device *dev,
259 struct raw_sock *ro = raw_sk(sk);
262 err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
264 err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
266 raw_disable_filters(net, dev, sk, ro->filter,
273 static void raw_notify(struct raw_sock *ro, unsigned long msg,
274 struct net_device *dev)
276 struct sock *sk = &ro->sk;
278 if (!net_eq(dev_net(dev), sock_net(sk)))
285 case NETDEV_UNREGISTER:
287 /* remove current filters & unregister */
289 raw_disable_allfilters(dev_net(dev), dev, sk);
301 if (!sock_flag(sk, SOCK_DEAD))
306 sk->sk_err = ENETDOWN;
307 if (!sock_flag(sk, SOCK_DEAD))
313 static int raw_notifier(struct notifier_block *nb, unsigned long msg,
316 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
318 if (dev->type != ARPHRD_CAN)
320 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
322 if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
325 spin_lock(&raw_notifier_lock);
326 list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
327 spin_unlock(&raw_notifier_lock);
328 raw_notify(raw_busy_notifier, msg, dev);
329 spin_lock(&raw_notifier_lock);
331 raw_busy_notifier = NULL;
332 spin_unlock(&raw_notifier_lock);
336 static int raw_init(struct sock *sk)
338 struct raw_sock *ro = raw_sk(sk);
344 /* set default filter to single entry dfilter */
345 ro->dfilter.can_id = 0;
346 ro->dfilter.can_mask = MASK_ALL;
347 ro->filter = &ro->dfilter;
350 /* set default loopback behaviour */
352 ro->recv_own_msgs = 0;
355 ro->join_filters = 0;
357 /* alloc_percpu provides zero'ed memory */
358 ro->uniq = alloc_percpu(struct uniqframe);
359 if (unlikely(!ro->uniq))
363 spin_lock(&raw_notifier_lock);
364 list_add_tail(&ro->notifier, &raw_notifier_list);
365 spin_unlock(&raw_notifier_lock);
370 static int raw_release(struct socket *sock)
372 struct sock *sk = sock->sk;
380 spin_lock(&raw_notifier_lock);
381 while (raw_busy_notifier == ro) {
382 spin_unlock(&raw_notifier_lock);
383 schedule_timeout_uninterruptible(1);
384 spin_lock(&raw_notifier_lock);
386 list_del(&ro->notifier);
387 spin_unlock(&raw_notifier_lock);
392 /* remove current filters & unregister */
395 raw_disable_allfilters(dev_net(ro->dev), ro->dev, sk);
397 raw_disable_allfilters(sock_net(sk), NULL, sk);
407 free_percpu(ro->uniq);
420 static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
422 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
423 struct sock *sk = sock->sk;
424 struct raw_sock *ro = raw_sk(sk);
425 struct net_device *dev = NULL;
428 int notify_enetdown = 0;
430 if (len < RAW_MIN_NAMELEN)
432 if (addr->can_family != AF_CAN)
438 if (ro->bound && addr->can_ifindex == ro->ifindex)
441 if (addr->can_ifindex) {
442 dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
447 if (dev->type != ARPHRD_CAN) {
452 if (!(dev->flags & IFF_UP))
455 ifindex = dev->ifindex;
457 /* filters set by default/setsockopt */
458 err = raw_enable_allfilters(sock_net(sk), dev, sk);
463 /* filters set by default/setsockopt */
464 err = raw_enable_allfilters(sock_net(sk), NULL, sk);
469 /* unregister old filters */
471 raw_disable_allfilters(dev_net(ro->dev),
474 raw_disable_allfilters(sock_net(sk), NULL, sk);
476 ro->ifindex = ifindex;
485 if (notify_enetdown) {
486 sk->sk_err = ENETDOWN;
487 if (!sock_flag(sk, SOCK_DEAD))
494 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
497 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
498 struct sock *sk = sock->sk;
499 struct raw_sock *ro = raw_sk(sk);
504 memset(addr, 0, RAW_MIN_NAMELEN);
505 addr->can_family = AF_CAN;
506 addr->can_ifindex = ro->ifindex;
508 return RAW_MIN_NAMELEN;
511 static int raw_setsockopt(struct socket *sock, int level, int optname,
512 sockptr_t optval, unsigned int optlen)
514 struct sock *sk = sock->sk;
515 struct raw_sock *ro = raw_sk(sk);
516 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
517 struct can_filter sfilter; /* single filter */
518 struct net_device *dev = NULL;
519 can_err_mask_t err_mask = 0;
524 if (level != SOL_CAN_RAW)
529 if (optlen % sizeof(struct can_filter) != 0)
532 if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
535 count = optlen / sizeof(struct can_filter);
538 /* filter does not fit into dfilter => alloc space */
539 filter = memdup_sockptr(optval, optlen);
541 return PTR_ERR(filter);
542 } else if (count == 1) {
543 if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
551 if (ro->bound && dev) {
552 if (dev->reg_state != NETREG_REGISTERED) {
561 /* (try to) register the new filters */
563 err = raw_enable_filters(sock_net(sk), dev, sk,
566 err = raw_enable_filters(sock_net(sk), dev, sk,
574 /* remove old filter registrations */
575 raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
579 /* remove old filter space */
583 /* link new filters to the socket */
585 /* copy filter data for single filter */
586 ro->dfilter = sfilter;
587 filter = &ro->dfilter;
598 case CAN_RAW_ERR_FILTER:
599 if (optlen != sizeof(err_mask))
602 if (copy_from_sockptr(&err_mask, optval, optlen))
605 err_mask &= CAN_ERR_MASK;
611 if (ro->bound && dev) {
612 if (dev->reg_state != NETREG_REGISTERED) {
618 /* remove current error mask */
620 /* (try to) register the new err_mask */
621 err = raw_enable_errfilter(sock_net(sk), dev, sk,
627 /* remove old err_mask registration */
628 raw_disable_errfilter(sock_net(sk), dev, sk,
632 /* link new err_mask to the socket */
633 ro->err_mask = err_mask;
641 case CAN_RAW_LOOPBACK:
642 if (optlen != sizeof(ro->loopback))
645 if (copy_from_sockptr(&ro->loopback, optval, optlen))
650 case CAN_RAW_RECV_OWN_MSGS:
651 if (optlen != sizeof(ro->recv_own_msgs))
654 if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
659 case CAN_RAW_FD_FRAMES:
660 if (optlen != sizeof(fd_frames))
663 if (copy_from_sockptr(&fd_frames, optval, optlen))
666 /* Enabling CAN XL includes CAN FD */
667 if (ro->xl_frames && !fd_frames)
670 ro->fd_frames = fd_frames;
673 case CAN_RAW_XL_FRAMES:
674 if (optlen != sizeof(ro->xl_frames))
677 if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
680 /* Enabling CAN XL includes CAN FD */
682 ro->fd_frames = ro->xl_frames;
685 case CAN_RAW_JOIN_FILTERS:
686 if (optlen != sizeof(ro->join_filters))
689 if (copy_from_sockptr(&ro->join_filters, optval, optlen))
700 static int raw_getsockopt(struct socket *sock, int level, int optname,
701 char __user *optval, int __user *optlen)
703 struct sock *sk = sock->sk;
704 struct raw_sock *ro = raw_sk(sk);
709 if (level != SOL_CAN_RAW)
711 if (get_user(len, optlen))
720 int fsize = ro->count * sizeof(struct can_filter);
722 /* user space buffer to small for filter list? */
724 /* return -ERANGE and needed space in optlen */
726 if (put_user(fsize, optlen))
731 if (copy_to_user(optval, ro->filter, len))
740 err = put_user(len, optlen);
743 case CAN_RAW_ERR_FILTER:
744 if (len > sizeof(can_err_mask_t))
745 len = sizeof(can_err_mask_t);
749 case CAN_RAW_LOOPBACK:
750 if (len > sizeof(int))
755 case CAN_RAW_RECV_OWN_MSGS:
756 if (len > sizeof(int))
758 val = &ro->recv_own_msgs;
761 case CAN_RAW_FD_FRAMES:
762 if (len > sizeof(int))
764 val = &ro->fd_frames;
767 case CAN_RAW_XL_FRAMES:
768 if (len > sizeof(int))
770 val = &ro->xl_frames;
773 case CAN_RAW_JOIN_FILTERS:
774 if (len > sizeof(int))
776 val = &ro->join_filters;
783 if (put_user(len, optlen))
785 if (copy_to_user(optval, val, len))
790 static bool raw_bad_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
792 /* Classical CAN -> no checks for flags and device capabilities */
793 if (can_is_can_skb(skb))
796 /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
797 if (ro->fd_frames && can_is_canfd_skb(skb) &&
798 (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
801 /* CAN XL -> needs to be enabled and a CAN XL device */
802 if (ro->xl_frames && can_is_canxl_skb(skb) &&
803 can_is_canxl_dev_mtu(mtu))
809 static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
811 struct sock *sk = sock->sk;
812 struct raw_sock *ro = raw_sk(sk);
813 struct sockcm_cookie sockc;
815 struct net_device *dev;
819 /* check for valid CAN frame sizes */
820 if (size < CANXL_HDR_SIZE + CANXL_MIN_DLEN || size > CANXL_MTU)
824 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
826 if (msg->msg_namelen < RAW_MIN_NAMELEN)
829 if (addr->can_family != AF_CAN)
832 ifindex = addr->can_ifindex;
834 ifindex = ro->ifindex;
837 dev = dev_get_by_index(sock_net(sk), ifindex);
841 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
842 msg->msg_flags & MSG_DONTWAIT, &err);
846 can_skb_reserve(skb);
847 can_skb_prv(skb)->ifindex = dev->ifindex;
848 can_skb_prv(skb)->skbcnt = 0;
850 /* fill the skb before testing for valid CAN frames */
851 err = memcpy_from_msg(skb_put(skb, size), msg, size);
856 if (raw_bad_txframe(ro, skb, dev->mtu))
859 sockcm_init(&sockc, sk);
860 if (msg->msg_controllen) {
861 err = sock_cmsg_send(sk, msg, &sockc);
867 skb->priority = sk->sk_priority;
868 skb->mark = READ_ONCE(sk->sk_mark);
869 skb->tstamp = sockc.transmit_time;
871 skb_setup_tx_timestamp(skb, sockc.tsflags);
873 err = can_send(skb, ro->loopback);
890 static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
893 struct sock *sk = sock->sk;
897 if (flags & MSG_ERRQUEUE)
898 return sock_recv_errqueue(sk, msg, size,
899 SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
901 skb = skb_recv_datagram(sk, flags, &err);
906 msg->msg_flags |= MSG_TRUNC;
910 err = memcpy_to_msg(msg, skb->data, size);
912 skb_free_datagram(sk, skb);
916 sock_recv_cmsgs(msg, sk, skb);
919 __sockaddr_check_size(RAW_MIN_NAMELEN);
920 msg->msg_namelen = RAW_MIN_NAMELEN;
921 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
924 /* assign the flags that have been recorded in raw_rcv() */
925 msg->msg_flags |= *(raw_flags(skb));
927 skb_free_datagram(sk, skb);
932 static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
935 /* no ioctls for socket layer -> hand it down to NIC layer */
939 static const struct proto_ops raw_ops = {
941 .release = raw_release,
943 .connect = sock_no_connect,
944 .socketpair = sock_no_socketpair,
945 .accept = sock_no_accept,
946 .getname = raw_getname,
947 .poll = datagram_poll,
948 .ioctl = raw_sock_no_ioctlcmd,
949 .gettstamp = sock_gettstamp,
950 .listen = sock_no_listen,
951 .shutdown = sock_no_shutdown,
952 .setsockopt = raw_setsockopt,
953 .getsockopt = raw_getsockopt,
954 .sendmsg = raw_sendmsg,
955 .recvmsg = raw_recvmsg,
956 .mmap = sock_no_mmap,
959 static struct proto raw_proto __read_mostly = {
961 .owner = THIS_MODULE,
962 .obj_size = sizeof(struct raw_sock),
966 static const struct can_proto raw_can_proto = {
973 static struct notifier_block canraw_notifier = {
974 .notifier_call = raw_notifier
977 static __init int raw_module_init(void)
981 pr_info("can: raw protocol\n");
983 err = register_netdevice_notifier(&canraw_notifier);
987 err = can_proto_register(&raw_can_proto);
989 pr_err("can: registration of raw protocol failed\n");
990 goto register_proto_failed;
995 register_proto_failed:
996 unregister_netdevice_notifier(&canraw_notifier);
1000 static __exit void raw_module_exit(void)
1002 can_proto_unregister(&raw_can_proto);
1003 unregister_netdevice_notifier(&canraw_notifier);
1006 module_init(raw_module_init);
1007 module_exit(raw_module_exit);