blob: 641e1c895123ac114330635c43d0b2ed778c2ae7 [file] [log] [blame]
Oliver Hartkoppc18ce102007-11-16 15:53:09 -08001/*
2 * raw.c - Raw sockets for protocol family CAN
3 *
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
58#define CAN_RAW_VERSION CAN_VERSION
Andi Kleen6299b662012-10-04 17:12:08 -070059static __initconst const char banner[] =
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080060 KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
61
62MODULE_DESCRIPTION("PF_CAN raw protocol");
63MODULE_LICENSE("Dual BSD/GPL");
64MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
Lothar Waßmannb13bb2e2009-07-14 23:12:25 +000065MODULE_ALIAS("can-proto-1");
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080066
67#define MASK_ALL 0
68
69/*
70 * A raw socket has a list of can_filters attached to it, each receiving
71 * the CAN frames matching that filter. If the filter list is empty,
72 * no CAN frames will be received by the socket. The default after
73 * opening the socket, is to have one filter which receives all frames.
74 * The filter list is allocated dynamically with the exception of the
75 * list containing only one item. This common case is optimized by
76 * storing the single filter in dfilter, to avoid using dynamic memory.
77 */
78
79struct raw_sock {
80 struct sock sk;
81 int bound;
82 int ifindex;
83 struct notifier_block notifier;
84 int loopback;
85 int recv_own_msgs;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +020086 int fd_frames;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080087 int count; /* number of active filters */
88 struct can_filter dfilter; /* default/single filter */
89 struct can_filter *filter; /* pointer to filter(s) */
90 can_err_mask_t err_mask;
91};
92
Oliver Hartkopp1e556592010-10-19 09:32:04 +000093/*
94 * Return pointer to store the extra msg flags for raw_recvmsg().
95 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
96 * in skb->cb.
97 */
98static inline unsigned int *raw_flags(struct sk_buff *skb)
99{
100 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
101 sizeof(unsigned int)));
102
103 /* return pointer after struct sockaddr_can */
104 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
105}
106
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800107static inline struct raw_sock *raw_sk(const struct sock *sk)
108{
109 return (struct raw_sock *)sk;
110}
111
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000112static void raw_rcv(struct sk_buff *oskb, void *data)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800113{
114 struct sock *sk = (struct sock *)data;
115 struct raw_sock *ro = raw_sk(sk);
116 struct sockaddr_can *addr;
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000117 struct sk_buff *skb;
118 unsigned int *pflags;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800119
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800120 /* check the received tx sock reference */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000121 if (!ro->recv_own_msgs && oskb->sk == sk)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800122 return;
123
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200124 /* do not pass frames with DLC > 8 to a legacy socket */
125 if (!ro->fd_frames) {
126 struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;
127
128 if (unlikely(cfd->len > CAN_MAX_DLEN))
129 return;
130 }
131
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800132 /* clone the given skb to be able to enqueue it into the rcv queue */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000133 skb = skb_clone(oskb, GFP_ATOMIC);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800134 if (!skb)
135 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800136
137 /*
138 * Put the datagram to the queue so that raw_recvmsg() can
139 * get it from there. We need to pass the interface index to
140 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
141 * containing the interface index.
142 */
143
144 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
145 addr = (struct sockaddr_can *)skb->cb;
146 memset(addr, 0, sizeof(*addr));
147 addr->can_family = AF_CAN;
148 addr->can_ifindex = skb->dev->ifindex;
149
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000150 /* add CAN specific message flags for raw_recvmsg() */
151 pflags = raw_flags(skb);
152 *pflags = 0;
153 if (oskb->sk)
154 *pflags |= MSG_DONTROUTE;
155 if (oskb->sk == sk)
156 *pflags |= MSG_CONFIRM;
157
Urs Thuermanna2199942008-02-07 18:05:04 -0800158 if (sock_queue_rcv_skb(sk, skb) < 0)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800159 kfree_skb(skb);
160}
161
162static int raw_enable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800163 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800164{
165 int err = 0;
166 int i;
167
168 for (i = 0; i < count; i++) {
169 err = can_rx_register(dev, filter[i].can_id,
170 filter[i].can_mask,
171 raw_rcv, sk, "raw");
172 if (err) {
173 /* clean up successfully registered filters */
174 while (--i >= 0)
175 can_rx_unregister(dev, filter[i].can_id,
176 filter[i].can_mask,
177 raw_rcv, sk);
178 break;
179 }
180 }
181
182 return err;
183}
184
185static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
186 can_err_mask_t err_mask)
187{
188 int err = 0;
189
190 if (err_mask)
191 err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
192 raw_rcv, sk, "raw");
193
194 return err;
195}
196
197static void raw_disable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800198 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800199{
200 int i;
201
202 for (i = 0; i < count; i++)
203 can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
204 raw_rcv, sk);
205}
206
207static inline void raw_disable_errfilter(struct net_device *dev,
208 struct sock *sk,
209 can_err_mask_t err_mask)
210
211{
212 if (err_mask)
213 can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
214 raw_rcv, sk);
215}
216
217static inline void raw_disable_allfilters(struct net_device *dev,
218 struct sock *sk)
219{
220 struct raw_sock *ro = raw_sk(sk);
221
222 raw_disable_filters(dev, sk, ro->filter, ro->count);
223 raw_disable_errfilter(dev, sk, ro->err_mask);
224}
225
226static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
227{
228 struct raw_sock *ro = raw_sk(sk);
229 int err;
230
231 err = raw_enable_filters(dev, sk, ro->filter, ro->count);
232 if (!err) {
233 err = raw_enable_errfilter(dev, sk, ro->err_mask);
234 if (err)
235 raw_disable_filters(dev, sk, ro->filter, ro->count);
236 }
237
238 return err;
239}
240
241static int raw_notifier(struct notifier_block *nb,
Jiri Pirko351638e2013-05-28 01:30:21 +0000242 unsigned long msg, void *ptr)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800243{
Jiri Pirko351638e2013-05-28 01:30:21 +0000244 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800245 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
246 struct sock *sk = &ro->sk;
247
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700248 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800249 return NOTIFY_DONE;
250
251 if (dev->type != ARPHRD_CAN)
252 return NOTIFY_DONE;
253
254 if (ro->ifindex != dev->ifindex)
255 return NOTIFY_DONE;
256
257 switch (msg) {
258
259 case NETDEV_UNREGISTER:
260 lock_sock(sk);
261 /* remove current filters & unregister */
262 if (ro->bound)
263 raw_disable_allfilters(dev, sk);
264
265 if (ro->count > 1)
266 kfree(ro->filter);
267
268 ro->ifindex = 0;
269 ro->bound = 0;
270 ro->count = 0;
271 release_sock(sk);
272
273 sk->sk_err = ENODEV;
274 if (!sock_flag(sk, SOCK_DEAD))
275 sk->sk_error_report(sk);
276 break;
277
278 case NETDEV_DOWN:
279 sk->sk_err = ENETDOWN;
280 if (!sock_flag(sk, SOCK_DEAD))
281 sk->sk_error_report(sk);
282 break;
283 }
284
285 return NOTIFY_DONE;
286}
287
288static int raw_init(struct sock *sk)
289{
290 struct raw_sock *ro = raw_sk(sk);
291
292 ro->bound = 0;
293 ro->ifindex = 0;
294
295 /* set default filter to single entry dfilter */
296 ro->dfilter.can_id = 0;
297 ro->dfilter.can_mask = MASK_ALL;
298 ro->filter = &ro->dfilter;
299 ro->count = 1;
300
301 /* set default loopback behaviour */
302 ro->loopback = 1;
303 ro->recv_own_msgs = 0;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200304 ro->fd_frames = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800305
306 /* set notifier */
307 ro->notifier.notifier_call = raw_notifier;
308
309 register_netdevice_notifier(&ro->notifier);
310
311 return 0;
312}
313
314static int raw_release(struct socket *sock)
315{
316 struct sock *sk = sock->sk;
Oliver Hartkopp10022a62011-04-20 01:57:15 +0000317 struct raw_sock *ro;
318
319 if (!sk)
320 return 0;
321
322 ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800323
324 unregister_netdevice_notifier(&ro->notifier);
325
326 lock_sock(sk);
327
328 /* remove current filters & unregister */
329 if (ro->bound) {
330 if (ro->ifindex) {
331 struct net_device *dev;
332
333 dev = dev_get_by_index(&init_net, ro->ifindex);
334 if (dev) {
335 raw_disable_allfilters(dev, sk);
336 dev_put(dev);
337 }
338 } else
339 raw_disable_allfilters(NULL, sk);
340 }
341
342 if (ro->count > 1)
343 kfree(ro->filter);
344
345 ro->ifindex = 0;
346 ro->bound = 0;
347 ro->count = 0;
348
Lothar Waßmannf7e5cc02009-07-14 23:10:21 +0000349 sock_orphan(sk);
350 sock->sk = NULL;
351
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800352 release_sock(sk);
353 sock_put(sk);
354
355 return 0;
356}
357
358static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
359{
360 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
361 struct sock *sk = sock->sk;
362 struct raw_sock *ro = raw_sk(sk);
363 int ifindex;
364 int err = 0;
365 int notify_enetdown = 0;
366
367 if (len < sizeof(*addr))
368 return -EINVAL;
369
370 lock_sock(sk);
371
372 if (ro->bound && addr->can_ifindex == ro->ifindex)
373 goto out;
374
375 if (addr->can_ifindex) {
376 struct net_device *dev;
377
378 dev = dev_get_by_index(&init_net, addr->can_ifindex);
379 if (!dev) {
380 err = -ENODEV;
381 goto out;
382 }
383 if (dev->type != ARPHRD_CAN) {
384 dev_put(dev);
385 err = -ENODEV;
386 goto out;
387 }
388 if (!(dev->flags & IFF_UP))
389 notify_enetdown = 1;
390
391 ifindex = dev->ifindex;
392
393 /* filters set by default/setsockopt */
394 err = raw_enable_allfilters(dev, sk);
395 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800396 } else {
397 ifindex = 0;
398
399 /* filters set by default/setsockopt */
400 err = raw_enable_allfilters(NULL, sk);
401 }
402
403 if (!err) {
404 if (ro->bound) {
405 /* unregister old filters */
406 if (ro->ifindex) {
407 struct net_device *dev;
408
409 dev = dev_get_by_index(&init_net, ro->ifindex);
410 if (dev) {
411 raw_disable_allfilters(dev, sk);
412 dev_put(dev);
413 }
414 } else
415 raw_disable_allfilters(NULL, sk);
416 }
417 ro->ifindex = ifindex;
418 ro->bound = 1;
419 }
420
421 out:
422 release_sock(sk);
423
424 if (notify_enetdown) {
425 sk->sk_err = ENETDOWN;
426 if (!sock_flag(sk, SOCK_DEAD))
427 sk->sk_error_report(sk);
428 }
429
430 return err;
431}
432
433static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
434 int *len, int peer)
435{
436 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
437 struct sock *sk = sock->sk;
438 struct raw_sock *ro = raw_sk(sk);
439
440 if (peer)
441 return -EOPNOTSUPP;
442
Eric Dumazete84b90a2009-08-06 20:27:04 +0000443 memset(addr, 0, sizeof(*addr));
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800444 addr->can_family = AF_CAN;
445 addr->can_ifindex = ro->ifindex;
446
447 *len = sizeof(*addr);
448
449 return 0;
450}
451
452static int raw_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700453 char __user *optval, unsigned int optlen)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800454{
455 struct sock *sk = sock->sk;
456 struct raw_sock *ro = raw_sk(sk);
457 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
458 struct can_filter sfilter; /* single filter */
459 struct net_device *dev = NULL;
460 can_err_mask_t err_mask = 0;
461 int count = 0;
462 int err = 0;
463
464 if (level != SOL_CAN_RAW)
465 return -EINVAL;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800466
467 switch (optname) {
468
469 case CAN_RAW_FILTER:
470 if (optlen % sizeof(struct can_filter) != 0)
471 return -EINVAL;
472
473 count = optlen / sizeof(struct can_filter);
474
475 if (count > 1) {
476 /* filter does not fit into dfilter => alloc space */
Julia Lawall16dff912010-05-21 22:18:34 +0000477 filter = memdup_user(optval, optlen);
478 if (IS_ERR(filter))
479 return PTR_ERR(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800480 } else if (count == 1) {
Eric Dumazet4ffa8702010-04-09 23:47:31 +0000481 if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700482 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800483 }
484
485 lock_sock(sk);
486
487 if (ro->bound && ro->ifindex)
488 dev = dev_get_by_index(&init_net, ro->ifindex);
489
490 if (ro->bound) {
491 /* (try to) register the new filters */
492 if (count == 1)
493 err = raw_enable_filters(dev, sk, &sfilter, 1);
494 else
495 err = raw_enable_filters(dev, sk, filter,
496 count);
497 if (err) {
498 if (count > 1)
499 kfree(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800500 goto out_fil;
501 }
502
503 /* remove old filter registrations */
504 raw_disable_filters(dev, sk, ro->filter, ro->count);
505 }
506
507 /* remove old filter space */
508 if (ro->count > 1)
509 kfree(ro->filter);
510
511 /* link new filters to the socket */
512 if (count == 1) {
513 /* copy filter data for single filter */
514 ro->dfilter = sfilter;
515 filter = &ro->dfilter;
516 }
517 ro->filter = filter;
518 ro->count = count;
519
520 out_fil:
521 if (dev)
522 dev_put(dev);
523
524 release_sock(sk);
525
526 break;
527
528 case CAN_RAW_ERR_FILTER:
529 if (optlen != sizeof(err_mask))
530 return -EINVAL;
531
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700532 if (copy_from_user(&err_mask, optval, optlen))
533 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800534
535 err_mask &= CAN_ERR_MASK;
536
537 lock_sock(sk);
538
539 if (ro->bound && ro->ifindex)
540 dev = dev_get_by_index(&init_net, ro->ifindex);
541
542 /* remove current error mask */
543 if (ro->bound) {
544 /* (try to) register the new err_mask */
545 err = raw_enable_errfilter(dev, sk, err_mask);
546
547 if (err)
548 goto out_err;
549
550 /* remove old err_mask registration */
551 raw_disable_errfilter(dev, sk, ro->err_mask);
552 }
553
554 /* link new err_mask to the socket */
555 ro->err_mask = err_mask;
556
557 out_err:
558 if (dev)
559 dev_put(dev);
560
561 release_sock(sk);
562
563 break;
564
565 case CAN_RAW_LOOPBACK:
566 if (optlen != sizeof(ro->loopback))
567 return -EINVAL;
568
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700569 if (copy_from_user(&ro->loopback, optval, optlen))
570 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800571
572 break;
573
574 case CAN_RAW_RECV_OWN_MSGS:
575 if (optlen != sizeof(ro->recv_own_msgs))
576 return -EINVAL;
577
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700578 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
579 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800580
581 break;
582
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200583 case CAN_RAW_FD_FRAMES:
584 if (optlen != sizeof(ro->fd_frames))
585 return -EINVAL;
586
587 if (copy_from_user(&ro->fd_frames, optval, optlen))
588 return -EFAULT;
589
590 break;
591
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800592 default:
593 return -ENOPROTOOPT;
594 }
595 return err;
596}
597
598static int raw_getsockopt(struct socket *sock, int level, int optname,
599 char __user *optval, int __user *optlen)
600{
601 struct sock *sk = sock->sk;
602 struct raw_sock *ro = raw_sk(sk);
603 int len;
604 void *val;
605 int err = 0;
606
607 if (level != SOL_CAN_RAW)
608 return -EINVAL;
609 if (get_user(len, optlen))
610 return -EFAULT;
611 if (len < 0)
612 return -EINVAL;
613
614 switch (optname) {
615
616 case CAN_RAW_FILTER:
617 lock_sock(sk);
618 if (ro->count > 0) {
619 int fsize = ro->count * sizeof(struct can_filter);
620 if (len > fsize)
621 len = fsize;
Pavel Emelyanov653252c2008-04-25 01:49:48 -0700622 if (copy_to_user(optval, ro->filter, len))
623 err = -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800624 } else
625 len = 0;
626 release_sock(sk);
627
628 if (!err)
629 err = put_user(len, optlen);
630 return err;
631
632 case CAN_RAW_ERR_FILTER:
633 if (len > sizeof(can_err_mask_t))
634 len = sizeof(can_err_mask_t);
635 val = &ro->err_mask;
636 break;
637
638 case CAN_RAW_LOOPBACK:
639 if (len > sizeof(int))
640 len = sizeof(int);
641 val = &ro->loopback;
642 break;
643
644 case CAN_RAW_RECV_OWN_MSGS:
645 if (len > sizeof(int))
646 len = sizeof(int);
647 val = &ro->recv_own_msgs;
648 break;
649
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200650 case CAN_RAW_FD_FRAMES:
651 if (len > sizeof(int))
652 len = sizeof(int);
653 val = &ro->fd_frames;
654 break;
655
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800656 default:
657 return -ENOPROTOOPT;
658 }
659
660 if (put_user(len, optlen))
661 return -EFAULT;
662 if (copy_to_user(optval, val, len))
663 return -EFAULT;
664 return 0;
665}
666
667static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
668 struct msghdr *msg, size_t size)
669{
670 struct sock *sk = sock->sk;
671 struct raw_sock *ro = raw_sk(sk);
672 struct sk_buff *skb;
673 struct net_device *dev;
674 int ifindex;
675 int err;
676
677 if (msg->msg_name) {
678 struct sockaddr_can *addr =
679 (struct sockaddr_can *)msg->msg_name;
680
Kurt Van Dijck5e507322011-01-15 20:56:42 -0800681 if (msg->msg_namelen < sizeof(*addr))
682 return -EINVAL;
683
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800684 if (addr->can_family != AF_CAN)
685 return -EINVAL;
686
687 ifindex = addr->can_ifindex;
688 } else
689 ifindex = ro->ifindex;
690
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200691 if (ro->fd_frames) {
692 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
693 return -EINVAL;
694 } else {
695 if (unlikely(size != CAN_MTU))
696 return -EINVAL;
697 }
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700698
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800699 dev = dev_get_by_index(&init_net, ifindex);
700 if (!dev)
701 return -ENXIO;
702
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100703 skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv),
704 msg->msg_flags & MSG_DONTWAIT, &err);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800705 if (!skb)
706 goto put_dev;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800707
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000708 can_skb_reserve(skb);
709 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100710
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800711 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800712 if (err < 0)
713 goto free_skb;
Daniel Borkmannbf84a012013-04-14 08:08:13 +0000714
715 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Oliver Hartkoppcff0d6e2010-08-03 00:31:48 -0700716
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800717 skb->dev = dev;
718 skb->sk = sk;
719
720 err = can_send(skb, ro->loopback);
721
722 dev_put(dev);
723
724 if (err)
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800725 goto send_failed;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800726
727 return size;
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800728
729free_skb:
730 kfree_skb(skb);
731put_dev:
732 dev_put(dev);
733send_failed:
734 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800735}
736
737static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
738 struct msghdr *msg, size_t size, int flags)
739{
740 struct sock *sk = sock->sk;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200741 struct raw_sock *ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800742 struct sk_buff *skb;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200743 int rxmtu;
Urs Thuermanna2199942008-02-07 18:05:04 -0800744 int err = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800745 int noblock;
746
747 noblock = flags & MSG_DONTWAIT;
748 flags &= ~MSG_DONTWAIT;
749
Urs Thuermanna2199942008-02-07 18:05:04 -0800750 skb = skb_recv_datagram(sk, flags, noblock, &err);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800751 if (!skb)
Urs Thuermanna2199942008-02-07 18:05:04 -0800752 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800753
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200754 /*
755 * when serving a legacy socket the DLC <= 8 is already checked inside
756 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
757 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
758 */
759 if (!ro->fd_frames)
760 rxmtu = CAN_MTU;
761 else
762 rxmtu = skb->len;
763
764 if (size < rxmtu)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800765 msg->msg_flags |= MSG_TRUNC;
766 else
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200767 size = rxmtu;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800768
Urs Thuermanna2199942008-02-07 18:05:04 -0800769 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
770 if (err < 0) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800771 skb_free_datagram(sk, skb);
Urs Thuermanna2199942008-02-07 18:05:04 -0800772 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800773 }
774
Neil Horman3b885782009-10-12 13:26:31 -0700775 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800776
777 if (msg->msg_name) {
778 msg->msg_namelen = sizeof(struct sockaddr_can);
779 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
780 }
781
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000782 /* assign the flags that have been recorded in raw_rcv() */
783 msg->msg_flags |= *(raw_flags(skb));
784
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800785 skb_free_datagram(sk, skb);
786
787 return size;
788}
789
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000790static const struct proto_ops raw_ops = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800791 .family = PF_CAN,
792 .release = raw_release,
793 .bind = raw_bind,
794 .connect = sock_no_connect,
795 .socketpair = sock_no_socketpair,
796 .accept = sock_no_accept,
797 .getname = raw_getname,
798 .poll = datagram_poll,
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000799 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800800 .listen = sock_no_listen,
801 .shutdown = sock_no_shutdown,
802 .setsockopt = raw_setsockopt,
803 .getsockopt = raw_getsockopt,
804 .sendmsg = raw_sendmsg,
805 .recvmsg = raw_recvmsg,
806 .mmap = sock_no_mmap,
807 .sendpage = sock_no_sendpage,
808};
809
810static struct proto raw_proto __read_mostly = {
811 .name = "CAN_RAW",
812 .owner = THIS_MODULE,
813 .obj_size = sizeof(struct raw_sock),
814 .init = raw_init,
815};
816
Kurt Van Dijck16506292011-05-03 18:40:57 +0000817static const struct can_proto raw_can_proto = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800818 .type = SOCK_RAW,
819 .protocol = CAN_RAW,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800820 .ops = &raw_ops,
821 .prot = &raw_proto,
822};
823
824static __init int raw_module_init(void)
825{
826 int err;
827
828 printk(banner);
829
830 err = can_proto_register(&raw_can_proto);
831 if (err < 0)
832 printk(KERN_ERR "can: registration of raw protocol failed\n");
833
834 return err;
835}
836
837static __exit void raw_module_exit(void)
838{
839 can_proto_unregister(&raw_can_proto);
840}
841
842module_init(raw_module_init);
843module_exit(raw_module_exit);