xref: /openbmc/linux/net/can/raw.c (revision 473d924d)
1fba76a58SOliver Hartkopp // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
2c18ce101SOliver Hartkopp /*
3c18ce101SOliver Hartkopp  * raw.c - Raw sockets for protocol family CAN
4c18ce101SOliver Hartkopp  *
5c18ce101SOliver Hartkopp  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
6c18ce101SOliver Hartkopp  * All rights reserved.
7c18ce101SOliver Hartkopp  *
8c18ce101SOliver Hartkopp  * Redistribution and use in source and binary forms, with or without
9c18ce101SOliver Hartkopp  * modification, are permitted provided that the following conditions
10c18ce101SOliver Hartkopp  * are met:
11c18ce101SOliver Hartkopp  * 1. Redistributions of source code must retain the above copyright
12c18ce101SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer.
13c18ce101SOliver Hartkopp  * 2. Redistributions in binary form must reproduce the above copyright
14c18ce101SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer in the
15c18ce101SOliver Hartkopp  *    documentation and/or other materials provided with the distribution.
16c18ce101SOliver Hartkopp  * 3. Neither the name of Volkswagen nor the names of its contributors
17c18ce101SOliver Hartkopp  *    may be used to endorse or promote products derived from this software
18c18ce101SOliver Hartkopp  *    without specific prior written permission.
19c18ce101SOliver Hartkopp  *
20c18ce101SOliver Hartkopp  * Alternatively, provided that this notice is retained in full, this
21c18ce101SOliver Hartkopp  * software may be distributed under the terms of the GNU General
22c18ce101SOliver Hartkopp  * Public License ("GPL") version 2, in which case the provisions of the
23c18ce101SOliver Hartkopp  * GPL apply INSTEAD OF those given above.
24c18ce101SOliver Hartkopp  *
25c18ce101SOliver Hartkopp  * The provided data structures and external interfaces from this code
26c18ce101SOliver Hartkopp  * are not restricted to be used by modules with a GPL compatible license.
27c18ce101SOliver Hartkopp  *
28c18ce101SOliver Hartkopp  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29c18ce101SOliver Hartkopp  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30c18ce101SOliver Hartkopp  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31c18ce101SOliver Hartkopp  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32c18ce101SOliver Hartkopp  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33c18ce101SOliver Hartkopp  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34c18ce101SOliver Hartkopp  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35c18ce101SOliver Hartkopp  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36c18ce101SOliver Hartkopp  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37c18ce101SOliver Hartkopp  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38c18ce101SOliver Hartkopp  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39c18ce101SOliver Hartkopp  * DAMAGE.
40c18ce101SOliver Hartkopp  *
41c18ce101SOliver Hartkopp  */
42c18ce101SOliver Hartkopp 
43c18ce101SOliver Hartkopp #include <linux/module.h>
44c18ce101SOliver Hartkopp #include <linux/init.h>
45c18ce101SOliver Hartkopp #include <linux/uio.h>
46c18ce101SOliver Hartkopp #include <linux/net.h>
475a0e3ad6STejun Heo #include <linux/slab.h>
48c18ce101SOliver Hartkopp #include <linux/netdevice.h>
49c18ce101SOliver Hartkopp #include <linux/socket.h>
50c18ce101SOliver Hartkopp #include <linux/if_arp.h>
51c18ce101SOliver Hartkopp #include <linux/skbuff.h>
52c18ce101SOliver Hartkopp #include <linux/can.h>
53c18ce101SOliver Hartkopp #include <linux/can/core.h>
54156c2bb9SOliver Hartkopp #include <linux/can/skb.h>
55c18ce101SOliver Hartkopp #include <linux/can/raw.h>
56c18ce101SOliver Hartkopp #include <net/sock.h>
57c18ce101SOliver Hartkopp #include <net/net_namespace.h>
58c18ce101SOliver Hartkopp 
59c18ce101SOliver Hartkopp #define CAN_RAW_VERSION CAN_VERSION
60c18ce101SOliver Hartkopp 
61c18ce101SOliver Hartkopp MODULE_DESCRIPTION("PF_CAN raw protocol");
62c18ce101SOliver Hartkopp MODULE_LICENSE("Dual BSD/GPL");
63c18ce101SOliver Hartkopp MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
64b13bb2e9SLothar Waßmann MODULE_ALIAS("can-proto-1");
65c18ce101SOliver Hartkopp 
66c18ce101SOliver Hartkopp #define MASK_ALL 0
67c18ce101SOliver Hartkopp 
68c18ce101SOliver Hartkopp /*
69c18ce101SOliver Hartkopp  * A raw socket has a list of can_filters attached to it, each receiving
70c18ce101SOliver Hartkopp  * the CAN frames matching that filter.  If the filter list is empty,
71c18ce101SOliver Hartkopp  * no CAN frames will be received by the socket.  The default after
72c18ce101SOliver Hartkopp  * opening the socket, is to have one filter which receives all frames.
73c18ce101SOliver Hartkopp  * The filter list is allocated dynamically with the exception of the
74c18ce101SOliver Hartkopp  * list containing only one item.  This common case is optimized by
75c18ce101SOliver Hartkopp  * storing the single filter in dfilter, to avoid using dynamic memory.
76c18ce101SOliver Hartkopp  */
77c18ce101SOliver Hartkopp 
78514ac99cSOliver Hartkopp struct uniqframe {
79d3b58c47SOliver Hartkopp 	int skbcnt;
80514ac99cSOliver Hartkopp 	const struct sk_buff *skb;
81a5581ef4SOliver Hartkopp 	unsigned int join_rx_count;
82514ac99cSOliver Hartkopp };
83514ac99cSOliver Hartkopp 
84c18ce101SOliver Hartkopp struct raw_sock {
85c18ce101SOliver Hartkopp 	struct sock sk;
86c18ce101SOliver Hartkopp 	int bound;
87c18ce101SOliver Hartkopp 	int ifindex;
88c18ce101SOliver Hartkopp 	struct notifier_block notifier;
89c18ce101SOliver Hartkopp 	int loopback;
90c18ce101SOliver Hartkopp 	int recv_own_msgs;
91e2d265d3SOliver Hartkopp 	int fd_frames;
92a5581ef4SOliver Hartkopp 	int join_filters;
93c18ce101SOliver Hartkopp 	int count;                 /* number of active filters */
94c18ce101SOliver Hartkopp 	struct can_filter dfilter; /* default/single filter */
95c18ce101SOliver Hartkopp 	struct can_filter *filter; /* pointer to filter(s) */
96c18ce101SOliver Hartkopp 	can_err_mask_t err_mask;
97514ac99cSOliver Hartkopp 	struct uniqframe __percpu *uniq;
98c18ce101SOliver Hartkopp };
99c18ce101SOliver Hartkopp 
1001e55659cSOliver Hartkopp /*
1011e55659cSOliver Hartkopp  * Return pointer to store the extra msg flags for raw_recvmsg().
1021e55659cSOliver Hartkopp  * We use the space of one unsigned int beyond the 'struct sockaddr_can'
1031e55659cSOliver Hartkopp  * in skb->cb.
1041e55659cSOliver Hartkopp  */
1051e55659cSOliver Hartkopp static inline unsigned int *raw_flags(struct sk_buff *skb)
1061e55659cSOliver Hartkopp {
107b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
108b4772ef8SEyal Birger 			       sizeof(unsigned int));
1091e55659cSOliver Hartkopp 
1101e55659cSOliver Hartkopp 	/* return pointer after struct sockaddr_can */
1111e55659cSOliver Hartkopp 	return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
1121e55659cSOliver Hartkopp }
1131e55659cSOliver Hartkopp 
114c18ce101SOliver Hartkopp static inline struct raw_sock *raw_sk(const struct sock *sk)
115c18ce101SOliver Hartkopp {
116c18ce101SOliver Hartkopp 	return (struct raw_sock *)sk;
117c18ce101SOliver Hartkopp }
118c18ce101SOliver Hartkopp 
1191e55659cSOliver Hartkopp static void raw_rcv(struct sk_buff *oskb, void *data)
120c18ce101SOliver Hartkopp {
121c18ce101SOliver Hartkopp 	struct sock *sk = (struct sock *)data;
122c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
123c18ce101SOliver Hartkopp 	struct sockaddr_can *addr;
1241e55659cSOliver Hartkopp 	struct sk_buff *skb;
1251e55659cSOliver Hartkopp 	unsigned int *pflags;
126c18ce101SOliver Hartkopp 
127c18ce101SOliver Hartkopp 	/* check the received tx sock reference */
1281e55659cSOliver Hartkopp 	if (!ro->recv_own_msgs && oskb->sk == sk)
129c18ce101SOliver Hartkopp 		return;
1301fa17d4bSOliver Hartkopp 
131821047c4SOliver Hartkopp 	/* do not pass non-CAN2.0 frames to a legacy socket */
132821047c4SOliver Hartkopp 	if (!ro->fd_frames && oskb->len != CAN_MTU)
133e2d265d3SOliver Hartkopp 		return;
134e2d265d3SOliver Hartkopp 
135514ac99cSOliver Hartkopp 	/* eliminate multiple filter matches for the same skb */
136514ac99cSOliver Hartkopp 	if (this_cpu_ptr(ro->uniq)->skb == oskb &&
137d3b58c47SOliver Hartkopp 	    this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
138a5581ef4SOliver Hartkopp 		if (ro->join_filters) {
139a5581ef4SOliver Hartkopp 			this_cpu_inc(ro->uniq->join_rx_count);
140a5581ef4SOliver Hartkopp 			/* drop frame until all enabled filters matched */
141a5581ef4SOliver Hartkopp 			if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
142514ac99cSOliver Hartkopp 				return;
143514ac99cSOliver Hartkopp 		} else {
144a5581ef4SOliver Hartkopp 			return;
145a5581ef4SOliver Hartkopp 		}
146a5581ef4SOliver Hartkopp 	} else {
147514ac99cSOliver Hartkopp 		this_cpu_ptr(ro->uniq)->skb = oskb;
148d3b58c47SOliver Hartkopp 		this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
149a5581ef4SOliver Hartkopp 		this_cpu_ptr(ro->uniq)->join_rx_count = 1;
150a5581ef4SOliver Hartkopp 		/* drop first frame to check all enabled filters? */
151a5581ef4SOliver Hartkopp 		if (ro->join_filters && ro->count > 1)
152a5581ef4SOliver Hartkopp 			return;
153514ac99cSOliver Hartkopp 	}
154514ac99cSOliver Hartkopp 
1551fa17d4bSOliver Hartkopp 	/* clone the given skb to be able to enqueue it into the rcv queue */
1561e55659cSOliver Hartkopp 	skb = skb_clone(oskb, GFP_ATOMIC);
1571fa17d4bSOliver Hartkopp 	if (!skb)
1581fa17d4bSOliver Hartkopp 		return;
159c18ce101SOliver Hartkopp 
160c18ce101SOliver Hartkopp 	/*
161c18ce101SOliver Hartkopp 	 *  Put the datagram to the queue so that raw_recvmsg() can
162c18ce101SOliver Hartkopp 	 *  get it from there.  We need to pass the interface index to
163c18ce101SOliver Hartkopp 	 *  raw_recvmsg().  We pass a whole struct sockaddr_can in skb->cb
164c18ce101SOliver Hartkopp 	 *  containing the interface index.
165c18ce101SOliver Hartkopp 	 */
166c18ce101SOliver Hartkopp 
167b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct sockaddr_can));
168c18ce101SOliver Hartkopp 	addr = (struct sockaddr_can *)skb->cb;
169c18ce101SOliver Hartkopp 	memset(addr, 0, sizeof(*addr));
170c18ce101SOliver Hartkopp 	addr->can_family  = AF_CAN;
171c18ce101SOliver Hartkopp 	addr->can_ifindex = skb->dev->ifindex;
172c18ce101SOliver Hartkopp 
1731e55659cSOliver Hartkopp 	/* add CAN specific message flags for raw_recvmsg() */
1741e55659cSOliver Hartkopp 	pflags = raw_flags(skb);
1751e55659cSOliver Hartkopp 	*pflags = 0;
1761e55659cSOliver Hartkopp 	if (oskb->sk)
1771e55659cSOliver Hartkopp 		*pflags |= MSG_DONTROUTE;
1781e55659cSOliver Hartkopp 	if (oskb->sk == sk)
1791e55659cSOliver Hartkopp 		*pflags |= MSG_CONFIRM;
1801e55659cSOliver Hartkopp 
181a219994bSUrs Thuermann 	if (sock_queue_rcv_skb(sk, skb) < 0)
182c18ce101SOliver Hartkopp 		kfree_skb(skb);
183c18ce101SOliver Hartkopp }
184c18ce101SOliver Hartkopp 
1858e8cda6dSMario Kicherer static int raw_enable_filters(struct net *net, struct net_device *dev,
1868e8cda6dSMario Kicherer 			      struct sock *sk, struct can_filter *filter,
1878e8cda6dSMario Kicherer 			      int count)
188c18ce101SOliver Hartkopp {
189c18ce101SOliver Hartkopp 	int err = 0;
190c18ce101SOliver Hartkopp 	int i;
191c18ce101SOliver Hartkopp 
192c18ce101SOliver Hartkopp 	for (i = 0; i < count; i++) {
1938e8cda6dSMario Kicherer 		err = can_rx_register(net, dev, filter[i].can_id,
194c18ce101SOliver Hartkopp 				      filter[i].can_mask,
195f1712c73SEric Dumazet 				      raw_rcv, sk, "raw", sk);
196c18ce101SOliver Hartkopp 		if (err) {
197c18ce101SOliver Hartkopp 			/* clean up successfully registered filters */
198c18ce101SOliver Hartkopp 			while (--i >= 0)
1998e8cda6dSMario Kicherer 				can_rx_unregister(net, dev, filter[i].can_id,
200c18ce101SOliver Hartkopp 						  filter[i].can_mask,
201c18ce101SOliver Hartkopp 						  raw_rcv, sk);
202c18ce101SOliver Hartkopp 			break;
203c18ce101SOliver Hartkopp 		}
204c18ce101SOliver Hartkopp 	}
205c18ce101SOliver Hartkopp 
206c18ce101SOliver Hartkopp 	return err;
207c18ce101SOliver Hartkopp }
208c18ce101SOliver Hartkopp 
2098e8cda6dSMario Kicherer static int raw_enable_errfilter(struct net *net, struct net_device *dev,
2108e8cda6dSMario Kicherer 				struct sock *sk, can_err_mask_t err_mask)
211c18ce101SOliver Hartkopp {
212c18ce101SOliver Hartkopp 	int err = 0;
213c18ce101SOliver Hartkopp 
214c18ce101SOliver Hartkopp 	if (err_mask)
2158e8cda6dSMario Kicherer 		err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
216f1712c73SEric Dumazet 				      raw_rcv, sk, "raw", sk);
217c18ce101SOliver Hartkopp 
218c18ce101SOliver Hartkopp 	return err;
219c18ce101SOliver Hartkopp }
220c18ce101SOliver Hartkopp 
2218e8cda6dSMario Kicherer static void raw_disable_filters(struct net *net, struct net_device *dev,
2228e8cda6dSMario Kicherer 				struct sock *sk, struct can_filter *filter,
2238e8cda6dSMario Kicherer 				int count)
224c18ce101SOliver Hartkopp {
225c18ce101SOliver Hartkopp 	int i;
226c18ce101SOliver Hartkopp 
227c18ce101SOliver Hartkopp 	for (i = 0; i < count; i++)
2288e8cda6dSMario Kicherer 		can_rx_unregister(net, dev, filter[i].can_id,
2298e8cda6dSMario Kicherer 				  filter[i].can_mask, raw_rcv, sk);
230c18ce101SOliver Hartkopp }
231c18ce101SOliver Hartkopp 
2328e8cda6dSMario Kicherer static inline void raw_disable_errfilter(struct net *net,
2338e8cda6dSMario Kicherer 					 struct net_device *dev,
234c18ce101SOliver Hartkopp 					 struct sock *sk,
235c18ce101SOliver Hartkopp 					 can_err_mask_t err_mask)
236c18ce101SOliver Hartkopp 
237c18ce101SOliver Hartkopp {
238c18ce101SOliver Hartkopp 	if (err_mask)
2398e8cda6dSMario Kicherer 		can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
240c18ce101SOliver Hartkopp 				  raw_rcv, sk);
241c18ce101SOliver Hartkopp }
242c18ce101SOliver Hartkopp 
2438e8cda6dSMario Kicherer static inline void raw_disable_allfilters(struct net *net,
2448e8cda6dSMario Kicherer 					  struct net_device *dev,
245c18ce101SOliver Hartkopp 					  struct sock *sk)
246c18ce101SOliver Hartkopp {
247c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
248c18ce101SOliver Hartkopp 
2498e8cda6dSMario Kicherer 	raw_disable_filters(net, dev, sk, ro->filter, ro->count);
2508e8cda6dSMario Kicherer 	raw_disable_errfilter(net, dev, sk, ro->err_mask);
251c18ce101SOliver Hartkopp }
252c18ce101SOliver Hartkopp 
2538e8cda6dSMario Kicherer static int raw_enable_allfilters(struct net *net, struct net_device *dev,
2548e8cda6dSMario Kicherer 				 struct sock *sk)
255c18ce101SOliver Hartkopp {
256c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
257c18ce101SOliver Hartkopp 	int err;
258c18ce101SOliver Hartkopp 
2598e8cda6dSMario Kicherer 	err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
260c18ce101SOliver Hartkopp 	if (!err) {
2618e8cda6dSMario Kicherer 		err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
262c18ce101SOliver Hartkopp 		if (err)
2638e8cda6dSMario Kicherer 			raw_disable_filters(net, dev, sk, ro->filter,
2648e8cda6dSMario Kicherer 					    ro->count);
265c18ce101SOliver Hartkopp 	}
266c18ce101SOliver Hartkopp 
267c18ce101SOliver Hartkopp 	return err;
268c18ce101SOliver Hartkopp }
269c18ce101SOliver Hartkopp 
270c18ce101SOliver Hartkopp static int raw_notifier(struct notifier_block *nb,
271351638e7SJiri Pirko 			unsigned long msg, void *ptr)
272c18ce101SOliver Hartkopp {
273351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
274c18ce101SOliver Hartkopp 	struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
275c18ce101SOliver Hartkopp 	struct sock *sk = &ro->sk;
276c18ce101SOliver Hartkopp 
2778e8cda6dSMario Kicherer 	if (!net_eq(dev_net(dev), sock_net(sk)))
278c18ce101SOliver Hartkopp 		return NOTIFY_DONE;
279c18ce101SOliver Hartkopp 
280c18ce101SOliver Hartkopp 	if (dev->type != ARPHRD_CAN)
281c18ce101SOliver Hartkopp 		return NOTIFY_DONE;
282c18ce101SOliver Hartkopp 
283c18ce101SOliver Hartkopp 	if (ro->ifindex != dev->ifindex)
284c18ce101SOliver Hartkopp 		return NOTIFY_DONE;
285c18ce101SOliver Hartkopp 
286c18ce101SOliver Hartkopp 	switch (msg) {
287c18ce101SOliver Hartkopp 
288c18ce101SOliver Hartkopp 	case NETDEV_UNREGISTER:
289c18ce101SOliver Hartkopp 		lock_sock(sk);
290c18ce101SOliver Hartkopp 		/* remove current filters & unregister */
291c18ce101SOliver Hartkopp 		if (ro->bound)
2928e8cda6dSMario Kicherer 			raw_disable_allfilters(dev_net(dev), dev, sk);
293c18ce101SOliver Hartkopp 
294c18ce101SOliver Hartkopp 		if (ro->count > 1)
295c18ce101SOliver Hartkopp 			kfree(ro->filter);
296c18ce101SOliver Hartkopp 
297c18ce101SOliver Hartkopp 		ro->ifindex = 0;
298c18ce101SOliver Hartkopp 		ro->bound   = 0;
299c18ce101SOliver Hartkopp 		ro->count   = 0;
300c18ce101SOliver Hartkopp 		release_sock(sk);
301c18ce101SOliver Hartkopp 
302c18ce101SOliver Hartkopp 		sk->sk_err = ENODEV;
303c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
304c18ce101SOliver Hartkopp 			sk->sk_error_report(sk);
305c18ce101SOliver Hartkopp 		break;
306c18ce101SOliver Hartkopp 
307c18ce101SOliver Hartkopp 	case NETDEV_DOWN:
308c18ce101SOliver Hartkopp 		sk->sk_err = ENETDOWN;
309c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
310c18ce101SOliver Hartkopp 			sk->sk_error_report(sk);
311c18ce101SOliver Hartkopp 		break;
312c18ce101SOliver Hartkopp 	}
313c18ce101SOliver Hartkopp 
314c18ce101SOliver Hartkopp 	return NOTIFY_DONE;
315c18ce101SOliver Hartkopp }
316c18ce101SOliver Hartkopp 
317c18ce101SOliver Hartkopp static int raw_init(struct sock *sk)
318c18ce101SOliver Hartkopp {
319c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
320c18ce101SOliver Hartkopp 
321c18ce101SOliver Hartkopp 	ro->bound            = 0;
322c18ce101SOliver Hartkopp 	ro->ifindex          = 0;
323c18ce101SOliver Hartkopp 
324c18ce101SOliver Hartkopp 	/* set default filter to single entry dfilter */
325c18ce101SOliver Hartkopp 	ro->dfilter.can_id   = 0;
326c18ce101SOliver Hartkopp 	ro->dfilter.can_mask = MASK_ALL;
327c18ce101SOliver Hartkopp 	ro->filter           = &ro->dfilter;
328c18ce101SOliver Hartkopp 	ro->count            = 1;
329c18ce101SOliver Hartkopp 
330c18ce101SOliver Hartkopp 	/* set default loopback behaviour */
331c18ce101SOliver Hartkopp 	ro->loopback         = 1;
332c18ce101SOliver Hartkopp 	ro->recv_own_msgs    = 0;
333e2d265d3SOliver Hartkopp 	ro->fd_frames        = 0;
334a5581ef4SOliver Hartkopp 	ro->join_filters     = 0;
335c18ce101SOliver Hartkopp 
336514ac99cSOliver Hartkopp 	/* alloc_percpu provides zero'ed memory */
337514ac99cSOliver Hartkopp 	ro->uniq = alloc_percpu(struct uniqframe);
338514ac99cSOliver Hartkopp 	if (unlikely(!ro->uniq))
339514ac99cSOliver Hartkopp 		return -ENOMEM;
340514ac99cSOliver Hartkopp 
341c18ce101SOliver Hartkopp 	/* set notifier */
342c18ce101SOliver Hartkopp 	ro->notifier.notifier_call = raw_notifier;
343c18ce101SOliver Hartkopp 
344c18ce101SOliver Hartkopp 	register_netdevice_notifier(&ro->notifier);
345c18ce101SOliver Hartkopp 
346c18ce101SOliver Hartkopp 	return 0;
347c18ce101SOliver Hartkopp }
348c18ce101SOliver Hartkopp 
349c18ce101SOliver Hartkopp static int raw_release(struct socket *sock)
350c18ce101SOliver Hartkopp {
351c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
35210022a6cSOliver Hartkopp 	struct raw_sock *ro;
35310022a6cSOliver Hartkopp 
35410022a6cSOliver Hartkopp 	if (!sk)
35510022a6cSOliver Hartkopp 		return 0;
35610022a6cSOliver Hartkopp 
35710022a6cSOliver Hartkopp 	ro = raw_sk(sk);
358c18ce101SOliver Hartkopp 
359c18ce101SOliver Hartkopp 	unregister_netdevice_notifier(&ro->notifier);
360c18ce101SOliver Hartkopp 
361c18ce101SOliver Hartkopp 	lock_sock(sk);
362c18ce101SOliver Hartkopp 
363c18ce101SOliver Hartkopp 	/* remove current filters & unregister */
364c18ce101SOliver Hartkopp 	if (ro->bound) {
365c18ce101SOliver Hartkopp 		if (ro->ifindex) {
366c18ce101SOliver Hartkopp 			struct net_device *dev;
367c18ce101SOliver Hartkopp 
3688e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
369c18ce101SOliver Hartkopp 			if (dev) {
3708e8cda6dSMario Kicherer 				raw_disable_allfilters(dev_net(dev), dev, sk);
371c18ce101SOliver Hartkopp 				dev_put(dev);
372c18ce101SOliver Hartkopp 			}
373c18ce101SOliver Hartkopp 		} else
3748e8cda6dSMario Kicherer 			raw_disable_allfilters(sock_net(sk), NULL, sk);
375c18ce101SOliver Hartkopp 	}
376c18ce101SOliver Hartkopp 
377c18ce101SOliver Hartkopp 	if (ro->count > 1)
378c18ce101SOliver Hartkopp 		kfree(ro->filter);
379c18ce101SOliver Hartkopp 
380c18ce101SOliver Hartkopp 	ro->ifindex = 0;
381c18ce101SOliver Hartkopp 	ro->bound   = 0;
382c18ce101SOliver Hartkopp 	ro->count   = 0;
383514ac99cSOliver Hartkopp 	free_percpu(ro->uniq);
384c18ce101SOliver Hartkopp 
385f7e5cc0cSLothar Waßmann 	sock_orphan(sk);
386f7e5cc0cSLothar Waßmann 	sock->sk = NULL;
387f7e5cc0cSLothar Waßmann 
388c18ce101SOliver Hartkopp 	release_sock(sk);
389c18ce101SOliver Hartkopp 	sock_put(sk);
390c18ce101SOliver Hartkopp 
391c18ce101SOliver Hartkopp 	return 0;
392c18ce101SOliver Hartkopp }
393c18ce101SOliver Hartkopp 
394c18ce101SOliver Hartkopp static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
395c18ce101SOliver Hartkopp {
396c18ce101SOliver Hartkopp 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
397c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
398c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
399c18ce101SOliver Hartkopp 	int ifindex;
400c18ce101SOliver Hartkopp 	int err = 0;
401c18ce101SOliver Hartkopp 	int notify_enetdown = 0;
402c18ce101SOliver Hartkopp 
403c18ce101SOliver Hartkopp 	if (len < sizeof(*addr))
404c18ce101SOliver Hartkopp 		return -EINVAL;
405adb552c3SMarc Kleine-Budde 	if (addr->can_family != AF_CAN)
406adb552c3SMarc Kleine-Budde 		return -EINVAL;
407c18ce101SOliver Hartkopp 
408c18ce101SOliver Hartkopp 	lock_sock(sk);
409c18ce101SOliver Hartkopp 
410c18ce101SOliver Hartkopp 	if (ro->bound && addr->can_ifindex == ro->ifindex)
411c18ce101SOliver Hartkopp 		goto out;
412c18ce101SOliver Hartkopp 
413c18ce101SOliver Hartkopp 	if (addr->can_ifindex) {
414c18ce101SOliver Hartkopp 		struct net_device *dev;
415c18ce101SOliver Hartkopp 
4168e8cda6dSMario Kicherer 		dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
417c18ce101SOliver Hartkopp 		if (!dev) {
418c18ce101SOliver Hartkopp 			err = -ENODEV;
419c18ce101SOliver Hartkopp 			goto out;
420c18ce101SOliver Hartkopp 		}
421c18ce101SOliver Hartkopp 		if (dev->type != ARPHRD_CAN) {
422c18ce101SOliver Hartkopp 			dev_put(dev);
423c18ce101SOliver Hartkopp 			err = -ENODEV;
424c18ce101SOliver Hartkopp 			goto out;
425c18ce101SOliver Hartkopp 		}
426c18ce101SOliver Hartkopp 		if (!(dev->flags & IFF_UP))
427c18ce101SOliver Hartkopp 			notify_enetdown = 1;
428c18ce101SOliver Hartkopp 
429c18ce101SOliver Hartkopp 		ifindex = dev->ifindex;
430c18ce101SOliver Hartkopp 
431c18ce101SOliver Hartkopp 		/* filters set by default/setsockopt */
4328e8cda6dSMario Kicherer 		err = raw_enable_allfilters(sock_net(sk), dev, sk);
433c18ce101SOliver Hartkopp 		dev_put(dev);
434c18ce101SOliver Hartkopp 	} else {
435c18ce101SOliver Hartkopp 		ifindex = 0;
436c18ce101SOliver Hartkopp 
437c18ce101SOliver Hartkopp 		/* filters set by default/setsockopt */
4388e8cda6dSMario Kicherer 		err = raw_enable_allfilters(sock_net(sk), NULL, sk);
439c18ce101SOliver Hartkopp 	}
440c18ce101SOliver Hartkopp 
441c18ce101SOliver Hartkopp 	if (!err) {
442c18ce101SOliver Hartkopp 		if (ro->bound) {
443c18ce101SOliver Hartkopp 			/* unregister old filters */
444c18ce101SOliver Hartkopp 			if (ro->ifindex) {
445c18ce101SOliver Hartkopp 				struct net_device *dev;
446c18ce101SOliver Hartkopp 
4478e8cda6dSMario Kicherer 				dev = dev_get_by_index(sock_net(sk),
4488e8cda6dSMario Kicherer 						       ro->ifindex);
449c18ce101SOliver Hartkopp 				if (dev) {
4508e8cda6dSMario Kicherer 					raw_disable_allfilters(dev_net(dev),
4518e8cda6dSMario Kicherer 							       dev, sk);
452c18ce101SOliver Hartkopp 					dev_put(dev);
453c18ce101SOliver Hartkopp 				}
454c18ce101SOliver Hartkopp 			} else
4558e8cda6dSMario Kicherer 				raw_disable_allfilters(sock_net(sk), NULL, sk);
456c18ce101SOliver Hartkopp 		}
457c18ce101SOliver Hartkopp 		ro->ifindex = ifindex;
458c18ce101SOliver Hartkopp 		ro->bound = 1;
459c18ce101SOliver Hartkopp 	}
460c18ce101SOliver Hartkopp 
461c18ce101SOliver Hartkopp  out:
462c18ce101SOliver Hartkopp 	release_sock(sk);
463c18ce101SOliver Hartkopp 
464c18ce101SOliver Hartkopp 	if (notify_enetdown) {
465c18ce101SOliver Hartkopp 		sk->sk_err = ENETDOWN;
466c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
467c18ce101SOliver Hartkopp 			sk->sk_error_report(sk);
468c18ce101SOliver Hartkopp 	}
469c18ce101SOliver Hartkopp 
470c18ce101SOliver Hartkopp 	return err;
471c18ce101SOliver Hartkopp }
472c18ce101SOliver Hartkopp 
473c18ce101SOliver Hartkopp static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
4749b2c45d4SDenys Vlasenko 		       int peer)
475c18ce101SOliver Hartkopp {
476c18ce101SOliver Hartkopp 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
477c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
478c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
479c18ce101SOliver Hartkopp 
480c18ce101SOliver Hartkopp 	if (peer)
481c18ce101SOliver Hartkopp 		return -EOPNOTSUPP;
482c18ce101SOliver Hartkopp 
483e84b90aeSEric Dumazet 	memset(addr, 0, sizeof(*addr));
484c18ce101SOliver Hartkopp 	addr->can_family  = AF_CAN;
485c18ce101SOliver Hartkopp 	addr->can_ifindex = ro->ifindex;
486c18ce101SOliver Hartkopp 
4879b2c45d4SDenys Vlasenko 	return sizeof(*addr);
488c18ce101SOliver Hartkopp }
489c18ce101SOliver Hartkopp 
490c18ce101SOliver Hartkopp static int raw_setsockopt(struct socket *sock, int level, int optname,
491b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
492c18ce101SOliver Hartkopp {
493c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
494c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
495c18ce101SOliver Hartkopp 	struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
496c18ce101SOliver Hartkopp 	struct can_filter sfilter;         /* single filter */
497c18ce101SOliver Hartkopp 	struct net_device *dev = NULL;
498c18ce101SOliver Hartkopp 	can_err_mask_t err_mask = 0;
499c18ce101SOliver Hartkopp 	int count = 0;
500c18ce101SOliver Hartkopp 	int err = 0;
501c18ce101SOliver Hartkopp 
502c18ce101SOliver Hartkopp 	if (level != SOL_CAN_RAW)
503c18ce101SOliver Hartkopp 		return -EINVAL;
504c18ce101SOliver Hartkopp 
505c18ce101SOliver Hartkopp 	switch (optname) {
506c18ce101SOliver Hartkopp 
507c18ce101SOliver Hartkopp 	case CAN_RAW_FILTER:
508c18ce101SOliver Hartkopp 		if (optlen % sizeof(struct can_filter) != 0)
509c18ce101SOliver Hartkopp 			return -EINVAL;
510c18ce101SOliver Hartkopp 
511332b05caSMarc Kleine-Budde 		if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
512332b05caSMarc Kleine-Budde 			return -EINVAL;
513332b05caSMarc Kleine-Budde 
514c18ce101SOliver Hartkopp 		count = optlen / sizeof(struct can_filter);
515c18ce101SOliver Hartkopp 
516c18ce101SOliver Hartkopp 		if (count > 1) {
517c18ce101SOliver Hartkopp 			/* filter does not fit into dfilter => alloc space */
51816dff918SJulia Lawall 			filter = memdup_user(optval, optlen);
51916dff918SJulia Lawall 			if (IS_ERR(filter))
52016dff918SJulia Lawall 				return PTR_ERR(filter);
521c18ce101SOliver Hartkopp 		} else if (count == 1) {
5224ffa8701SEric Dumazet 			if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
5233f91bd42SSam Ravnborg 				return -EFAULT;
524c18ce101SOliver Hartkopp 		}
525c18ce101SOliver Hartkopp 
526c18ce101SOliver Hartkopp 		lock_sock(sk);
527c18ce101SOliver Hartkopp 
528c18ce101SOliver Hartkopp 		if (ro->bound && ro->ifindex)
5298e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
530c18ce101SOliver Hartkopp 
531c18ce101SOliver Hartkopp 		if (ro->bound) {
532c18ce101SOliver Hartkopp 			/* (try to) register the new filters */
533c18ce101SOliver Hartkopp 			if (count == 1)
5348e8cda6dSMario Kicherer 				err = raw_enable_filters(sock_net(sk), dev, sk,
5358e8cda6dSMario Kicherer 							 &sfilter, 1);
536c18ce101SOliver Hartkopp 			else
5378e8cda6dSMario Kicherer 				err = raw_enable_filters(sock_net(sk), dev, sk,
5388e8cda6dSMario Kicherer 							 filter, count);
539c18ce101SOliver Hartkopp 			if (err) {
540c18ce101SOliver Hartkopp 				if (count > 1)
541c18ce101SOliver Hartkopp 					kfree(filter);
542c18ce101SOliver Hartkopp 				goto out_fil;
543c18ce101SOliver Hartkopp 			}
544c18ce101SOliver Hartkopp 
545c18ce101SOliver Hartkopp 			/* remove old filter registrations */
5468e8cda6dSMario Kicherer 			raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
5478e8cda6dSMario Kicherer 					    ro->count);
548c18ce101SOliver Hartkopp 		}
549c18ce101SOliver Hartkopp 
550c18ce101SOliver Hartkopp 		/* remove old filter space */
551c18ce101SOliver Hartkopp 		if (ro->count > 1)
552c18ce101SOliver Hartkopp 			kfree(ro->filter);
553c18ce101SOliver Hartkopp 
554c18ce101SOliver Hartkopp 		/* link new filters to the socket */
555c18ce101SOliver Hartkopp 		if (count == 1) {
556c18ce101SOliver Hartkopp 			/* copy filter data for single filter */
557c18ce101SOliver Hartkopp 			ro->dfilter = sfilter;
558c18ce101SOliver Hartkopp 			filter = &ro->dfilter;
559c18ce101SOliver Hartkopp 		}
560c18ce101SOliver Hartkopp 		ro->filter = filter;
561c18ce101SOliver Hartkopp 		ro->count  = count;
562c18ce101SOliver Hartkopp 
563c18ce101SOliver Hartkopp  out_fil:
564c18ce101SOliver Hartkopp 		if (dev)
565c18ce101SOliver Hartkopp 			dev_put(dev);
566c18ce101SOliver Hartkopp 
567c18ce101SOliver Hartkopp 		release_sock(sk);
568c18ce101SOliver Hartkopp 
569c18ce101SOliver Hartkopp 		break;
570c18ce101SOliver Hartkopp 
571c18ce101SOliver Hartkopp 	case CAN_RAW_ERR_FILTER:
572c18ce101SOliver Hartkopp 		if (optlen != sizeof(err_mask))
573c18ce101SOliver Hartkopp 			return -EINVAL;
574c18ce101SOliver Hartkopp 
5753f91bd42SSam Ravnborg 		if (copy_from_user(&err_mask, optval, optlen))
5763f91bd42SSam Ravnborg 			return -EFAULT;
577c18ce101SOliver Hartkopp 
578c18ce101SOliver Hartkopp 		err_mask &= CAN_ERR_MASK;
579c18ce101SOliver Hartkopp 
580c18ce101SOliver Hartkopp 		lock_sock(sk);
581c18ce101SOliver Hartkopp 
582c18ce101SOliver Hartkopp 		if (ro->bound && ro->ifindex)
5838e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
584c18ce101SOliver Hartkopp 
585c18ce101SOliver Hartkopp 		/* remove current error mask */
586c18ce101SOliver Hartkopp 		if (ro->bound) {
587c18ce101SOliver Hartkopp 			/* (try to) register the new err_mask */
5888e8cda6dSMario Kicherer 			err = raw_enable_errfilter(sock_net(sk), dev, sk,
5898e8cda6dSMario Kicherer 						   err_mask);
590c18ce101SOliver Hartkopp 
591c18ce101SOliver Hartkopp 			if (err)
592c18ce101SOliver Hartkopp 				goto out_err;
593c18ce101SOliver Hartkopp 
594c18ce101SOliver Hartkopp 			/* remove old err_mask registration */
5958e8cda6dSMario Kicherer 			raw_disable_errfilter(sock_net(sk), dev, sk,
5968e8cda6dSMario Kicherer 					      ro->err_mask);
597c18ce101SOliver Hartkopp 		}
598c18ce101SOliver Hartkopp 
599c18ce101SOliver Hartkopp 		/* link new err_mask to the socket */
600c18ce101SOliver Hartkopp 		ro->err_mask = err_mask;
601c18ce101SOliver Hartkopp 
602c18ce101SOliver Hartkopp  out_err:
603c18ce101SOliver Hartkopp 		if (dev)
604c18ce101SOliver Hartkopp 			dev_put(dev);
605c18ce101SOliver Hartkopp 
606c18ce101SOliver Hartkopp 		release_sock(sk);
607c18ce101SOliver Hartkopp 
608c18ce101SOliver Hartkopp 		break;
609c18ce101SOliver Hartkopp 
610c18ce101SOliver Hartkopp 	case CAN_RAW_LOOPBACK:
611c18ce101SOliver Hartkopp 		if (optlen != sizeof(ro->loopback))
612c18ce101SOliver Hartkopp 			return -EINVAL;
613c18ce101SOliver Hartkopp 
6143f91bd42SSam Ravnborg 		if (copy_from_user(&ro->loopback, optval, optlen))
6153f91bd42SSam Ravnborg 			return -EFAULT;
616c18ce101SOliver Hartkopp 
617c18ce101SOliver Hartkopp 		break;
618c18ce101SOliver Hartkopp 
619c18ce101SOliver Hartkopp 	case CAN_RAW_RECV_OWN_MSGS:
620c18ce101SOliver Hartkopp 		if (optlen != sizeof(ro->recv_own_msgs))
621c18ce101SOliver Hartkopp 			return -EINVAL;
622c18ce101SOliver Hartkopp 
6233f91bd42SSam Ravnborg 		if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
6243f91bd42SSam Ravnborg 			return -EFAULT;
625c18ce101SOliver Hartkopp 
626c18ce101SOliver Hartkopp 		break;
627c18ce101SOliver Hartkopp 
628e2d265d3SOliver Hartkopp 	case CAN_RAW_FD_FRAMES:
629e2d265d3SOliver Hartkopp 		if (optlen != sizeof(ro->fd_frames))
630e2d265d3SOliver Hartkopp 			return -EINVAL;
631e2d265d3SOliver Hartkopp 
632e2d265d3SOliver Hartkopp 		if (copy_from_user(&ro->fd_frames, optval, optlen))
633e2d265d3SOliver Hartkopp 			return -EFAULT;
634e2d265d3SOliver Hartkopp 
635e2d265d3SOliver Hartkopp 		break;
636e2d265d3SOliver Hartkopp 
637a5581ef4SOliver Hartkopp 	case CAN_RAW_JOIN_FILTERS:
638a5581ef4SOliver Hartkopp 		if (optlen != sizeof(ro->join_filters))
639a5581ef4SOliver Hartkopp 			return -EINVAL;
640a5581ef4SOliver Hartkopp 
641a5581ef4SOliver Hartkopp 		if (copy_from_user(&ro->join_filters, optval, optlen))
642a5581ef4SOliver Hartkopp 			return -EFAULT;
643a5581ef4SOliver Hartkopp 
644a5581ef4SOliver Hartkopp 		break;
645a5581ef4SOliver Hartkopp 
646c18ce101SOliver Hartkopp 	default:
647c18ce101SOliver Hartkopp 		return -ENOPROTOOPT;
648c18ce101SOliver Hartkopp 	}
649c18ce101SOliver Hartkopp 	return err;
650c18ce101SOliver Hartkopp }
651c18ce101SOliver Hartkopp 
652c18ce101SOliver Hartkopp static int raw_getsockopt(struct socket *sock, int level, int optname,
653c18ce101SOliver Hartkopp 			  char __user *optval, int __user *optlen)
654c18ce101SOliver Hartkopp {
655c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
656c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
657c18ce101SOliver Hartkopp 	int len;
658c18ce101SOliver Hartkopp 	void *val;
659c18ce101SOliver Hartkopp 	int err = 0;
660c18ce101SOliver Hartkopp 
661c18ce101SOliver Hartkopp 	if (level != SOL_CAN_RAW)
662c18ce101SOliver Hartkopp 		return -EINVAL;
663c18ce101SOliver Hartkopp 	if (get_user(len, optlen))
664c18ce101SOliver Hartkopp 		return -EFAULT;
665c18ce101SOliver Hartkopp 	if (len < 0)
666c18ce101SOliver Hartkopp 		return -EINVAL;
667c18ce101SOliver Hartkopp 
668c18ce101SOliver Hartkopp 	switch (optname) {
669c18ce101SOliver Hartkopp 
670c18ce101SOliver Hartkopp 	case CAN_RAW_FILTER:
671c18ce101SOliver Hartkopp 		lock_sock(sk);
672c18ce101SOliver Hartkopp 		if (ro->count > 0) {
673c18ce101SOliver Hartkopp 			int fsize = ro->count * sizeof(struct can_filter);
674c18ce101SOliver Hartkopp 			if (len > fsize)
675c18ce101SOliver Hartkopp 				len = fsize;
676653252c2SPavel Emelyanov 			if (copy_to_user(optval, ro->filter, len))
677653252c2SPavel Emelyanov 				err = -EFAULT;
678c18ce101SOliver Hartkopp 		} else
679c18ce101SOliver Hartkopp 			len = 0;
680c18ce101SOliver Hartkopp 		release_sock(sk);
681c18ce101SOliver Hartkopp 
682c18ce101SOliver Hartkopp 		if (!err)
683c18ce101SOliver Hartkopp 			err = put_user(len, optlen);
684c18ce101SOliver Hartkopp 		return err;
685c18ce101SOliver Hartkopp 
686c18ce101SOliver Hartkopp 	case CAN_RAW_ERR_FILTER:
687c18ce101SOliver Hartkopp 		if (len > sizeof(can_err_mask_t))
688c18ce101SOliver Hartkopp 			len = sizeof(can_err_mask_t);
689c18ce101SOliver Hartkopp 		val = &ro->err_mask;
690c18ce101SOliver Hartkopp 		break;
691c18ce101SOliver Hartkopp 
692c18ce101SOliver Hartkopp 	case CAN_RAW_LOOPBACK:
693c18ce101SOliver Hartkopp 		if (len > sizeof(int))
694c18ce101SOliver Hartkopp 			len = sizeof(int);
695c18ce101SOliver Hartkopp 		val = &ro->loopback;
696c18ce101SOliver Hartkopp 		break;
697c18ce101SOliver Hartkopp 
698c18ce101SOliver Hartkopp 	case CAN_RAW_RECV_OWN_MSGS:
699c18ce101SOliver Hartkopp 		if (len > sizeof(int))
700c18ce101SOliver Hartkopp 			len = sizeof(int);
701c18ce101SOliver Hartkopp 		val = &ro->recv_own_msgs;
702c18ce101SOliver Hartkopp 		break;
703c18ce101SOliver Hartkopp 
704e2d265d3SOliver Hartkopp 	case CAN_RAW_FD_FRAMES:
705e2d265d3SOliver Hartkopp 		if (len > sizeof(int))
706e2d265d3SOliver Hartkopp 			len = sizeof(int);
707e2d265d3SOliver Hartkopp 		val = &ro->fd_frames;
708e2d265d3SOliver Hartkopp 		break;
709e2d265d3SOliver Hartkopp 
710a5581ef4SOliver Hartkopp 	case CAN_RAW_JOIN_FILTERS:
711a5581ef4SOliver Hartkopp 		if (len > sizeof(int))
712a5581ef4SOliver Hartkopp 			len = sizeof(int);
713a5581ef4SOliver Hartkopp 		val = &ro->join_filters;
714a5581ef4SOliver Hartkopp 		break;
715a5581ef4SOliver Hartkopp 
716c18ce101SOliver Hartkopp 	default:
717c18ce101SOliver Hartkopp 		return -ENOPROTOOPT;
718c18ce101SOliver Hartkopp 	}
719c18ce101SOliver Hartkopp 
720c18ce101SOliver Hartkopp 	if (put_user(len, optlen))
721c18ce101SOliver Hartkopp 		return -EFAULT;
722c18ce101SOliver Hartkopp 	if (copy_to_user(optval, val, len))
723c18ce101SOliver Hartkopp 		return -EFAULT;
724c18ce101SOliver Hartkopp 	return 0;
725c18ce101SOliver Hartkopp }
726c18ce101SOliver Hartkopp 
7271b784140SYing Xue static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
728c18ce101SOliver Hartkopp {
729c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
730c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
731c18ce101SOliver Hartkopp 	struct sk_buff *skb;
732c18ce101SOliver Hartkopp 	struct net_device *dev;
733c18ce101SOliver Hartkopp 	int ifindex;
734c18ce101SOliver Hartkopp 	int err;
735c18ce101SOliver Hartkopp 
736c18ce101SOliver Hartkopp 	if (msg->msg_name) {
737342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
738c18ce101SOliver Hartkopp 
7395e507328SKurt Van Dijck 		if (msg->msg_namelen < sizeof(*addr))
7405e507328SKurt Van Dijck 			return -EINVAL;
7415e507328SKurt Van Dijck 
742c18ce101SOliver Hartkopp 		if (addr->can_family != AF_CAN)
743c18ce101SOliver Hartkopp 			return -EINVAL;
744c18ce101SOliver Hartkopp 
745c18ce101SOliver Hartkopp 		ifindex = addr->can_ifindex;
746c18ce101SOliver Hartkopp 	} else
747c18ce101SOliver Hartkopp 		ifindex = ro->ifindex;
748c18ce101SOliver Hartkopp 
7498e8cda6dSMario Kicherer 	dev = dev_get_by_index(sock_net(sk), ifindex);
750c18ce101SOliver Hartkopp 	if (!dev)
751c18ce101SOliver Hartkopp 		return -ENXIO;
752c18ce101SOliver Hartkopp 
753a43608faSOliver Hartkopp 	err = -EINVAL;
754a43608faSOliver Hartkopp 	if (ro->fd_frames && dev->mtu == CANFD_MTU) {
755a43608faSOliver Hartkopp 		if (unlikely(size != CANFD_MTU && size != CAN_MTU))
756a43608faSOliver Hartkopp 			goto put_dev;
757a43608faSOliver Hartkopp 	} else {
758a43608faSOliver Hartkopp 		if (unlikely(size != CAN_MTU))
759a43608faSOliver Hartkopp 			goto put_dev;
760a43608faSOliver Hartkopp 	}
761a43608faSOliver Hartkopp 
762156c2bb9SOliver Hartkopp 	skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
763156c2bb9SOliver Hartkopp 				  msg->msg_flags & MSG_DONTWAIT, &err);
764ebad5c09SIlpo Järvinen 	if (!skb)
765ebad5c09SIlpo Järvinen 		goto put_dev;
766c18ce101SOliver Hartkopp 
7672bf3440dSOliver Hartkopp 	can_skb_reserve(skb);
7682bf3440dSOliver Hartkopp 	can_skb_prv(skb)->ifindex = dev->ifindex;
769d3b58c47SOliver Hartkopp 	can_skb_prv(skb)->skbcnt = 0;
770156c2bb9SOliver Hartkopp 
7716ce8e9ceSAl Viro 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
772ebad5c09SIlpo Järvinen 	if (err < 0)
773ebad5c09SIlpo Järvinen 		goto free_skb;
774bf84a010SDaniel Borkmann 
7758f932f76SWillem de Bruijn 	skb_setup_tx_timestamp(skb, sk->sk_tsflags);
776cff0d6e6SOliver Hartkopp 
777c18ce101SOliver Hartkopp 	skb->dev = dev;
778c18ce101SOliver Hartkopp 	skb->sk  = sk;
779bb5ecb0cSRostislav Lisovy 	skb->priority = sk->sk_priority;
780c18ce101SOliver Hartkopp 
781c18ce101SOliver Hartkopp 	err = can_send(skb, ro->loopback);
782c18ce101SOliver Hartkopp 
783c18ce101SOliver Hartkopp 	dev_put(dev);
784c18ce101SOliver Hartkopp 
785c18ce101SOliver Hartkopp 	if (err)
786ebad5c09SIlpo Järvinen 		goto send_failed;
787c18ce101SOliver Hartkopp 
788c18ce101SOliver Hartkopp 	return size;
789ebad5c09SIlpo Järvinen 
790ebad5c09SIlpo Järvinen free_skb:
791ebad5c09SIlpo Järvinen 	kfree_skb(skb);
792ebad5c09SIlpo Järvinen put_dev:
793ebad5c09SIlpo Järvinen 	dev_put(dev);
794ebad5c09SIlpo Järvinen send_failed:
795ebad5c09SIlpo Järvinen 	return err;
796c18ce101SOliver Hartkopp }
797c18ce101SOliver Hartkopp 
7981b784140SYing Xue static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
7991b784140SYing Xue 		       int flags)
800c18ce101SOliver Hartkopp {
801c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
802c18ce101SOliver Hartkopp 	struct sk_buff *skb;
803a219994bSUrs Thuermann 	int err = 0;
804c18ce101SOliver Hartkopp 	int noblock;
805c18ce101SOliver Hartkopp 
806c18ce101SOliver Hartkopp 	noblock =  flags & MSG_DONTWAIT;
807c18ce101SOliver Hartkopp 	flags   &= ~MSG_DONTWAIT;
808c18ce101SOliver Hartkopp 
809a219994bSUrs Thuermann 	skb = skb_recv_datagram(sk, flags, noblock, &err);
810c18ce101SOliver Hartkopp 	if (!skb)
811a219994bSUrs Thuermann 		return err;
812c18ce101SOliver Hartkopp 
813821047c4SOliver Hartkopp 	if (size < skb->len)
814c18ce101SOliver Hartkopp 		msg->msg_flags |= MSG_TRUNC;
815c18ce101SOliver Hartkopp 	else
816821047c4SOliver Hartkopp 		size = skb->len;
817c18ce101SOliver Hartkopp 
8187eab8d9eSAl Viro 	err = memcpy_to_msg(msg, skb->data, size);
819a219994bSUrs Thuermann 	if (err < 0) {
820c18ce101SOliver Hartkopp 		skb_free_datagram(sk, skb);
821a219994bSUrs Thuermann 		return err;
822c18ce101SOliver Hartkopp 	}
823c18ce101SOliver Hartkopp 
8243b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
825c18ce101SOliver Hartkopp 
826c18ce101SOliver Hartkopp 	if (msg->msg_name) {
827342dfc30SSteffen Hurrle 		__sockaddr_check_size(sizeof(struct sockaddr_can));
828c18ce101SOliver Hartkopp 		msg->msg_namelen = sizeof(struct sockaddr_can);
829c18ce101SOliver Hartkopp 		memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
830c18ce101SOliver Hartkopp 	}
831c18ce101SOliver Hartkopp 
8321e55659cSOliver Hartkopp 	/* assign the flags that have been recorded in raw_rcv() */
8331e55659cSOliver Hartkopp 	msg->msg_flags |= *(raw_flags(skb));
8341e55659cSOliver Hartkopp 
835c18ce101SOliver Hartkopp 	skb_free_datagram(sk, skb);
836c18ce101SOliver Hartkopp 
837c18ce101SOliver Hartkopp 	return size;
838c18ce101SOliver Hartkopp }
839c18ce101SOliver Hartkopp 
840473d924dSOliver Hartkopp int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
841473d924dSOliver Hartkopp 			 unsigned long arg)
842473d924dSOliver Hartkopp {
843473d924dSOliver Hartkopp 	/* no ioctls for socket layer -> hand it down to NIC layer */
844473d924dSOliver Hartkopp 	return -ENOIOCTLCMD;
845473d924dSOliver Hartkopp }
846473d924dSOliver Hartkopp 
84753914b67SOliver Hartkopp static const struct proto_ops raw_ops = {
848c18ce101SOliver Hartkopp 	.family        = PF_CAN,
849c18ce101SOliver Hartkopp 	.release       = raw_release,
850c18ce101SOliver Hartkopp 	.bind          = raw_bind,
851c18ce101SOliver Hartkopp 	.connect       = sock_no_connect,
852c18ce101SOliver Hartkopp 	.socketpair    = sock_no_socketpair,
853c18ce101SOliver Hartkopp 	.accept        = sock_no_accept,
854c18ce101SOliver Hartkopp 	.getname       = raw_getname,
855a11e1d43SLinus Torvalds 	.poll          = datagram_poll,
856473d924dSOliver Hartkopp 	.ioctl         = raw_sock_no_ioctlcmd,
857c7cbdbf2SArnd Bergmann 	.gettstamp     = sock_gettstamp,
858c18ce101SOliver Hartkopp 	.listen        = sock_no_listen,
859c18ce101SOliver Hartkopp 	.shutdown      = sock_no_shutdown,
860c18ce101SOliver Hartkopp 	.setsockopt    = raw_setsockopt,
861c18ce101SOliver Hartkopp 	.getsockopt    = raw_getsockopt,
862c18ce101SOliver Hartkopp 	.sendmsg       = raw_sendmsg,
863c18ce101SOliver Hartkopp 	.recvmsg       = raw_recvmsg,
864c18ce101SOliver Hartkopp 	.mmap          = sock_no_mmap,
865c18ce101SOliver Hartkopp 	.sendpage      = sock_no_sendpage,
866c18ce101SOliver Hartkopp };
867c18ce101SOliver Hartkopp 
868c18ce101SOliver Hartkopp static struct proto raw_proto __read_mostly = {
869c18ce101SOliver Hartkopp 	.name       = "CAN_RAW",
870c18ce101SOliver Hartkopp 	.owner      = THIS_MODULE,
871c18ce101SOliver Hartkopp 	.obj_size   = sizeof(struct raw_sock),
872c18ce101SOliver Hartkopp 	.init       = raw_init,
873c18ce101SOliver Hartkopp };
874c18ce101SOliver Hartkopp 
8751650629dSKurt Van Dijck static const struct can_proto raw_can_proto = {
876c18ce101SOliver Hartkopp 	.type       = SOCK_RAW,
877c18ce101SOliver Hartkopp 	.protocol   = CAN_RAW,
878c18ce101SOliver Hartkopp 	.ops        = &raw_ops,
879c18ce101SOliver Hartkopp 	.prot       = &raw_proto,
880c18ce101SOliver Hartkopp };
881c18ce101SOliver Hartkopp 
882c18ce101SOliver Hartkopp static __init int raw_module_init(void)
883c18ce101SOliver Hartkopp {
884c18ce101SOliver Hartkopp 	int err;
885c18ce101SOliver Hartkopp 
886b111b78cSJeremiah Mahler 	pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n");
887c18ce101SOliver Hartkopp 
888c18ce101SOliver Hartkopp 	err = can_proto_register(&raw_can_proto);
889c18ce101SOliver Hartkopp 	if (err < 0)
890c18ce101SOliver Hartkopp 		printk(KERN_ERR "can: registration of raw protocol failed\n");
891c18ce101SOliver Hartkopp 
892c18ce101SOliver Hartkopp 	return err;
893c18ce101SOliver Hartkopp }
894c18ce101SOliver Hartkopp 
895c18ce101SOliver Hartkopp static __exit void raw_module_exit(void)
896c18ce101SOliver Hartkopp {
897c18ce101SOliver Hartkopp 	can_proto_unregister(&raw_can_proto);
898c18ce101SOliver Hartkopp }
899c18ce101SOliver Hartkopp 
900c18ce101SOliver Hartkopp module_init(raw_module_init);
901c18ce101SOliver Hartkopp module_exit(raw_module_exit);
902