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>
5362633269SOliver Hartkopp #include <linux/can/dev.h> /* for can_is_canxl_dev_mtu() */
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 MODULE_DESCRIPTION("PF_CAN raw protocol");
60c18ce101SOliver Hartkopp MODULE_LICENSE("Dual BSD/GPL");
61c18ce101SOliver Hartkopp MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
62b13bb2e9SLothar Waßmann MODULE_ALIAS("can-proto-1");
63c18ce101SOliver Hartkopp
649e971474SOliver Hartkopp #define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
659e971474SOliver Hartkopp
66c18ce101SOliver Hartkopp #define MASK_ALL 0
67c18ce101SOliver Hartkopp
68d6ada83bSMarc Kleine-Budde /* A raw socket has a list of can_filters attached to it, each receiving
69c18ce101SOliver Hartkopp * the CAN frames matching that filter. If the filter list is empty,
70c18ce101SOliver Hartkopp * no CAN frames will be received by the socket. The default after
71c18ce101SOliver Hartkopp * opening the socket, is to have one filter which receives all frames.
72c18ce101SOliver Hartkopp * The filter list is allocated dynamically with the exception of the
73c18ce101SOliver Hartkopp * list containing only one item. This common case is optimized by
74c18ce101SOliver Hartkopp * storing the single filter in dfilter, to avoid using dynamic memory.
75c18ce101SOliver Hartkopp */
76c18ce101SOliver Hartkopp
77514ac99cSOliver Hartkopp struct uniqframe {
78d3b58c47SOliver Hartkopp int skbcnt;
79514ac99cSOliver Hartkopp const struct sk_buff *skb;
80a5581ef4SOliver Hartkopp unsigned int join_rx_count;
81514ac99cSOliver Hartkopp };
82514ac99cSOliver Hartkopp
83c18ce101SOliver Hartkopp struct raw_sock {
84c18ce101SOliver Hartkopp struct sock sk;
85c18ce101SOliver Hartkopp int bound;
86c18ce101SOliver Hartkopp int ifindex;
87ee8b94c8SZiyang Xuan struct net_device *dev;
88*c275a176SOliver Hartkopp netdevice_tracker dev_tracker;
898d0caedbSTetsuo Handa struct list_head notifier;
90c18ce101SOliver Hartkopp int loopback;
91c18ce101SOliver Hartkopp int recv_own_msgs;
92e2d265d3SOliver Hartkopp int fd_frames;
9362633269SOliver Hartkopp int xl_frames;
94a5581ef4SOliver Hartkopp int join_filters;
95c18ce101SOliver Hartkopp int count; /* number of active filters */
96c18ce101SOliver Hartkopp struct can_filter dfilter; /* default/single filter */
97c18ce101SOliver Hartkopp struct can_filter *filter; /* pointer to filter(s) */
98c18ce101SOliver Hartkopp can_err_mask_t err_mask;
99514ac99cSOliver Hartkopp struct uniqframe __percpu *uniq;
100c18ce101SOliver Hartkopp };
101c18ce101SOliver Hartkopp
1028d0caedbSTetsuo Handa static LIST_HEAD(raw_notifier_list);
1038d0caedbSTetsuo Handa static DEFINE_SPINLOCK(raw_notifier_lock);
1048d0caedbSTetsuo Handa static struct raw_sock *raw_busy_notifier;
1058d0caedbSTetsuo Handa
106d6ada83bSMarc Kleine-Budde /* Return pointer to store the extra msg flags for raw_recvmsg().
1071e55659cSOliver Hartkopp * We use the space of one unsigned int beyond the 'struct sockaddr_can'
1081e55659cSOliver Hartkopp * in skb->cb.
1091e55659cSOliver Hartkopp */
raw_flags(struct sk_buff * skb)1101e55659cSOliver Hartkopp static inline unsigned int *raw_flags(struct sk_buff *skb)
1111e55659cSOliver Hartkopp {
112b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
113b4772ef8SEyal Birger sizeof(unsigned int));
1141e55659cSOliver Hartkopp
1151e55659cSOliver Hartkopp /* return pointer after struct sockaddr_can */
1161e55659cSOliver Hartkopp return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
1171e55659cSOliver Hartkopp }
1181e55659cSOliver Hartkopp
raw_sk(const struct sock * sk)119c18ce101SOliver Hartkopp static inline struct raw_sock *raw_sk(const struct sock *sk)
120c18ce101SOliver Hartkopp {
121c18ce101SOliver Hartkopp return (struct raw_sock *)sk;
122c18ce101SOliver Hartkopp }
123c18ce101SOliver Hartkopp
raw_rcv(struct sk_buff * oskb,void * data)1241e55659cSOliver Hartkopp static void raw_rcv(struct sk_buff *oskb, void *data)
125c18ce101SOliver Hartkopp {
126c18ce101SOliver Hartkopp struct sock *sk = (struct sock *)data;
127c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
128c18ce101SOliver Hartkopp struct sockaddr_can *addr;
1291e55659cSOliver Hartkopp struct sk_buff *skb;
1301e55659cSOliver Hartkopp unsigned int *pflags;
131c18ce101SOliver Hartkopp
132c18ce101SOliver Hartkopp /* check the received tx sock reference */
1331e55659cSOliver Hartkopp if (!ro->recv_own_msgs && oskb->sk == sk)
134c18ce101SOliver Hartkopp return;
1351fa17d4bSOliver Hartkopp
13662633269SOliver Hartkopp /* make sure to not pass oversized frames to the socket */
1373793301cSOliver Hartkopp if ((!ro->fd_frames && can_is_canfd_skb(oskb)) ||
1383793301cSOliver Hartkopp (!ro->xl_frames && can_is_canxl_skb(oskb)))
139e2d265d3SOliver Hartkopp return;
140e2d265d3SOliver Hartkopp
141514ac99cSOliver Hartkopp /* eliminate multiple filter matches for the same skb */
142514ac99cSOliver Hartkopp if (this_cpu_ptr(ro->uniq)->skb == oskb &&
143d3b58c47SOliver Hartkopp this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
144170277c5SZiyang Xuan if (!ro->join_filters)
145170277c5SZiyang Xuan return;
146170277c5SZiyang Xuan
147a5581ef4SOliver Hartkopp this_cpu_inc(ro->uniq->join_rx_count);
148a5581ef4SOliver Hartkopp /* drop frame until all enabled filters matched */
149a5581ef4SOliver Hartkopp if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
150514ac99cSOliver Hartkopp return;
151514ac99cSOliver Hartkopp } else {
152514ac99cSOliver Hartkopp this_cpu_ptr(ro->uniq)->skb = oskb;
153d3b58c47SOliver Hartkopp this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
154a5581ef4SOliver Hartkopp this_cpu_ptr(ro->uniq)->join_rx_count = 1;
155a5581ef4SOliver Hartkopp /* drop first frame to check all enabled filters? */
156a5581ef4SOliver Hartkopp if (ro->join_filters && ro->count > 1)
157a5581ef4SOliver Hartkopp return;
158514ac99cSOliver Hartkopp }
159514ac99cSOliver Hartkopp
1601fa17d4bSOliver Hartkopp /* clone the given skb to be able to enqueue it into the rcv queue */
1611e55659cSOliver Hartkopp skb = skb_clone(oskb, GFP_ATOMIC);
1621fa17d4bSOliver Hartkopp if (!skb)
1631fa17d4bSOliver Hartkopp return;
164c18ce101SOliver Hartkopp
1656a54dde8SMarc Kleine-Budde /* Put the datagram to the queue so that raw_recvmsg() can get
1666a54dde8SMarc Kleine-Budde * it from there. We need to pass the interface index to
1676a54dde8SMarc Kleine-Budde * raw_recvmsg(). We pass a whole struct sockaddr_can in
1686a54dde8SMarc Kleine-Budde * skb->cb containing the interface index.
169c18ce101SOliver Hartkopp */
170c18ce101SOliver Hartkopp
171b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sockaddr_can));
172c18ce101SOliver Hartkopp addr = (struct sockaddr_can *)skb->cb;
173c18ce101SOliver Hartkopp memset(addr, 0, sizeof(*addr));
174c18ce101SOliver Hartkopp addr->can_family = AF_CAN;
175c18ce101SOliver Hartkopp addr->can_ifindex = skb->dev->ifindex;
176c18ce101SOliver Hartkopp
1771e55659cSOliver Hartkopp /* add CAN specific message flags for raw_recvmsg() */
1781e55659cSOliver Hartkopp pflags = raw_flags(skb);
1791e55659cSOliver Hartkopp *pflags = 0;
1801e55659cSOliver Hartkopp if (oskb->sk)
1811e55659cSOliver Hartkopp *pflags |= MSG_DONTROUTE;
1821e55659cSOliver Hartkopp if (oskb->sk == sk)
1831e55659cSOliver Hartkopp *pflags |= MSG_CONFIRM;
1841e55659cSOliver Hartkopp
185a219994bSUrs Thuermann if (sock_queue_rcv_skb(sk, skb) < 0)
186c18ce101SOliver Hartkopp kfree_skb(skb);
187c18ce101SOliver Hartkopp }
188c18ce101SOliver Hartkopp
raw_enable_filters(struct net * net,struct net_device * dev,struct sock * sk,struct can_filter * filter,int count)1898e8cda6dSMario Kicherer static int raw_enable_filters(struct net *net, struct net_device *dev,
1908e8cda6dSMario Kicherer struct sock *sk, struct can_filter *filter,
1918e8cda6dSMario Kicherer int count)
192c18ce101SOliver Hartkopp {
193c18ce101SOliver Hartkopp int err = 0;
194c18ce101SOliver Hartkopp int i;
195c18ce101SOliver Hartkopp
196c18ce101SOliver Hartkopp for (i = 0; i < count; i++) {
1978e8cda6dSMario Kicherer err = can_rx_register(net, dev, filter[i].can_id,
198c18ce101SOliver Hartkopp filter[i].can_mask,
199f1712c73SEric Dumazet raw_rcv, sk, "raw", sk);
200c18ce101SOliver Hartkopp if (err) {
201c18ce101SOliver Hartkopp /* clean up successfully registered filters */
202c18ce101SOliver Hartkopp while (--i >= 0)
2038e8cda6dSMario Kicherer can_rx_unregister(net, dev, filter[i].can_id,
204c18ce101SOliver Hartkopp filter[i].can_mask,
205c18ce101SOliver Hartkopp raw_rcv, sk);
206c18ce101SOliver Hartkopp break;
207c18ce101SOliver Hartkopp }
208c18ce101SOliver Hartkopp }
209c18ce101SOliver Hartkopp
210c18ce101SOliver Hartkopp return err;
211c18ce101SOliver Hartkopp }
212c18ce101SOliver Hartkopp
raw_enable_errfilter(struct net * net,struct net_device * dev,struct sock * sk,can_err_mask_t err_mask)2138e8cda6dSMario Kicherer static int raw_enable_errfilter(struct net *net, struct net_device *dev,
2148e8cda6dSMario Kicherer struct sock *sk, can_err_mask_t err_mask)
215c18ce101SOliver Hartkopp {
216c18ce101SOliver Hartkopp int err = 0;
217c18ce101SOliver Hartkopp
218c18ce101SOliver Hartkopp if (err_mask)
2198e8cda6dSMario Kicherer err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
220f1712c73SEric Dumazet raw_rcv, sk, "raw", sk);
221c18ce101SOliver Hartkopp
222c18ce101SOliver Hartkopp return err;
223c18ce101SOliver Hartkopp }
224c18ce101SOliver Hartkopp
raw_disable_filters(struct net * net,struct net_device * dev,struct sock * sk,struct can_filter * filter,int count)2258e8cda6dSMario Kicherer static void raw_disable_filters(struct net *net, struct net_device *dev,
2268e8cda6dSMario Kicherer struct sock *sk, struct can_filter *filter,
2278e8cda6dSMario Kicherer int count)
228c18ce101SOliver Hartkopp {
229c18ce101SOliver Hartkopp int i;
230c18ce101SOliver Hartkopp
231c18ce101SOliver Hartkopp for (i = 0; i < count; i++)
2328e8cda6dSMario Kicherer can_rx_unregister(net, dev, filter[i].can_id,
2338e8cda6dSMario Kicherer filter[i].can_mask, raw_rcv, sk);
234c18ce101SOliver Hartkopp }
235c18ce101SOliver Hartkopp
raw_disable_errfilter(struct net * net,struct net_device * dev,struct sock * sk,can_err_mask_t err_mask)2368e8cda6dSMario Kicherer static inline void raw_disable_errfilter(struct net *net,
2378e8cda6dSMario Kicherer struct net_device *dev,
238c18ce101SOliver Hartkopp struct sock *sk,
239c18ce101SOliver Hartkopp can_err_mask_t err_mask)
240c18ce101SOliver Hartkopp
241c18ce101SOliver Hartkopp {
242c18ce101SOliver Hartkopp if (err_mask)
2438e8cda6dSMario Kicherer can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
244c18ce101SOliver Hartkopp raw_rcv, sk);
245c18ce101SOliver Hartkopp }
246c18ce101SOliver Hartkopp
raw_disable_allfilters(struct net * net,struct net_device * dev,struct sock * sk)2478e8cda6dSMario Kicherer static inline void raw_disable_allfilters(struct net *net,
2488e8cda6dSMario Kicherer struct net_device *dev,
249c18ce101SOliver Hartkopp struct sock *sk)
250c18ce101SOliver Hartkopp {
251c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
252c18ce101SOliver Hartkopp
2538e8cda6dSMario Kicherer raw_disable_filters(net, dev, sk, ro->filter, ro->count);
2548e8cda6dSMario Kicherer raw_disable_errfilter(net, dev, sk, ro->err_mask);
255c18ce101SOliver Hartkopp }
256c18ce101SOliver Hartkopp
raw_enable_allfilters(struct net * net,struct net_device * dev,struct sock * sk)2578e8cda6dSMario Kicherer static int raw_enable_allfilters(struct net *net, struct net_device *dev,
2588e8cda6dSMario Kicherer struct sock *sk)
259c18ce101SOliver Hartkopp {
260c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
261c18ce101SOliver Hartkopp int err;
262c18ce101SOliver Hartkopp
2638e8cda6dSMario Kicherer err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
264c18ce101SOliver Hartkopp if (!err) {
2658e8cda6dSMario Kicherer err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
266c18ce101SOliver Hartkopp if (err)
2678e8cda6dSMario Kicherer raw_disable_filters(net, dev, sk, ro->filter,
2688e8cda6dSMario Kicherer ro->count);
269c18ce101SOliver Hartkopp }
270c18ce101SOliver Hartkopp
271c18ce101SOliver Hartkopp return err;
272c18ce101SOliver Hartkopp }
273c18ce101SOliver Hartkopp
raw_notify(struct raw_sock * ro,unsigned long msg,struct net_device * dev)2748d0caedbSTetsuo Handa static void raw_notify(struct raw_sock *ro, unsigned long msg,
2758d0caedbSTetsuo Handa struct net_device *dev)
276c18ce101SOliver Hartkopp {
277c18ce101SOliver Hartkopp struct sock *sk = &ro->sk;
278c18ce101SOliver Hartkopp
2798e8cda6dSMario Kicherer if (!net_eq(dev_net(dev), sock_net(sk)))
2808d0caedbSTetsuo Handa return;
281c18ce101SOliver Hartkopp
282ee8b94c8SZiyang Xuan if (ro->dev != dev)
2838d0caedbSTetsuo Handa return;
284c18ce101SOliver Hartkopp
285c18ce101SOliver Hartkopp switch (msg) {
286c18ce101SOliver Hartkopp case NETDEV_UNREGISTER:
287c18ce101SOliver Hartkopp lock_sock(sk);
288c18ce101SOliver Hartkopp /* remove current filters & unregister */
289*c275a176SOliver Hartkopp if (ro->bound) {
2908e8cda6dSMario Kicherer raw_disable_allfilters(dev_net(dev), dev, sk);
291*c275a176SOliver Hartkopp netdev_put(dev, &ro->dev_tracker);
292*c275a176SOliver Hartkopp }
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;
299ee8b94c8SZiyang Xuan ro->dev = NULL;
300c18ce101SOliver Hartkopp ro->count = 0;
301c18ce101SOliver Hartkopp release_sock(sk);
302c18ce101SOliver Hartkopp
303c18ce101SOliver Hartkopp sk->sk_err = ENODEV;
304c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD))
305e3ae2365SAlexander Aring sk_error_report(sk);
306c18ce101SOliver Hartkopp break;
307c18ce101SOliver Hartkopp
308c18ce101SOliver Hartkopp case NETDEV_DOWN:
309c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN;
310c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD))
311e3ae2365SAlexander Aring sk_error_report(sk);
312c18ce101SOliver Hartkopp break;
313c18ce101SOliver Hartkopp }
3148d0caedbSTetsuo Handa }
315c18ce101SOliver Hartkopp
raw_notifier(struct notifier_block * nb,unsigned long msg,void * ptr)3168d0caedbSTetsuo Handa static int raw_notifier(struct notifier_block *nb, unsigned long msg,
3178d0caedbSTetsuo Handa void *ptr)
3188d0caedbSTetsuo Handa {
3198d0caedbSTetsuo Handa struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3208d0caedbSTetsuo Handa
3218d0caedbSTetsuo Handa if (dev->type != ARPHRD_CAN)
3228d0caedbSTetsuo Handa return NOTIFY_DONE;
3238d0caedbSTetsuo Handa if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
3248d0caedbSTetsuo Handa return NOTIFY_DONE;
3258d0caedbSTetsuo Handa if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
3268d0caedbSTetsuo Handa return NOTIFY_DONE;
3278d0caedbSTetsuo Handa
3288d0caedbSTetsuo Handa spin_lock(&raw_notifier_lock);
3298d0caedbSTetsuo Handa list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
3308d0caedbSTetsuo Handa spin_unlock(&raw_notifier_lock);
3318d0caedbSTetsuo Handa raw_notify(raw_busy_notifier, msg, dev);
3328d0caedbSTetsuo Handa spin_lock(&raw_notifier_lock);
3338d0caedbSTetsuo Handa }
3348d0caedbSTetsuo Handa raw_busy_notifier = NULL;
3358d0caedbSTetsuo Handa spin_unlock(&raw_notifier_lock);
336c18ce101SOliver Hartkopp return NOTIFY_DONE;
337c18ce101SOliver Hartkopp }
338c18ce101SOliver Hartkopp
raw_init(struct sock * sk)339c18ce101SOliver Hartkopp static int raw_init(struct sock *sk)
340c18ce101SOliver Hartkopp {
341c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
342c18ce101SOliver Hartkopp
343c18ce101SOliver Hartkopp ro->bound = 0;
344c18ce101SOliver Hartkopp ro->ifindex = 0;
345ee8b94c8SZiyang Xuan ro->dev = NULL;
346c18ce101SOliver Hartkopp
347c18ce101SOliver Hartkopp /* set default filter to single entry dfilter */
348c18ce101SOliver Hartkopp ro->dfilter.can_id = 0;
349c18ce101SOliver Hartkopp ro->dfilter.can_mask = MASK_ALL;
350c18ce101SOliver Hartkopp ro->filter = &ro->dfilter;
351c18ce101SOliver Hartkopp ro->count = 1;
352c18ce101SOliver Hartkopp
353c18ce101SOliver Hartkopp /* set default loopback behaviour */
354c18ce101SOliver Hartkopp ro->loopback = 1;
355c18ce101SOliver Hartkopp ro->recv_own_msgs = 0;
356e2d265d3SOliver Hartkopp ro->fd_frames = 0;
35762633269SOliver Hartkopp ro->xl_frames = 0;
358a5581ef4SOliver Hartkopp ro->join_filters = 0;
359c18ce101SOliver Hartkopp
360514ac99cSOliver Hartkopp /* alloc_percpu provides zero'ed memory */
361514ac99cSOliver Hartkopp ro->uniq = alloc_percpu(struct uniqframe);
362514ac99cSOliver Hartkopp if (unlikely(!ro->uniq))
363514ac99cSOliver Hartkopp return -ENOMEM;
364514ac99cSOliver Hartkopp
365c18ce101SOliver Hartkopp /* set notifier */
3668d0caedbSTetsuo Handa spin_lock(&raw_notifier_lock);
3678d0caedbSTetsuo Handa list_add_tail(&ro->notifier, &raw_notifier_list);
3688d0caedbSTetsuo Handa spin_unlock(&raw_notifier_lock);
369c18ce101SOliver Hartkopp
370c18ce101SOliver Hartkopp return 0;
371c18ce101SOliver Hartkopp }
372c18ce101SOliver Hartkopp
raw_release(struct socket * sock)373c18ce101SOliver Hartkopp static int raw_release(struct socket *sock)
374c18ce101SOliver Hartkopp {
375c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
37610022a6cSOliver Hartkopp struct raw_sock *ro;
37710022a6cSOliver Hartkopp
37810022a6cSOliver Hartkopp if (!sk)
37910022a6cSOliver Hartkopp return 0;
38010022a6cSOliver Hartkopp
38110022a6cSOliver Hartkopp ro = raw_sk(sk);
382c18ce101SOliver Hartkopp
3838d0caedbSTetsuo Handa spin_lock(&raw_notifier_lock);
3848d0caedbSTetsuo Handa while (raw_busy_notifier == ro) {
3858d0caedbSTetsuo Handa spin_unlock(&raw_notifier_lock);
3868d0caedbSTetsuo Handa schedule_timeout_uninterruptible(1);
3878d0caedbSTetsuo Handa spin_lock(&raw_notifier_lock);
3888d0caedbSTetsuo Handa }
3898d0caedbSTetsuo Handa list_del(&ro->notifier);
3908d0caedbSTetsuo Handa spin_unlock(&raw_notifier_lock);
391c18ce101SOliver Hartkopp
39211c9027cSEric Dumazet rtnl_lock();
393c18ce101SOliver Hartkopp lock_sock(sk);
394c18ce101SOliver Hartkopp
395c18ce101SOliver Hartkopp /* remove current filters & unregister */
396c18ce101SOliver Hartkopp if (ro->bound) {
397*c275a176SOliver Hartkopp if (ro->dev) {
398ee8b94c8SZiyang Xuan raw_disable_allfilters(dev_net(ro->dev), ro->dev, sk);
399*c275a176SOliver Hartkopp netdev_put(ro->dev, &ro->dev_tracker);
400*c275a176SOliver Hartkopp } else {
4018e8cda6dSMario Kicherer raw_disable_allfilters(sock_net(sk), NULL, sk);
402c18ce101SOliver Hartkopp }
403*c275a176SOliver Hartkopp }
404c18ce101SOliver Hartkopp
405c18ce101SOliver Hartkopp if (ro->count > 1)
406c18ce101SOliver Hartkopp kfree(ro->filter);
407c18ce101SOliver Hartkopp
408c18ce101SOliver Hartkopp ro->ifindex = 0;
409c18ce101SOliver Hartkopp ro->bound = 0;
410ee8b94c8SZiyang Xuan ro->dev = NULL;
411c18ce101SOliver Hartkopp ro->count = 0;
412514ac99cSOliver Hartkopp free_percpu(ro->uniq);
413c18ce101SOliver Hartkopp
414f7e5cc0cSLothar Waßmann sock_orphan(sk);
415f7e5cc0cSLothar Waßmann sock->sk = NULL;
416f7e5cc0cSLothar Waßmann
417c18ce101SOliver Hartkopp release_sock(sk);
41811c9027cSEric Dumazet rtnl_unlock();
41911c9027cSEric Dumazet
420c18ce101SOliver Hartkopp sock_put(sk);
421c18ce101SOliver Hartkopp
422c18ce101SOliver Hartkopp return 0;
423c18ce101SOliver Hartkopp }
424c18ce101SOliver Hartkopp
raw_bind(struct socket * sock,struct sockaddr * uaddr,int len)425c18ce101SOliver Hartkopp static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
426c18ce101SOliver Hartkopp {
427c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
428c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
429c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
430ee8b94c8SZiyang Xuan struct net_device *dev = NULL;
431c18ce101SOliver Hartkopp int ifindex;
432c18ce101SOliver Hartkopp int err = 0;
433c18ce101SOliver Hartkopp int notify_enetdown = 0;
434c18ce101SOliver Hartkopp
4359e971474SOliver Hartkopp if (len < RAW_MIN_NAMELEN)
436c18ce101SOliver Hartkopp return -EINVAL;
437adb552c3SMarc Kleine-Budde if (addr->can_family != AF_CAN)
438adb552c3SMarc Kleine-Budde return -EINVAL;
439c18ce101SOliver Hartkopp
440ee8b94c8SZiyang Xuan rtnl_lock();
441c18ce101SOliver Hartkopp lock_sock(sk);
442c18ce101SOliver Hartkopp
443c18ce101SOliver Hartkopp if (ro->bound && addr->can_ifindex == ro->ifindex)
444c18ce101SOliver Hartkopp goto out;
445c18ce101SOliver Hartkopp
446c18ce101SOliver Hartkopp if (addr->can_ifindex) {
4478e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
448c18ce101SOliver Hartkopp if (!dev) {
449c18ce101SOliver Hartkopp err = -ENODEV;
450c18ce101SOliver Hartkopp goto out;
451c18ce101SOliver Hartkopp }
452c18ce101SOliver Hartkopp if (dev->type != ARPHRD_CAN) {
453c18ce101SOliver Hartkopp err = -ENODEV;
454*c275a176SOliver Hartkopp goto out_put_dev;
455c18ce101SOliver Hartkopp }
456*c275a176SOliver Hartkopp
457c18ce101SOliver Hartkopp if (!(dev->flags & IFF_UP))
458c18ce101SOliver Hartkopp notify_enetdown = 1;
459c18ce101SOliver Hartkopp
460c18ce101SOliver Hartkopp ifindex = dev->ifindex;
461c18ce101SOliver Hartkopp
462c18ce101SOliver Hartkopp /* filters set by default/setsockopt */
4638e8cda6dSMario Kicherer err = raw_enable_allfilters(sock_net(sk), dev, sk);
464*c275a176SOliver Hartkopp if (err)
465*c275a176SOliver Hartkopp goto out_put_dev;
466*c275a176SOliver Hartkopp
467c18ce101SOliver Hartkopp } else {
468c18ce101SOliver Hartkopp ifindex = 0;
469c18ce101SOliver Hartkopp
470c18ce101SOliver Hartkopp /* filters set by default/setsockopt */
4718e8cda6dSMario Kicherer err = raw_enable_allfilters(sock_net(sk), NULL, sk);
472c18ce101SOliver Hartkopp }
473c18ce101SOliver Hartkopp
474c18ce101SOliver Hartkopp if (!err) {
475c18ce101SOliver Hartkopp if (ro->bound) {
476c18ce101SOliver Hartkopp /* unregister old filters */
477*c275a176SOliver Hartkopp if (ro->dev) {
478ee8b94c8SZiyang Xuan raw_disable_allfilters(dev_net(ro->dev),
479ee8b94c8SZiyang Xuan ro->dev, sk);
480*c275a176SOliver Hartkopp /* drop reference to old ro->dev */
481*c275a176SOliver Hartkopp netdev_put(ro->dev, &ro->dev_tracker);
482*c275a176SOliver Hartkopp } else {
4838e8cda6dSMario Kicherer raw_disable_allfilters(sock_net(sk), NULL, sk);
484c18ce101SOliver Hartkopp }
485*c275a176SOliver Hartkopp }
486c18ce101SOliver Hartkopp ro->ifindex = ifindex;
487c18ce101SOliver Hartkopp ro->bound = 1;
488*c275a176SOliver Hartkopp /* bind() ok -> hold a reference for new ro->dev */
489ee8b94c8SZiyang Xuan ro->dev = dev;
490*c275a176SOliver Hartkopp if (ro->dev)
491*c275a176SOliver Hartkopp netdev_hold(ro->dev, &ro->dev_tracker, GFP_KERNEL);
492c18ce101SOliver Hartkopp }
493c18ce101SOliver Hartkopp
494*c275a176SOliver Hartkopp out_put_dev:
495*c275a176SOliver Hartkopp /* remove potential reference from dev_get_by_index() */
496*c275a176SOliver Hartkopp if (dev)
497*c275a176SOliver Hartkopp dev_put(dev);
498c18ce101SOliver Hartkopp out:
499c18ce101SOliver Hartkopp release_sock(sk);
500ee8b94c8SZiyang Xuan rtnl_unlock();
501c18ce101SOliver Hartkopp
502c18ce101SOliver Hartkopp if (notify_enetdown) {
503c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN;
504c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD))
505e3ae2365SAlexander Aring sk_error_report(sk);
506c18ce101SOliver Hartkopp }
507c18ce101SOliver Hartkopp
508c18ce101SOliver Hartkopp return err;
509c18ce101SOliver Hartkopp }
510c18ce101SOliver Hartkopp
raw_getname(struct socket * sock,struct sockaddr * uaddr,int peer)511c18ce101SOliver Hartkopp static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
5129b2c45d4SDenys Vlasenko int peer)
513c18ce101SOliver Hartkopp {
514c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
515c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
516c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
517c18ce101SOliver Hartkopp
518c18ce101SOliver Hartkopp if (peer)
519c18ce101SOliver Hartkopp return -EOPNOTSUPP;
520c18ce101SOliver Hartkopp
5219e971474SOliver Hartkopp memset(addr, 0, RAW_MIN_NAMELEN);
522c18ce101SOliver Hartkopp addr->can_family = AF_CAN;
523c18ce101SOliver Hartkopp addr->can_ifindex = ro->ifindex;
524c18ce101SOliver Hartkopp
5259e971474SOliver Hartkopp return RAW_MIN_NAMELEN;
526c18ce101SOliver Hartkopp }
527c18ce101SOliver Hartkopp
raw_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)528c18ce101SOliver Hartkopp static int raw_setsockopt(struct socket *sock, int level, int optname,
529a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen)
530c18ce101SOliver Hartkopp {
531c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
532c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
533c18ce101SOliver Hartkopp struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
534c18ce101SOliver Hartkopp struct can_filter sfilter; /* single filter */
535c18ce101SOliver Hartkopp struct net_device *dev = NULL;
536c18ce101SOliver Hartkopp can_err_mask_t err_mask = 0;
537f2f527d5SOliver Hartkopp int fd_frames;
538c18ce101SOliver Hartkopp int count = 0;
539c18ce101SOliver Hartkopp int err = 0;
540c18ce101SOliver Hartkopp
541c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW)
542c18ce101SOliver Hartkopp return -EINVAL;
543c18ce101SOliver Hartkopp
544c18ce101SOliver Hartkopp switch (optname) {
545c18ce101SOliver Hartkopp case CAN_RAW_FILTER:
546c18ce101SOliver Hartkopp if (optlen % sizeof(struct can_filter) != 0)
547c18ce101SOliver Hartkopp return -EINVAL;
548c18ce101SOliver Hartkopp
549332b05caSMarc Kleine-Budde if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
550332b05caSMarc Kleine-Budde return -EINVAL;
551332b05caSMarc Kleine-Budde
552c18ce101SOliver Hartkopp count = optlen / sizeof(struct can_filter);
553c18ce101SOliver Hartkopp
554c18ce101SOliver Hartkopp if (count > 1) {
555c18ce101SOliver Hartkopp /* filter does not fit into dfilter => alloc space */
556a7b75c5aSChristoph Hellwig filter = memdup_sockptr(optval, optlen);
55716dff918SJulia Lawall if (IS_ERR(filter))
55816dff918SJulia Lawall return PTR_ERR(filter);
559c18ce101SOliver Hartkopp } else if (count == 1) {
560a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
5613f91bd42SSam Ravnborg return -EFAULT;
562c18ce101SOliver Hartkopp }
563c18ce101SOliver Hartkopp
56454f93336SZiyang Xuan rtnl_lock();
565c18ce101SOliver Hartkopp lock_sock(sk);
566c18ce101SOliver Hartkopp
567ee8b94c8SZiyang Xuan dev = ro->dev;
568ee8b94c8SZiyang Xuan if (ro->bound && dev) {
569ee8b94c8SZiyang Xuan if (dev->reg_state != NETREG_REGISTERED) {
57054f93336SZiyang Xuan if (count > 1)
57154f93336SZiyang Xuan kfree(filter);
57254f93336SZiyang Xuan err = -ENODEV;
57354f93336SZiyang Xuan goto out_fil;
57454f93336SZiyang Xuan }
57554f93336SZiyang Xuan }
576c18ce101SOliver Hartkopp
577c18ce101SOliver Hartkopp if (ro->bound) {
578c18ce101SOliver Hartkopp /* (try to) register the new filters */
579c18ce101SOliver Hartkopp if (count == 1)
5808e8cda6dSMario Kicherer err = raw_enable_filters(sock_net(sk), dev, sk,
5818e8cda6dSMario Kicherer &sfilter, 1);
582c18ce101SOliver Hartkopp else
5838e8cda6dSMario Kicherer err = raw_enable_filters(sock_net(sk), dev, sk,
5848e8cda6dSMario Kicherer filter, count);
585c18ce101SOliver Hartkopp if (err) {
586c18ce101SOliver Hartkopp if (count > 1)
587c18ce101SOliver Hartkopp kfree(filter);
588c18ce101SOliver Hartkopp goto out_fil;
589c18ce101SOliver Hartkopp }
590c18ce101SOliver Hartkopp
591c18ce101SOliver Hartkopp /* remove old filter registrations */
5928e8cda6dSMario Kicherer raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
5938e8cda6dSMario Kicherer ro->count);
594c18ce101SOliver Hartkopp }
595c18ce101SOliver Hartkopp
596c18ce101SOliver Hartkopp /* remove old filter space */
597c18ce101SOliver Hartkopp if (ro->count > 1)
598c18ce101SOliver Hartkopp kfree(ro->filter);
599c18ce101SOliver Hartkopp
600c18ce101SOliver Hartkopp /* link new filters to the socket */
601c18ce101SOliver Hartkopp if (count == 1) {
602c18ce101SOliver Hartkopp /* copy filter data for single filter */
603c18ce101SOliver Hartkopp ro->dfilter = sfilter;
604c18ce101SOliver Hartkopp filter = &ro->dfilter;
605c18ce101SOliver Hartkopp }
606c18ce101SOliver Hartkopp ro->filter = filter;
607c18ce101SOliver Hartkopp ro->count = count;
608c18ce101SOliver Hartkopp
609c18ce101SOliver Hartkopp out_fil:
610c18ce101SOliver Hartkopp release_sock(sk);
61154f93336SZiyang Xuan rtnl_unlock();
612c18ce101SOliver Hartkopp
613c18ce101SOliver Hartkopp break;
614c18ce101SOliver Hartkopp
615c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER:
616c18ce101SOliver Hartkopp if (optlen != sizeof(err_mask))
617c18ce101SOliver Hartkopp return -EINVAL;
618c18ce101SOliver Hartkopp
619a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&err_mask, optval, optlen))
6203f91bd42SSam Ravnborg return -EFAULT;
621c18ce101SOliver Hartkopp
622c18ce101SOliver Hartkopp err_mask &= CAN_ERR_MASK;
623c18ce101SOliver Hartkopp
62454f93336SZiyang Xuan rtnl_lock();
625c18ce101SOliver Hartkopp lock_sock(sk);
626c18ce101SOliver Hartkopp
627ee8b94c8SZiyang Xuan dev = ro->dev;
628ee8b94c8SZiyang Xuan if (ro->bound && dev) {
629ee8b94c8SZiyang Xuan if (dev->reg_state != NETREG_REGISTERED) {
63054f93336SZiyang Xuan err = -ENODEV;
63154f93336SZiyang Xuan goto out_err;
63254f93336SZiyang Xuan }
63354f93336SZiyang Xuan }
634c18ce101SOliver Hartkopp
635c18ce101SOliver Hartkopp /* remove current error mask */
636c18ce101SOliver Hartkopp if (ro->bound) {
637c18ce101SOliver Hartkopp /* (try to) register the new err_mask */
6388e8cda6dSMario Kicherer err = raw_enable_errfilter(sock_net(sk), dev, sk,
6398e8cda6dSMario Kicherer err_mask);
640c18ce101SOliver Hartkopp
641c18ce101SOliver Hartkopp if (err)
642c18ce101SOliver Hartkopp goto out_err;
643c18ce101SOliver Hartkopp
644c18ce101SOliver Hartkopp /* remove old err_mask registration */
6458e8cda6dSMario Kicherer raw_disable_errfilter(sock_net(sk), dev, sk,
6468e8cda6dSMario Kicherer ro->err_mask);
647c18ce101SOliver Hartkopp }
648c18ce101SOliver Hartkopp
649c18ce101SOliver Hartkopp /* link new err_mask to the socket */
650c18ce101SOliver Hartkopp ro->err_mask = err_mask;
651c18ce101SOliver Hartkopp
652c18ce101SOliver Hartkopp out_err:
653c18ce101SOliver Hartkopp release_sock(sk);
65454f93336SZiyang Xuan rtnl_unlock();
655c18ce101SOliver Hartkopp
656c18ce101SOliver Hartkopp break;
657c18ce101SOliver Hartkopp
658c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK:
659c18ce101SOliver Hartkopp if (optlen != sizeof(ro->loopback))
660c18ce101SOliver Hartkopp return -EINVAL;
661c18ce101SOliver Hartkopp
662a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&ro->loopback, optval, optlen))
6633f91bd42SSam Ravnborg return -EFAULT;
664c18ce101SOliver Hartkopp
665c18ce101SOliver Hartkopp break;
666c18ce101SOliver Hartkopp
667c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS:
668c18ce101SOliver Hartkopp if (optlen != sizeof(ro->recv_own_msgs))
669c18ce101SOliver Hartkopp return -EINVAL;
670c18ce101SOliver Hartkopp
671a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
6723f91bd42SSam Ravnborg return -EFAULT;
673c18ce101SOliver Hartkopp
674c18ce101SOliver Hartkopp break;
675c18ce101SOliver Hartkopp
676e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES:
677f2f527d5SOliver Hartkopp if (optlen != sizeof(fd_frames))
678e2d265d3SOliver Hartkopp return -EINVAL;
679e2d265d3SOliver Hartkopp
680f2f527d5SOliver Hartkopp if (copy_from_sockptr(&fd_frames, optval, optlen))
681e2d265d3SOliver Hartkopp return -EFAULT;
682e2d265d3SOliver Hartkopp
6833793301cSOliver Hartkopp /* Enabling CAN XL includes CAN FD */
684f2f527d5SOliver Hartkopp if (ro->xl_frames && !fd_frames)
6853793301cSOliver Hartkopp return -EINVAL;
686f2f527d5SOliver Hartkopp
687f2f527d5SOliver Hartkopp ro->fd_frames = fd_frames;
688e2d265d3SOliver Hartkopp break;
689e2d265d3SOliver Hartkopp
69062633269SOliver Hartkopp case CAN_RAW_XL_FRAMES:
69162633269SOliver Hartkopp if (optlen != sizeof(ro->xl_frames))
69262633269SOliver Hartkopp return -EINVAL;
69362633269SOliver Hartkopp
69462633269SOliver Hartkopp if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
69562633269SOliver Hartkopp return -EFAULT;
69662633269SOliver Hartkopp
6973793301cSOliver Hartkopp /* Enabling CAN XL includes CAN FD */
6983793301cSOliver Hartkopp if (ro->xl_frames)
6993793301cSOliver Hartkopp ro->fd_frames = ro->xl_frames;
70062633269SOliver Hartkopp break;
70162633269SOliver Hartkopp
702a5581ef4SOliver Hartkopp case CAN_RAW_JOIN_FILTERS:
703a5581ef4SOliver Hartkopp if (optlen != sizeof(ro->join_filters))
704a5581ef4SOliver Hartkopp return -EINVAL;
705a5581ef4SOliver Hartkopp
706a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&ro->join_filters, optval, optlen))
707a5581ef4SOliver Hartkopp return -EFAULT;
708a5581ef4SOliver Hartkopp
709a5581ef4SOliver Hartkopp break;
710a5581ef4SOliver Hartkopp
711c18ce101SOliver Hartkopp default:
712c18ce101SOliver Hartkopp return -ENOPROTOOPT;
713c18ce101SOliver Hartkopp }
714c18ce101SOliver Hartkopp return err;
715c18ce101SOliver Hartkopp }
716c18ce101SOliver Hartkopp
raw_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)717c18ce101SOliver Hartkopp static int raw_getsockopt(struct socket *sock, int level, int optname,
718c18ce101SOliver Hartkopp char __user *optval, int __user *optlen)
719c18ce101SOliver Hartkopp {
720c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
721c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
722c18ce101SOliver Hartkopp int len;
723c18ce101SOliver Hartkopp void *val;
724c18ce101SOliver Hartkopp int err = 0;
725c18ce101SOliver Hartkopp
726c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW)
727c18ce101SOliver Hartkopp return -EINVAL;
728c18ce101SOliver Hartkopp if (get_user(len, optlen))
729c18ce101SOliver Hartkopp return -EFAULT;
730c18ce101SOliver Hartkopp if (len < 0)
731c18ce101SOliver Hartkopp return -EINVAL;
732c18ce101SOliver Hartkopp
733c18ce101SOliver Hartkopp switch (optname) {
734c18ce101SOliver Hartkopp case CAN_RAW_FILTER:
735c18ce101SOliver Hartkopp lock_sock(sk);
736c18ce101SOliver Hartkopp if (ro->count > 0) {
737c18ce101SOliver Hartkopp int fsize = ro->count * sizeof(struct can_filter);
738d5e4ecacSMarc Kleine-Budde
7390de70e28SOliver Hartkopp /* user space buffer to small for filter list? */
7400de70e28SOliver Hartkopp if (len < fsize) {
7410de70e28SOliver Hartkopp /* return -ERANGE and needed space in optlen */
7420de70e28SOliver Hartkopp err = -ERANGE;
7430de70e28SOliver Hartkopp if (put_user(fsize, optlen))
7440de70e28SOliver Hartkopp err = -EFAULT;
7450de70e28SOliver Hartkopp } else {
746c18ce101SOliver Hartkopp if (len > fsize)
747c18ce101SOliver Hartkopp len = fsize;
748653252c2SPavel Emelyanov if (copy_to_user(optval, ro->filter, len))
749653252c2SPavel Emelyanov err = -EFAULT;
7500de70e28SOliver Hartkopp }
751bff10040SMarc Kleine-Budde } else {
752c18ce101SOliver Hartkopp len = 0;
753bff10040SMarc Kleine-Budde }
754c18ce101SOliver Hartkopp release_sock(sk);
755c18ce101SOliver Hartkopp
756c18ce101SOliver Hartkopp if (!err)
757c18ce101SOliver Hartkopp err = put_user(len, optlen);
758c18ce101SOliver Hartkopp return err;
759c18ce101SOliver Hartkopp
760c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER:
761c18ce101SOliver Hartkopp if (len > sizeof(can_err_mask_t))
762c18ce101SOliver Hartkopp len = sizeof(can_err_mask_t);
763c18ce101SOliver Hartkopp val = &ro->err_mask;
764c18ce101SOliver Hartkopp break;
765c18ce101SOliver Hartkopp
766c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK:
767c18ce101SOliver Hartkopp if (len > sizeof(int))
768c18ce101SOliver Hartkopp len = sizeof(int);
769c18ce101SOliver Hartkopp val = &ro->loopback;
770c18ce101SOliver Hartkopp break;
771c18ce101SOliver Hartkopp
772c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS:
773c18ce101SOliver Hartkopp if (len > sizeof(int))
774c18ce101SOliver Hartkopp len = sizeof(int);
775c18ce101SOliver Hartkopp val = &ro->recv_own_msgs;
776c18ce101SOliver Hartkopp break;
777c18ce101SOliver Hartkopp
778e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES:
779e2d265d3SOliver Hartkopp if (len > sizeof(int))
780e2d265d3SOliver Hartkopp len = sizeof(int);
781e2d265d3SOliver Hartkopp val = &ro->fd_frames;
782e2d265d3SOliver Hartkopp break;
783e2d265d3SOliver Hartkopp
78462633269SOliver Hartkopp case CAN_RAW_XL_FRAMES:
78562633269SOliver Hartkopp if (len > sizeof(int))
78662633269SOliver Hartkopp len = sizeof(int);
78762633269SOliver Hartkopp val = &ro->xl_frames;
78862633269SOliver Hartkopp break;
78962633269SOliver Hartkopp
790a5581ef4SOliver Hartkopp case CAN_RAW_JOIN_FILTERS:
791a5581ef4SOliver Hartkopp if (len > sizeof(int))
792a5581ef4SOliver Hartkopp len = sizeof(int);
793a5581ef4SOliver Hartkopp val = &ro->join_filters;
794a5581ef4SOliver Hartkopp break;
795a5581ef4SOliver Hartkopp
796c18ce101SOliver Hartkopp default:
797c18ce101SOliver Hartkopp return -ENOPROTOOPT;
798c18ce101SOliver Hartkopp }
799c18ce101SOliver Hartkopp
800c18ce101SOliver Hartkopp if (put_user(len, optlen))
801c18ce101SOliver Hartkopp return -EFAULT;
802c18ce101SOliver Hartkopp if (copy_to_user(optval, val, len))
803c18ce101SOliver Hartkopp return -EFAULT;
804c18ce101SOliver Hartkopp return 0;
805c18ce101SOliver Hartkopp }
806c18ce101SOliver Hartkopp
raw_bad_txframe(struct raw_sock * ro,struct sk_buff * skb,int mtu)8073793301cSOliver Hartkopp static bool raw_bad_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
8083793301cSOliver Hartkopp {
8093793301cSOliver Hartkopp /* Classical CAN -> no checks for flags and device capabilities */
8103793301cSOliver Hartkopp if (can_is_can_skb(skb))
8113793301cSOliver Hartkopp return false;
8123793301cSOliver Hartkopp
8133793301cSOliver Hartkopp /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
8143793301cSOliver Hartkopp if (ro->fd_frames && can_is_canfd_skb(skb) &&
8153793301cSOliver Hartkopp (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
8163793301cSOliver Hartkopp return false;
8173793301cSOliver Hartkopp
8183793301cSOliver Hartkopp /* CAN XL -> needs to be enabled and a CAN XL device */
8193793301cSOliver Hartkopp if (ro->xl_frames && can_is_canxl_skb(skb) &&
8203793301cSOliver Hartkopp can_is_canxl_dev_mtu(mtu))
8213793301cSOliver Hartkopp return false;
8223793301cSOliver Hartkopp
8233793301cSOliver Hartkopp return true;
8243793301cSOliver Hartkopp }
8253793301cSOliver Hartkopp
raw_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)8261b784140SYing Xue static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
827c18ce101SOliver Hartkopp {
828c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
829c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk);
83051a0d5e5SMarc Kleine-Budde struct sockcm_cookie sockc;
831c18ce101SOliver Hartkopp struct sk_buff *skb;
832c18ce101SOliver Hartkopp struct net_device *dev;
833c18ce101SOliver Hartkopp int ifindex;
83462633269SOliver Hartkopp int err = -EINVAL;
83562633269SOliver Hartkopp
83662633269SOliver Hartkopp /* check for valid CAN frame sizes */
83762633269SOliver Hartkopp if (size < CANXL_HDR_SIZE + CANXL_MIN_DLEN || size > CANXL_MTU)
83862633269SOliver Hartkopp return -EINVAL;
839c18ce101SOliver Hartkopp
840c18ce101SOliver Hartkopp if (msg->msg_name) {
841342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
842c18ce101SOliver Hartkopp
8439e971474SOliver Hartkopp if (msg->msg_namelen < RAW_MIN_NAMELEN)
8445e507328SKurt Van Dijck return -EINVAL;
8455e507328SKurt Van Dijck
846c18ce101SOliver Hartkopp if (addr->can_family != AF_CAN)
847c18ce101SOliver Hartkopp return -EINVAL;
848c18ce101SOliver Hartkopp
849c18ce101SOliver Hartkopp ifindex = addr->can_ifindex;
850bff10040SMarc Kleine-Budde } else {
851c18ce101SOliver Hartkopp ifindex = ro->ifindex;
852bff10040SMarc Kleine-Budde }
853c18ce101SOliver Hartkopp
8548e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), ifindex);
855c18ce101SOliver Hartkopp if (!dev)
856c18ce101SOliver Hartkopp return -ENXIO;
857c18ce101SOliver Hartkopp
858156c2bb9SOliver Hartkopp skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
859156c2bb9SOliver Hartkopp msg->msg_flags & MSG_DONTWAIT, &err);
860ebad5c09SIlpo Järvinen if (!skb)
861ebad5c09SIlpo Järvinen goto put_dev;
862c18ce101SOliver Hartkopp
8632bf3440dSOliver Hartkopp can_skb_reserve(skb);
8642bf3440dSOliver Hartkopp can_skb_prv(skb)->ifindex = dev->ifindex;
865d3b58c47SOliver Hartkopp can_skb_prv(skb)->skbcnt = 0;
866156c2bb9SOliver Hartkopp
86762633269SOliver Hartkopp /* fill the skb before testing for valid CAN frames */
8686ce8e9ceSAl Viro err = memcpy_from_msg(skb_put(skb, size), msg, size);
869ebad5c09SIlpo Järvinen if (err < 0)
870ebad5c09SIlpo Järvinen goto free_skb;
871bf84a010SDaniel Borkmann
87262633269SOliver Hartkopp err = -EINVAL;
8733793301cSOliver Hartkopp if (raw_bad_txframe(ro, skb, dev->mtu))
87462633269SOliver Hartkopp goto free_skb;
87562633269SOliver Hartkopp
87651a0d5e5SMarc Kleine-Budde sockcm_init(&sockc, sk);
87751a0d5e5SMarc Kleine-Budde if (msg->msg_controllen) {
87851a0d5e5SMarc Kleine-Budde err = sock_cmsg_send(sk, msg, &sockc);
87951a0d5e5SMarc Kleine-Budde if (unlikely(err))
88051a0d5e5SMarc Kleine-Budde goto free_skb;
88151a0d5e5SMarc Kleine-Budde }
882cff0d6e6SOliver Hartkopp
883c18ce101SOliver Hartkopp skb->dev = dev;
884bb5ecb0cSRostislav Lisovy skb->priority = sk->sk_priority;
8853c5b4d69SEric Dumazet skb->mark = READ_ONCE(sk->sk_mark);
88651a0d5e5SMarc Kleine-Budde skb->tstamp = sockc.transmit_time;
88751a0d5e5SMarc Kleine-Budde
88851a0d5e5SMarc Kleine-Budde skb_setup_tx_timestamp(skb, sockc.tsflags);
889c18ce101SOliver Hartkopp
890c18ce101SOliver Hartkopp err = can_send(skb, ro->loopback);
891c18ce101SOliver Hartkopp
892c18ce101SOliver Hartkopp dev_put(dev);
893c18ce101SOliver Hartkopp
894c18ce101SOliver Hartkopp if (err)
895ebad5c09SIlpo Järvinen goto send_failed;
896c18ce101SOliver Hartkopp
897c18ce101SOliver Hartkopp return size;
898ebad5c09SIlpo Järvinen
899ebad5c09SIlpo Järvinen free_skb:
900ebad5c09SIlpo Järvinen kfree_skb(skb);
901ebad5c09SIlpo Järvinen put_dev:
902ebad5c09SIlpo Järvinen dev_put(dev);
903ebad5c09SIlpo Järvinen send_failed:
904ebad5c09SIlpo Järvinen return err;
905c18ce101SOliver Hartkopp }
906c18ce101SOliver Hartkopp
raw_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)9071b784140SYing Xue static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
9081b784140SYing Xue int flags)
909c18ce101SOliver Hartkopp {
910c18ce101SOliver Hartkopp struct sock *sk = sock->sk;
911c18ce101SOliver Hartkopp struct sk_buff *skb;
912a219994bSUrs Thuermann int err = 0;
913c18ce101SOliver Hartkopp
914eb88531bSVincent Mailhol if (flags & MSG_ERRQUEUE)
915eb88531bSVincent Mailhol return sock_recv_errqueue(sk, msg, size,
916eb88531bSVincent Mailhol SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
917eb88531bSVincent Mailhol
918f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err);
919c18ce101SOliver Hartkopp if (!skb)
920a219994bSUrs Thuermann return err;
921c18ce101SOliver Hartkopp
922821047c4SOliver Hartkopp if (size < skb->len)
923c18ce101SOliver Hartkopp msg->msg_flags |= MSG_TRUNC;
924c18ce101SOliver Hartkopp else
925821047c4SOliver Hartkopp size = skb->len;
926c18ce101SOliver Hartkopp
9277eab8d9eSAl Viro err = memcpy_to_msg(msg, skb->data, size);
928a219994bSUrs Thuermann if (err < 0) {
929c18ce101SOliver Hartkopp skb_free_datagram(sk, skb);
930a219994bSUrs Thuermann return err;
931c18ce101SOliver Hartkopp }
932c18ce101SOliver Hartkopp
9336fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb);
934c18ce101SOliver Hartkopp
935c18ce101SOliver Hartkopp if (msg->msg_name) {
9369e971474SOliver Hartkopp __sockaddr_check_size(RAW_MIN_NAMELEN);
9379e971474SOliver Hartkopp msg->msg_namelen = RAW_MIN_NAMELEN;
938c18ce101SOliver Hartkopp memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
939c18ce101SOliver Hartkopp }
940c18ce101SOliver Hartkopp
9411e55659cSOliver Hartkopp /* assign the flags that have been recorded in raw_rcv() */
9421e55659cSOliver Hartkopp msg->msg_flags |= *(raw_flags(skb));
9431e55659cSOliver Hartkopp
944c18ce101SOliver Hartkopp skb_free_datagram(sk, skb);
945c18ce101SOliver Hartkopp
946c18ce101SOliver Hartkopp return size;
947c18ce101SOliver Hartkopp }
948c18ce101SOliver Hartkopp
raw_sock_no_ioctlcmd(struct socket * sock,unsigned int cmd,unsigned long arg)949af0b1470SMarc Kleine-Budde static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
950473d924dSOliver Hartkopp unsigned long arg)
951473d924dSOliver Hartkopp {
952473d924dSOliver Hartkopp /* no ioctls for socket layer -> hand it down to NIC layer */
953473d924dSOliver Hartkopp return -ENOIOCTLCMD;
954473d924dSOliver Hartkopp }
955473d924dSOliver Hartkopp
95653914b67SOliver Hartkopp static const struct proto_ops raw_ops = {
957c18ce101SOliver Hartkopp .family = PF_CAN,
958c18ce101SOliver Hartkopp .release = raw_release,
959c18ce101SOliver Hartkopp .bind = raw_bind,
960c18ce101SOliver Hartkopp .connect = sock_no_connect,
961c18ce101SOliver Hartkopp .socketpair = sock_no_socketpair,
962c18ce101SOliver Hartkopp .accept = sock_no_accept,
963c18ce101SOliver Hartkopp .getname = raw_getname,
964a11e1d43SLinus Torvalds .poll = datagram_poll,
965473d924dSOliver Hartkopp .ioctl = raw_sock_no_ioctlcmd,
966c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp,
967c18ce101SOliver Hartkopp .listen = sock_no_listen,
968c18ce101SOliver Hartkopp .shutdown = sock_no_shutdown,
969c18ce101SOliver Hartkopp .setsockopt = raw_setsockopt,
970c18ce101SOliver Hartkopp .getsockopt = raw_getsockopt,
971c18ce101SOliver Hartkopp .sendmsg = raw_sendmsg,
972c18ce101SOliver Hartkopp .recvmsg = raw_recvmsg,
973c18ce101SOliver Hartkopp .mmap = sock_no_mmap,
974c18ce101SOliver Hartkopp };
975c18ce101SOliver Hartkopp
976c18ce101SOliver Hartkopp static struct proto raw_proto __read_mostly = {
977c18ce101SOliver Hartkopp .name = "CAN_RAW",
978c18ce101SOliver Hartkopp .owner = THIS_MODULE,
979c18ce101SOliver Hartkopp .obj_size = sizeof(struct raw_sock),
980c18ce101SOliver Hartkopp .init = raw_init,
981c18ce101SOliver Hartkopp };
982c18ce101SOliver Hartkopp
9831650629dSKurt Van Dijck static const struct can_proto raw_can_proto = {
984c18ce101SOliver Hartkopp .type = SOCK_RAW,
985c18ce101SOliver Hartkopp .protocol = CAN_RAW,
986c18ce101SOliver Hartkopp .ops = &raw_ops,
987c18ce101SOliver Hartkopp .prot = &raw_proto,
988c18ce101SOliver Hartkopp };
989c18ce101SOliver Hartkopp
9908d0caedbSTetsuo Handa static struct notifier_block canraw_notifier = {
9918d0caedbSTetsuo Handa .notifier_call = raw_notifier
9928d0caedbSTetsuo Handa };
9938d0caedbSTetsuo Handa
raw_module_init(void)994c18ce101SOliver Hartkopp static __init int raw_module_init(void)
995c18ce101SOliver Hartkopp {
996c18ce101SOliver Hartkopp int err;
997c18ce101SOliver Hartkopp
998f726f3d3SOliver Hartkopp pr_info("can: raw protocol\n");
999c18ce101SOliver Hartkopp
1000c28b3bffSZiyang Xuan err = register_netdevice_notifier(&canraw_notifier);
1001c28b3bffSZiyang Xuan if (err)
1002c28b3bffSZiyang Xuan return err;
1003c18ce101SOliver Hartkopp
1004c28b3bffSZiyang Xuan err = can_proto_register(&raw_can_proto);
1005c28b3bffSZiyang Xuan if (err < 0) {
1006c28b3bffSZiyang Xuan pr_err("can: registration of raw protocol failed\n");
1007c28b3bffSZiyang Xuan goto register_proto_failed;
1008c28b3bffSZiyang Xuan }
1009c28b3bffSZiyang Xuan
1010c28b3bffSZiyang Xuan return 0;
1011c28b3bffSZiyang Xuan
1012c28b3bffSZiyang Xuan register_proto_failed:
1013c28b3bffSZiyang Xuan unregister_netdevice_notifier(&canraw_notifier);
1014c18ce101SOliver Hartkopp return err;
1015c18ce101SOliver Hartkopp }
1016c18ce101SOliver Hartkopp
raw_module_exit(void)1017c18ce101SOliver Hartkopp static __exit void raw_module_exit(void)
1018c18ce101SOliver Hartkopp {
1019c18ce101SOliver Hartkopp can_proto_unregister(&raw_can_proto);
10208d0caedbSTetsuo Handa unregister_netdevice_notifier(&canraw_notifier);
1021c18ce101SOliver Hartkopp }
1022c18ce101SOliver Hartkopp
1023c18ce101SOliver Hartkopp module_init(raw_module_init);
1024c18ce101SOliver Hartkopp module_exit(raw_module_exit);
1025