xref: /openbmc/linux/net/can/raw.c (revision 6fd1d51c)
1d77cd7feSMasahiro Yamada // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2d6ada83bSMarc Kleine-Budde /* raw.c - Raw sockets for protocol family CAN
3c18ce101SOliver Hartkopp  *
4c18ce101SOliver Hartkopp  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5c18ce101SOliver Hartkopp  * All rights reserved.
6c18ce101SOliver Hartkopp  *
7c18ce101SOliver Hartkopp  * Redistribution and use in source and binary forms, with or without
8c18ce101SOliver Hartkopp  * modification, are permitted provided that the following conditions
9c18ce101SOliver Hartkopp  * are met:
10c18ce101SOliver Hartkopp  * 1. Redistributions of source code must retain the above copyright
11c18ce101SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer.
12c18ce101SOliver Hartkopp  * 2. Redistributions in binary form must reproduce the above copyright
13c18ce101SOliver Hartkopp  *    notice, this list of conditions and the following disclaimer in the
14c18ce101SOliver Hartkopp  *    documentation and/or other materials provided with the distribution.
15c18ce101SOliver Hartkopp  * 3. Neither the name of Volkswagen nor the names of its contributors
16c18ce101SOliver Hartkopp  *    may be used to endorse or promote products derived from this software
17c18ce101SOliver Hartkopp  *    without specific prior written permission.
18c18ce101SOliver Hartkopp  *
19c18ce101SOliver Hartkopp  * Alternatively, provided that this notice is retained in full, this
20c18ce101SOliver Hartkopp  * software may be distributed under the terms of the GNU General
21c18ce101SOliver Hartkopp  * Public License ("GPL") version 2, in which case the provisions of the
22c18ce101SOliver Hartkopp  * GPL apply INSTEAD OF those given above.
23c18ce101SOliver Hartkopp  *
24c18ce101SOliver Hartkopp  * The provided data structures and external interfaces from this code
25c18ce101SOliver Hartkopp  * are not restricted to be used by modules with a GPL compatible license.
26c18ce101SOliver Hartkopp  *
27c18ce101SOliver Hartkopp  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28c18ce101SOliver Hartkopp  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29c18ce101SOliver Hartkopp  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30c18ce101SOliver Hartkopp  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31c18ce101SOliver Hartkopp  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32c18ce101SOliver Hartkopp  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33c18ce101SOliver Hartkopp  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34c18ce101SOliver Hartkopp  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35c18ce101SOliver Hartkopp  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36c18ce101SOliver Hartkopp  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37c18ce101SOliver Hartkopp  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38c18ce101SOliver Hartkopp  * DAMAGE.
39c18ce101SOliver Hartkopp  *
40c18ce101SOliver Hartkopp  */
41c18ce101SOliver Hartkopp 
42c18ce101SOliver Hartkopp #include <linux/module.h>
43c18ce101SOliver Hartkopp #include <linux/init.h>
44c18ce101SOliver Hartkopp #include <linux/uio.h>
45c18ce101SOliver Hartkopp #include <linux/net.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47c18ce101SOliver Hartkopp #include <linux/netdevice.h>
48c18ce101SOliver Hartkopp #include <linux/socket.h>
49c18ce101SOliver Hartkopp #include <linux/if_arp.h>
50c18ce101SOliver Hartkopp #include <linux/skbuff.h>
51c18ce101SOliver Hartkopp #include <linux/can.h>
52c18ce101SOliver Hartkopp #include <linux/can/core.h>
53156c2bb9SOliver Hartkopp #include <linux/can/skb.h>
54c18ce101SOliver Hartkopp #include <linux/can/raw.h>
55c18ce101SOliver Hartkopp #include <net/sock.h>
56c18ce101SOliver Hartkopp #include <net/net_namespace.h>
57c18ce101SOliver Hartkopp 
58c18ce101SOliver Hartkopp MODULE_DESCRIPTION("PF_CAN raw protocol");
59c18ce101SOliver Hartkopp MODULE_LICENSE("Dual BSD/GPL");
60c18ce101SOliver Hartkopp MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
61b13bb2e9SLothar Waßmann MODULE_ALIAS("can-proto-1");
62c18ce101SOliver Hartkopp 
639e971474SOliver Hartkopp #define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
649e971474SOliver Hartkopp 
65c18ce101SOliver Hartkopp #define MASK_ALL 0
66c18ce101SOliver Hartkopp 
67d6ada83bSMarc Kleine-Budde /* A raw socket has a list of can_filters attached to it, each receiving
68c18ce101SOliver Hartkopp  * the CAN frames matching that filter.  If the filter list is empty,
69c18ce101SOliver Hartkopp  * no CAN frames will be received by the socket.  The default after
70c18ce101SOliver Hartkopp  * opening the socket, is to have one filter which receives all frames.
71c18ce101SOliver Hartkopp  * The filter list is allocated dynamically with the exception of the
72c18ce101SOliver Hartkopp  * list containing only one item.  This common case is optimized by
73c18ce101SOliver Hartkopp  * storing the single filter in dfilter, to avoid using dynamic memory.
74c18ce101SOliver Hartkopp  */
75c18ce101SOliver Hartkopp 
76514ac99cSOliver Hartkopp struct uniqframe {
77d3b58c47SOliver Hartkopp 	int skbcnt;
78514ac99cSOliver Hartkopp 	const struct sk_buff *skb;
79a5581ef4SOliver Hartkopp 	unsigned int join_rx_count;
80514ac99cSOliver Hartkopp };
81514ac99cSOliver Hartkopp 
82c18ce101SOliver Hartkopp struct raw_sock {
83c18ce101SOliver Hartkopp 	struct sock sk;
84c18ce101SOliver Hartkopp 	int bound;
85c18ce101SOliver Hartkopp 	int ifindex;
868d0caedbSTetsuo Handa 	struct list_head notifier;
87c18ce101SOliver Hartkopp 	int loopback;
88c18ce101SOliver Hartkopp 	int recv_own_msgs;
89e2d265d3SOliver Hartkopp 	int fd_frames;
90a5581ef4SOliver Hartkopp 	int join_filters;
91c18ce101SOliver Hartkopp 	int count;                 /* number of active filters */
92c18ce101SOliver Hartkopp 	struct can_filter dfilter; /* default/single filter */
93c18ce101SOliver Hartkopp 	struct can_filter *filter; /* pointer to filter(s) */
94c18ce101SOliver Hartkopp 	can_err_mask_t err_mask;
95514ac99cSOliver Hartkopp 	struct uniqframe __percpu *uniq;
96c18ce101SOliver Hartkopp };
97c18ce101SOliver Hartkopp 
988d0caedbSTetsuo Handa static LIST_HEAD(raw_notifier_list);
998d0caedbSTetsuo Handa static DEFINE_SPINLOCK(raw_notifier_lock);
1008d0caedbSTetsuo Handa static struct raw_sock *raw_busy_notifier;
1018d0caedbSTetsuo Handa 
102d6ada83bSMarc Kleine-Budde /* Return pointer to store the extra msg flags for raw_recvmsg().
1031e55659cSOliver Hartkopp  * We use the space of one unsigned int beyond the 'struct sockaddr_can'
1041e55659cSOliver Hartkopp  * in skb->cb.
1051e55659cSOliver Hartkopp  */
1061e55659cSOliver Hartkopp static inline unsigned int *raw_flags(struct sk_buff *skb)
1071e55659cSOliver Hartkopp {
108b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
109b4772ef8SEyal Birger 			       sizeof(unsigned int));
1101e55659cSOliver Hartkopp 
1111e55659cSOliver Hartkopp 	/* return pointer after struct sockaddr_can */
1121e55659cSOliver Hartkopp 	return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
1131e55659cSOliver Hartkopp }
1141e55659cSOliver Hartkopp 
115c18ce101SOliver Hartkopp static inline struct raw_sock *raw_sk(const struct sock *sk)
116c18ce101SOliver Hartkopp {
117c18ce101SOliver Hartkopp 	return (struct raw_sock *)sk;
118c18ce101SOliver Hartkopp }
119c18ce101SOliver Hartkopp 
1201e55659cSOliver Hartkopp static void raw_rcv(struct sk_buff *oskb, void *data)
121c18ce101SOliver Hartkopp {
122c18ce101SOliver Hartkopp 	struct sock *sk = (struct sock *)data;
123c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
124c18ce101SOliver Hartkopp 	struct sockaddr_can *addr;
1251e55659cSOliver Hartkopp 	struct sk_buff *skb;
1261e55659cSOliver Hartkopp 	unsigned int *pflags;
127c18ce101SOliver Hartkopp 
128c18ce101SOliver Hartkopp 	/* check the received tx sock reference */
1291e55659cSOliver Hartkopp 	if (!ro->recv_own_msgs && oskb->sk == sk)
130c18ce101SOliver Hartkopp 		return;
1311fa17d4bSOliver Hartkopp 
132821047c4SOliver Hartkopp 	/* do not pass non-CAN2.0 frames to a legacy socket */
133821047c4SOliver Hartkopp 	if (!ro->fd_frames && oskb->len != CAN_MTU)
134e2d265d3SOliver Hartkopp 		return;
135e2d265d3SOliver Hartkopp 
136514ac99cSOliver Hartkopp 	/* eliminate multiple filter matches for the same skb */
137514ac99cSOliver Hartkopp 	if (this_cpu_ptr(ro->uniq)->skb == oskb &&
138d3b58c47SOliver Hartkopp 	    this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
139a5581ef4SOliver Hartkopp 		if (ro->join_filters) {
140a5581ef4SOliver Hartkopp 			this_cpu_inc(ro->uniq->join_rx_count);
141a5581ef4SOliver Hartkopp 			/* drop frame until all enabled filters matched */
142a5581ef4SOliver Hartkopp 			if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
143514ac99cSOliver Hartkopp 				return;
144514ac99cSOliver Hartkopp 		} else {
145a5581ef4SOliver Hartkopp 			return;
146a5581ef4SOliver Hartkopp 		}
147a5581ef4SOliver Hartkopp 	} else {
148514ac99cSOliver Hartkopp 		this_cpu_ptr(ro->uniq)->skb = oskb;
149d3b58c47SOliver Hartkopp 		this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
150a5581ef4SOliver Hartkopp 		this_cpu_ptr(ro->uniq)->join_rx_count = 1;
151a5581ef4SOliver Hartkopp 		/* drop first frame to check all enabled filters? */
152a5581ef4SOliver Hartkopp 		if (ro->join_filters && ro->count > 1)
153a5581ef4SOliver Hartkopp 			return;
154514ac99cSOliver Hartkopp 	}
155514ac99cSOliver Hartkopp 
1561fa17d4bSOliver Hartkopp 	/* clone the given skb to be able to enqueue it into the rcv queue */
1571e55659cSOliver Hartkopp 	skb = skb_clone(oskb, GFP_ATOMIC);
1581fa17d4bSOliver Hartkopp 	if (!skb)
1591fa17d4bSOliver Hartkopp 		return;
160c18ce101SOliver Hartkopp 
1616a54dde8SMarc Kleine-Budde 	/* Put the datagram to the queue so that raw_recvmsg() can get
1626a54dde8SMarc Kleine-Budde 	 * it from there. We need to pass the interface index to
1636a54dde8SMarc Kleine-Budde 	 * raw_recvmsg(). We pass a whole struct sockaddr_can in
1646a54dde8SMarc Kleine-Budde 	 * skb->cb 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 
2708d0caedbSTetsuo Handa static void raw_notify(struct raw_sock *ro, unsigned long msg,
2718d0caedbSTetsuo Handa 		       struct net_device *dev)
272c18ce101SOliver Hartkopp {
273c18ce101SOliver Hartkopp 	struct sock *sk = &ro->sk;
274c18ce101SOliver Hartkopp 
2758e8cda6dSMario Kicherer 	if (!net_eq(dev_net(dev), sock_net(sk)))
2768d0caedbSTetsuo Handa 		return;
277c18ce101SOliver Hartkopp 
278c18ce101SOliver Hartkopp 	if (ro->ifindex != dev->ifindex)
2798d0caedbSTetsuo Handa 		return;
280c18ce101SOliver Hartkopp 
281c18ce101SOliver Hartkopp 	switch (msg) {
282c18ce101SOliver Hartkopp 	case NETDEV_UNREGISTER:
283c18ce101SOliver Hartkopp 		lock_sock(sk);
284c18ce101SOliver Hartkopp 		/* remove current filters & unregister */
285c18ce101SOliver Hartkopp 		if (ro->bound)
2868e8cda6dSMario Kicherer 			raw_disable_allfilters(dev_net(dev), dev, sk);
287c18ce101SOliver Hartkopp 
288c18ce101SOliver Hartkopp 		if (ro->count > 1)
289c18ce101SOliver Hartkopp 			kfree(ro->filter);
290c18ce101SOliver Hartkopp 
291c18ce101SOliver Hartkopp 		ro->ifindex = 0;
292c18ce101SOliver Hartkopp 		ro->bound = 0;
293c18ce101SOliver Hartkopp 		ro->count = 0;
294c18ce101SOliver Hartkopp 		release_sock(sk);
295c18ce101SOliver Hartkopp 
296c18ce101SOliver Hartkopp 		sk->sk_err = ENODEV;
297c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
298e3ae2365SAlexander Aring 			sk_error_report(sk);
299c18ce101SOliver Hartkopp 		break;
300c18ce101SOliver Hartkopp 
301c18ce101SOliver Hartkopp 	case NETDEV_DOWN:
302c18ce101SOliver Hartkopp 		sk->sk_err = ENETDOWN;
303c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
304e3ae2365SAlexander Aring 			sk_error_report(sk);
305c18ce101SOliver Hartkopp 		break;
306c18ce101SOliver Hartkopp 	}
3078d0caedbSTetsuo Handa }
308c18ce101SOliver Hartkopp 
3098d0caedbSTetsuo Handa static int raw_notifier(struct notifier_block *nb, unsigned long msg,
3108d0caedbSTetsuo Handa 			void *ptr)
3118d0caedbSTetsuo Handa {
3128d0caedbSTetsuo Handa 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3138d0caedbSTetsuo Handa 
3148d0caedbSTetsuo Handa 	if (dev->type != ARPHRD_CAN)
3158d0caedbSTetsuo Handa 		return NOTIFY_DONE;
3168d0caedbSTetsuo Handa 	if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
3178d0caedbSTetsuo Handa 		return NOTIFY_DONE;
3188d0caedbSTetsuo Handa 	if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
3198d0caedbSTetsuo Handa 		return NOTIFY_DONE;
3208d0caedbSTetsuo Handa 
3218d0caedbSTetsuo Handa 	spin_lock(&raw_notifier_lock);
3228d0caedbSTetsuo Handa 	list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
3238d0caedbSTetsuo Handa 		spin_unlock(&raw_notifier_lock);
3248d0caedbSTetsuo Handa 		raw_notify(raw_busy_notifier, msg, dev);
3258d0caedbSTetsuo Handa 		spin_lock(&raw_notifier_lock);
3268d0caedbSTetsuo Handa 	}
3278d0caedbSTetsuo Handa 	raw_busy_notifier = NULL;
3288d0caedbSTetsuo Handa 	spin_unlock(&raw_notifier_lock);
329c18ce101SOliver Hartkopp 	return NOTIFY_DONE;
330c18ce101SOliver Hartkopp }
331c18ce101SOliver Hartkopp 
332c18ce101SOliver Hartkopp static int raw_init(struct sock *sk)
333c18ce101SOliver Hartkopp {
334c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
335c18ce101SOliver Hartkopp 
336c18ce101SOliver Hartkopp 	ro->bound            = 0;
337c18ce101SOliver Hartkopp 	ro->ifindex          = 0;
338c18ce101SOliver Hartkopp 
339c18ce101SOliver Hartkopp 	/* set default filter to single entry dfilter */
340c18ce101SOliver Hartkopp 	ro->dfilter.can_id   = 0;
341c18ce101SOliver Hartkopp 	ro->dfilter.can_mask = MASK_ALL;
342c18ce101SOliver Hartkopp 	ro->filter           = &ro->dfilter;
343c18ce101SOliver Hartkopp 	ro->count            = 1;
344c18ce101SOliver Hartkopp 
345c18ce101SOliver Hartkopp 	/* set default loopback behaviour */
346c18ce101SOliver Hartkopp 	ro->loopback         = 1;
347c18ce101SOliver Hartkopp 	ro->recv_own_msgs    = 0;
348e2d265d3SOliver Hartkopp 	ro->fd_frames        = 0;
349a5581ef4SOliver Hartkopp 	ro->join_filters     = 0;
350c18ce101SOliver Hartkopp 
351514ac99cSOliver Hartkopp 	/* alloc_percpu provides zero'ed memory */
352514ac99cSOliver Hartkopp 	ro->uniq = alloc_percpu(struct uniqframe);
353514ac99cSOliver Hartkopp 	if (unlikely(!ro->uniq))
354514ac99cSOliver Hartkopp 		return -ENOMEM;
355514ac99cSOliver Hartkopp 
356c18ce101SOliver Hartkopp 	/* set notifier */
3578d0caedbSTetsuo Handa 	spin_lock(&raw_notifier_lock);
3588d0caedbSTetsuo Handa 	list_add_tail(&ro->notifier, &raw_notifier_list);
3598d0caedbSTetsuo Handa 	spin_unlock(&raw_notifier_lock);
360c18ce101SOliver Hartkopp 
361c18ce101SOliver Hartkopp 	return 0;
362c18ce101SOliver Hartkopp }
363c18ce101SOliver Hartkopp 
364c18ce101SOliver Hartkopp static int raw_release(struct socket *sock)
365c18ce101SOliver Hartkopp {
366c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
36710022a6cSOliver Hartkopp 	struct raw_sock *ro;
36810022a6cSOliver Hartkopp 
36910022a6cSOliver Hartkopp 	if (!sk)
37010022a6cSOliver Hartkopp 		return 0;
37110022a6cSOliver Hartkopp 
37210022a6cSOliver Hartkopp 	ro = raw_sk(sk);
373c18ce101SOliver Hartkopp 
3748d0caedbSTetsuo Handa 	spin_lock(&raw_notifier_lock);
3758d0caedbSTetsuo Handa 	while (raw_busy_notifier == ro) {
3768d0caedbSTetsuo Handa 		spin_unlock(&raw_notifier_lock);
3778d0caedbSTetsuo Handa 		schedule_timeout_uninterruptible(1);
3788d0caedbSTetsuo Handa 		spin_lock(&raw_notifier_lock);
3798d0caedbSTetsuo Handa 	}
3808d0caedbSTetsuo Handa 	list_del(&ro->notifier);
3818d0caedbSTetsuo Handa 	spin_unlock(&raw_notifier_lock);
382c18ce101SOliver Hartkopp 
383c18ce101SOliver Hartkopp 	lock_sock(sk);
384c18ce101SOliver Hartkopp 
385c18ce101SOliver Hartkopp 	/* remove current filters & unregister */
386c18ce101SOliver Hartkopp 	if (ro->bound) {
387c18ce101SOliver Hartkopp 		if (ro->ifindex) {
388c18ce101SOliver Hartkopp 			struct net_device *dev;
389c18ce101SOliver Hartkopp 
3908e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
391c18ce101SOliver Hartkopp 			if (dev) {
3928e8cda6dSMario Kicherer 				raw_disable_allfilters(dev_net(dev), dev, sk);
393c18ce101SOliver Hartkopp 				dev_put(dev);
394c18ce101SOliver Hartkopp 			}
395bff10040SMarc Kleine-Budde 		} else {
3968e8cda6dSMario Kicherer 			raw_disable_allfilters(sock_net(sk), NULL, sk);
397c18ce101SOliver Hartkopp 		}
398bff10040SMarc Kleine-Budde 	}
399c18ce101SOliver Hartkopp 
400c18ce101SOliver Hartkopp 	if (ro->count > 1)
401c18ce101SOliver Hartkopp 		kfree(ro->filter);
402c18ce101SOliver Hartkopp 
403c18ce101SOliver Hartkopp 	ro->ifindex = 0;
404c18ce101SOliver Hartkopp 	ro->bound = 0;
405c18ce101SOliver Hartkopp 	ro->count = 0;
406514ac99cSOliver Hartkopp 	free_percpu(ro->uniq);
407c18ce101SOliver Hartkopp 
408f7e5cc0cSLothar Waßmann 	sock_orphan(sk);
409f7e5cc0cSLothar Waßmann 	sock->sk = NULL;
410f7e5cc0cSLothar Waßmann 
411c18ce101SOliver Hartkopp 	release_sock(sk);
412c18ce101SOliver Hartkopp 	sock_put(sk);
413c18ce101SOliver Hartkopp 
414c18ce101SOliver Hartkopp 	return 0;
415c18ce101SOliver Hartkopp }
416c18ce101SOliver Hartkopp 
417c18ce101SOliver Hartkopp static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
418c18ce101SOliver Hartkopp {
419c18ce101SOliver Hartkopp 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
420c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
421c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
422c18ce101SOliver Hartkopp 	int ifindex;
423c18ce101SOliver Hartkopp 	int err = 0;
424c18ce101SOliver Hartkopp 	int notify_enetdown = 0;
425c18ce101SOliver Hartkopp 
4269e971474SOliver Hartkopp 	if (len < RAW_MIN_NAMELEN)
427c18ce101SOliver Hartkopp 		return -EINVAL;
428adb552c3SMarc Kleine-Budde 	if (addr->can_family != AF_CAN)
429adb552c3SMarc Kleine-Budde 		return -EINVAL;
430c18ce101SOliver Hartkopp 
431c18ce101SOliver Hartkopp 	lock_sock(sk);
432c18ce101SOliver Hartkopp 
433c18ce101SOliver Hartkopp 	if (ro->bound && addr->can_ifindex == ro->ifindex)
434c18ce101SOliver Hartkopp 		goto out;
435c18ce101SOliver Hartkopp 
436c18ce101SOliver Hartkopp 	if (addr->can_ifindex) {
437c18ce101SOliver Hartkopp 		struct net_device *dev;
438c18ce101SOliver Hartkopp 
4398e8cda6dSMario Kicherer 		dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
440c18ce101SOliver Hartkopp 		if (!dev) {
441c18ce101SOliver Hartkopp 			err = -ENODEV;
442c18ce101SOliver Hartkopp 			goto out;
443c18ce101SOliver Hartkopp 		}
444c18ce101SOliver Hartkopp 		if (dev->type != ARPHRD_CAN) {
445c18ce101SOliver Hartkopp 			dev_put(dev);
446c18ce101SOliver Hartkopp 			err = -ENODEV;
447c18ce101SOliver Hartkopp 			goto out;
448c18ce101SOliver Hartkopp 		}
449c18ce101SOliver Hartkopp 		if (!(dev->flags & IFF_UP))
450c18ce101SOliver Hartkopp 			notify_enetdown = 1;
451c18ce101SOliver Hartkopp 
452c18ce101SOliver Hartkopp 		ifindex = dev->ifindex;
453c18ce101SOliver Hartkopp 
454c18ce101SOliver Hartkopp 		/* filters set by default/setsockopt */
4558e8cda6dSMario Kicherer 		err = raw_enable_allfilters(sock_net(sk), dev, sk);
456c18ce101SOliver Hartkopp 		dev_put(dev);
457c18ce101SOliver Hartkopp 	} else {
458c18ce101SOliver Hartkopp 		ifindex = 0;
459c18ce101SOliver Hartkopp 
460c18ce101SOliver Hartkopp 		/* filters set by default/setsockopt */
4618e8cda6dSMario Kicherer 		err = raw_enable_allfilters(sock_net(sk), NULL, sk);
462c18ce101SOliver Hartkopp 	}
463c18ce101SOliver Hartkopp 
464c18ce101SOliver Hartkopp 	if (!err) {
465c18ce101SOliver Hartkopp 		if (ro->bound) {
466c18ce101SOliver Hartkopp 			/* unregister old filters */
467c18ce101SOliver Hartkopp 			if (ro->ifindex) {
468c18ce101SOliver Hartkopp 				struct net_device *dev;
469c18ce101SOliver Hartkopp 
4708e8cda6dSMario Kicherer 				dev = dev_get_by_index(sock_net(sk),
4718e8cda6dSMario Kicherer 						       ro->ifindex);
472c18ce101SOliver Hartkopp 				if (dev) {
4738e8cda6dSMario Kicherer 					raw_disable_allfilters(dev_net(dev),
4748e8cda6dSMario Kicherer 							       dev, sk);
475c18ce101SOliver Hartkopp 					dev_put(dev);
476c18ce101SOliver Hartkopp 				}
477bff10040SMarc Kleine-Budde 			} else {
4788e8cda6dSMario Kicherer 				raw_disable_allfilters(sock_net(sk), NULL, sk);
479c18ce101SOliver Hartkopp 			}
480bff10040SMarc Kleine-Budde 		}
481c18ce101SOliver Hartkopp 		ro->ifindex = ifindex;
482c18ce101SOliver Hartkopp 		ro->bound = 1;
483c18ce101SOliver Hartkopp 	}
484c18ce101SOliver Hartkopp 
485c18ce101SOliver Hartkopp  out:
486c18ce101SOliver Hartkopp 	release_sock(sk);
487c18ce101SOliver Hartkopp 
488c18ce101SOliver Hartkopp 	if (notify_enetdown) {
489c18ce101SOliver Hartkopp 		sk->sk_err = ENETDOWN;
490c18ce101SOliver Hartkopp 		if (!sock_flag(sk, SOCK_DEAD))
491e3ae2365SAlexander Aring 			sk_error_report(sk);
492c18ce101SOliver Hartkopp 	}
493c18ce101SOliver Hartkopp 
494c18ce101SOliver Hartkopp 	return err;
495c18ce101SOliver Hartkopp }
496c18ce101SOliver Hartkopp 
497c18ce101SOliver Hartkopp static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
4989b2c45d4SDenys Vlasenko 		       int peer)
499c18ce101SOliver Hartkopp {
500c18ce101SOliver Hartkopp 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
501c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
502c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
503c18ce101SOliver Hartkopp 
504c18ce101SOliver Hartkopp 	if (peer)
505c18ce101SOliver Hartkopp 		return -EOPNOTSUPP;
506c18ce101SOliver Hartkopp 
5079e971474SOliver Hartkopp 	memset(addr, 0, RAW_MIN_NAMELEN);
508c18ce101SOliver Hartkopp 	addr->can_family  = AF_CAN;
509c18ce101SOliver Hartkopp 	addr->can_ifindex = ro->ifindex;
510c18ce101SOliver Hartkopp 
5119e971474SOliver Hartkopp 	return RAW_MIN_NAMELEN;
512c18ce101SOliver Hartkopp }
513c18ce101SOliver Hartkopp 
514c18ce101SOliver Hartkopp static int raw_setsockopt(struct socket *sock, int level, int optname,
515a7b75c5aSChristoph Hellwig 			  sockptr_t optval, unsigned int optlen)
516c18ce101SOliver Hartkopp {
517c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
518c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
519c18ce101SOliver Hartkopp 	struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
520c18ce101SOliver Hartkopp 	struct can_filter sfilter;         /* single filter */
521c18ce101SOliver Hartkopp 	struct net_device *dev = NULL;
522c18ce101SOliver Hartkopp 	can_err_mask_t err_mask = 0;
523c18ce101SOliver Hartkopp 	int count = 0;
524c18ce101SOliver Hartkopp 	int err = 0;
525c18ce101SOliver Hartkopp 
526c18ce101SOliver Hartkopp 	if (level != SOL_CAN_RAW)
527c18ce101SOliver Hartkopp 		return -EINVAL;
528c18ce101SOliver Hartkopp 
529c18ce101SOliver Hartkopp 	switch (optname) {
530c18ce101SOliver Hartkopp 	case CAN_RAW_FILTER:
531c18ce101SOliver Hartkopp 		if (optlen % sizeof(struct can_filter) != 0)
532c18ce101SOliver Hartkopp 			return -EINVAL;
533c18ce101SOliver Hartkopp 
534332b05caSMarc Kleine-Budde 		if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
535332b05caSMarc Kleine-Budde 			return -EINVAL;
536332b05caSMarc Kleine-Budde 
537c18ce101SOliver Hartkopp 		count = optlen / sizeof(struct can_filter);
538c18ce101SOliver Hartkopp 
539c18ce101SOliver Hartkopp 		if (count > 1) {
540c18ce101SOliver Hartkopp 			/* filter does not fit into dfilter => alloc space */
541a7b75c5aSChristoph Hellwig 			filter = memdup_sockptr(optval, optlen);
54216dff918SJulia Lawall 			if (IS_ERR(filter))
54316dff918SJulia Lawall 				return PTR_ERR(filter);
544c18ce101SOliver Hartkopp 		} else if (count == 1) {
545a7b75c5aSChristoph Hellwig 			if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
5463f91bd42SSam Ravnborg 				return -EFAULT;
547c18ce101SOliver Hartkopp 		}
548c18ce101SOliver Hartkopp 
54954f93336SZiyang Xuan 		rtnl_lock();
550c18ce101SOliver Hartkopp 		lock_sock(sk);
551c18ce101SOliver Hartkopp 
55254f93336SZiyang Xuan 		if (ro->bound && ro->ifindex) {
5538e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
55454f93336SZiyang Xuan 			if (!dev) {
55554f93336SZiyang Xuan 				if (count > 1)
55654f93336SZiyang Xuan 					kfree(filter);
55754f93336SZiyang Xuan 				err = -ENODEV;
55854f93336SZiyang Xuan 				goto out_fil;
55954f93336SZiyang Xuan 			}
56054f93336SZiyang Xuan 		}
561c18ce101SOliver Hartkopp 
562c18ce101SOliver Hartkopp 		if (ro->bound) {
563c18ce101SOliver Hartkopp 			/* (try to) register the new filters */
564c18ce101SOliver Hartkopp 			if (count == 1)
5658e8cda6dSMario Kicherer 				err = raw_enable_filters(sock_net(sk), dev, sk,
5668e8cda6dSMario Kicherer 							 &sfilter, 1);
567c18ce101SOliver Hartkopp 			else
5688e8cda6dSMario Kicherer 				err = raw_enable_filters(sock_net(sk), dev, sk,
5698e8cda6dSMario Kicherer 							 filter, count);
570c18ce101SOliver Hartkopp 			if (err) {
571c18ce101SOliver Hartkopp 				if (count > 1)
572c18ce101SOliver Hartkopp 					kfree(filter);
573c18ce101SOliver Hartkopp 				goto out_fil;
574c18ce101SOliver Hartkopp 			}
575c18ce101SOliver Hartkopp 
576c18ce101SOliver Hartkopp 			/* remove old filter registrations */
5778e8cda6dSMario Kicherer 			raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
5788e8cda6dSMario Kicherer 					    ro->count);
579c18ce101SOliver Hartkopp 		}
580c18ce101SOliver Hartkopp 
581c18ce101SOliver Hartkopp 		/* remove old filter space */
582c18ce101SOliver Hartkopp 		if (ro->count > 1)
583c18ce101SOliver Hartkopp 			kfree(ro->filter);
584c18ce101SOliver Hartkopp 
585c18ce101SOliver Hartkopp 		/* link new filters to the socket */
586c18ce101SOliver Hartkopp 		if (count == 1) {
587c18ce101SOliver Hartkopp 			/* copy filter data for single filter */
588c18ce101SOliver Hartkopp 			ro->dfilter = sfilter;
589c18ce101SOliver Hartkopp 			filter = &ro->dfilter;
590c18ce101SOliver Hartkopp 		}
591c18ce101SOliver Hartkopp 		ro->filter = filter;
592c18ce101SOliver Hartkopp 		ro->count  = count;
593c18ce101SOliver Hartkopp 
594c18ce101SOliver Hartkopp  out_fil:
595c18ce101SOliver Hartkopp 		dev_put(dev);
596c18ce101SOliver Hartkopp 		release_sock(sk);
59754f93336SZiyang Xuan 		rtnl_unlock();
598c18ce101SOliver Hartkopp 
599c18ce101SOliver Hartkopp 		break;
600c18ce101SOliver Hartkopp 
601c18ce101SOliver Hartkopp 	case CAN_RAW_ERR_FILTER:
602c18ce101SOliver Hartkopp 		if (optlen != sizeof(err_mask))
603c18ce101SOliver Hartkopp 			return -EINVAL;
604c18ce101SOliver Hartkopp 
605a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&err_mask, optval, optlen))
6063f91bd42SSam Ravnborg 			return -EFAULT;
607c18ce101SOliver Hartkopp 
608c18ce101SOliver Hartkopp 		err_mask &= CAN_ERR_MASK;
609c18ce101SOliver Hartkopp 
61054f93336SZiyang Xuan 		rtnl_lock();
611c18ce101SOliver Hartkopp 		lock_sock(sk);
612c18ce101SOliver Hartkopp 
61354f93336SZiyang Xuan 		if (ro->bound && ro->ifindex) {
6148e8cda6dSMario Kicherer 			dev = dev_get_by_index(sock_net(sk), ro->ifindex);
61554f93336SZiyang Xuan 			if (!dev) {
61654f93336SZiyang Xuan 				err = -ENODEV;
61754f93336SZiyang Xuan 				goto out_err;
61854f93336SZiyang Xuan 			}
61954f93336SZiyang Xuan 		}
620c18ce101SOliver Hartkopp 
621c18ce101SOliver Hartkopp 		/* remove current error mask */
622c18ce101SOliver Hartkopp 		if (ro->bound) {
623c18ce101SOliver Hartkopp 			/* (try to) register the new err_mask */
6248e8cda6dSMario Kicherer 			err = raw_enable_errfilter(sock_net(sk), dev, sk,
6258e8cda6dSMario Kicherer 						   err_mask);
626c18ce101SOliver Hartkopp 
627c18ce101SOliver Hartkopp 			if (err)
628c18ce101SOliver Hartkopp 				goto out_err;
629c18ce101SOliver Hartkopp 
630c18ce101SOliver Hartkopp 			/* remove old err_mask registration */
6318e8cda6dSMario Kicherer 			raw_disable_errfilter(sock_net(sk), dev, sk,
6328e8cda6dSMario Kicherer 					      ro->err_mask);
633c18ce101SOliver Hartkopp 		}
634c18ce101SOliver Hartkopp 
635c18ce101SOliver Hartkopp 		/* link new err_mask to the socket */
636c18ce101SOliver Hartkopp 		ro->err_mask = err_mask;
637c18ce101SOliver Hartkopp 
638c18ce101SOliver Hartkopp  out_err:
639c18ce101SOliver Hartkopp 		dev_put(dev);
640c18ce101SOliver Hartkopp 		release_sock(sk);
64154f93336SZiyang Xuan 		rtnl_unlock();
642c18ce101SOliver Hartkopp 
643c18ce101SOliver Hartkopp 		break;
644c18ce101SOliver Hartkopp 
645c18ce101SOliver Hartkopp 	case CAN_RAW_LOOPBACK:
646c18ce101SOliver Hartkopp 		if (optlen != sizeof(ro->loopback))
647c18ce101SOliver Hartkopp 			return -EINVAL;
648c18ce101SOliver Hartkopp 
649a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&ro->loopback, optval, optlen))
6503f91bd42SSam Ravnborg 			return -EFAULT;
651c18ce101SOliver Hartkopp 
652c18ce101SOliver Hartkopp 		break;
653c18ce101SOliver Hartkopp 
654c18ce101SOliver Hartkopp 	case CAN_RAW_RECV_OWN_MSGS:
655c18ce101SOliver Hartkopp 		if (optlen != sizeof(ro->recv_own_msgs))
656c18ce101SOliver Hartkopp 			return -EINVAL;
657c18ce101SOliver Hartkopp 
658a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
6593f91bd42SSam Ravnborg 			return -EFAULT;
660c18ce101SOliver Hartkopp 
661c18ce101SOliver Hartkopp 		break;
662c18ce101SOliver Hartkopp 
663e2d265d3SOliver Hartkopp 	case CAN_RAW_FD_FRAMES:
664e2d265d3SOliver Hartkopp 		if (optlen != sizeof(ro->fd_frames))
665e2d265d3SOliver Hartkopp 			return -EINVAL;
666e2d265d3SOliver Hartkopp 
667a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
668e2d265d3SOliver Hartkopp 			return -EFAULT;
669e2d265d3SOliver Hartkopp 
670e2d265d3SOliver Hartkopp 		break;
671e2d265d3SOliver Hartkopp 
672a5581ef4SOliver Hartkopp 	case CAN_RAW_JOIN_FILTERS:
673a5581ef4SOliver Hartkopp 		if (optlen != sizeof(ro->join_filters))
674a5581ef4SOliver Hartkopp 			return -EINVAL;
675a5581ef4SOliver Hartkopp 
676a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&ro->join_filters, optval, optlen))
677a5581ef4SOliver Hartkopp 			return -EFAULT;
678a5581ef4SOliver Hartkopp 
679a5581ef4SOliver Hartkopp 		break;
680a5581ef4SOliver Hartkopp 
681c18ce101SOliver Hartkopp 	default:
682c18ce101SOliver Hartkopp 		return -ENOPROTOOPT;
683c18ce101SOliver Hartkopp 	}
684c18ce101SOliver Hartkopp 	return err;
685c18ce101SOliver Hartkopp }
686c18ce101SOliver Hartkopp 
687c18ce101SOliver Hartkopp static int raw_getsockopt(struct socket *sock, int level, int optname,
688c18ce101SOliver Hartkopp 			  char __user *optval, int __user *optlen)
689c18ce101SOliver Hartkopp {
690c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
691c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
692c18ce101SOliver Hartkopp 	int len;
693c18ce101SOliver Hartkopp 	void *val;
694c18ce101SOliver Hartkopp 	int err = 0;
695c18ce101SOliver Hartkopp 
696c18ce101SOliver Hartkopp 	if (level != SOL_CAN_RAW)
697c18ce101SOliver Hartkopp 		return -EINVAL;
698c18ce101SOliver Hartkopp 	if (get_user(len, optlen))
699c18ce101SOliver Hartkopp 		return -EFAULT;
700c18ce101SOliver Hartkopp 	if (len < 0)
701c18ce101SOliver Hartkopp 		return -EINVAL;
702c18ce101SOliver Hartkopp 
703c18ce101SOliver Hartkopp 	switch (optname) {
704c18ce101SOliver Hartkopp 	case CAN_RAW_FILTER:
705c18ce101SOliver Hartkopp 		lock_sock(sk);
706c18ce101SOliver Hartkopp 		if (ro->count > 0) {
707c18ce101SOliver Hartkopp 			int fsize = ro->count * sizeof(struct can_filter);
708d5e4ecacSMarc Kleine-Budde 
7090de70e28SOliver Hartkopp 			/* user space buffer to small for filter list? */
7100de70e28SOliver Hartkopp 			if (len < fsize) {
7110de70e28SOliver Hartkopp 				/* return -ERANGE and needed space in optlen */
7120de70e28SOliver Hartkopp 				err = -ERANGE;
7130de70e28SOliver Hartkopp 				if (put_user(fsize, optlen))
7140de70e28SOliver Hartkopp 					err = -EFAULT;
7150de70e28SOliver Hartkopp 			} else {
716c18ce101SOliver Hartkopp 				if (len > fsize)
717c18ce101SOliver Hartkopp 					len = fsize;
718653252c2SPavel Emelyanov 				if (copy_to_user(optval, ro->filter, len))
719653252c2SPavel Emelyanov 					err = -EFAULT;
7200de70e28SOliver Hartkopp 			}
721bff10040SMarc Kleine-Budde 		} else {
722c18ce101SOliver Hartkopp 			len = 0;
723bff10040SMarc Kleine-Budde 		}
724c18ce101SOliver Hartkopp 		release_sock(sk);
725c18ce101SOliver Hartkopp 
726c18ce101SOliver Hartkopp 		if (!err)
727c18ce101SOliver Hartkopp 			err = put_user(len, optlen);
728c18ce101SOliver Hartkopp 		return err;
729c18ce101SOliver Hartkopp 
730c18ce101SOliver Hartkopp 	case CAN_RAW_ERR_FILTER:
731c18ce101SOliver Hartkopp 		if (len > sizeof(can_err_mask_t))
732c18ce101SOliver Hartkopp 			len = sizeof(can_err_mask_t);
733c18ce101SOliver Hartkopp 		val = &ro->err_mask;
734c18ce101SOliver Hartkopp 		break;
735c18ce101SOliver Hartkopp 
736c18ce101SOliver Hartkopp 	case CAN_RAW_LOOPBACK:
737c18ce101SOliver Hartkopp 		if (len > sizeof(int))
738c18ce101SOliver Hartkopp 			len = sizeof(int);
739c18ce101SOliver Hartkopp 		val = &ro->loopback;
740c18ce101SOliver Hartkopp 		break;
741c18ce101SOliver Hartkopp 
742c18ce101SOliver Hartkopp 	case CAN_RAW_RECV_OWN_MSGS:
743c18ce101SOliver Hartkopp 		if (len > sizeof(int))
744c18ce101SOliver Hartkopp 			len = sizeof(int);
745c18ce101SOliver Hartkopp 		val = &ro->recv_own_msgs;
746c18ce101SOliver Hartkopp 		break;
747c18ce101SOliver Hartkopp 
748e2d265d3SOliver Hartkopp 	case CAN_RAW_FD_FRAMES:
749e2d265d3SOliver Hartkopp 		if (len > sizeof(int))
750e2d265d3SOliver Hartkopp 			len = sizeof(int);
751e2d265d3SOliver Hartkopp 		val = &ro->fd_frames;
752e2d265d3SOliver Hartkopp 		break;
753e2d265d3SOliver Hartkopp 
754a5581ef4SOliver Hartkopp 	case CAN_RAW_JOIN_FILTERS:
755a5581ef4SOliver Hartkopp 		if (len > sizeof(int))
756a5581ef4SOliver Hartkopp 			len = sizeof(int);
757a5581ef4SOliver Hartkopp 		val = &ro->join_filters;
758a5581ef4SOliver Hartkopp 		break;
759a5581ef4SOliver Hartkopp 
760c18ce101SOliver Hartkopp 	default:
761c18ce101SOliver Hartkopp 		return -ENOPROTOOPT;
762c18ce101SOliver Hartkopp 	}
763c18ce101SOliver Hartkopp 
764c18ce101SOliver Hartkopp 	if (put_user(len, optlen))
765c18ce101SOliver Hartkopp 		return -EFAULT;
766c18ce101SOliver Hartkopp 	if (copy_to_user(optval, val, len))
767c18ce101SOliver Hartkopp 		return -EFAULT;
768c18ce101SOliver Hartkopp 	return 0;
769c18ce101SOliver Hartkopp }
770c18ce101SOliver Hartkopp 
7711b784140SYing Xue static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
772c18ce101SOliver Hartkopp {
773c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
774c18ce101SOliver Hartkopp 	struct raw_sock *ro = raw_sk(sk);
775c18ce101SOliver Hartkopp 	struct sk_buff *skb;
776c18ce101SOliver Hartkopp 	struct net_device *dev;
777c18ce101SOliver Hartkopp 	int ifindex;
778c18ce101SOliver Hartkopp 	int err;
779c18ce101SOliver Hartkopp 
780c18ce101SOliver Hartkopp 	if (msg->msg_name) {
781342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
782c18ce101SOliver Hartkopp 
7839e971474SOliver Hartkopp 		if (msg->msg_namelen < RAW_MIN_NAMELEN)
7845e507328SKurt Van Dijck 			return -EINVAL;
7855e507328SKurt Van Dijck 
786c18ce101SOliver Hartkopp 		if (addr->can_family != AF_CAN)
787c18ce101SOliver Hartkopp 			return -EINVAL;
788c18ce101SOliver Hartkopp 
789c18ce101SOliver Hartkopp 		ifindex = addr->can_ifindex;
790bff10040SMarc Kleine-Budde 	} else {
791c18ce101SOliver Hartkopp 		ifindex = ro->ifindex;
792bff10040SMarc Kleine-Budde 	}
793c18ce101SOliver Hartkopp 
7948e8cda6dSMario Kicherer 	dev = dev_get_by_index(sock_net(sk), ifindex);
795c18ce101SOliver Hartkopp 	if (!dev)
796c18ce101SOliver Hartkopp 		return -ENXIO;
797c18ce101SOliver Hartkopp 
798a43608faSOliver Hartkopp 	err = -EINVAL;
799a43608faSOliver Hartkopp 	if (ro->fd_frames && dev->mtu == CANFD_MTU) {
800a43608faSOliver Hartkopp 		if (unlikely(size != CANFD_MTU && size != CAN_MTU))
801a43608faSOliver Hartkopp 			goto put_dev;
802a43608faSOliver Hartkopp 	} else {
803a43608faSOliver Hartkopp 		if (unlikely(size != CAN_MTU))
804a43608faSOliver Hartkopp 			goto put_dev;
805a43608faSOliver Hartkopp 	}
806a43608faSOliver Hartkopp 
807156c2bb9SOliver Hartkopp 	skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
808156c2bb9SOliver Hartkopp 				  msg->msg_flags & MSG_DONTWAIT, &err);
809ebad5c09SIlpo Järvinen 	if (!skb)
810ebad5c09SIlpo Järvinen 		goto put_dev;
811c18ce101SOliver Hartkopp 
8122bf3440dSOliver Hartkopp 	can_skb_reserve(skb);
8132bf3440dSOliver Hartkopp 	can_skb_prv(skb)->ifindex = dev->ifindex;
814d3b58c47SOliver Hartkopp 	can_skb_prv(skb)->skbcnt = 0;
815156c2bb9SOliver Hartkopp 
8166ce8e9ceSAl Viro 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
817ebad5c09SIlpo Järvinen 	if (err < 0)
818ebad5c09SIlpo Järvinen 		goto free_skb;
819bf84a010SDaniel Borkmann 
8208f932f76SWillem de Bruijn 	skb_setup_tx_timestamp(skb, sk->sk_tsflags);
821cff0d6e6SOliver Hartkopp 
822c18ce101SOliver Hartkopp 	skb->dev = dev;
823c18ce101SOliver Hartkopp 	skb->sk = sk;
824bb5ecb0cSRostislav Lisovy 	skb->priority = sk->sk_priority;
825c18ce101SOliver Hartkopp 
826c18ce101SOliver Hartkopp 	err = can_send(skb, ro->loopback);
827c18ce101SOliver Hartkopp 
828c18ce101SOliver Hartkopp 	dev_put(dev);
829c18ce101SOliver Hartkopp 
830c18ce101SOliver Hartkopp 	if (err)
831ebad5c09SIlpo Järvinen 		goto send_failed;
832c18ce101SOliver Hartkopp 
833c18ce101SOliver Hartkopp 	return size;
834ebad5c09SIlpo Järvinen 
835ebad5c09SIlpo Järvinen free_skb:
836ebad5c09SIlpo Järvinen 	kfree_skb(skb);
837ebad5c09SIlpo Järvinen put_dev:
838ebad5c09SIlpo Järvinen 	dev_put(dev);
839ebad5c09SIlpo Järvinen send_failed:
840ebad5c09SIlpo Järvinen 	return err;
841c18ce101SOliver Hartkopp }
842c18ce101SOliver Hartkopp 
8431b784140SYing Xue static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
8441b784140SYing Xue 		       int flags)
845c18ce101SOliver Hartkopp {
846c18ce101SOliver Hartkopp 	struct sock *sk = sock->sk;
847c18ce101SOliver Hartkopp 	struct sk_buff *skb;
848a219994bSUrs Thuermann 	int err = 0;
849c18ce101SOliver Hartkopp 
850eb88531bSVincent Mailhol 	if (flags & MSG_ERRQUEUE)
851eb88531bSVincent Mailhol 		return sock_recv_errqueue(sk, msg, size,
852eb88531bSVincent Mailhol 					  SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
853eb88531bSVincent Mailhol 
854f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
855c18ce101SOliver Hartkopp 	if (!skb)
856a219994bSUrs Thuermann 		return err;
857c18ce101SOliver Hartkopp 
858821047c4SOliver Hartkopp 	if (size < skb->len)
859c18ce101SOliver Hartkopp 		msg->msg_flags |= MSG_TRUNC;
860c18ce101SOliver Hartkopp 	else
861821047c4SOliver Hartkopp 		size = skb->len;
862c18ce101SOliver Hartkopp 
8637eab8d9eSAl Viro 	err = memcpy_to_msg(msg, skb->data, size);
864a219994bSUrs Thuermann 	if (err < 0) {
865c18ce101SOliver Hartkopp 		skb_free_datagram(sk, skb);
866a219994bSUrs Thuermann 		return err;
867c18ce101SOliver Hartkopp 	}
868c18ce101SOliver Hartkopp 
869*6fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
870c18ce101SOliver Hartkopp 
871c18ce101SOliver Hartkopp 	if (msg->msg_name) {
8729e971474SOliver Hartkopp 		__sockaddr_check_size(RAW_MIN_NAMELEN);
8739e971474SOliver Hartkopp 		msg->msg_namelen = RAW_MIN_NAMELEN;
874c18ce101SOliver Hartkopp 		memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
875c18ce101SOliver Hartkopp 	}
876c18ce101SOliver Hartkopp 
8771e55659cSOliver Hartkopp 	/* assign the flags that have been recorded in raw_rcv() */
8781e55659cSOliver Hartkopp 	msg->msg_flags |= *(raw_flags(skb));
8791e55659cSOliver Hartkopp 
880c18ce101SOliver Hartkopp 	skb_free_datagram(sk, skb);
881c18ce101SOliver Hartkopp 
882c18ce101SOliver Hartkopp 	return size;
883c18ce101SOliver Hartkopp }
884c18ce101SOliver Hartkopp 
885af0b1470SMarc Kleine-Budde static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
886473d924dSOliver Hartkopp 				unsigned long arg)
887473d924dSOliver Hartkopp {
888473d924dSOliver Hartkopp 	/* no ioctls for socket layer -> hand it down to NIC layer */
889473d924dSOliver Hartkopp 	return -ENOIOCTLCMD;
890473d924dSOliver Hartkopp }
891473d924dSOliver Hartkopp 
89253914b67SOliver Hartkopp static const struct proto_ops raw_ops = {
893c18ce101SOliver Hartkopp 	.family        = PF_CAN,
894c18ce101SOliver Hartkopp 	.release       = raw_release,
895c18ce101SOliver Hartkopp 	.bind          = raw_bind,
896c18ce101SOliver Hartkopp 	.connect       = sock_no_connect,
897c18ce101SOliver Hartkopp 	.socketpair    = sock_no_socketpair,
898c18ce101SOliver Hartkopp 	.accept        = sock_no_accept,
899c18ce101SOliver Hartkopp 	.getname       = raw_getname,
900a11e1d43SLinus Torvalds 	.poll          = datagram_poll,
901473d924dSOliver Hartkopp 	.ioctl         = raw_sock_no_ioctlcmd,
902c7cbdbf2SArnd Bergmann 	.gettstamp     = sock_gettstamp,
903c18ce101SOliver Hartkopp 	.listen        = sock_no_listen,
904c18ce101SOliver Hartkopp 	.shutdown      = sock_no_shutdown,
905c18ce101SOliver Hartkopp 	.setsockopt    = raw_setsockopt,
906c18ce101SOliver Hartkopp 	.getsockopt    = raw_getsockopt,
907c18ce101SOliver Hartkopp 	.sendmsg       = raw_sendmsg,
908c18ce101SOliver Hartkopp 	.recvmsg       = raw_recvmsg,
909c18ce101SOliver Hartkopp 	.mmap          = sock_no_mmap,
910c18ce101SOliver Hartkopp 	.sendpage      = sock_no_sendpage,
911c18ce101SOliver Hartkopp };
912c18ce101SOliver Hartkopp 
913c18ce101SOliver Hartkopp static struct proto raw_proto __read_mostly = {
914c18ce101SOliver Hartkopp 	.name       = "CAN_RAW",
915c18ce101SOliver Hartkopp 	.owner      = THIS_MODULE,
916c18ce101SOliver Hartkopp 	.obj_size   = sizeof(struct raw_sock),
917c18ce101SOliver Hartkopp 	.init       = raw_init,
918c18ce101SOliver Hartkopp };
919c18ce101SOliver Hartkopp 
9201650629dSKurt Van Dijck static const struct can_proto raw_can_proto = {
921c18ce101SOliver Hartkopp 	.type       = SOCK_RAW,
922c18ce101SOliver Hartkopp 	.protocol   = CAN_RAW,
923c18ce101SOliver Hartkopp 	.ops        = &raw_ops,
924c18ce101SOliver Hartkopp 	.prot       = &raw_proto,
925c18ce101SOliver Hartkopp };
926c18ce101SOliver Hartkopp 
9278d0caedbSTetsuo Handa static struct notifier_block canraw_notifier = {
9288d0caedbSTetsuo Handa 	.notifier_call = raw_notifier
9298d0caedbSTetsuo Handa };
9308d0caedbSTetsuo Handa 
931c18ce101SOliver Hartkopp static __init int raw_module_init(void)
932c18ce101SOliver Hartkopp {
933c18ce101SOliver Hartkopp 	int err;
934c18ce101SOliver Hartkopp 
935f726f3d3SOliver Hartkopp 	pr_info("can: raw protocol\n");
936c18ce101SOliver Hartkopp 
937c18ce101SOliver Hartkopp 	err = can_proto_register(&raw_can_proto);
938c18ce101SOliver Hartkopp 	if (err < 0)
939d956b1a8SMarc Kleine-Budde 		pr_err("can: registration of raw protocol failed\n");
9408d0caedbSTetsuo Handa 	else
9418d0caedbSTetsuo Handa 		register_netdevice_notifier(&canraw_notifier);
942c18ce101SOliver Hartkopp 
943c18ce101SOliver Hartkopp 	return err;
944c18ce101SOliver Hartkopp }
945c18ce101SOliver Hartkopp 
946c18ce101SOliver Hartkopp static __exit void raw_module_exit(void)
947c18ce101SOliver Hartkopp {
948c18ce101SOliver Hartkopp 	can_proto_unregister(&raw_can_proto);
9498d0caedbSTetsuo Handa 	unregister_netdevice_notifier(&canraw_notifier);
950c18ce101SOliver Hartkopp }
951c18ce101SOliver Hartkopp 
952c18ce101SOliver Hartkopp module_init(raw_module_init);
953c18ce101SOliver Hartkopp module_exit(raw_module_exit);
954