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/skb.h>
54 #include <linux/can/raw.h>
56 #include <net/net_namespace.h>
58 MODULE_DESCRIPTION("PF_CAN raw protocol");
59 MODULE_LICENSE("Dual BSD/GPL");
60 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
61 MODULE_ALIAS("can-proto-1");
63 #define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
67 /* A raw socket has a list of can_filters attached to it, each receiving
68 * the CAN frames matching that filter. If the filter list is empty,
69 * no CAN frames will be received by the socket. The default after
70 * opening the socket, is to have one filter which receives all frames.
71 * The filter list is allocated dynamically with the exception of the
72 * list containing only one item. This common case is optimized by
73 * storing the single filter in dfilter, to avoid using dynamic memory.
78 const struct sk_buff *skb;
79 unsigned int join_rx_count;
86 struct list_head notifier;
91 int count; /* number of active filters */
92 struct can_filter dfilter; /* default/single filter */
93 struct can_filter *filter; /* pointer to filter(s) */
94 can_err_mask_t err_mask;
95 struct uniqframe __percpu *uniq;
98 static LIST_HEAD(raw_notifier_list);
99 static DEFINE_SPINLOCK(raw_notifier_lock);
100 static struct raw_sock *raw_busy_notifier;
102 /* Return pointer to store the extra msg flags for raw_recvmsg().
103 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
106 static inline unsigned int *raw_flags(struct sk_buff *skb)
108 sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
109 sizeof(unsigned int));
111 /* return pointer after struct sockaddr_can */
112 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
115 static inline struct raw_sock *raw_sk(const struct sock *sk)
117 return (struct raw_sock *)sk;
120 static void raw_rcv(struct sk_buff *oskb, void *data)
122 struct sock *sk = (struct sock *)data;
123 struct raw_sock *ro = raw_sk(sk);
124 struct sockaddr_can *addr;
126 unsigned int *pflags;
128 /* check the received tx sock reference */
129 if (!ro->recv_own_msgs && oskb->sk == sk)
132 /* do not pass non-CAN2.0 frames to a legacy socket */
133 if (!ro->fd_frames && oskb->len != CAN_MTU)
136 /* eliminate multiple filter matches for the same skb */
137 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
138 this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
139 if (ro->join_filters) {
140 this_cpu_inc(ro->uniq->join_rx_count);
141 /* drop frame until all enabled filters matched */
142 if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
148 this_cpu_ptr(ro->uniq)->skb = oskb;
149 this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
150 this_cpu_ptr(ro->uniq)->join_rx_count = 1;
151 /* drop first frame to check all enabled filters? */
152 if (ro->join_filters && ro->count > 1)
156 /* clone the given skb to be able to enqueue it into the rcv queue */
157 skb = skb_clone(oskb, GFP_ATOMIC);
161 /* Put the datagram to the queue so that raw_recvmsg() can get
162 * it from there. We need to pass the interface index to
163 * raw_recvmsg(). We pass a whole struct sockaddr_can in
164 * skb->cb containing the interface index.
167 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
168 addr = (struct sockaddr_can *)skb->cb;
169 memset(addr, 0, sizeof(*addr));
170 addr->can_family = AF_CAN;
171 addr->can_ifindex = skb->dev->ifindex;
173 /* add CAN specific message flags for raw_recvmsg() */
174 pflags = raw_flags(skb);
177 *pflags |= MSG_DONTROUTE;
179 *pflags |= MSG_CONFIRM;
181 if (sock_queue_rcv_skb(sk, skb) < 0)
185 static int raw_enable_filters(struct net *net, struct net_device *dev,
186 struct sock *sk, struct can_filter *filter,
192 for (i = 0; i < count; i++) {
193 err = can_rx_register(net, dev, filter[i].can_id,
195 raw_rcv, sk, "raw", sk);
197 /* clean up successfully registered filters */
199 can_rx_unregister(net, dev, filter[i].can_id,
209 static int raw_enable_errfilter(struct net *net, struct net_device *dev,
210 struct sock *sk, can_err_mask_t err_mask)
215 err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
216 raw_rcv, sk, "raw", sk);
221 static void raw_disable_filters(struct net *net, struct net_device *dev,
222 struct sock *sk, struct can_filter *filter,
227 for (i = 0; i < count; i++)
228 can_rx_unregister(net, dev, filter[i].can_id,
229 filter[i].can_mask, raw_rcv, sk);
232 static inline void raw_disable_errfilter(struct net *net,
233 struct net_device *dev,
235 can_err_mask_t err_mask)
239 can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
243 static inline void raw_disable_allfilters(struct net *net,
244 struct net_device *dev,
247 struct raw_sock *ro = raw_sk(sk);
249 raw_disable_filters(net, dev, sk, ro->filter, ro->count);
250 raw_disable_errfilter(net, dev, sk, ro->err_mask);
253 static int raw_enable_allfilters(struct net *net, struct net_device *dev,
256 struct raw_sock *ro = raw_sk(sk);
259 err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
261 err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
263 raw_disable_filters(net, dev, sk, ro->filter,
270 static void raw_notify(struct raw_sock *ro, unsigned long msg,
271 struct net_device *dev)
273 struct sock *sk = &ro->sk;
275 if (!net_eq(dev_net(dev), sock_net(sk)))
278 if (ro->ifindex != dev->ifindex)
282 case NETDEV_UNREGISTER:
284 /* remove current filters & unregister */
286 raw_disable_allfilters(dev_net(dev), dev, sk);
297 if (!sock_flag(sk, SOCK_DEAD))
302 sk->sk_err = ENETDOWN;
303 if (!sock_flag(sk, SOCK_DEAD))
309 static int raw_notifier(struct notifier_block *nb, unsigned long msg,
312 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
314 if (dev->type != ARPHRD_CAN)
316 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
318 if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
321 spin_lock(&raw_notifier_lock);
322 list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
323 spin_unlock(&raw_notifier_lock);
324 raw_notify(raw_busy_notifier, msg, dev);
325 spin_lock(&raw_notifier_lock);
327 raw_busy_notifier = NULL;
328 spin_unlock(&raw_notifier_lock);
332 static int raw_init(struct sock *sk)
334 struct raw_sock *ro = raw_sk(sk);
339 /* set default filter to single entry dfilter */
340 ro->dfilter.can_id = 0;
341 ro->dfilter.can_mask = MASK_ALL;
342 ro->filter = &ro->dfilter;
345 /* set default loopback behaviour */
347 ro->recv_own_msgs = 0;
349 ro->join_filters = 0;
351 /* alloc_percpu provides zero'ed memory */
352 ro->uniq = alloc_percpu(struct uniqframe);
353 if (unlikely(!ro->uniq))
357 spin_lock(&raw_notifier_lock);
358 list_add_tail(&ro->notifier, &raw_notifier_list);
359 spin_unlock(&raw_notifier_lock);
364 static int raw_release(struct socket *sock)
366 struct sock *sk = sock->sk;
374 spin_lock(&raw_notifier_lock);
375 while (raw_busy_notifier == ro) {
376 spin_unlock(&raw_notifier_lock);
377 schedule_timeout_uninterruptible(1);
378 spin_lock(&raw_notifier_lock);
380 list_del(&ro->notifier);
381 spin_unlock(&raw_notifier_lock);
385 /* remove current filters & unregister */
388 struct net_device *dev;
390 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
392 raw_disable_allfilters(dev_net(dev), dev, sk);
396 raw_disable_allfilters(sock_net(sk), NULL, sk);
406 free_percpu(ro->uniq);
417 static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
419 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
420 struct sock *sk = sock->sk;
421 struct raw_sock *ro = raw_sk(sk);
424 int notify_enetdown = 0;
426 if (len < RAW_MIN_NAMELEN)
428 if (addr->can_family != AF_CAN)
433 if (ro->bound && addr->can_ifindex == ro->ifindex)
436 if (addr->can_ifindex) {
437 struct net_device *dev;
439 dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
444 if (dev->type != ARPHRD_CAN) {
449 if (!(dev->flags & IFF_UP))
452 ifindex = dev->ifindex;
454 /* filters set by default/setsockopt */
455 err = raw_enable_allfilters(sock_net(sk), dev, sk);
460 /* filters set by default/setsockopt */
461 err = raw_enable_allfilters(sock_net(sk), NULL, sk);
466 /* unregister old filters */
468 struct net_device *dev;
470 dev = dev_get_by_index(sock_net(sk),
473 raw_disable_allfilters(dev_net(dev),
478 raw_disable_allfilters(sock_net(sk), NULL, sk);
481 ro->ifindex = ifindex;
488 if (notify_enetdown) {
489 sk->sk_err = ENETDOWN;
490 if (!sock_flag(sk, SOCK_DEAD))
497 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
500 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
501 struct sock *sk = sock->sk;
502 struct raw_sock *ro = raw_sk(sk);
507 memset(addr, 0, RAW_MIN_NAMELEN);
508 addr->can_family = AF_CAN;
509 addr->can_ifindex = ro->ifindex;
511 return RAW_MIN_NAMELEN;
514 static int raw_setsockopt(struct socket *sock, int level, int optname,
515 sockptr_t optval, unsigned int optlen)
517 struct sock *sk = sock->sk;
518 struct raw_sock *ro = raw_sk(sk);
519 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
520 struct can_filter sfilter; /* single filter */
521 struct net_device *dev = NULL;
522 can_err_mask_t err_mask = 0;
526 if (level != SOL_CAN_RAW)
531 if (optlen % sizeof(struct can_filter) != 0)
534 if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
537 count = optlen / sizeof(struct can_filter);
540 /* filter does not fit into dfilter => alloc space */
541 filter = memdup_sockptr(optval, optlen);
543 return PTR_ERR(filter);
544 } else if (count == 1) {
545 if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
551 if (ro->bound && ro->ifindex)
552 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
555 /* (try to) register the new filters */
557 err = raw_enable_filters(sock_net(sk), dev, sk,
560 err = raw_enable_filters(sock_net(sk), dev, sk,
568 /* remove old filter registrations */
569 raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
573 /* remove old filter space */
577 /* link new filters to the socket */
579 /* copy filter data for single filter */
580 ro->dfilter = sfilter;
581 filter = &ro->dfilter;
594 case CAN_RAW_ERR_FILTER:
595 if (optlen != sizeof(err_mask))
598 if (copy_from_sockptr(&err_mask, optval, optlen))
601 err_mask &= CAN_ERR_MASK;
605 if (ro->bound && ro->ifindex)
606 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
608 /* remove current error mask */
610 /* (try to) register the new err_mask */
611 err = raw_enable_errfilter(sock_net(sk), dev, sk,
617 /* remove old err_mask registration */
618 raw_disable_errfilter(sock_net(sk), dev, sk,
622 /* link new err_mask to the socket */
623 ro->err_mask = err_mask;
633 case CAN_RAW_LOOPBACK:
634 if (optlen != sizeof(ro->loopback))
637 if (copy_from_sockptr(&ro->loopback, optval, optlen))
642 case CAN_RAW_RECV_OWN_MSGS:
643 if (optlen != sizeof(ro->recv_own_msgs))
646 if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
651 case CAN_RAW_FD_FRAMES:
652 if (optlen != sizeof(ro->fd_frames))
655 if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
660 case CAN_RAW_JOIN_FILTERS:
661 if (optlen != sizeof(ro->join_filters))
664 if (copy_from_sockptr(&ro->join_filters, optval, optlen))
675 static int raw_getsockopt(struct socket *sock, int level, int optname,
676 char __user *optval, int __user *optlen)
678 struct sock *sk = sock->sk;
679 struct raw_sock *ro = raw_sk(sk);
684 if (level != SOL_CAN_RAW)
686 if (get_user(len, optlen))
695 int fsize = ro->count * sizeof(struct can_filter);
697 /* user space buffer to small for filter list? */
699 /* return -ERANGE and needed space in optlen */
701 if (put_user(fsize, optlen))
706 if (copy_to_user(optval, ro->filter, len))
715 err = put_user(len, optlen);
718 case CAN_RAW_ERR_FILTER:
719 if (len > sizeof(can_err_mask_t))
720 len = sizeof(can_err_mask_t);
724 case CAN_RAW_LOOPBACK:
725 if (len > sizeof(int))
730 case CAN_RAW_RECV_OWN_MSGS:
731 if (len > sizeof(int))
733 val = &ro->recv_own_msgs;
736 case CAN_RAW_FD_FRAMES:
737 if (len > sizeof(int))
739 val = &ro->fd_frames;
742 case CAN_RAW_JOIN_FILTERS:
743 if (len > sizeof(int))
745 val = &ro->join_filters;
752 if (put_user(len, optlen))
754 if (copy_to_user(optval, val, len))
759 static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
761 struct sock *sk = sock->sk;
762 struct raw_sock *ro = raw_sk(sk);
764 struct net_device *dev;
769 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
771 if (msg->msg_namelen < RAW_MIN_NAMELEN)
774 if (addr->can_family != AF_CAN)
777 ifindex = addr->can_ifindex;
779 ifindex = ro->ifindex;
782 dev = dev_get_by_index(sock_net(sk), ifindex);
787 if (ro->fd_frames && dev->mtu == CANFD_MTU) {
788 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
791 if (unlikely(size != CAN_MTU))
795 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
796 msg->msg_flags & MSG_DONTWAIT, &err);
800 can_skb_reserve(skb);
801 can_skb_prv(skb)->ifindex = dev->ifindex;
802 can_skb_prv(skb)->skbcnt = 0;
804 err = memcpy_from_msg(skb_put(skb, size), msg, size);
808 skb_setup_tx_timestamp(skb, sk->sk_tsflags);
812 skb->priority = sk->sk_priority;
814 err = can_send(skb, ro->loopback);
831 static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
834 struct sock *sk = sock->sk;
839 noblock = flags & MSG_DONTWAIT;
840 flags &= ~MSG_DONTWAIT;
842 if (flags & MSG_ERRQUEUE)
843 return sock_recv_errqueue(sk, msg, size,
844 SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
846 skb = skb_recv_datagram(sk, flags, noblock, &err);
851 msg->msg_flags |= MSG_TRUNC;
855 err = memcpy_to_msg(msg, skb->data, size);
857 skb_free_datagram(sk, skb);
861 sock_recv_ts_and_drops(msg, sk, skb);
864 __sockaddr_check_size(RAW_MIN_NAMELEN);
865 msg->msg_namelen = RAW_MIN_NAMELEN;
866 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
869 /* assign the flags that have been recorded in raw_rcv() */
870 msg->msg_flags |= *(raw_flags(skb));
872 skb_free_datagram(sk, skb);
877 static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
880 /* no ioctls for socket layer -> hand it down to NIC layer */
884 static const struct proto_ops raw_ops = {
886 .release = raw_release,
888 .connect = sock_no_connect,
889 .socketpair = sock_no_socketpair,
890 .accept = sock_no_accept,
891 .getname = raw_getname,
892 .poll = datagram_poll,
893 .ioctl = raw_sock_no_ioctlcmd,
894 .gettstamp = sock_gettstamp,
895 .listen = sock_no_listen,
896 .shutdown = sock_no_shutdown,
897 .setsockopt = raw_setsockopt,
898 .getsockopt = raw_getsockopt,
899 .sendmsg = raw_sendmsg,
900 .recvmsg = raw_recvmsg,
901 .mmap = sock_no_mmap,
902 .sendpage = sock_no_sendpage,
905 static struct proto raw_proto __read_mostly = {
907 .owner = THIS_MODULE,
908 .obj_size = sizeof(struct raw_sock),
912 static const struct can_proto raw_can_proto = {
919 static struct notifier_block canraw_notifier = {
920 .notifier_call = raw_notifier
923 static __init int raw_module_init(void)
927 pr_info("can: raw protocol\n");
929 err = can_proto_register(&raw_can_proto);
931 pr_err("can: registration of raw protocol failed\n");
933 register_netdevice_notifier(&canraw_notifier);
938 static __exit void raw_module_exit(void)
940 can_proto_unregister(&raw_can_proto);
941 unregister_netdevice_notifier(&canraw_notifier);
944 module_init(raw_module_init);
945 module_exit(raw_module_exit);