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 list_head notifier;
93 int count; /* number of active filters */
94 struct can_filter dfilter; /* default/single filter */
95 struct can_filter *filter; /* pointer to filter(s) */
96 can_err_mask_t err_mask;
97 struct uniqframe __percpu *uniq;
100 static LIST_HEAD(raw_notifier_list);
101 static DEFINE_SPINLOCK(raw_notifier_lock);
102 static struct raw_sock *raw_busy_notifier;
104 /* Return pointer to store the extra msg flags for raw_recvmsg().
105 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
108 static inline unsigned int *raw_flags(struct sk_buff *skb)
110 sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
111 sizeof(unsigned int));
113 /* return pointer after struct sockaddr_can */
114 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
117 static inline struct raw_sock *raw_sk(const struct sock *sk)
119 return (struct raw_sock *)sk;
122 static void raw_rcv(struct sk_buff *oskb, void *data)
124 struct sock *sk = (struct sock *)data;
125 struct raw_sock *ro = raw_sk(sk);
126 struct sockaddr_can *addr;
128 unsigned int *pflags;
130 /* check the received tx sock reference */
131 if (!ro->recv_own_msgs && oskb->sk == sk)
134 /* make sure to not pass oversized frames to the socket */
135 if ((!ro->fd_frames && can_is_canfd_skb(oskb)) ||
136 (!ro->xl_frames && can_is_canxl_skb(oskb)))
139 /* eliminate multiple filter matches for the same skb */
140 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
141 this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
142 if (!ro->join_filters)
145 this_cpu_inc(ro->uniq->join_rx_count);
146 /* drop frame until all enabled filters matched */
147 if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
150 this_cpu_ptr(ro->uniq)->skb = oskb;
151 this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
152 this_cpu_ptr(ro->uniq)->join_rx_count = 1;
153 /* drop first frame to check all enabled filters? */
154 if (ro->join_filters && ro->count > 1)
158 /* clone the given skb to be able to enqueue it into the rcv queue */
159 skb = skb_clone(oskb, GFP_ATOMIC);
163 /* Put the datagram to the queue so that raw_recvmsg() can get
164 * it from there. We need to pass the interface index to
165 * raw_recvmsg(). We pass a whole struct sockaddr_can in
166 * skb->cb containing the interface index.
169 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
170 addr = (struct sockaddr_can *)skb->cb;
171 memset(addr, 0, sizeof(*addr));
172 addr->can_family = AF_CAN;
173 addr->can_ifindex = skb->dev->ifindex;
175 /* add CAN specific message flags for raw_recvmsg() */
176 pflags = raw_flags(skb);
179 *pflags |= MSG_DONTROUTE;
181 *pflags |= MSG_CONFIRM;
183 if (sock_queue_rcv_skb(sk, skb) < 0)
187 static int raw_enable_filters(struct net *net, struct net_device *dev,
188 struct sock *sk, struct can_filter *filter,
194 for (i = 0; i < count; i++) {
195 err = can_rx_register(net, dev, filter[i].can_id,
197 raw_rcv, sk, "raw", sk);
199 /* clean up successfully registered filters */
201 can_rx_unregister(net, dev, filter[i].can_id,
211 static int raw_enable_errfilter(struct net *net, struct net_device *dev,
212 struct sock *sk, can_err_mask_t err_mask)
217 err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
218 raw_rcv, sk, "raw", sk);
223 static void raw_disable_filters(struct net *net, struct net_device *dev,
224 struct sock *sk, struct can_filter *filter,
229 for (i = 0; i < count; i++)
230 can_rx_unregister(net, dev, filter[i].can_id,
231 filter[i].can_mask, raw_rcv, sk);
234 static inline void raw_disable_errfilter(struct net *net,
235 struct net_device *dev,
237 can_err_mask_t err_mask)
241 can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
245 static inline void raw_disable_allfilters(struct net *net,
246 struct net_device *dev,
249 struct raw_sock *ro = raw_sk(sk);
251 raw_disable_filters(net, dev, sk, ro->filter, ro->count);
252 raw_disable_errfilter(net, dev, sk, ro->err_mask);
255 static int raw_enable_allfilters(struct net *net, struct net_device *dev,
258 struct raw_sock *ro = raw_sk(sk);
261 err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
263 err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
265 raw_disable_filters(net, dev, sk, ro->filter,
272 static void raw_notify(struct raw_sock *ro, unsigned long msg,
273 struct net_device *dev)
275 struct sock *sk = &ro->sk;
277 if (!net_eq(dev_net(dev), sock_net(sk)))
280 if (ro->ifindex != dev->ifindex)
284 case NETDEV_UNREGISTER:
286 /* remove current filters & unregister */
288 raw_disable_allfilters(dev_net(dev), dev, sk);
299 if (!sock_flag(sk, SOCK_DEAD))
304 sk->sk_err = ENETDOWN;
305 if (!sock_flag(sk, SOCK_DEAD))
311 static int raw_notifier(struct notifier_block *nb, unsigned long msg,
314 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
316 if (dev->type != ARPHRD_CAN)
318 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
320 if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
323 spin_lock(&raw_notifier_lock);
324 list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
325 spin_unlock(&raw_notifier_lock);
326 raw_notify(raw_busy_notifier, msg, dev);
327 spin_lock(&raw_notifier_lock);
329 raw_busy_notifier = NULL;
330 spin_unlock(&raw_notifier_lock);
334 static int raw_init(struct sock *sk)
336 struct raw_sock *ro = raw_sk(sk);
341 /* set default filter to single entry dfilter */
342 ro->dfilter.can_id = 0;
343 ro->dfilter.can_mask = MASK_ALL;
344 ro->filter = &ro->dfilter;
347 /* set default loopback behaviour */
349 ro->recv_own_msgs = 0;
352 ro->join_filters = 0;
354 /* alloc_percpu provides zero'ed memory */
355 ro->uniq = alloc_percpu(struct uniqframe);
356 if (unlikely(!ro->uniq))
360 spin_lock(&raw_notifier_lock);
361 list_add_tail(&ro->notifier, &raw_notifier_list);
362 spin_unlock(&raw_notifier_lock);
367 static int raw_release(struct socket *sock)
369 struct sock *sk = sock->sk;
377 spin_lock(&raw_notifier_lock);
378 while (raw_busy_notifier == ro) {
379 spin_unlock(&raw_notifier_lock);
380 schedule_timeout_uninterruptible(1);
381 spin_lock(&raw_notifier_lock);
383 list_del(&ro->notifier);
384 spin_unlock(&raw_notifier_lock);
388 /* remove current filters & unregister */
391 struct net_device *dev;
393 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
395 raw_disable_allfilters(dev_net(dev), dev, sk);
399 raw_disable_allfilters(sock_net(sk), NULL, sk);
409 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);
427 int notify_enetdown = 0;
429 if (len < RAW_MIN_NAMELEN)
431 if (addr->can_family != AF_CAN)
436 if (ro->bound && addr->can_ifindex == ro->ifindex)
439 if (addr->can_ifindex) {
440 struct net_device *dev;
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 struct net_device *dev;
473 dev = dev_get_by_index(sock_net(sk),
476 raw_disable_allfilters(dev_net(dev),
481 raw_disable_allfilters(sock_net(sk), NULL, sk);
484 ro->ifindex = ifindex;
491 if (notify_enetdown) {
492 sk->sk_err = ENETDOWN;
493 if (!sock_flag(sk, SOCK_DEAD))
500 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
503 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
504 struct sock *sk = sock->sk;
505 struct raw_sock *ro = raw_sk(sk);
510 memset(addr, 0, RAW_MIN_NAMELEN);
511 addr->can_family = AF_CAN;
512 addr->can_ifindex = ro->ifindex;
514 return RAW_MIN_NAMELEN;
517 static int raw_setsockopt(struct socket *sock, int level, int optname,
518 sockptr_t optval, unsigned int optlen)
520 struct sock *sk = sock->sk;
521 struct raw_sock *ro = raw_sk(sk);
522 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
523 struct can_filter sfilter; /* single filter */
524 struct net_device *dev = NULL;
525 can_err_mask_t err_mask = 0;
529 if (level != SOL_CAN_RAW)
534 if (optlen % sizeof(struct can_filter) != 0)
537 if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
540 count = optlen / sizeof(struct can_filter);
543 /* filter does not fit into dfilter => alloc space */
544 filter = memdup_sockptr(optval, optlen);
546 return PTR_ERR(filter);
547 } else if (count == 1) {
548 if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
555 if (ro->bound && ro->ifindex) {
556 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
566 /* (try to) register the new filters */
568 err = raw_enable_filters(sock_net(sk), dev, sk,
571 err = raw_enable_filters(sock_net(sk), dev, sk,
579 /* remove old filter registrations */
580 raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
584 /* remove old filter space */
588 /* link new filters to the socket */
590 /* copy filter data for single filter */
591 ro->dfilter = sfilter;
592 filter = &ro->dfilter;
604 case CAN_RAW_ERR_FILTER:
605 if (optlen != sizeof(err_mask))
608 if (copy_from_sockptr(&err_mask, optval, optlen))
611 err_mask &= CAN_ERR_MASK;
616 if (ro->bound && ro->ifindex) {
617 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
624 /* remove current error mask */
626 /* (try to) register the new err_mask */
627 err = raw_enable_errfilter(sock_net(sk), dev, sk,
633 /* remove old err_mask registration */
634 raw_disable_errfilter(sock_net(sk), dev, sk,
638 /* link new err_mask to the socket */
639 ro->err_mask = err_mask;
648 case CAN_RAW_LOOPBACK:
649 if (optlen != sizeof(ro->loopback))
652 if (copy_from_sockptr(&ro->loopback, optval, optlen))
657 case CAN_RAW_RECV_OWN_MSGS:
658 if (optlen != sizeof(ro->recv_own_msgs))
661 if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
666 case CAN_RAW_FD_FRAMES:
667 if (optlen != sizeof(ro->fd_frames))
670 if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
673 /* Enabling CAN XL includes CAN FD */
674 if (ro->xl_frames && !ro->fd_frames) {
675 ro->fd_frames = ro->xl_frames;
680 case CAN_RAW_XL_FRAMES:
681 if (optlen != sizeof(ro->xl_frames))
684 if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
687 /* Enabling CAN XL includes CAN FD */
689 ro->fd_frames = ro->xl_frames;
692 case CAN_RAW_JOIN_FILTERS:
693 if (optlen != sizeof(ro->join_filters))
696 if (copy_from_sockptr(&ro->join_filters, optval, optlen))
707 static int raw_getsockopt(struct socket *sock, int level, int optname,
708 char __user *optval, int __user *optlen)
710 struct sock *sk = sock->sk;
711 struct raw_sock *ro = raw_sk(sk);
716 if (level != SOL_CAN_RAW)
718 if (get_user(len, optlen))
727 int fsize = ro->count * sizeof(struct can_filter);
729 /* user space buffer to small for filter list? */
731 /* return -ERANGE and needed space in optlen */
733 if (put_user(fsize, optlen))
738 if (copy_to_user(optval, ro->filter, len))
747 err = put_user(len, optlen);
750 case CAN_RAW_ERR_FILTER:
751 if (len > sizeof(can_err_mask_t))
752 len = sizeof(can_err_mask_t);
756 case CAN_RAW_LOOPBACK:
757 if (len > sizeof(int))
762 case CAN_RAW_RECV_OWN_MSGS:
763 if (len > sizeof(int))
765 val = &ro->recv_own_msgs;
768 case CAN_RAW_FD_FRAMES:
769 if (len > sizeof(int))
771 val = &ro->fd_frames;
774 case CAN_RAW_XL_FRAMES:
775 if (len > sizeof(int))
777 val = &ro->xl_frames;
780 case CAN_RAW_JOIN_FILTERS:
781 if (len > sizeof(int))
783 val = &ro->join_filters;
790 if (put_user(len, optlen))
792 if (copy_to_user(optval, val, len))
797 static bool raw_bad_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
799 /* Classical CAN -> no checks for flags and device capabilities */
800 if (can_is_can_skb(skb))
803 /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
804 if (ro->fd_frames && can_is_canfd_skb(skb) &&
805 (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
808 /* CAN XL -> needs to be enabled and a CAN XL device */
809 if (ro->xl_frames && can_is_canxl_skb(skb) &&
810 can_is_canxl_dev_mtu(mtu))
816 static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
818 struct sock *sk = sock->sk;
819 struct raw_sock *ro = raw_sk(sk);
820 struct sockcm_cookie sockc;
822 struct net_device *dev;
826 /* check for valid CAN frame sizes */
827 if (size < CANXL_HDR_SIZE + CANXL_MIN_DLEN || size > CANXL_MTU)
831 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
833 if (msg->msg_namelen < RAW_MIN_NAMELEN)
836 if (addr->can_family != AF_CAN)
839 ifindex = addr->can_ifindex;
841 ifindex = ro->ifindex;
844 dev = dev_get_by_index(sock_net(sk), ifindex);
848 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
849 msg->msg_flags & MSG_DONTWAIT, &err);
853 can_skb_reserve(skb);
854 can_skb_prv(skb)->ifindex = dev->ifindex;
855 can_skb_prv(skb)->skbcnt = 0;
857 /* fill the skb before testing for valid CAN frames */
858 err = memcpy_from_msg(skb_put(skb, size), msg, size);
863 if (raw_bad_txframe(ro, skb, dev->mtu))
866 sockcm_init(&sockc, sk);
867 if (msg->msg_controllen) {
868 err = sock_cmsg_send(sk, msg, &sockc);
874 skb->priority = sk->sk_priority;
875 skb->tstamp = sockc.transmit_time;
877 skb_setup_tx_timestamp(skb, sockc.tsflags);
879 err = can_send(skb, ro->loopback);
896 static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
899 struct sock *sk = sock->sk;
903 if (flags & MSG_ERRQUEUE)
904 return sock_recv_errqueue(sk, msg, size,
905 SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
907 skb = skb_recv_datagram(sk, flags, &err);
912 msg->msg_flags |= MSG_TRUNC;
916 err = memcpy_to_msg(msg, skb->data, size);
918 skb_free_datagram(sk, skb);
922 sock_recv_cmsgs(msg, sk, skb);
925 __sockaddr_check_size(RAW_MIN_NAMELEN);
926 msg->msg_namelen = RAW_MIN_NAMELEN;
927 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
930 /* assign the flags that have been recorded in raw_rcv() */
931 msg->msg_flags |= *(raw_flags(skb));
933 skb_free_datagram(sk, skb);
938 static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
941 /* no ioctls for socket layer -> hand it down to NIC layer */
945 static const struct proto_ops raw_ops = {
947 .release = raw_release,
949 .connect = sock_no_connect,
950 .socketpair = sock_no_socketpair,
951 .accept = sock_no_accept,
952 .getname = raw_getname,
953 .poll = datagram_poll,
954 .ioctl = raw_sock_no_ioctlcmd,
955 .gettstamp = sock_gettstamp,
956 .listen = sock_no_listen,
957 .shutdown = sock_no_shutdown,
958 .setsockopt = raw_setsockopt,
959 .getsockopt = raw_getsockopt,
960 .sendmsg = raw_sendmsg,
961 .recvmsg = raw_recvmsg,
962 .mmap = sock_no_mmap,
963 .sendpage = sock_no_sendpage,
966 static struct proto raw_proto __read_mostly = {
968 .owner = THIS_MODULE,
969 .obj_size = sizeof(struct raw_sock),
973 static const struct can_proto raw_can_proto = {
980 static struct notifier_block canraw_notifier = {
981 .notifier_call = raw_notifier
984 static __init int raw_module_init(void)
988 pr_info("can: raw protocol\n");
990 err = register_netdevice_notifier(&canraw_notifier);
994 err = can_proto_register(&raw_can_proto);
996 pr_err("can: registration of raw protocol failed\n");
997 goto register_proto_failed;
1002 register_proto_failed:
1003 unregister_netdevice_notifier(&canraw_notifier);
1007 static __exit void raw_module_exit(void)
1009 can_proto_unregister(&raw_can_proto);
1010 unregister_netdevice_notifier(&canraw_notifier);
1013 module_init(raw_module_init);
1014 module_exit(raw_module_exit);