dm thin: wake worker when discard is prepared
[linux-2.6.git] / net / can / raw.c
blob5b0e3e330d97c6f4172bf52db70a4cee6b7f4247
1 /*
2 * raw.c - Raw sockets for protocol family CAN
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
38 * DAMAGE.
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/raw.h>
54 #include <net/sock.h>
55 #include <net/net_namespace.h>
57 #define CAN_RAW_VERSION CAN_VERSION
58 static __initconst const char banner[] =
59 KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
61 MODULE_DESCRIPTION("PF_CAN raw protocol");
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
64 MODULE_ALIAS("can-proto-1");
66 #define MASK_ALL 0
69 * A raw socket has a list of can_filters attached to it, each receiving
70 * the CAN frames matching that filter. If the filter list is empty,
71 * no CAN frames will be received by the socket. The default after
72 * opening the socket, is to have one filter which receives all frames.
73 * The filter list is allocated dynamically with the exception of the
74 * list containing only one item. This common case is optimized by
75 * storing the single filter in dfilter, to avoid using dynamic memory.
78 struct raw_sock {
79 struct sock sk;
80 int bound;
81 int ifindex;
82 struct notifier_block notifier;
83 int loopback;
84 int recv_own_msgs;
85 int fd_frames;
86 int count; /* number of active filters */
87 struct can_filter dfilter; /* default/single filter */
88 struct can_filter *filter; /* pointer to filter(s) */
89 can_err_mask_t err_mask;
93 * Return pointer to store the extra msg flags for raw_recvmsg().
94 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
95 * in skb->cb.
97 static inline unsigned int *raw_flags(struct sk_buff *skb)
99 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
100 sizeof(unsigned int)));
102 /* return pointer after struct sockaddr_can */
103 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
106 static inline struct raw_sock *raw_sk(const struct sock *sk)
108 return (struct raw_sock *)sk;
111 static void raw_rcv(struct sk_buff *oskb, void *data)
113 struct sock *sk = (struct sock *)data;
114 struct raw_sock *ro = raw_sk(sk);
115 struct sockaddr_can *addr;
116 struct sk_buff *skb;
117 unsigned int *pflags;
119 /* check the received tx sock reference */
120 if (!ro->recv_own_msgs && oskb->sk == sk)
121 return;
123 /* do not pass frames with DLC > 8 to a legacy socket */
124 if (!ro->fd_frames) {
125 struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;
127 if (unlikely(cfd->len > CAN_MAX_DLEN))
128 return;
131 /* clone the given skb to be able to enqueue it into the rcv queue */
132 skb = skb_clone(oskb, GFP_ATOMIC);
133 if (!skb)
134 return;
137 * Put the datagram to the queue so that raw_recvmsg() can
138 * get it from there. We need to pass the interface index to
139 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
140 * containing the interface index.
143 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
144 addr = (struct sockaddr_can *)skb->cb;
145 memset(addr, 0, sizeof(*addr));
146 addr->can_family = AF_CAN;
147 addr->can_ifindex = skb->dev->ifindex;
149 /* add CAN specific message flags for raw_recvmsg() */
150 pflags = raw_flags(skb);
151 *pflags = 0;
152 if (oskb->sk)
153 *pflags |= MSG_DONTROUTE;
154 if (oskb->sk == sk)
155 *pflags |= MSG_CONFIRM;
157 if (sock_queue_rcv_skb(sk, skb) < 0)
158 kfree_skb(skb);
161 static int raw_enable_filters(struct net_device *dev, struct sock *sk,
162 struct can_filter *filter, int count)
164 int err = 0;
165 int i;
167 for (i = 0; i < count; i++) {
168 err = can_rx_register(dev, filter[i].can_id,
169 filter[i].can_mask,
170 raw_rcv, sk, "raw");
171 if (err) {
172 /* clean up successfully registered filters */
173 while (--i >= 0)
174 can_rx_unregister(dev, filter[i].can_id,
175 filter[i].can_mask,
176 raw_rcv, sk);
177 break;
181 return err;
184 static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
185 can_err_mask_t err_mask)
187 int err = 0;
189 if (err_mask)
190 err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
191 raw_rcv, sk, "raw");
193 return err;
196 static void raw_disable_filters(struct net_device *dev, struct sock *sk,
197 struct can_filter *filter, int count)
199 int i;
201 for (i = 0; i < count; i++)
202 can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
203 raw_rcv, sk);
206 static inline void raw_disable_errfilter(struct net_device *dev,
207 struct sock *sk,
208 can_err_mask_t err_mask)
211 if (err_mask)
212 can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
213 raw_rcv, sk);
216 static inline void raw_disable_allfilters(struct net_device *dev,
217 struct sock *sk)
219 struct raw_sock *ro = raw_sk(sk);
221 raw_disable_filters(dev, sk, ro->filter, ro->count);
222 raw_disable_errfilter(dev, sk, ro->err_mask);
225 static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
227 struct raw_sock *ro = raw_sk(sk);
228 int err;
230 err = raw_enable_filters(dev, sk, ro->filter, ro->count);
231 if (!err) {
232 err = raw_enable_errfilter(dev, sk, ro->err_mask);
233 if (err)
234 raw_disable_filters(dev, sk, ro->filter, ro->count);
237 return err;
240 static int raw_notifier(struct notifier_block *nb,
241 unsigned long msg, void *data)
243 struct net_device *dev = (struct net_device *)data;
244 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
245 struct sock *sk = &ro->sk;
247 if (!net_eq(dev_net(dev), &init_net))
248 return NOTIFY_DONE;
250 if (dev->type != ARPHRD_CAN)
251 return NOTIFY_DONE;
253 if (ro->ifindex != dev->ifindex)
254 return NOTIFY_DONE;
256 switch (msg) {
258 case NETDEV_UNREGISTER:
259 lock_sock(sk);
260 /* remove current filters & unregister */
261 if (ro->bound)
262 raw_disable_allfilters(dev, sk);
264 if (ro->count > 1)
265 kfree(ro->filter);
267 ro->ifindex = 0;
268 ro->bound = 0;
269 ro->count = 0;
270 release_sock(sk);
272 sk->sk_err = ENODEV;
273 if (!sock_flag(sk, SOCK_DEAD))
274 sk->sk_error_report(sk);
275 break;
277 case NETDEV_DOWN:
278 sk->sk_err = ENETDOWN;
279 if (!sock_flag(sk, SOCK_DEAD))
280 sk->sk_error_report(sk);
281 break;
284 return NOTIFY_DONE;
287 static int raw_init(struct sock *sk)
289 struct raw_sock *ro = raw_sk(sk);
291 ro->bound = 0;
292 ro->ifindex = 0;
294 /* set default filter to single entry dfilter */
295 ro->dfilter.can_id = 0;
296 ro->dfilter.can_mask = MASK_ALL;
297 ro->filter = &ro->dfilter;
298 ro->count = 1;
300 /* set default loopback behaviour */
301 ro->loopback = 1;
302 ro->recv_own_msgs = 0;
303 ro->fd_frames = 0;
305 /* set notifier */
306 ro->notifier.notifier_call = raw_notifier;
308 register_netdevice_notifier(&ro->notifier);
310 return 0;
313 static int raw_release(struct socket *sock)
315 struct sock *sk = sock->sk;
316 struct raw_sock *ro;
318 if (!sk)
319 return 0;
321 ro = raw_sk(sk);
323 unregister_netdevice_notifier(&ro->notifier);
325 lock_sock(sk);
327 /* remove current filters & unregister */
328 if (ro->bound) {
329 if (ro->ifindex) {
330 struct net_device *dev;
332 dev = dev_get_by_index(&init_net, ro->ifindex);
333 if (dev) {
334 raw_disable_allfilters(dev, sk);
335 dev_put(dev);
337 } else
338 raw_disable_allfilters(NULL, sk);
341 if (ro->count > 1)
342 kfree(ro->filter);
344 ro->ifindex = 0;
345 ro->bound = 0;
346 ro->count = 0;
348 sock_orphan(sk);
349 sock->sk = NULL;
351 release_sock(sk);
352 sock_put(sk);
354 return 0;
357 static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
359 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
360 struct sock *sk = sock->sk;
361 struct raw_sock *ro = raw_sk(sk);
362 int ifindex;
363 int err = 0;
364 int notify_enetdown = 0;
366 if (len < sizeof(*addr))
367 return -EINVAL;
369 lock_sock(sk);
371 if (ro->bound && addr->can_ifindex == ro->ifindex)
372 goto out;
374 if (addr->can_ifindex) {
375 struct net_device *dev;
377 dev = dev_get_by_index(&init_net, addr->can_ifindex);
378 if (!dev) {
379 err = -ENODEV;
380 goto out;
382 if (dev->type != ARPHRD_CAN) {
383 dev_put(dev);
384 err = -ENODEV;
385 goto out;
387 if (!(dev->flags & IFF_UP))
388 notify_enetdown = 1;
390 ifindex = dev->ifindex;
392 /* filters set by default/setsockopt */
393 err = raw_enable_allfilters(dev, sk);
394 dev_put(dev);
395 } else {
396 ifindex = 0;
398 /* filters set by default/setsockopt */
399 err = raw_enable_allfilters(NULL, sk);
402 if (!err) {
403 if (ro->bound) {
404 /* unregister old filters */
405 if (ro->ifindex) {
406 struct net_device *dev;
408 dev = dev_get_by_index(&init_net, ro->ifindex);
409 if (dev) {
410 raw_disable_allfilters(dev, sk);
411 dev_put(dev);
413 } else
414 raw_disable_allfilters(NULL, sk);
416 ro->ifindex = ifindex;
417 ro->bound = 1;
420 out:
421 release_sock(sk);
423 if (notify_enetdown) {
424 sk->sk_err = ENETDOWN;
425 if (!sock_flag(sk, SOCK_DEAD))
426 sk->sk_error_report(sk);
429 return err;
432 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
433 int *len, int peer)
435 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
436 struct sock *sk = sock->sk;
437 struct raw_sock *ro = raw_sk(sk);
439 if (peer)
440 return -EOPNOTSUPP;
442 memset(addr, 0, sizeof(*addr));
443 addr->can_family = AF_CAN;
444 addr->can_ifindex = ro->ifindex;
446 *len = sizeof(*addr);
448 return 0;
451 static int raw_setsockopt(struct socket *sock, int level, int optname,
452 char __user *optval, unsigned int optlen)
454 struct sock *sk = sock->sk;
455 struct raw_sock *ro = raw_sk(sk);
456 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
457 struct can_filter sfilter; /* single filter */
458 struct net_device *dev = NULL;
459 can_err_mask_t err_mask = 0;
460 int count = 0;
461 int err = 0;
463 if (level != SOL_CAN_RAW)
464 return -EINVAL;
466 switch (optname) {
468 case CAN_RAW_FILTER:
469 if (optlen % sizeof(struct can_filter) != 0)
470 return -EINVAL;
472 count = optlen / sizeof(struct can_filter);
474 if (count > 1) {
475 /* filter does not fit into dfilter => alloc space */
476 filter = memdup_user(optval, optlen);
477 if (IS_ERR(filter))
478 return PTR_ERR(filter);
479 } else if (count == 1) {
480 if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
481 return -EFAULT;
484 lock_sock(sk);
486 if (ro->bound && ro->ifindex)
487 dev = dev_get_by_index(&init_net, ro->ifindex);
489 if (ro->bound) {
490 /* (try to) register the new filters */
491 if (count == 1)
492 err = raw_enable_filters(dev, sk, &sfilter, 1);
493 else
494 err = raw_enable_filters(dev, sk, filter,
495 count);
496 if (err) {
497 if (count > 1)
498 kfree(filter);
499 goto out_fil;
502 /* remove old filter registrations */
503 raw_disable_filters(dev, sk, ro->filter, ro->count);
506 /* remove old filter space */
507 if (ro->count > 1)
508 kfree(ro->filter);
510 /* link new filters to the socket */
511 if (count == 1) {
512 /* copy filter data for single filter */
513 ro->dfilter = sfilter;
514 filter = &ro->dfilter;
516 ro->filter = filter;
517 ro->count = count;
519 out_fil:
520 if (dev)
521 dev_put(dev);
523 release_sock(sk);
525 break;
527 case CAN_RAW_ERR_FILTER:
528 if (optlen != sizeof(err_mask))
529 return -EINVAL;
531 if (copy_from_user(&err_mask, optval, optlen))
532 return -EFAULT;
534 err_mask &= CAN_ERR_MASK;
536 lock_sock(sk);
538 if (ro->bound && ro->ifindex)
539 dev = dev_get_by_index(&init_net, ro->ifindex);
541 /* remove current error mask */
542 if (ro->bound) {
543 /* (try to) register the new err_mask */
544 err = raw_enable_errfilter(dev, sk, err_mask);
546 if (err)
547 goto out_err;
549 /* remove old err_mask registration */
550 raw_disable_errfilter(dev, sk, ro->err_mask);
553 /* link new err_mask to the socket */
554 ro->err_mask = err_mask;
556 out_err:
557 if (dev)
558 dev_put(dev);
560 release_sock(sk);
562 break;
564 case CAN_RAW_LOOPBACK:
565 if (optlen != sizeof(ro->loopback))
566 return -EINVAL;
568 if (copy_from_user(&ro->loopback, optval, optlen))
569 return -EFAULT;
571 break;
573 case CAN_RAW_RECV_OWN_MSGS:
574 if (optlen != sizeof(ro->recv_own_msgs))
575 return -EINVAL;
577 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
578 return -EFAULT;
580 break;
582 case CAN_RAW_FD_FRAMES:
583 if (optlen != sizeof(ro->fd_frames))
584 return -EINVAL;
586 if (copy_from_user(&ro->fd_frames, optval, optlen))
587 return -EFAULT;
589 break;
591 default:
592 return -ENOPROTOOPT;
594 return err;
597 static int raw_getsockopt(struct socket *sock, int level, int optname,
598 char __user *optval, int __user *optlen)
600 struct sock *sk = sock->sk;
601 struct raw_sock *ro = raw_sk(sk);
602 int len;
603 void *val;
604 int err = 0;
606 if (level != SOL_CAN_RAW)
607 return -EINVAL;
608 if (get_user(len, optlen))
609 return -EFAULT;
610 if (len < 0)
611 return -EINVAL;
613 switch (optname) {
615 case CAN_RAW_FILTER:
616 lock_sock(sk);
617 if (ro->count > 0) {
618 int fsize = ro->count * sizeof(struct can_filter);
619 if (len > fsize)
620 len = fsize;
621 if (copy_to_user(optval, ro->filter, len))
622 err = -EFAULT;
623 } else
624 len = 0;
625 release_sock(sk);
627 if (!err)
628 err = put_user(len, optlen);
629 return err;
631 case CAN_RAW_ERR_FILTER:
632 if (len > sizeof(can_err_mask_t))
633 len = sizeof(can_err_mask_t);
634 val = &ro->err_mask;
635 break;
637 case CAN_RAW_LOOPBACK:
638 if (len > sizeof(int))
639 len = sizeof(int);
640 val = &ro->loopback;
641 break;
643 case CAN_RAW_RECV_OWN_MSGS:
644 if (len > sizeof(int))
645 len = sizeof(int);
646 val = &ro->recv_own_msgs;
647 break;
649 case CAN_RAW_FD_FRAMES:
650 if (len > sizeof(int))
651 len = sizeof(int);
652 val = &ro->fd_frames;
653 break;
655 default:
656 return -ENOPROTOOPT;
659 if (put_user(len, optlen))
660 return -EFAULT;
661 if (copy_to_user(optval, val, len))
662 return -EFAULT;
663 return 0;
666 static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
667 struct msghdr *msg, size_t size)
669 struct sock *sk = sock->sk;
670 struct raw_sock *ro = raw_sk(sk);
671 struct sk_buff *skb;
672 struct net_device *dev;
673 int ifindex;
674 int err;
676 if (msg->msg_name) {
677 struct sockaddr_can *addr =
678 (struct sockaddr_can *)msg->msg_name;
680 if (msg->msg_namelen < sizeof(*addr))
681 return -EINVAL;
683 if (addr->can_family != AF_CAN)
684 return -EINVAL;
686 ifindex = addr->can_ifindex;
687 } else
688 ifindex = ro->ifindex;
690 if (ro->fd_frames) {
691 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
692 return -EINVAL;
693 } else {
694 if (unlikely(size != CAN_MTU))
695 return -EINVAL;
698 dev = dev_get_by_index(&init_net, ifindex);
699 if (!dev)
700 return -ENXIO;
702 skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
703 &err);
704 if (!skb)
705 goto put_dev;
707 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
708 if (err < 0)
709 goto free_skb;
710 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
711 if (err < 0)
712 goto free_skb;
714 skb->dev = dev;
715 skb->sk = sk;
717 err = can_send(skb, ro->loopback);
719 dev_put(dev);
721 if (err)
722 goto send_failed;
724 return size;
726 free_skb:
727 kfree_skb(skb);
728 put_dev:
729 dev_put(dev);
730 send_failed:
731 return err;
734 static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
735 struct msghdr *msg, size_t size, int flags)
737 struct sock *sk = sock->sk;
738 struct raw_sock *ro = raw_sk(sk);
739 struct sk_buff *skb;
740 int rxmtu;
741 int err = 0;
742 int noblock;
744 noblock = flags & MSG_DONTWAIT;
745 flags &= ~MSG_DONTWAIT;
747 skb = skb_recv_datagram(sk, flags, noblock, &err);
748 if (!skb)
749 return err;
752 * when serving a legacy socket the DLC <= 8 is already checked inside
753 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
754 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
756 if (!ro->fd_frames)
757 rxmtu = CAN_MTU;
758 else
759 rxmtu = skb->len;
761 if (size < rxmtu)
762 msg->msg_flags |= MSG_TRUNC;
763 else
764 size = rxmtu;
766 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
767 if (err < 0) {
768 skb_free_datagram(sk, skb);
769 return err;
772 sock_recv_ts_and_drops(msg, sk, skb);
774 if (msg->msg_name) {
775 msg->msg_namelen = sizeof(struct sockaddr_can);
776 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
779 /* assign the flags that have been recorded in raw_rcv() */
780 msg->msg_flags |= *(raw_flags(skb));
782 skb_free_datagram(sk, skb);
784 return size;
787 static const struct proto_ops raw_ops = {
788 .family = PF_CAN,
789 .release = raw_release,
790 .bind = raw_bind,
791 .connect = sock_no_connect,
792 .socketpair = sock_no_socketpair,
793 .accept = sock_no_accept,
794 .getname = raw_getname,
795 .poll = datagram_poll,
796 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
797 .listen = sock_no_listen,
798 .shutdown = sock_no_shutdown,
799 .setsockopt = raw_setsockopt,
800 .getsockopt = raw_getsockopt,
801 .sendmsg = raw_sendmsg,
802 .recvmsg = raw_recvmsg,
803 .mmap = sock_no_mmap,
804 .sendpage = sock_no_sendpage,
807 static struct proto raw_proto __read_mostly = {
808 .name = "CAN_RAW",
809 .owner = THIS_MODULE,
810 .obj_size = sizeof(struct raw_sock),
811 .init = raw_init,
814 static const struct can_proto raw_can_proto = {
815 .type = SOCK_RAW,
816 .protocol = CAN_RAW,
817 .ops = &raw_ops,
818 .prot = &raw_proto,
821 static __init int raw_module_init(void)
823 int err;
825 printk(banner);
827 err = can_proto_register(&raw_can_proto);
828 if (err < 0)
829 printk(KERN_ERR "can: registration of raw protocol failed\n");
831 return err;
834 static __exit void raw_module_exit(void)
836 can_proto_unregister(&raw_can_proto);
839 module_init(raw_module_init);
840 module_exit(raw_module_exit);