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)))
552 if (ro->bound && ro->ifindex) {
553 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
563 /* (try to) register the new filters */
565 err = raw_enable_filters(sock_net(sk), dev, sk,
568 err = raw_enable_filters(sock_net(sk), dev, sk,
576 /* remove old filter registrations */
577 raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
581 /* remove old filter space */
585 /* link new filters to the socket */
587 /* copy filter data for single filter */
588 ro->dfilter = sfilter;
589 filter = &ro->dfilter;
603 case CAN_RAW_ERR_FILTER:
604 if (optlen != sizeof(err_mask))
607 if (copy_from_sockptr(&err_mask, optval, optlen))
610 err_mask &= CAN_ERR_MASK;
615 if (ro->bound && ro->ifindex) {
616 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
623 /* remove current error mask */
625 /* (try to) register the new err_mask */
626 err = raw_enable_errfilter(sock_net(sk), dev, sk,
632 /* remove old err_mask registration */
633 raw_disable_errfilter(sock_net(sk), dev, sk,
637 /* link new err_mask to the socket */
638 ro->err_mask = err_mask;
649 case CAN_RAW_LOOPBACK:
650 if (optlen != sizeof(ro->loopback))
653 if (copy_from_sockptr(&ro->loopback, optval, optlen))
658 case CAN_RAW_RECV_OWN_MSGS:
659 if (optlen != sizeof(ro->recv_own_msgs))
662 if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
667 case CAN_RAW_FD_FRAMES:
668 if (optlen != sizeof(ro->fd_frames))
671 if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
676 case CAN_RAW_JOIN_FILTERS:
677 if (optlen != sizeof(ro->join_filters))
680 if (copy_from_sockptr(&ro->join_filters, optval, optlen))
691 static int raw_getsockopt(struct socket *sock, int level, int optname,
692 char __user *optval, int __user *optlen)
694 struct sock *sk = sock->sk;
695 struct raw_sock *ro = raw_sk(sk);
700 if (level != SOL_CAN_RAW)
702 if (get_user(len, optlen))
711 int fsize = ro->count * sizeof(struct can_filter);
713 /* user space buffer to small for filter list? */
715 /* return -ERANGE and needed space in optlen */
717 if (put_user(fsize, optlen))
722 if (copy_to_user(optval, ro->filter, len))
731 err = put_user(len, optlen);
734 case CAN_RAW_ERR_FILTER:
735 if (len > sizeof(can_err_mask_t))
736 len = sizeof(can_err_mask_t);
740 case CAN_RAW_LOOPBACK:
741 if (len > sizeof(int))
746 case CAN_RAW_RECV_OWN_MSGS:
747 if (len > sizeof(int))
749 val = &ro->recv_own_msgs;
752 case CAN_RAW_FD_FRAMES:
753 if (len > sizeof(int))
755 val = &ro->fd_frames;
758 case CAN_RAW_JOIN_FILTERS:
759 if (len > sizeof(int))
761 val = &ro->join_filters;
768 if (put_user(len, optlen))
770 if (copy_to_user(optval, val, len))
775 static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
777 struct sock *sk = sock->sk;
778 struct raw_sock *ro = raw_sk(sk);
780 struct net_device *dev;
785 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
787 if (msg->msg_namelen < RAW_MIN_NAMELEN)
790 if (addr->can_family != AF_CAN)
793 ifindex = addr->can_ifindex;
795 ifindex = ro->ifindex;
798 dev = dev_get_by_index(sock_net(sk), ifindex);
803 if (ro->fd_frames && dev->mtu == CANFD_MTU) {
804 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
807 if (unlikely(size != CAN_MTU))
811 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
812 msg->msg_flags & MSG_DONTWAIT, &err);
816 can_skb_reserve(skb);
817 can_skb_prv(skb)->ifindex = dev->ifindex;
818 can_skb_prv(skb)->skbcnt = 0;
820 err = memcpy_from_msg(skb_put(skb, size), msg, size);
824 skb_setup_tx_timestamp(skb, sk->sk_tsflags);
828 skb->priority = sk->sk_priority;
830 err = can_send(skb, ro->loopback);
847 static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
850 struct sock *sk = sock->sk;
855 noblock = flags & MSG_DONTWAIT;
856 flags &= ~MSG_DONTWAIT;
858 if (flags & MSG_ERRQUEUE)
859 return sock_recv_errqueue(sk, msg, size,
860 SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
862 skb = skb_recv_datagram(sk, flags, noblock, &err);
867 msg->msg_flags |= MSG_TRUNC;
871 err = memcpy_to_msg(msg, skb->data, size);
873 skb_free_datagram(sk, skb);
877 sock_recv_ts_and_drops(msg, sk, skb);
880 __sockaddr_check_size(RAW_MIN_NAMELEN);
881 msg->msg_namelen = RAW_MIN_NAMELEN;
882 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
885 /* assign the flags that have been recorded in raw_rcv() */
886 msg->msg_flags |= *(raw_flags(skb));
888 skb_free_datagram(sk, skb);
893 static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
896 /* no ioctls for socket layer -> hand it down to NIC layer */
900 static const struct proto_ops raw_ops = {
902 .release = raw_release,
904 .connect = sock_no_connect,
905 .socketpair = sock_no_socketpair,
906 .accept = sock_no_accept,
907 .getname = raw_getname,
908 .poll = datagram_poll,
909 .ioctl = raw_sock_no_ioctlcmd,
910 .gettstamp = sock_gettstamp,
911 .listen = sock_no_listen,
912 .shutdown = sock_no_shutdown,
913 .setsockopt = raw_setsockopt,
914 .getsockopt = raw_getsockopt,
915 .sendmsg = raw_sendmsg,
916 .recvmsg = raw_recvmsg,
917 .mmap = sock_no_mmap,
918 .sendpage = sock_no_sendpage,
921 static struct proto raw_proto __read_mostly = {
923 .owner = THIS_MODULE,
924 .obj_size = sizeof(struct raw_sock),
928 static const struct can_proto raw_can_proto = {
935 static struct notifier_block canraw_notifier = {
936 .notifier_call = raw_notifier
939 static __init int raw_module_init(void)
943 pr_info("can: raw protocol\n");
945 err = can_proto_register(&raw_can_proto);
947 pr_err("can: registration of raw protocol failed\n");
949 register_netdevice_notifier(&canraw_notifier);
954 static __exit void raw_module_exit(void)
956 can_proto_unregister(&raw_can_proto);
957 unregister_netdevice_notifier(&canraw_notifier);
960 module_init(raw_module_init);
961 module_exit(raw_module_exit);