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 #define CAN_RAW_VERSION CAN_VERSION
60 MODULE_DESCRIPTION("PF_CAN raw protocol");
61 MODULE_LICENSE("Dual BSD/GPL");
62 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
63 MODULE_ALIAS("can-proto-1");
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;
92 int count; /* number of active filters */
93 struct can_filter dfilter; /* default/single filter */
94 struct can_filter *filter; /* pointer to filter(s) */
95 can_err_mask_t err_mask;
96 struct uniqframe __percpu *uniq;
99 static LIST_HEAD(raw_notifier_list);
100 static DEFINE_SPINLOCK(raw_notifier_lock);
101 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 /* do not pass non-CAN2.0 frames to a legacy socket */
135 if (!ro->fd_frames && oskb->len != CAN_MTU)
138 /* eliminate multiple filter matches for the same skb */
139 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
140 this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
141 if (ro->join_filters) {
142 this_cpu_inc(ro->uniq->join_rx_count);
143 /* drop frame until all enabled filters matched */
144 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);
164 * Put the datagram to the queue so that raw_recvmsg() can
165 * get it from there. We need to pass the interface index to
166 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
167 * 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)))
281 if (ro->ifindex != dev->ifindex)
286 case NETDEV_UNREGISTER:
288 /* remove current filters & unregister */
290 raw_disable_allfilters(dev_net(dev), dev, sk);
301 if (!sock_flag(sk, SOCK_DEAD))
302 sk->sk_error_report(sk);
306 sk->sk_err = ENETDOWN;
307 if (!sock_flag(sk, SOCK_DEAD))
308 sk->sk_error_report(sk);
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);
343 /* set default filter to single entry dfilter */
344 ro->dfilter.can_id = 0;
345 ro->dfilter.can_mask = MASK_ALL;
346 ro->filter = &ro->dfilter;
349 /* set default loopback behaviour */
351 ro->recv_own_msgs = 0;
353 ro->join_filters = 0;
355 /* alloc_percpu provides zero'ed memory */
356 ro->uniq = alloc_percpu(struct uniqframe);
357 if (unlikely(!ro->uniq))
361 spin_lock(&raw_notifier_lock);
362 list_add_tail(&ro->notifier, &raw_notifier_list);
363 spin_unlock(&raw_notifier_lock);
368 static int raw_release(struct socket *sock)
370 struct sock *sk = sock->sk;
378 spin_lock(&raw_notifier_lock);
379 while (raw_busy_notifier == ro) {
380 spin_unlock(&raw_notifier_lock);
381 schedule_timeout_uninterruptible(1);
382 spin_lock(&raw_notifier_lock);
384 list_del(&ro->notifier);
385 spin_unlock(&raw_notifier_lock);
389 /* remove current filters & unregister */
392 struct net_device *dev;
394 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
396 raw_disable_allfilters(dev_net(dev), dev, sk);
400 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 < sizeof(*addr))
434 if (ro->bound && addr->can_ifindex == ro->ifindex)
437 if (addr->can_ifindex) {
438 struct net_device *dev;
440 dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
445 if (dev->type != ARPHRD_CAN) {
450 if (!(dev->flags & IFF_UP))
453 ifindex = dev->ifindex;
455 /* filters set by default/setsockopt */
456 err = raw_enable_allfilters(sock_net(sk), dev, sk);
461 /* filters set by default/setsockopt */
462 err = raw_enable_allfilters(sock_net(sk), NULL, sk);
467 /* unregister old filters */
469 struct net_device *dev;
471 dev = dev_get_by_index(sock_net(sk),
474 raw_disable_allfilters(dev_net(dev),
479 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))
491 sk->sk_error_report(sk);
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, sizeof(*addr));
508 addr->can_family = AF_CAN;
509 addr->can_ifindex = ro->ifindex;
511 *len = sizeof(*addr);
516 static int raw_setsockopt(struct socket *sock, int level, int optname,
517 char __user *optval, unsigned int optlen)
519 struct sock *sk = sock->sk;
520 struct raw_sock *ro = raw_sk(sk);
521 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
522 struct can_filter sfilter; /* single filter */
523 struct net_device *dev = NULL;
524 can_err_mask_t err_mask = 0;
528 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_user(optval, optlen);
546 return PTR_ERR(filter);
547 } else if (count == 1) {
548 if (copy_from_user(&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;
606 case CAN_RAW_ERR_FILTER:
607 if (optlen != sizeof(err_mask))
610 if (copy_from_user(&err_mask, optval, optlen))
613 err_mask &= CAN_ERR_MASK;
618 if (ro->bound && ro->ifindex) {
619 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
626 /* remove current error mask */
628 /* (try to) register the new err_mask */
629 err = raw_enable_errfilter(sock_net(sk), dev, sk,
635 /* remove old err_mask registration */
636 raw_disable_errfilter(sock_net(sk), dev, sk,
640 /* link new err_mask to the socket */
641 ro->err_mask = err_mask;
652 case CAN_RAW_LOOPBACK:
653 if (optlen != sizeof(ro->loopback))
656 if (copy_from_user(&ro->loopback, optval, optlen))
661 case CAN_RAW_RECV_OWN_MSGS:
662 if (optlen != sizeof(ro->recv_own_msgs))
665 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
670 case CAN_RAW_FD_FRAMES:
671 if (optlen != sizeof(ro->fd_frames))
674 if (copy_from_user(&ro->fd_frames, optval, optlen))
679 case CAN_RAW_JOIN_FILTERS:
680 if (optlen != sizeof(ro->join_filters))
683 if (copy_from_user(&ro->join_filters, optval, optlen))
694 static int raw_getsockopt(struct socket *sock, int level, int optname,
695 char __user *optval, int __user *optlen)
697 struct sock *sk = sock->sk;
698 struct raw_sock *ro = raw_sk(sk);
703 if (level != SOL_CAN_RAW)
705 if (get_user(len, optlen))
715 int fsize = ro->count * sizeof(struct can_filter);
718 if (copy_to_user(optval, ro->filter, len))
725 err = put_user(len, optlen);
728 case CAN_RAW_ERR_FILTER:
729 if (len > sizeof(can_err_mask_t))
730 len = sizeof(can_err_mask_t);
734 case CAN_RAW_LOOPBACK:
735 if (len > sizeof(int))
740 case CAN_RAW_RECV_OWN_MSGS:
741 if (len > sizeof(int))
743 val = &ro->recv_own_msgs;
746 case CAN_RAW_FD_FRAMES:
747 if (len > sizeof(int))
749 val = &ro->fd_frames;
752 case CAN_RAW_JOIN_FILTERS:
753 if (len > sizeof(int))
755 val = &ro->join_filters;
762 if (put_user(len, optlen))
764 if (copy_to_user(optval, val, len))
769 static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
771 struct sock *sk = sock->sk;
772 struct raw_sock *ro = raw_sk(sk);
774 struct net_device *dev;
779 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
781 if (msg->msg_namelen < sizeof(*addr))
784 if (addr->can_family != AF_CAN)
787 ifindex = addr->can_ifindex;
789 ifindex = ro->ifindex;
791 dev = dev_get_by_index(sock_net(sk), ifindex);
796 if (ro->fd_frames && dev->mtu == CANFD_MTU) {
797 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
800 if (unlikely(size != CAN_MTU))
804 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
805 msg->msg_flags & MSG_DONTWAIT, &err);
809 can_skb_reserve(skb);
810 can_skb_prv(skb)->ifindex = dev->ifindex;
811 can_skb_prv(skb)->skbcnt = 0;
813 err = memcpy_from_msg(skb_put(skb, size), msg, size);
817 sock_tx_timestamp(sk, sk->sk_tsflags, &skb_shinfo(skb)->tx_flags);
821 skb->priority = sk->sk_priority;
823 err = can_send(skb, ro->loopback);
840 static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
843 struct sock *sk = sock->sk;
848 noblock = flags & MSG_DONTWAIT;
849 flags &= ~MSG_DONTWAIT;
851 skb = skb_recv_datagram(sk, flags, noblock, &err);
856 msg->msg_flags |= MSG_TRUNC;
860 err = memcpy_to_msg(msg, skb->data, size);
862 skb_free_datagram(sk, skb);
866 sock_recv_ts_and_drops(msg, sk, skb);
869 __sockaddr_check_size(sizeof(struct sockaddr_can));
870 msg->msg_namelen = sizeof(struct sockaddr_can);
871 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
874 /* assign the flags that have been recorded in raw_rcv() */
875 msg->msg_flags |= *(raw_flags(skb));
877 skb_free_datagram(sk, skb);
882 static const struct proto_ops raw_ops = {
884 .release = raw_release,
886 .connect = sock_no_connect,
887 .socketpair = sock_no_socketpair,
888 .accept = sock_no_accept,
889 .getname = raw_getname,
890 .poll = datagram_poll,
891 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
892 .listen = sock_no_listen,
893 .shutdown = sock_no_shutdown,
894 .setsockopt = raw_setsockopt,
895 .getsockopt = raw_getsockopt,
896 .sendmsg = raw_sendmsg,
897 .recvmsg = raw_recvmsg,
898 .mmap = sock_no_mmap,
899 .sendpage = sock_no_sendpage,
902 static struct proto raw_proto __read_mostly = {
904 .owner = THIS_MODULE,
905 .obj_size = sizeof(struct raw_sock),
909 static const struct can_proto raw_can_proto = {
916 static struct notifier_block canraw_notifier = {
917 .notifier_call = raw_notifier
920 static __init int raw_module_init(void)
924 pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n");
926 err = can_proto_register(&raw_can_proto);
928 printk(KERN_ERR "can: registration of raw protocol failed\n");
930 register_netdevice_notifier(&canraw_notifier);
935 static __exit void raw_module_exit(void)
937 can_proto_unregister(&raw_can_proto);
938 unregister_netdevice_notifier(&canraw_notifier);
941 module_init(raw_module_init);
942 module_exit(raw_module_exit);