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
59 static __initconst
const char banner
[] =
60 KERN_INFO
"can: raw protocol (rev " CAN_RAW_VERSION
")\n";
62 MODULE_DESCRIPTION("PF_CAN raw protocol");
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
65 MODULE_ALIAS("can-proto-1");
70 * A raw socket has a list of can_filters attached to it, each receiving
71 * the CAN frames matching that filter. If the filter list is empty,
72 * no CAN frames will be received by the socket. The default after
73 * opening the socket, is to have one filter which receives all frames.
74 * The filter list is allocated dynamically with the exception of the
75 * list containing only one item. This common case is optimized by
76 * storing the single filter in dfilter, to avoid using dynamic memory.
83 struct notifier_block notifier
;
87 int count
; /* number of active filters */
88 struct can_filter dfilter
; /* default/single filter */
89 struct can_filter
*filter
; /* pointer to filter(s) */
90 can_err_mask_t err_mask
;
94 * Return pointer to store the extra msg flags for raw_recvmsg().
95 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
98 static inline unsigned int *raw_flags(struct sk_buff
*skb
)
100 BUILD_BUG_ON(sizeof(skb
->cb
) <= (sizeof(struct sockaddr_can
) +
101 sizeof(unsigned int)));
103 /* return pointer after struct sockaddr_can */
104 return (unsigned int *)(&((struct sockaddr_can
*)skb
->cb
)[1]);
107 static inline struct raw_sock
*raw_sk(const struct sock
*sk
)
109 return (struct raw_sock
*)sk
;
112 static void raw_rcv(struct sk_buff
*oskb
, void *data
)
114 struct sock
*sk
= (struct sock
*)data
;
115 struct raw_sock
*ro
= raw_sk(sk
);
116 struct sockaddr_can
*addr
;
118 unsigned int *pflags
;
120 /* check the received tx sock reference */
121 if (!ro
->recv_own_msgs
&& oskb
->sk
== sk
)
124 /* do not pass frames with DLC > 8 to a legacy socket */
125 if (!ro
->fd_frames
) {
126 struct canfd_frame
*cfd
= (struct canfd_frame
*)oskb
->data
;
128 if (unlikely(cfd
->len
> CAN_MAX_DLEN
))
132 /* clone the given skb to be able to enqueue it into the rcv queue */
133 skb
= skb_clone(oskb
, GFP_ATOMIC
);
138 * Put the datagram to the queue so that raw_recvmsg() can
139 * get it from there. We need to pass the interface index to
140 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
141 * containing the interface index.
144 BUILD_BUG_ON(sizeof(skb
->cb
) < sizeof(struct sockaddr_can
));
145 addr
= (struct sockaddr_can
*)skb
->cb
;
146 memset(addr
, 0, sizeof(*addr
));
147 addr
->can_family
= AF_CAN
;
148 addr
->can_ifindex
= skb
->dev
->ifindex
;
150 /* add CAN specific message flags for raw_recvmsg() */
151 pflags
= raw_flags(skb
);
154 *pflags
|= MSG_DONTROUTE
;
156 *pflags
|= MSG_CONFIRM
;
158 if (sock_queue_rcv_skb(sk
, skb
) < 0)
162 static int raw_enable_filters(struct net_device
*dev
, struct sock
*sk
,
163 struct can_filter
*filter
, int count
)
168 for (i
= 0; i
< count
; i
++) {
169 err
= can_rx_register(dev
, filter
[i
].can_id
,
173 /* clean up successfully registered filters */
175 can_rx_unregister(dev
, filter
[i
].can_id
,
185 static int raw_enable_errfilter(struct net_device
*dev
, struct sock
*sk
,
186 can_err_mask_t err_mask
)
191 err
= can_rx_register(dev
, 0, err_mask
| CAN_ERR_FLAG
,
197 static void raw_disable_filters(struct net_device
*dev
, struct sock
*sk
,
198 struct can_filter
*filter
, int count
)
202 for (i
= 0; i
< count
; i
++)
203 can_rx_unregister(dev
, filter
[i
].can_id
, filter
[i
].can_mask
,
207 static inline void raw_disable_errfilter(struct net_device
*dev
,
209 can_err_mask_t err_mask
)
213 can_rx_unregister(dev
, 0, err_mask
| CAN_ERR_FLAG
,
217 static inline void raw_disable_allfilters(struct net_device
*dev
,
220 struct raw_sock
*ro
= raw_sk(sk
);
222 raw_disable_filters(dev
, sk
, ro
->filter
, ro
->count
);
223 raw_disable_errfilter(dev
, sk
, ro
->err_mask
);
226 static int raw_enable_allfilters(struct net_device
*dev
, struct sock
*sk
)
228 struct raw_sock
*ro
= raw_sk(sk
);
231 err
= raw_enable_filters(dev
, sk
, ro
->filter
, ro
->count
);
233 err
= raw_enable_errfilter(dev
, sk
, ro
->err_mask
);
235 raw_disable_filters(dev
, sk
, ro
->filter
, ro
->count
);
241 static int raw_notifier(struct notifier_block
*nb
,
242 unsigned long msg
, void *ptr
)
244 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
245 struct raw_sock
*ro
= container_of(nb
, struct raw_sock
, notifier
);
246 struct sock
*sk
= &ro
->sk
;
248 if (!net_eq(dev_net(dev
), &init_net
))
251 if (dev
->type
!= ARPHRD_CAN
)
254 if (ro
->ifindex
!= dev
->ifindex
)
259 case NETDEV_UNREGISTER
:
261 /* remove current filters & unregister */
263 raw_disable_allfilters(dev
, sk
);
274 if (!sock_flag(sk
, SOCK_DEAD
))
275 sk
->sk_error_report(sk
);
279 sk
->sk_err
= ENETDOWN
;
280 if (!sock_flag(sk
, SOCK_DEAD
))
281 sk
->sk_error_report(sk
);
288 static int raw_init(struct sock
*sk
)
290 struct raw_sock
*ro
= raw_sk(sk
);
295 /* set default filter to single entry dfilter */
296 ro
->dfilter
.can_id
= 0;
297 ro
->dfilter
.can_mask
= MASK_ALL
;
298 ro
->filter
= &ro
->dfilter
;
301 /* set default loopback behaviour */
303 ro
->recv_own_msgs
= 0;
307 ro
->notifier
.notifier_call
= raw_notifier
;
309 register_netdevice_notifier(&ro
->notifier
);
314 static int raw_release(struct socket
*sock
)
316 struct sock
*sk
= sock
->sk
;
324 unregister_netdevice_notifier(&ro
->notifier
);
328 /* remove current filters & unregister */
331 struct net_device
*dev
;
333 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
335 raw_disable_allfilters(dev
, sk
);
339 raw_disable_allfilters(NULL
, sk
);
358 static int raw_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int len
)
360 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
361 struct sock
*sk
= sock
->sk
;
362 struct raw_sock
*ro
= raw_sk(sk
);
365 int notify_enetdown
= 0;
367 if (len
< sizeof(*addr
))
372 if (ro
->bound
&& addr
->can_ifindex
== ro
->ifindex
)
375 if (addr
->can_ifindex
) {
376 struct net_device
*dev
;
378 dev
= dev_get_by_index(&init_net
, addr
->can_ifindex
);
383 if (dev
->type
!= ARPHRD_CAN
) {
388 if (!(dev
->flags
& IFF_UP
))
391 ifindex
= dev
->ifindex
;
393 /* filters set by default/setsockopt */
394 err
= raw_enable_allfilters(dev
, sk
);
399 /* filters set by default/setsockopt */
400 err
= raw_enable_allfilters(NULL
, sk
);
405 /* unregister old filters */
407 struct net_device
*dev
;
409 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
411 raw_disable_allfilters(dev
, sk
);
415 raw_disable_allfilters(NULL
, sk
);
417 ro
->ifindex
= ifindex
;
424 if (notify_enetdown
) {
425 sk
->sk_err
= ENETDOWN
;
426 if (!sock_flag(sk
, SOCK_DEAD
))
427 sk
->sk_error_report(sk
);
433 static int raw_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
436 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
437 struct sock
*sk
= sock
->sk
;
438 struct raw_sock
*ro
= raw_sk(sk
);
443 memset(addr
, 0, sizeof(*addr
));
444 addr
->can_family
= AF_CAN
;
445 addr
->can_ifindex
= ro
->ifindex
;
447 *len
= sizeof(*addr
);
452 static int raw_setsockopt(struct socket
*sock
, int level
, int optname
,
453 char __user
*optval
, unsigned int optlen
)
455 struct sock
*sk
= sock
->sk
;
456 struct raw_sock
*ro
= raw_sk(sk
);
457 struct can_filter
*filter
= NULL
; /* dyn. alloc'ed filters */
458 struct can_filter sfilter
; /* single filter */
459 struct net_device
*dev
= NULL
;
460 can_err_mask_t err_mask
= 0;
464 if (level
!= SOL_CAN_RAW
)
470 if (optlen
% sizeof(struct can_filter
) != 0)
473 count
= optlen
/ sizeof(struct can_filter
);
476 /* filter does not fit into dfilter => alloc space */
477 filter
= memdup_user(optval
, optlen
);
479 return PTR_ERR(filter
);
480 } else if (count
== 1) {
481 if (copy_from_user(&sfilter
, optval
, sizeof(sfilter
)))
487 if (ro
->bound
&& ro
->ifindex
)
488 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
491 /* (try to) register the new filters */
493 err
= raw_enable_filters(dev
, sk
, &sfilter
, 1);
495 err
= raw_enable_filters(dev
, sk
, filter
,
503 /* remove old filter registrations */
504 raw_disable_filters(dev
, sk
, ro
->filter
, ro
->count
);
507 /* remove old filter space */
511 /* link new filters to the socket */
513 /* copy filter data for single filter */
514 ro
->dfilter
= sfilter
;
515 filter
= &ro
->dfilter
;
528 case CAN_RAW_ERR_FILTER
:
529 if (optlen
!= sizeof(err_mask
))
532 if (copy_from_user(&err_mask
, optval
, optlen
))
535 err_mask
&= CAN_ERR_MASK
;
539 if (ro
->bound
&& ro
->ifindex
)
540 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
542 /* remove current error mask */
544 /* (try to) register the new err_mask */
545 err
= raw_enable_errfilter(dev
, sk
, err_mask
);
550 /* remove old err_mask registration */
551 raw_disable_errfilter(dev
, sk
, ro
->err_mask
);
554 /* link new err_mask to the socket */
555 ro
->err_mask
= err_mask
;
565 case CAN_RAW_LOOPBACK
:
566 if (optlen
!= sizeof(ro
->loopback
))
569 if (copy_from_user(&ro
->loopback
, optval
, optlen
))
574 case CAN_RAW_RECV_OWN_MSGS
:
575 if (optlen
!= sizeof(ro
->recv_own_msgs
))
578 if (copy_from_user(&ro
->recv_own_msgs
, optval
, optlen
))
583 case CAN_RAW_FD_FRAMES
:
584 if (optlen
!= sizeof(ro
->fd_frames
))
587 if (copy_from_user(&ro
->fd_frames
, optval
, optlen
))
598 static int raw_getsockopt(struct socket
*sock
, int level
, int optname
,
599 char __user
*optval
, int __user
*optlen
)
601 struct sock
*sk
= sock
->sk
;
602 struct raw_sock
*ro
= raw_sk(sk
);
607 if (level
!= SOL_CAN_RAW
)
609 if (get_user(len
, optlen
))
619 int fsize
= ro
->count
* sizeof(struct can_filter
);
622 if (copy_to_user(optval
, ro
->filter
, len
))
629 err
= put_user(len
, optlen
);
632 case CAN_RAW_ERR_FILTER
:
633 if (len
> sizeof(can_err_mask_t
))
634 len
= sizeof(can_err_mask_t
);
638 case CAN_RAW_LOOPBACK
:
639 if (len
> sizeof(int))
644 case CAN_RAW_RECV_OWN_MSGS
:
645 if (len
> sizeof(int))
647 val
= &ro
->recv_own_msgs
;
650 case CAN_RAW_FD_FRAMES
:
651 if (len
> sizeof(int))
653 val
= &ro
->fd_frames
;
660 if (put_user(len
, optlen
))
662 if (copy_to_user(optval
, val
, len
))
667 static int raw_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
668 struct msghdr
*msg
, size_t size
)
670 struct sock
*sk
= sock
->sk
;
671 struct raw_sock
*ro
= raw_sk(sk
);
673 struct net_device
*dev
;
678 struct sockaddr_can
*addr
=
679 (struct sockaddr_can
*)msg
->msg_name
;
681 if (msg
->msg_namelen
< sizeof(*addr
))
684 if (addr
->can_family
!= AF_CAN
)
687 ifindex
= addr
->can_ifindex
;
689 ifindex
= ro
->ifindex
;
692 if (unlikely(size
!= CANFD_MTU
&& size
!= CAN_MTU
))
695 if (unlikely(size
!= CAN_MTU
))
699 dev
= dev_get_by_index(&init_net
, ifindex
);
703 skb
= sock_alloc_send_skb(sk
, size
+ sizeof(struct can_skb_priv
),
704 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
708 can_skb_reserve(skb
);
709 can_skb_prv(skb
)->ifindex
= dev
->ifindex
;
711 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
715 sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
720 err
= can_send(skb
, ro
->loopback
);
737 static int raw_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
738 struct msghdr
*msg
, size_t size
, int flags
)
740 struct sock
*sk
= sock
->sk
;
741 struct raw_sock
*ro
= raw_sk(sk
);
747 noblock
= flags
& MSG_DONTWAIT
;
748 flags
&= ~MSG_DONTWAIT
;
750 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
755 * when serving a legacy socket the DLC <= 8 is already checked inside
756 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
757 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
765 msg
->msg_flags
|= MSG_TRUNC
;
769 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
);
771 skb_free_datagram(sk
, skb
);
775 sock_recv_ts_and_drops(msg
, sk
, skb
);
778 msg
->msg_namelen
= sizeof(struct sockaddr_can
);
779 memcpy(msg
->msg_name
, skb
->cb
, msg
->msg_namelen
);
782 /* assign the flags that have been recorded in raw_rcv() */
783 msg
->msg_flags
|= *(raw_flags(skb
));
785 skb_free_datagram(sk
, skb
);
790 static const struct proto_ops raw_ops
= {
792 .release
= raw_release
,
794 .connect
= sock_no_connect
,
795 .socketpair
= sock_no_socketpair
,
796 .accept
= sock_no_accept
,
797 .getname
= raw_getname
,
798 .poll
= datagram_poll
,
799 .ioctl
= can_ioctl
, /* use can_ioctl() from af_can.c */
800 .listen
= sock_no_listen
,
801 .shutdown
= sock_no_shutdown
,
802 .setsockopt
= raw_setsockopt
,
803 .getsockopt
= raw_getsockopt
,
804 .sendmsg
= raw_sendmsg
,
805 .recvmsg
= raw_recvmsg
,
806 .mmap
= sock_no_mmap
,
807 .sendpage
= sock_no_sendpage
,
810 static struct proto raw_proto __read_mostly
= {
812 .owner
= THIS_MODULE
,
813 .obj_size
= sizeof(struct raw_sock
),
817 static const struct can_proto raw_can_proto
= {
824 static __init
int raw_module_init(void)
830 err
= can_proto_register(&raw_can_proto
);
832 printk(KERN_ERR
"can: registration of raw protocol failed\n");
837 static __exit
void raw_module_exit(void)
839 can_proto_unregister(&raw_can_proto
);
842 module_init(raw_module_init
);
843 module_exit(raw_module_exit
);