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/raw.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");
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.
82 struct notifier_block notifier
;
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'
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
;
117 unsigned int *pflags
;
119 /* check the received tx sock reference */
120 if (!ro
->recv_own_msgs
&& oskb
->sk
== sk
)
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
))
131 /* clone the given skb to be able to enqueue it into the rcv queue */
132 skb
= skb_clone(oskb
, GFP_ATOMIC
);
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
);
153 *pflags
|= MSG_DONTROUTE
;
155 *pflags
|= MSG_CONFIRM
;
157 if (sock_queue_rcv_skb(sk
, skb
) < 0)
161 static int raw_enable_filters(struct net_device
*dev
, struct sock
*sk
,
162 struct can_filter
*filter
, int count
)
167 for (i
= 0; i
< count
; i
++) {
168 err
= can_rx_register(dev
, filter
[i
].can_id
,
172 /* clean up successfully registered filters */
174 can_rx_unregister(dev
, filter
[i
].can_id
,
184 static int raw_enable_errfilter(struct net_device
*dev
, struct sock
*sk
,
185 can_err_mask_t err_mask
)
190 err
= can_rx_register(dev
, 0, err_mask
| CAN_ERR_FLAG
,
196 static void raw_disable_filters(struct net_device
*dev
, struct sock
*sk
,
197 struct can_filter
*filter
, int count
)
201 for (i
= 0; i
< count
; i
++)
202 can_rx_unregister(dev
, filter
[i
].can_id
, filter
[i
].can_mask
,
206 static inline void raw_disable_errfilter(struct net_device
*dev
,
208 can_err_mask_t err_mask
)
212 can_rx_unregister(dev
, 0, err_mask
| CAN_ERR_FLAG
,
216 static inline void raw_disable_allfilters(struct net_device
*dev
,
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
);
230 err
= raw_enable_filters(dev
, sk
, ro
->filter
, ro
->count
);
232 err
= raw_enable_errfilter(dev
, sk
, ro
->err_mask
);
234 raw_disable_filters(dev
, sk
, ro
->filter
, ro
->count
);
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
))
250 if (dev
->type
!= ARPHRD_CAN
)
253 if (ro
->ifindex
!= dev
->ifindex
)
258 case NETDEV_UNREGISTER
:
260 /* remove current filters & unregister */
262 raw_disable_allfilters(dev
, sk
);
273 if (!sock_flag(sk
, SOCK_DEAD
))
274 sk
->sk_error_report(sk
);
278 sk
->sk_err
= ENETDOWN
;
279 if (!sock_flag(sk
, SOCK_DEAD
))
280 sk
->sk_error_report(sk
);
287 static int raw_init(struct sock
*sk
)
289 struct raw_sock
*ro
= raw_sk(sk
);
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
;
300 /* set default loopback behaviour */
302 ro
->recv_own_msgs
= 0;
306 ro
->notifier
.notifier_call
= raw_notifier
;
308 register_netdevice_notifier(&ro
->notifier
);
313 static int raw_release(struct socket
*sock
)
315 struct sock
*sk
= sock
->sk
;
323 unregister_netdevice_notifier(&ro
->notifier
);
327 /* remove current filters & unregister */
330 struct net_device
*dev
;
332 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
334 raw_disable_allfilters(dev
, sk
);
338 raw_disable_allfilters(NULL
, sk
);
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
);
364 int notify_enetdown
= 0;
366 if (len
< sizeof(*addr
))
371 if (ro
->bound
&& addr
->can_ifindex
== ro
->ifindex
)
374 if (addr
->can_ifindex
) {
375 struct net_device
*dev
;
377 dev
= dev_get_by_index(&init_net
, addr
->can_ifindex
);
382 if (dev
->type
!= ARPHRD_CAN
) {
387 if (!(dev
->flags
& IFF_UP
))
390 ifindex
= dev
->ifindex
;
392 /* filters set by default/setsockopt */
393 err
= raw_enable_allfilters(dev
, sk
);
398 /* filters set by default/setsockopt */
399 err
= raw_enable_allfilters(NULL
, sk
);
404 /* unregister old filters */
406 struct net_device
*dev
;
408 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
410 raw_disable_allfilters(dev
, sk
);
414 raw_disable_allfilters(NULL
, sk
);
416 ro
->ifindex
= ifindex
;
423 if (notify_enetdown
) {
424 sk
->sk_err
= ENETDOWN
;
425 if (!sock_flag(sk
, SOCK_DEAD
))
426 sk
->sk_error_report(sk
);
432 static int raw_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
435 struct sockaddr_can
*addr
= (struct sockaddr_can
*)uaddr
;
436 struct sock
*sk
= sock
->sk
;
437 struct raw_sock
*ro
= raw_sk(sk
);
442 memset(addr
, 0, sizeof(*addr
));
443 addr
->can_family
= AF_CAN
;
444 addr
->can_ifindex
= ro
->ifindex
;
446 *len
= sizeof(*addr
);
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;
463 if (level
!= SOL_CAN_RAW
)
469 if (optlen
% sizeof(struct can_filter
) != 0)
472 count
= optlen
/ sizeof(struct can_filter
);
475 /* filter does not fit into dfilter => alloc space */
476 filter
= memdup_user(optval
, optlen
);
478 return PTR_ERR(filter
);
479 } else if (count
== 1) {
480 if (copy_from_user(&sfilter
, optval
, sizeof(sfilter
)))
486 if (ro
->bound
&& ro
->ifindex
)
487 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
490 /* (try to) register the new filters */
492 err
= raw_enable_filters(dev
, sk
, &sfilter
, 1);
494 err
= raw_enable_filters(dev
, sk
, filter
,
502 /* remove old filter registrations */
503 raw_disable_filters(dev
, sk
, ro
->filter
, ro
->count
);
506 /* remove old filter space */
510 /* link new filters to the socket */
512 /* copy filter data for single filter */
513 ro
->dfilter
= sfilter
;
514 filter
= &ro
->dfilter
;
527 case CAN_RAW_ERR_FILTER
:
528 if (optlen
!= sizeof(err_mask
))
531 if (copy_from_user(&err_mask
, optval
, optlen
))
534 err_mask
&= CAN_ERR_MASK
;
538 if (ro
->bound
&& ro
->ifindex
)
539 dev
= dev_get_by_index(&init_net
, ro
->ifindex
);
541 /* remove current error mask */
543 /* (try to) register the new err_mask */
544 err
= raw_enable_errfilter(dev
, sk
, err_mask
);
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
;
564 case CAN_RAW_LOOPBACK
:
565 if (optlen
!= sizeof(ro
->loopback
))
568 if (copy_from_user(&ro
->loopback
, optval
, optlen
))
573 case CAN_RAW_RECV_OWN_MSGS
:
574 if (optlen
!= sizeof(ro
->recv_own_msgs
))
577 if (copy_from_user(&ro
->recv_own_msgs
, optval
, optlen
))
582 case CAN_RAW_FD_FRAMES
:
583 if (optlen
!= sizeof(ro
->fd_frames
))
586 if (copy_from_user(&ro
->fd_frames
, optval
, optlen
))
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
);
606 if (level
!= SOL_CAN_RAW
)
608 if (get_user(len
, optlen
))
618 int fsize
= ro
->count
* sizeof(struct can_filter
);
621 if (copy_to_user(optval
, ro
->filter
, len
))
628 err
= put_user(len
, optlen
);
631 case CAN_RAW_ERR_FILTER
:
632 if (len
> sizeof(can_err_mask_t
))
633 len
= sizeof(can_err_mask_t
);
637 case CAN_RAW_LOOPBACK
:
638 if (len
> sizeof(int))
643 case CAN_RAW_RECV_OWN_MSGS
:
644 if (len
> sizeof(int))
646 val
= &ro
->recv_own_msgs
;
649 case CAN_RAW_FD_FRAMES
:
650 if (len
> sizeof(int))
652 val
= &ro
->fd_frames
;
659 if (put_user(len
, optlen
))
661 if (copy_to_user(optval
, val
, len
))
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
);
672 struct net_device
*dev
;
677 struct sockaddr_can
*addr
=
678 (struct sockaddr_can
*)msg
->msg_name
;
680 if (msg
->msg_namelen
< sizeof(*addr
))
683 if (addr
->can_family
!= AF_CAN
)
686 ifindex
= addr
->can_ifindex
;
688 ifindex
= ro
->ifindex
;
691 if (unlikely(size
!= CANFD_MTU
&& size
!= CAN_MTU
))
694 if (unlikely(size
!= CAN_MTU
))
698 dev
= dev_get_by_index(&init_net
, ifindex
);
702 skb
= sock_alloc_send_skb(sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
,
707 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
710 err
= sock_tx_timestamp(sk
, &skb_shinfo(skb
)->tx_flags
);
717 err
= can_send(skb
, ro
->loopback
);
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
);
744 noblock
= flags
& MSG_DONTWAIT
;
745 flags
&= ~MSG_DONTWAIT
;
747 skb
= skb_recv_datagram(sk
, flags
, noblock
, &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
762 msg
->msg_flags
|= MSG_TRUNC
;
766 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, size
);
768 skb_free_datagram(sk
, skb
);
772 sock_recv_ts_and_drops(msg
, sk
, skb
);
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
);
787 static const struct proto_ops raw_ops
= {
789 .release
= raw_release
,
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
= {
809 .owner
= THIS_MODULE
,
810 .obj_size
= sizeof(struct raw_sock
),
814 static const struct can_proto raw_can_proto
= {
821 static __init
int raw_module_init(void)
827 err
= can_proto_register(&raw_can_proto
);
829 printk(KERN_ERR
"can: registration of raw protocol failed\n");
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
);