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
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/uio.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/socket.h>
51 #include <linux/if_arp.h>
52 #include <linux/skbuff.h>
53 #include <socketcan/can.h>
54 #include <socketcan/can/core.h>
55 #include <socketcan/can/raw.h>
57 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
58 #include <net/net_namespace.h>
60 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
64 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
67 #define CAN_RAW_VERSION CAN_VERSION
68 static __initdata const char banner[] =
69 KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
71 MODULE_DESCRIPTION("PF_CAN raw protocol");
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
74 MODULE_ALIAS("can-proto-1");
79 * A raw socket has a list of can_filters attached to it, each receiving
80 * the CAN frames matching that filter. If the filter list is empty,
81 * no CAN frames will be received by the socket. The default after
82 * opening the socket, is to have one filter which receives all frames.
83 * The filter list is allocated dynamically with the exception of the
84 * list containing only one item. This common case is optimized by
85 * storing the single filter in dfilter, to avoid using dynamic memory.
89 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
96 struct notifier_block notifier;
99 int count; /* number of active filters */
100 struct can_filter dfilter; /* default/single filter */
101 struct can_filter *filter; /* pointer to filter(s) */
102 can_err_mask_t err_mask;
106 * Return pointer to store the extra msg flags for raw_recvmsg().
107 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
110 static inline unsigned int *raw_flags(struct sk_buff *skb)
112 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
113 sizeof(unsigned int)));
115 /* return pointer after struct sockaddr_can */
116 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
119 static inline struct raw_sock *raw_sk(const struct sock *sk)
121 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
122 return (struct raw_sock *)sk;
124 return (struct raw_sock *)sk->sk_protinfo;
128 static void raw_rcv(struct sk_buff *oskb, void *data)
130 struct sock *sk = (struct sock *)data;
131 struct raw_sock *ro = raw_sk(sk);
132 struct sockaddr_can *addr;
134 unsigned int *pflags;
136 /* check the received tx sock reference */
137 if (!ro->recv_own_msgs && oskb->sk == sk)
140 /* clone the given skb to be able to enqueue it into the rcv queue */
141 skb = skb_clone(oskb, GFP_ATOMIC);
146 * Put the datagram to the queue so that raw_recvmsg() can
147 * get it from there. We need to pass the interface index to
148 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
149 * containing the interface index.
152 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
153 addr = (struct sockaddr_can *)skb->cb;
154 memset(addr, 0, sizeof(*addr));
155 addr->can_family = AF_CAN;
156 addr->can_ifindex = skb->dev->ifindex;
158 /* add CAN specific message flags for raw_recvmsg() */
159 pflags = raw_flags(skb);
162 *pflags |= MSG_DONTROUTE;
164 *pflags |= MSG_CONFIRM;
166 if (sock_queue_rcv_skb(sk, skb) < 0)
170 static int raw_enable_filters(struct net_device *dev, struct sock *sk,
171 struct can_filter *filter, int count)
176 for (i = 0; i < count; i++) {
177 err = can_rx_register(dev, filter[i].can_id,
181 /* clean up successfully registered filters */
183 can_rx_unregister(dev, filter[i].can_id,
193 static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
194 can_err_mask_t err_mask)
199 err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
205 static void raw_disable_filters(struct net_device *dev, struct sock *sk,
206 struct can_filter *filter, int count)
210 for (i = 0; i < count; i++)
211 can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
215 static inline void raw_disable_errfilter(struct net_device *dev,
217 can_err_mask_t err_mask)
221 can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
225 static inline void raw_disable_allfilters(struct net_device *dev,
228 struct raw_sock *ro = raw_sk(sk);
230 raw_disable_filters(dev, sk, ro->filter, ro->count);
231 raw_disable_errfilter(dev, sk, ro->err_mask);
234 static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
236 struct raw_sock *ro = raw_sk(sk);
239 err = raw_enable_filters(dev, sk, ro->filter, ro->count);
241 err = raw_enable_errfilter(dev, sk, ro->err_mask);
243 raw_disable_filters(dev, sk, ro->filter, ro->count);
249 static int raw_notifier(struct notifier_block *nb,
250 unsigned long msg, void *data)
252 struct net_device *dev = (struct net_device *)data;
253 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
254 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
255 struct sock *sk = &ro->sk;
257 struct sock *sk = ro->sk;
260 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
261 if (!net_eq(dev_net(dev), &init_net))
263 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
264 if (dev->nd_net != &init_net)
268 if (dev->type != ARPHRD_CAN)
271 if (ro->ifindex != dev->ifindex)
276 case NETDEV_UNREGISTER:
278 /* remove current filters & unregister */
280 raw_disable_allfilters(dev, sk);
291 if (!sock_flag(sk, SOCK_DEAD))
292 sk->sk_error_report(sk);
296 sk->sk_err = ENETDOWN;
297 if (!sock_flag(sk, SOCK_DEAD))
298 sk->sk_error_report(sk);
305 static int raw_init(struct sock *sk)
307 struct raw_sock *ro = raw_sk(sk);
309 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
315 /* set default filter to single entry dfilter */
316 ro->dfilter.can_id = 0;
317 ro->dfilter.can_mask = MASK_ALL;
318 ro->filter = &ro->dfilter;
321 /* set default loopback behaviour */
323 ro->recv_own_msgs = 0;
326 ro->notifier.notifier_call = raw_notifier;
328 register_netdevice_notifier(&ro->notifier);
333 static int raw_release(struct socket *sock)
335 struct sock *sk = sock->sk;
336 struct raw_sock *ro = raw_sk(sk);
338 unregister_netdevice_notifier(&ro->notifier);
342 /* remove current filters & unregister */
345 struct net_device *dev;
347 dev = dev_get_by_index(&init_net, ro->ifindex);
349 raw_disable_allfilters(dev, sk);
353 raw_disable_allfilters(NULL, sk);
372 static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
374 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
375 struct sock *sk = sock->sk;
376 struct raw_sock *ro = raw_sk(sk);
379 int notify_enetdown = 0;
381 if (len < sizeof(*addr))
386 if (ro->bound && addr->can_ifindex == ro->ifindex)
389 if (addr->can_ifindex) {
390 struct net_device *dev;
392 dev = dev_get_by_index(&init_net, addr->can_ifindex);
397 if (dev->type != ARPHRD_CAN) {
402 if (!(dev->flags & IFF_UP))
405 ifindex = dev->ifindex;
407 /* filters set by default/setsockopt */
408 err = raw_enable_allfilters(dev, sk);
413 /* filters set by default/setsockopt */
414 err = raw_enable_allfilters(NULL, sk);
419 /* unregister old filters */
421 struct net_device *dev;
423 dev = dev_get_by_index(&init_net, ro->ifindex);
425 raw_disable_allfilters(dev, sk);
429 raw_disable_allfilters(NULL, sk);
431 ro->ifindex = ifindex;
438 if (notify_enetdown) {
439 sk->sk_err = ENETDOWN;
440 if (!sock_flag(sk, SOCK_DEAD))
441 sk->sk_error_report(sk);
447 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
450 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
451 struct sock *sk = sock->sk;
452 struct raw_sock *ro = raw_sk(sk);
457 addr->can_family = AF_CAN;
458 addr->can_ifindex = ro->ifindex;
460 *len = sizeof(*addr);
465 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
466 static int raw_setsockopt(struct socket *sock, int level, int optname,
467 char __user *optval, unsigned int optlen)
469 static int raw_setsockopt(struct socket *sock, int level, int optname,
470 char __user *optval, int optlen)
473 struct sock *sk = sock->sk;
474 struct raw_sock *ro = raw_sk(sk);
475 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
476 struct can_filter sfilter; /* single filter */
477 struct net_device *dev = NULL;
478 can_err_mask_t err_mask = 0;
482 if (level != SOL_CAN_RAW)
490 if (optlen % sizeof(struct can_filter) != 0)
493 count = optlen / sizeof(struct can_filter);
496 /* filter does not fit into dfilter => alloc space */
497 filter = kmalloc(optlen, GFP_KERNEL);
501 if (copy_from_user(filter, optval, optlen)) {
505 } else if (count == 1) {
506 if (copy_from_user(&sfilter, optval, optlen))
512 if (ro->bound && ro->ifindex)
513 dev = dev_get_by_index(&init_net, ro->ifindex);
516 /* (try to) register the new filters */
518 err = raw_enable_filters(dev, sk, &sfilter, 1);
520 err = raw_enable_filters(dev, sk, filter,
528 /* remove old filter registrations */
529 raw_disable_filters(dev, sk, ro->filter, ro->count);
532 /* remove old filter space */
536 /* link new filters to the socket */
538 /* copy filter data for single filter */
539 ro->dfilter = sfilter;
540 filter = &ro->dfilter;
553 case CAN_RAW_ERR_FILTER:
554 if (optlen != sizeof(err_mask))
557 if (copy_from_user(&err_mask, optval, optlen))
560 err_mask &= CAN_ERR_MASK;
564 if (ro->bound && ro->ifindex)
565 dev = dev_get_by_index(&init_net, ro->ifindex);
567 /* remove current error mask */
569 /* (try to) register the new err_mask */
570 err = raw_enable_errfilter(dev, sk, err_mask);
575 /* remove old err_mask registration */
576 raw_disable_errfilter(dev, sk, ro->err_mask);
579 /* link new err_mask to the socket */
580 ro->err_mask = err_mask;
590 case CAN_RAW_LOOPBACK:
591 if (optlen != sizeof(ro->loopback))
594 if (copy_from_user(&ro->loopback, optval, optlen))
599 case CAN_RAW_RECV_OWN_MSGS:
600 if (optlen != sizeof(ro->recv_own_msgs))
603 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
614 static int raw_getsockopt(struct socket *sock, int level, int optname,
615 char __user *optval, int __user *optlen)
617 struct sock *sk = sock->sk;
618 struct raw_sock *ro = raw_sk(sk);
623 if (level != SOL_CAN_RAW)
625 if (get_user(len, optlen))
635 int fsize = ro->count * sizeof(struct can_filter);
638 if (copy_to_user(optval, ro->filter, len))
645 err = put_user(len, optlen);
648 case CAN_RAW_ERR_FILTER:
649 if (len > sizeof(can_err_mask_t))
650 len = sizeof(can_err_mask_t);
654 case CAN_RAW_LOOPBACK:
655 if (len > sizeof(int))
660 case CAN_RAW_RECV_OWN_MSGS:
661 if (len > sizeof(int))
663 val = &ro->recv_own_msgs;
670 if (put_user(len, optlen))
672 if (copy_to_user(optval, val, len))
677 static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
678 struct msghdr *msg, size_t size)
680 struct sock *sk = sock->sk;
681 struct raw_sock *ro = raw_sk(sk);
683 struct net_device *dev;
688 struct sockaddr_can *addr =
689 (struct sockaddr_can *)msg->msg_name;
691 if (addr->can_family != AF_CAN)
694 ifindex = addr->can_ifindex;
696 ifindex = ro->ifindex;
698 if (size != sizeof(struct can_frame))
701 dev = dev_get_by_index(&init_net, ifindex);
705 skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
710 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
716 err = can_send(skb, ro->loopback);
733 static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
734 struct msghdr *msg, size_t size, int flags)
736 struct sock *sk = sock->sk;
741 noblock = flags & MSG_DONTWAIT;
742 flags &= ~MSG_DONTWAIT;
744 skb = skb_recv_datagram(sk, flags, noblock, &err);
749 msg->msg_flags |= MSG_TRUNC;
753 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
755 skb_free_datagram(sk, skb);
759 sock_recv_timestamp(msg, sk, skb);
762 msg->msg_namelen = sizeof(struct sockaddr_can);
763 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
766 /* assign the flags that have been recorded in raw_rcv() */
767 msg->msg_flags |= *(raw_flags(skb));
769 skb_free_datagram(sk, skb);
774 static struct proto_ops raw_ops __read_mostly = {
776 .release = raw_release,
778 .connect = sock_no_connect,
779 .socketpair = sock_no_socketpair,
780 .accept = sock_no_accept,
781 .getname = raw_getname,
782 .poll = datagram_poll,
783 .ioctl = NULL, /* use can_ioctl() from af_can.c */
784 .listen = sock_no_listen,
785 .shutdown = sock_no_shutdown,
786 .setsockopt = raw_setsockopt,
787 .getsockopt = raw_getsockopt,
788 .sendmsg = raw_sendmsg,
789 .recvmsg = raw_recvmsg,
790 .mmap = sock_no_mmap,
791 .sendpage = sock_no_sendpage,
795 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
796 static struct proto raw_proto __read_mostly = {
798 .owner = THIS_MODULE,
799 .obj_size = sizeof(struct raw_sock),
803 static struct can_proto raw_can_proto __read_mostly = {
809 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
810 static struct proto raw_proto __read_mostly = {
812 .owner = THIS_MODULE,
813 .obj_size = sizeof(struct raw_sock),
817 static struct can_proto raw_can_proto __read_mostly = {
825 static struct can_proto raw_can_proto __read_mostly = {
830 .owner = THIS_MODULE,
831 .obj_size = sizeof(struct raw_sock),
836 static __init int raw_module_init(void)
842 err = can_proto_register(&raw_can_proto);
844 printk(KERN_ERR "can: registration of raw protocol failed\n");
849 static __exit void raw_module_exit(void)
851 can_proto_unregister(&raw_can_proto);
854 module_init(raw_module_init);
855 module_exit(raw_module_exit);