blob: 7105fa4824e4b4f48b30186828126c59fd5f2938 [file] [log] [blame]
Masahiro Yamadad77cd7f2020-04-03 16:37:41 +09001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
Marc Kleine-Budded6ada832019-07-24 14:16:29 +02002/* raw.c - Raw sockets for protocol family CAN
Oliver Hartkoppc18ce102007-11-16 15:53:09 -08003 *
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
23 *
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * DAMAGE.
39 *
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080040 */
41
42#include <linux/module.h>
43#include <linux/init.h>
44#include <linux/uio.h>
45#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080047#include <linux/netdevice.h>
48#include <linux/socket.h>
49#include <linux/if_arp.h>
50#include <linux/skbuff.h>
51#include <linux/can.h>
52#include <linux/can/core.h>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010053#include <linux/can/skb.h>
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080054#include <linux/can/raw.h>
55#include <net/sock.h>
56#include <net/net_namespace.h>
57
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080058MODULE_DESCRIPTION("PF_CAN raw protocol");
59MODULE_LICENSE("Dual BSD/GPL");
60MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
Lothar Waßmannb13bb2e2009-07-14 23:12:25 +000061MODULE_ALIAS("can-proto-1");
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080062
Oliver Hartkopp9e971472021-03-25 13:58:48 +010063#define RAW_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
64
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080065#define MASK_ALL 0
66
Marc Kleine-Budded6ada832019-07-24 14:16:29 +020067/* A raw socket has a list of can_filters attached to it, each receiving
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080068 * the CAN frames matching that filter. If the filter list is empty,
69 * no CAN frames will be received by the socket. The default after
70 * opening the socket, is to have one filter which receives all frames.
71 * The filter list is allocated dynamically with the exception of the
72 * list containing only one item. This common case is optimized by
73 * storing the single filter in dfilter, to avoid using dynamic memory.
74 */
75
Oliver Hartkopp514ac992015-04-01 07:50:28 +020076struct uniqframe {
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +020077 int skbcnt;
Oliver Hartkopp514ac992015-04-01 07:50:28 +020078 const struct sk_buff *skb;
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +020079 unsigned int join_rx_count;
Oliver Hartkopp514ac992015-04-01 07:50:28 +020080};
81
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080082struct raw_sock {
83 struct sock sk;
84 int bound;
85 int ifindex;
Tetsuo Handa8d0caed2021-06-05 19:26:35 +090086 struct list_head notifier;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080087 int loopback;
88 int recv_own_msgs;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +020089 int fd_frames;
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +020090 int join_filters;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080091 int count; /* number of active filters */
92 struct can_filter dfilter; /* default/single filter */
93 struct can_filter *filter; /* pointer to filter(s) */
94 can_err_mask_t err_mask;
Oliver Hartkopp514ac992015-04-01 07:50:28 +020095 struct uniqframe __percpu *uniq;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080096};
97
Tetsuo Handa8d0caed2021-06-05 19:26:35 +090098static LIST_HEAD(raw_notifier_list);
99static DEFINE_SPINLOCK(raw_notifier_lock);
100static struct raw_sock *raw_busy_notifier;
101
Marc Kleine-Budded6ada832019-07-24 14:16:29 +0200102/* Return pointer to store the extra msg flags for raw_recvmsg().
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000103 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
104 * in skb->cb.
105 */
106static inline unsigned int *raw_flags(struct sk_buff *skb)
107{
Eyal Birgerb4772ef2015-03-01 14:58:29 +0200108 sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
109 sizeof(unsigned int));
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000110
111 /* return pointer after struct sockaddr_can */
112 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
113}
114
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800115static inline struct raw_sock *raw_sk(const struct sock *sk)
116{
117 return (struct raw_sock *)sk;
118}
119
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000120static void raw_rcv(struct sk_buff *oskb, void *data)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800121{
122 struct sock *sk = (struct sock *)data;
123 struct raw_sock *ro = raw_sk(sk);
124 struct sockaddr_can *addr;
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000125 struct sk_buff *skb;
126 unsigned int *pflags;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800127
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800128 /* check the received tx sock reference */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000129 if (!ro->recv_own_msgs && oskb->sk == sk)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800130 return;
131
Oliver Hartkopp821047c2014-03-01 15:31:53 +0100132 /* do not pass non-CAN2.0 frames to a legacy socket */
133 if (!ro->fd_frames && oskb->len != CAN_MTU)
134 return;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200135
Oliver Hartkopp514ac992015-04-01 07:50:28 +0200136 /* eliminate multiple filter matches for the same skb */
137 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200138 this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200139 if (ro->join_filters) {
140 this_cpu_inc(ro->uniq->join_rx_count);
141 /* drop frame until all enabled filters matched */
142 if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
143 return;
144 } else {
145 return;
146 }
Oliver Hartkopp514ac992015-04-01 07:50:28 +0200147 } else {
148 this_cpu_ptr(ro->uniq)->skb = oskb;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200149 this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt;
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200150 this_cpu_ptr(ro->uniq)->join_rx_count = 1;
151 /* drop first frame to check all enabled filters? */
152 if (ro->join_filters && ro->count > 1)
153 return;
Oliver Hartkopp514ac992015-04-01 07:50:28 +0200154 }
155
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800156 /* clone the given skb to be able to enqueue it into the rcv queue */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000157 skb = skb_clone(oskb, GFP_ATOMIC);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800158 if (!skb)
159 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800160
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200161 /* Put the datagram to the queue so that raw_recvmsg() can get
162 * it from there. We need to pass the interface index to
163 * raw_recvmsg(). We pass a whole struct sockaddr_can in
164 * skb->cb containing the interface index.
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800165 */
166
Eyal Birgerb4772ef2015-03-01 14:58:29 +0200167 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800168 addr = (struct sockaddr_can *)skb->cb;
169 memset(addr, 0, sizeof(*addr));
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200170 addr->can_family = AF_CAN;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800171 addr->can_ifindex = skb->dev->ifindex;
172
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000173 /* add CAN specific message flags for raw_recvmsg() */
174 pflags = raw_flags(skb);
175 *pflags = 0;
176 if (oskb->sk)
177 *pflags |= MSG_DONTROUTE;
178 if (oskb->sk == sk)
179 *pflags |= MSG_CONFIRM;
180
Urs Thuermanna2199942008-02-07 18:05:04 -0800181 if (sock_queue_rcv_skb(sk, skb) < 0)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800182 kfree_skb(skb);
183}
184
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100185static int raw_enable_filters(struct net *net, struct net_device *dev,
186 struct sock *sk, struct can_filter *filter,
187 int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800188{
189 int err = 0;
190 int i;
191
192 for (i = 0; i < count; i++) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100193 err = can_rx_register(net, dev, filter[i].can_id,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800194 filter[i].can_mask,
Eric Dumazetf1712c72017-01-27 08:11:44 -0800195 raw_rcv, sk, "raw", sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800196 if (err) {
197 /* clean up successfully registered filters */
198 while (--i >= 0)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100199 can_rx_unregister(net, dev, filter[i].can_id,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800200 filter[i].can_mask,
201 raw_rcv, sk);
202 break;
203 }
204 }
205
206 return err;
207}
208
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100209static int raw_enable_errfilter(struct net *net, struct net_device *dev,
210 struct sock *sk, can_err_mask_t err_mask)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800211{
212 int err = 0;
213
214 if (err_mask)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100215 err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG,
Eric Dumazetf1712c72017-01-27 08:11:44 -0800216 raw_rcv, sk, "raw", sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800217
218 return err;
219}
220
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100221static void raw_disable_filters(struct net *net, struct net_device *dev,
222 struct sock *sk, struct can_filter *filter,
223 int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800224{
225 int i;
226
227 for (i = 0; i < count; i++)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100228 can_rx_unregister(net, dev, filter[i].can_id,
229 filter[i].can_mask, raw_rcv, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800230}
231
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100232static inline void raw_disable_errfilter(struct net *net,
233 struct net_device *dev,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800234 struct sock *sk,
235 can_err_mask_t err_mask)
236
237{
238 if (err_mask)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100239 can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800240 raw_rcv, sk);
241}
242
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100243static inline void raw_disable_allfilters(struct net *net,
244 struct net_device *dev,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800245 struct sock *sk)
246{
247 struct raw_sock *ro = raw_sk(sk);
248
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100249 raw_disable_filters(net, dev, sk, ro->filter, ro->count);
250 raw_disable_errfilter(net, dev, sk, ro->err_mask);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800251}
252
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100253static int raw_enable_allfilters(struct net *net, struct net_device *dev,
254 struct sock *sk)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800255{
256 struct raw_sock *ro = raw_sk(sk);
257 int err;
258
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100259 err = raw_enable_filters(net, dev, sk, ro->filter, ro->count);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800260 if (!err) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100261 err = raw_enable_errfilter(net, dev, sk, ro->err_mask);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800262 if (err)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100263 raw_disable_filters(net, dev, sk, ro->filter,
264 ro->count);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800265 }
266
267 return err;
268}
269
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900270static void raw_notify(struct raw_sock *ro, unsigned long msg,
271 struct net_device *dev)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800272{
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800273 struct sock *sk = &ro->sk;
274
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100275 if (!net_eq(dev_net(dev), sock_net(sk)))
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900276 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800277
278 if (ro->ifindex != dev->ifindex)
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900279 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800280
281 switch (msg) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800282 case NETDEV_UNREGISTER:
283 lock_sock(sk);
284 /* remove current filters & unregister */
285 if (ro->bound)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100286 raw_disable_allfilters(dev_net(dev), dev, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800287
288 if (ro->count > 1)
289 kfree(ro->filter);
290
291 ro->ifindex = 0;
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200292 ro->bound = 0;
293 ro->count = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800294 release_sock(sk);
295
296 sk->sk_err = ENODEV;
297 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400298 sk_error_report(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800299 break;
300
301 case NETDEV_DOWN:
302 sk->sk_err = ENETDOWN;
303 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400304 sk_error_report(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800305 break;
306 }
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900307}
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800308
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900309static int raw_notifier(struct notifier_block *nb, unsigned long msg,
310 void *ptr)
311{
312 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
313
314 if (dev->type != ARPHRD_CAN)
315 return NOTIFY_DONE;
316 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
317 return NOTIFY_DONE;
318 if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
319 return NOTIFY_DONE;
320
321 spin_lock(&raw_notifier_lock);
322 list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
323 spin_unlock(&raw_notifier_lock);
324 raw_notify(raw_busy_notifier, msg, dev);
325 spin_lock(&raw_notifier_lock);
326 }
327 raw_busy_notifier = NULL;
328 spin_unlock(&raw_notifier_lock);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800329 return NOTIFY_DONE;
330}
331
332static int raw_init(struct sock *sk)
333{
334 struct raw_sock *ro = raw_sk(sk);
335
336 ro->bound = 0;
337 ro->ifindex = 0;
338
339 /* set default filter to single entry dfilter */
340 ro->dfilter.can_id = 0;
341 ro->dfilter.can_mask = MASK_ALL;
342 ro->filter = &ro->dfilter;
343 ro->count = 1;
344
345 /* set default loopback behaviour */
346 ro->loopback = 1;
347 ro->recv_own_msgs = 0;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200348 ro->fd_frames = 0;
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200349 ro->join_filters = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800350
Oliver Hartkopp514ac992015-04-01 07:50:28 +0200351 /* alloc_percpu provides zero'ed memory */
352 ro->uniq = alloc_percpu(struct uniqframe);
353 if (unlikely(!ro->uniq))
354 return -ENOMEM;
355
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800356 /* set notifier */
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900357 spin_lock(&raw_notifier_lock);
358 list_add_tail(&ro->notifier, &raw_notifier_list);
359 spin_unlock(&raw_notifier_lock);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800360
361 return 0;
362}
363
364static int raw_release(struct socket *sock)
365{
366 struct sock *sk = sock->sk;
Oliver Hartkopp10022a62011-04-20 01:57:15 +0000367 struct raw_sock *ro;
368
369 if (!sk)
370 return 0;
371
372 ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800373
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900374 spin_lock(&raw_notifier_lock);
375 while (raw_busy_notifier == ro) {
376 spin_unlock(&raw_notifier_lock);
377 schedule_timeout_uninterruptible(1);
378 spin_lock(&raw_notifier_lock);
379 }
380 list_del(&ro->notifier);
381 spin_unlock(&raw_notifier_lock);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800382
383 lock_sock(sk);
384
385 /* remove current filters & unregister */
386 if (ro->bound) {
387 if (ro->ifindex) {
388 struct net_device *dev;
389
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100390 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800391 if (dev) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100392 raw_disable_allfilters(dev_net(dev), dev, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800393 dev_put(dev);
394 }
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200395 } else {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100396 raw_disable_allfilters(sock_net(sk), NULL, sk);
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200397 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800398 }
399
400 if (ro->count > 1)
401 kfree(ro->filter);
402
403 ro->ifindex = 0;
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200404 ro->bound = 0;
405 ro->count = 0;
Oliver Hartkopp514ac992015-04-01 07:50:28 +0200406 free_percpu(ro->uniq);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800407
Lothar Waßmannf7e5cc0c2009-07-14 23:10:21 +0000408 sock_orphan(sk);
409 sock->sk = NULL;
410
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800411 release_sock(sk);
412 sock_put(sk);
413
414 return 0;
415}
416
417static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
418{
419 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
420 struct sock *sk = sock->sk;
421 struct raw_sock *ro = raw_sk(sk);
422 int ifindex;
423 int err = 0;
424 int notify_enetdown = 0;
425
Oliver Hartkopp9e971472021-03-25 13:58:48 +0100426 if (len < RAW_MIN_NAMELEN)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800427 return -EINVAL;
Marc Kleine-Buddeadb552c2017-07-30 23:51:01 +0200428 if (addr->can_family != AF_CAN)
429 return -EINVAL;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800430
431 lock_sock(sk);
432
433 if (ro->bound && addr->can_ifindex == ro->ifindex)
434 goto out;
435
436 if (addr->can_ifindex) {
437 struct net_device *dev;
438
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100439 dev = dev_get_by_index(sock_net(sk), addr->can_ifindex);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800440 if (!dev) {
441 err = -ENODEV;
442 goto out;
443 }
444 if (dev->type != ARPHRD_CAN) {
445 dev_put(dev);
446 err = -ENODEV;
447 goto out;
448 }
449 if (!(dev->flags & IFF_UP))
450 notify_enetdown = 1;
451
452 ifindex = dev->ifindex;
453
454 /* filters set by default/setsockopt */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100455 err = raw_enable_allfilters(sock_net(sk), dev, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800456 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800457 } else {
458 ifindex = 0;
459
460 /* filters set by default/setsockopt */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100461 err = raw_enable_allfilters(sock_net(sk), NULL, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800462 }
463
464 if (!err) {
465 if (ro->bound) {
466 /* unregister old filters */
467 if (ro->ifindex) {
468 struct net_device *dev;
469
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100470 dev = dev_get_by_index(sock_net(sk),
471 ro->ifindex);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800472 if (dev) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100473 raw_disable_allfilters(dev_net(dev),
474 dev, sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800475 dev_put(dev);
476 }
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200477 } else {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100478 raw_disable_allfilters(sock_net(sk), NULL, sk);
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200479 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800480 }
481 ro->ifindex = ifindex;
482 ro->bound = 1;
483 }
484
485 out:
486 release_sock(sk);
487
488 if (notify_enetdown) {
489 sk->sk_err = ENETDOWN;
490 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -0400491 sk_error_report(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800492 }
493
494 return err;
495}
496
497static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100498 int peer)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800499{
500 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
501 struct sock *sk = sock->sk;
502 struct raw_sock *ro = raw_sk(sk);
503
504 if (peer)
505 return -EOPNOTSUPP;
506
Oliver Hartkopp9e971472021-03-25 13:58:48 +0100507 memset(addr, 0, RAW_MIN_NAMELEN);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800508 addr->can_family = AF_CAN;
509 addr->can_ifindex = ro->ifindex;
510
Oliver Hartkopp9e971472021-03-25 13:58:48 +0100511 return RAW_MIN_NAMELEN;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800512}
513
514static int raw_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200515 sockptr_t optval, unsigned int optlen)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800516{
517 struct sock *sk = sock->sk;
518 struct raw_sock *ro = raw_sk(sk);
519 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
520 struct can_filter sfilter; /* single filter */
521 struct net_device *dev = NULL;
522 can_err_mask_t err_mask = 0;
523 int count = 0;
524 int err = 0;
525
526 if (level != SOL_CAN_RAW)
527 return -EINVAL;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800528
529 switch (optname) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800530 case CAN_RAW_FILTER:
531 if (optlen % sizeof(struct can_filter) != 0)
532 return -EINVAL;
533
Marc Kleine-Budde332b05c2016-12-05 11:44:23 +0100534 if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter))
535 return -EINVAL;
536
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800537 count = optlen / sizeof(struct can_filter);
538
539 if (count > 1) {
540 /* filter does not fit into dfilter => alloc space */
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200541 filter = memdup_sockptr(optval, optlen);
Julia Lawall16dff912010-05-21 22:18:34 +0000542 if (IS_ERR(filter))
543 return PTR_ERR(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800544 } else if (count == 1) {
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200545 if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700546 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800547 }
548
Ziyang Xuan54f93332021-07-22 15:08:19 +0800549 rtnl_lock();
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800550 lock_sock(sk);
551
Ziyang Xuan54f93332021-07-22 15:08:19 +0800552 if (ro->bound && ro->ifindex) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100553 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
Ziyang Xuan54f93332021-07-22 15:08:19 +0800554 if (!dev) {
555 if (count > 1)
556 kfree(filter);
557 err = -ENODEV;
558 goto out_fil;
559 }
560 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800561
562 if (ro->bound) {
563 /* (try to) register the new filters */
564 if (count == 1)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100565 err = raw_enable_filters(sock_net(sk), dev, sk,
566 &sfilter, 1);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800567 else
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100568 err = raw_enable_filters(sock_net(sk), dev, sk,
569 filter, count);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800570 if (err) {
571 if (count > 1)
572 kfree(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800573 goto out_fil;
574 }
575
576 /* remove old filter registrations */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100577 raw_disable_filters(sock_net(sk), dev, sk, ro->filter,
578 ro->count);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800579 }
580
581 /* remove old filter space */
582 if (ro->count > 1)
583 kfree(ro->filter);
584
585 /* link new filters to the socket */
586 if (count == 1) {
587 /* copy filter data for single filter */
588 ro->dfilter = sfilter;
589 filter = &ro->dfilter;
590 }
591 ro->filter = filter;
592 ro->count = count;
593
594 out_fil:
Yajun Deng1160dfa2021-08-05 19:55:27 +0800595 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800596 release_sock(sk);
Ziyang Xuan54f93332021-07-22 15:08:19 +0800597 rtnl_unlock();
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800598
599 break;
600
601 case CAN_RAW_ERR_FILTER:
602 if (optlen != sizeof(err_mask))
603 return -EINVAL;
604
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200605 if (copy_from_sockptr(&err_mask, optval, optlen))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700606 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800607
608 err_mask &= CAN_ERR_MASK;
609
Ziyang Xuan54f93332021-07-22 15:08:19 +0800610 rtnl_lock();
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800611 lock_sock(sk);
612
Ziyang Xuan54f93332021-07-22 15:08:19 +0800613 if (ro->bound && ro->ifindex) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100614 dev = dev_get_by_index(sock_net(sk), ro->ifindex);
Ziyang Xuan54f93332021-07-22 15:08:19 +0800615 if (!dev) {
616 err = -ENODEV;
617 goto out_err;
618 }
619 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800620
621 /* remove current error mask */
622 if (ro->bound) {
623 /* (try to) register the new err_mask */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100624 err = raw_enable_errfilter(sock_net(sk), dev, sk,
625 err_mask);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800626
627 if (err)
628 goto out_err;
629
630 /* remove old err_mask registration */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100631 raw_disable_errfilter(sock_net(sk), dev, sk,
632 ro->err_mask);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800633 }
634
635 /* link new err_mask to the socket */
636 ro->err_mask = err_mask;
637
638 out_err:
Yajun Deng1160dfa2021-08-05 19:55:27 +0800639 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800640 release_sock(sk);
Ziyang Xuan54f93332021-07-22 15:08:19 +0800641 rtnl_unlock();
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800642
643 break;
644
645 case CAN_RAW_LOOPBACK:
646 if (optlen != sizeof(ro->loopback))
647 return -EINVAL;
648
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200649 if (copy_from_sockptr(&ro->loopback, optval, optlen))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700650 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800651
652 break;
653
654 case CAN_RAW_RECV_OWN_MSGS:
655 if (optlen != sizeof(ro->recv_own_msgs))
656 return -EINVAL;
657
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200658 if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700659 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800660
661 break;
662
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200663 case CAN_RAW_FD_FRAMES:
664 if (optlen != sizeof(ro->fd_frames))
665 return -EINVAL;
666
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200667 if (copy_from_sockptr(&ro->fd_frames, optval, optlen))
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200668 return -EFAULT;
669
670 break;
671
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200672 case CAN_RAW_JOIN_FILTERS:
673 if (optlen != sizeof(ro->join_filters))
674 return -EINVAL;
675
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200676 if (copy_from_sockptr(&ro->join_filters, optval, optlen))
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200677 return -EFAULT;
678
679 break;
680
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800681 default:
682 return -ENOPROTOOPT;
683 }
684 return err;
685}
686
687static int raw_getsockopt(struct socket *sock, int level, int optname,
688 char __user *optval, int __user *optlen)
689{
690 struct sock *sk = sock->sk;
691 struct raw_sock *ro = raw_sk(sk);
692 int len;
693 void *val;
694 int err = 0;
695
696 if (level != SOL_CAN_RAW)
697 return -EINVAL;
698 if (get_user(len, optlen))
699 return -EFAULT;
700 if (len < 0)
701 return -EINVAL;
702
703 switch (optname) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800704 case CAN_RAW_FILTER:
705 lock_sock(sk);
706 if (ro->count > 0) {
707 int fsize = ro->count * sizeof(struct can_filter);
Marc Kleine-Budded5e4eca2019-07-24 14:28:21 +0200708
Oliver Hartkopp0de70e22020-12-16 18:49:28 +0100709 /* user space buffer to small for filter list? */
710 if (len < fsize) {
711 /* return -ERANGE and needed space in optlen */
712 err = -ERANGE;
713 if (put_user(fsize, optlen))
714 err = -EFAULT;
715 } else {
716 if (len > fsize)
717 len = fsize;
718 if (copy_to_user(optval, ro->filter, len))
719 err = -EFAULT;
720 }
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200721 } else {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800722 len = 0;
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200723 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800724 release_sock(sk);
725
726 if (!err)
727 err = put_user(len, optlen);
728 return err;
729
730 case CAN_RAW_ERR_FILTER:
731 if (len > sizeof(can_err_mask_t))
732 len = sizeof(can_err_mask_t);
733 val = &ro->err_mask;
734 break;
735
736 case CAN_RAW_LOOPBACK:
737 if (len > sizeof(int))
738 len = sizeof(int);
739 val = &ro->loopback;
740 break;
741
742 case CAN_RAW_RECV_OWN_MSGS:
743 if (len > sizeof(int))
744 len = sizeof(int);
745 val = &ro->recv_own_msgs;
746 break;
747
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200748 case CAN_RAW_FD_FRAMES:
749 if (len > sizeof(int))
750 len = sizeof(int);
751 val = &ro->fd_frames;
752 break;
753
Oliver Hartkoppa5581ef2015-04-01 07:50:29 +0200754 case CAN_RAW_JOIN_FILTERS:
755 if (len > sizeof(int))
756 len = sizeof(int);
757 val = &ro->join_filters;
758 break;
759
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800760 default:
761 return -ENOPROTOOPT;
762 }
763
764 if (put_user(len, optlen))
765 return -EFAULT;
766 if (copy_to_user(optval, val, len))
767 return -EFAULT;
768 return 0;
769}
770
Ying Xue1b784142015-03-02 15:37:48 +0800771static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800772{
773 struct sock *sk = sock->sk;
774 struct raw_sock *ro = raw_sk(sk);
775 struct sk_buff *skb;
776 struct net_device *dev;
777 int ifindex;
778 int err;
779
780 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100781 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800782
Oliver Hartkopp9e971472021-03-25 13:58:48 +0100783 if (msg->msg_namelen < RAW_MIN_NAMELEN)
Kurt Van Dijck5e5073282011-01-15 20:56:42 -0800784 return -EINVAL;
785
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800786 if (addr->can_family != AF_CAN)
787 return -EINVAL;
788
789 ifindex = addr->can_ifindex;
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200790 } else {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800791 ifindex = ro->ifindex;
Marc Kleine-Buddebff10042019-08-13 09:03:55 +0200792 }
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800793
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100794 dev = dev_get_by_index(sock_net(sk), ifindex);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800795 if (!dev)
796 return -ENXIO;
797
Oliver Hartkoppa43608f2018-10-24 10:27:12 +0200798 err = -EINVAL;
799 if (ro->fd_frames && dev->mtu == CANFD_MTU) {
800 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
801 goto put_dev;
802 } else {
803 if (unlikely(size != CAN_MTU))
804 goto put_dev;
805 }
806
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100807 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
808 msg->msg_flags & MSG_DONTWAIT, &err);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800809 if (!skb)
810 goto put_dev;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800811
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000812 can_skb_reserve(skb);
813 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200814 can_skb_prv(skb)->skbcnt = 0;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100815
Al Viro6ce8e9c2014-04-06 21:25:44 -0400816 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800817 if (err < 0)
818 goto free_skb;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +0000819
Willem de Bruijn8f932f72018-12-17 12:24:00 -0500820 skb_setup_tx_timestamp(skb, sk->sk_tsflags);
Oliver Hartkoppcff0d6e2010-08-03 00:31:48 -0700821
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800822 skb->dev = dev;
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200823 skb->sk = sk;
Rostislav Lisovybb5ecb02014-01-24 13:17:37 +0100824 skb->priority = sk->sk_priority;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800825
826 err = can_send(skb, ro->loopback);
827
828 dev_put(dev);
829
830 if (err)
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800831 goto send_failed;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800832
833 return size;
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800834
835free_skb:
836 kfree_skb(skb);
837put_dev:
838 dev_put(dev);
839send_failed:
840 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800841}
842
Ying Xue1b784142015-03-02 15:37:48 +0800843static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
844 int flags)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800845{
846 struct sock *sk = sock->sk;
847 struct sk_buff *skb;
Urs Thuermanna2199942008-02-07 18:05:04 -0800848 int err = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800849 int noblock;
850
Marc Kleine-Budde6a54dde2020-09-16 00:34:56 +0200851 noblock = flags & MSG_DONTWAIT;
852 flags &= ~MSG_DONTWAIT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800853
Vincent Mailholeb885312020-09-27 01:24:31 +0900854 if (flags & MSG_ERRQUEUE)
855 return sock_recv_errqueue(sk, msg, size,
856 SOL_CAN_RAW, SCM_CAN_RAW_ERRQUEUE);
857
Urs Thuermanna2199942008-02-07 18:05:04 -0800858 skb = skb_recv_datagram(sk, flags, noblock, &err);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800859 if (!skb)
Urs Thuermanna2199942008-02-07 18:05:04 -0800860 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800861
Oliver Hartkopp821047c2014-03-01 15:31:53 +0100862 if (size < skb->len)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800863 msg->msg_flags |= MSG_TRUNC;
864 else
Oliver Hartkopp821047c2014-03-01 15:31:53 +0100865 size = skb->len;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800866
Al Viro7eab8d92014-04-06 21:51:23 -0400867 err = memcpy_to_msg(msg, skb->data, size);
Urs Thuermanna2199942008-02-07 18:05:04 -0800868 if (err < 0) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800869 skb_free_datagram(sk, skb);
Urs Thuermanna2199942008-02-07 18:05:04 -0800870 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800871 }
872
Neil Horman3b885782009-10-12 13:26:31 -0700873 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800874
875 if (msg->msg_name) {
Oliver Hartkopp9e971472021-03-25 13:58:48 +0100876 __sockaddr_check_size(RAW_MIN_NAMELEN);
877 msg->msg_namelen = RAW_MIN_NAMELEN;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800878 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
879 }
880
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000881 /* assign the flags that have been recorded in raw_rcv() */
882 msg->msg_flags |= *(raw_flags(skb));
883
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800884 skb_free_datagram(sk, skb);
885
886 return size;
887}
888
Marc Kleine-Buddeaf0b1472019-08-12 15:49:04 +0200889static int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
890 unsigned long arg)
Oliver Hartkopp473d9242019-07-29 22:40:56 +0200891{
892 /* no ioctls for socket layer -> hand it down to NIC layer */
893 return -ENOIOCTLCMD;
894}
895
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000896static const struct proto_ops raw_ops = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800897 .family = PF_CAN,
898 .release = raw_release,
899 .bind = raw_bind,
900 .connect = sock_no_connect,
901 .socketpair = sock_no_socketpair,
902 .accept = sock_no_accept,
903 .getname = raw_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700904 .poll = datagram_poll,
Oliver Hartkopp473d9242019-07-29 22:40:56 +0200905 .ioctl = raw_sock_no_ioctlcmd,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200906 .gettstamp = sock_gettstamp,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800907 .listen = sock_no_listen,
908 .shutdown = sock_no_shutdown,
909 .setsockopt = raw_setsockopt,
910 .getsockopt = raw_getsockopt,
911 .sendmsg = raw_sendmsg,
912 .recvmsg = raw_recvmsg,
913 .mmap = sock_no_mmap,
914 .sendpage = sock_no_sendpage,
915};
916
917static struct proto raw_proto __read_mostly = {
918 .name = "CAN_RAW",
919 .owner = THIS_MODULE,
920 .obj_size = sizeof(struct raw_sock),
921 .init = raw_init,
922};
923
Kurt Van Dijck16506292011-05-03 18:40:57 +0000924static const struct can_proto raw_can_proto = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800925 .type = SOCK_RAW,
926 .protocol = CAN_RAW,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800927 .ops = &raw_ops,
928 .prot = &raw_proto,
929};
930
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900931static struct notifier_block canraw_notifier = {
932 .notifier_call = raw_notifier
933};
934
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800935static __init int raw_module_init(void)
936{
937 int err;
938
Oliver Hartkoppf726f3d2020-10-12 09:43:54 +0200939 pr_info("can: raw protocol\n");
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800940
941 err = can_proto_register(&raw_can_proto);
942 if (err < 0)
Marc Kleine-Budded956b1a82019-08-13 09:07:14 +0200943 pr_err("can: registration of raw protocol failed\n");
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900944 else
945 register_netdevice_notifier(&canraw_notifier);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800946
947 return err;
948}
949
950static __exit void raw_module_exit(void)
951{
952 can_proto_unregister(&raw_can_proto);
Tetsuo Handa8d0caed2021-06-05 19:26:35 +0900953 unregister_netdevice_notifier(&canraw_notifier);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800954}
955
956module_init(raw_module_init);
957module_exit(raw_module_exit);