blob: 5b0e3e330d97c6f4172bf52db70a4cee6b7f4247 [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>
53#include <linux/can/raw.h>
54#include <net/sock.h>
55#include <net/net_namespace.h>
56
57#define CAN_RAW_VERSION CAN_VERSION
Andi Kleen6299b662012-10-04 17:12:08 -070058static __initconst const char banner[] =
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080059 KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
60
61MODULE_DESCRIPTION("PF_CAN raw protocol");
62MODULE_LICENSE("Dual BSD/GPL");
63MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
Lothar Waßmannb13bb2e2009-07-14 23:12:25 +000064MODULE_ALIAS("can-proto-1");
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080065
66#define MASK_ALL 0
67
68/*
69 * A raw socket has a list of can_filters attached to it, each receiving
70 * the CAN frames matching that filter. If the filter list is empty,
71 * no CAN frames will be received by the socket. The default after
72 * opening the socket, is to have one filter which receives all frames.
73 * The filter list is allocated dynamically with the exception of the
74 * list containing only one item. This common case is optimized by
75 * storing the single filter in dfilter, to avoid using dynamic memory.
76 */
77
78struct raw_sock {
79 struct sock sk;
80 int bound;
81 int ifindex;
82 struct notifier_block notifier;
83 int loopback;
84 int recv_own_msgs;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +020085 int fd_frames;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -080086 int count; /* number of active filters */
87 struct can_filter dfilter; /* default/single filter */
88 struct can_filter *filter; /* pointer to filter(s) */
89 can_err_mask_t err_mask;
90};
91
Oliver Hartkopp1e556592010-10-19 09:32:04 +000092/*
93 * Return pointer to store the extra msg flags for raw_recvmsg().
94 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
95 * in skb->cb.
96 */
97static inline unsigned int *raw_flags(struct sk_buff *skb)
98{
99 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
100 sizeof(unsigned int)));
101
102 /* return pointer after struct sockaddr_can */
103 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
104}
105
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800106static inline struct raw_sock *raw_sk(const struct sock *sk)
107{
108 return (struct raw_sock *)sk;
109}
110
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000111static void raw_rcv(struct sk_buff *oskb, void *data)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800112{
113 struct sock *sk = (struct sock *)data;
114 struct raw_sock *ro = raw_sk(sk);
115 struct sockaddr_can *addr;
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000116 struct sk_buff *skb;
117 unsigned int *pflags;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800118
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800119 /* check the received tx sock reference */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000120 if (!ro->recv_own_msgs && oskb->sk == sk)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800121 return;
122
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200123 /* do not pass frames with DLC > 8 to a legacy socket */
124 if (!ro->fd_frames) {
125 struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;
126
127 if (unlikely(cfd->len > CAN_MAX_DLEN))
128 return;
129 }
130
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800131 /* clone the given skb to be able to enqueue it into the rcv queue */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000132 skb = skb_clone(oskb, GFP_ATOMIC);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800133 if (!skb)
134 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800135
136 /*
137 * Put the datagram to the queue so that raw_recvmsg() can
138 * get it from there. We need to pass the interface index to
139 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
140 * containing the interface index.
141 */
142
143 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
144 addr = (struct sockaddr_can *)skb->cb;
145 memset(addr, 0, sizeof(*addr));
146 addr->can_family = AF_CAN;
147 addr->can_ifindex = skb->dev->ifindex;
148
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000149 /* add CAN specific message flags for raw_recvmsg() */
150 pflags = raw_flags(skb);
151 *pflags = 0;
152 if (oskb->sk)
153 *pflags |= MSG_DONTROUTE;
154 if (oskb->sk == sk)
155 *pflags |= MSG_CONFIRM;
156
Urs Thuermanna2199942008-02-07 18:05:04 -0800157 if (sock_queue_rcv_skb(sk, skb) < 0)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800158 kfree_skb(skb);
159}
160
161static int raw_enable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800162 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800163{
164 int err = 0;
165 int i;
166
167 for (i = 0; i < count; i++) {
168 err = can_rx_register(dev, filter[i].can_id,
169 filter[i].can_mask,
170 raw_rcv, sk, "raw");
171 if (err) {
172 /* clean up successfully registered filters */
173 while (--i >= 0)
174 can_rx_unregister(dev, filter[i].can_id,
175 filter[i].can_mask,
176 raw_rcv, sk);
177 break;
178 }
179 }
180
181 return err;
182}
183
184static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
185 can_err_mask_t err_mask)
186{
187 int err = 0;
188
189 if (err_mask)
190 err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
191 raw_rcv, sk, "raw");
192
193 return err;
194}
195
196static void raw_disable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800197 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800198{
199 int i;
200
201 for (i = 0; i < count; i++)
202 can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
203 raw_rcv, sk);
204}
205
206static inline void raw_disable_errfilter(struct net_device *dev,
207 struct sock *sk,
208 can_err_mask_t err_mask)
209
210{
211 if (err_mask)
212 can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
213 raw_rcv, sk);
214}
215
216static inline void raw_disable_allfilters(struct net_device *dev,
217 struct sock *sk)
218{
219 struct raw_sock *ro = raw_sk(sk);
220
221 raw_disable_filters(dev, sk, ro->filter, ro->count);
222 raw_disable_errfilter(dev, sk, ro->err_mask);
223}
224
225static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
226{
227 struct raw_sock *ro = raw_sk(sk);
228 int err;
229
230 err = raw_enable_filters(dev, sk, ro->filter, ro->count);
231 if (!err) {
232 err = raw_enable_errfilter(dev, sk, ro->err_mask);
233 if (err)
234 raw_disable_filters(dev, sk, ro->filter, ro->count);
235 }
236
237 return err;
238}
239
240static int raw_notifier(struct notifier_block *nb,
241 unsigned long msg, void *data)
242{
243 struct net_device *dev = (struct net_device *)data;
244 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
245 struct sock *sk = &ro->sk;
246
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700247 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800248 return NOTIFY_DONE;
249
250 if (dev->type != ARPHRD_CAN)
251 return NOTIFY_DONE;
252
253 if (ro->ifindex != dev->ifindex)
254 return NOTIFY_DONE;
255
256 switch (msg) {
257
258 case NETDEV_UNREGISTER:
259 lock_sock(sk);
260 /* remove current filters & unregister */
261 if (ro->bound)
262 raw_disable_allfilters(dev, sk);
263
264 if (ro->count > 1)
265 kfree(ro->filter);
266
267 ro->ifindex = 0;
268 ro->bound = 0;
269 ro->count = 0;
270 release_sock(sk);
271
272 sk->sk_err = ENODEV;
273 if (!sock_flag(sk, SOCK_DEAD))
274 sk->sk_error_report(sk);
275 break;
276
277 case NETDEV_DOWN:
278 sk->sk_err = ENETDOWN;
279 if (!sock_flag(sk, SOCK_DEAD))
280 sk->sk_error_report(sk);
281 break;
282 }
283
284 return NOTIFY_DONE;
285}
286
287static int raw_init(struct sock *sk)
288{
289 struct raw_sock *ro = raw_sk(sk);
290
291 ro->bound = 0;
292 ro->ifindex = 0;
293
294 /* set default filter to single entry dfilter */
295 ro->dfilter.can_id = 0;
296 ro->dfilter.can_mask = MASK_ALL;
297 ro->filter = &ro->dfilter;
298 ro->count = 1;
299
300 /* set default loopback behaviour */
301 ro->loopback = 1;
302 ro->recv_own_msgs = 0;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200303 ro->fd_frames = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800304
305 /* set notifier */
306 ro->notifier.notifier_call = raw_notifier;
307
308 register_netdevice_notifier(&ro->notifier);
309
310 return 0;
311}
312
313static int raw_release(struct socket *sock)
314{
315 struct sock *sk = sock->sk;
Oliver Hartkopp10022a62011-04-20 01:57:15 +0000316 struct raw_sock *ro;
317
318 if (!sk)
319 return 0;
320
321 ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800322
323 unregister_netdevice_notifier(&ro->notifier);
324
325 lock_sock(sk);
326
327 /* remove current filters & unregister */
328 if (ro->bound) {
329 if (ro->ifindex) {
330 struct net_device *dev;
331
332 dev = dev_get_by_index(&init_net, ro->ifindex);
333 if (dev) {
334 raw_disable_allfilters(dev, sk);
335 dev_put(dev);
336 }
337 } else
338 raw_disable_allfilters(NULL, sk);
339 }
340
341 if (ro->count > 1)
342 kfree(ro->filter);
343
344 ro->ifindex = 0;
345 ro->bound = 0;
346 ro->count = 0;
347
Lothar Waßmannf7e5cc02009-07-14 23:10:21 +0000348 sock_orphan(sk);
349 sock->sk = NULL;
350
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800351 release_sock(sk);
352 sock_put(sk);
353
354 return 0;
355}
356
357static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
358{
359 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
360 struct sock *sk = sock->sk;
361 struct raw_sock *ro = raw_sk(sk);
362 int ifindex;
363 int err = 0;
364 int notify_enetdown = 0;
365
366 if (len < sizeof(*addr))
367 return -EINVAL;
368
369 lock_sock(sk);
370
371 if (ro->bound && addr->can_ifindex == ro->ifindex)
372 goto out;
373
374 if (addr->can_ifindex) {
375 struct net_device *dev;
376
377 dev = dev_get_by_index(&init_net, addr->can_ifindex);
378 if (!dev) {
379 err = -ENODEV;
380 goto out;
381 }
382 if (dev->type != ARPHRD_CAN) {
383 dev_put(dev);
384 err = -ENODEV;
385 goto out;
386 }
387 if (!(dev->flags & IFF_UP))
388 notify_enetdown = 1;
389
390 ifindex = dev->ifindex;
391
392 /* filters set by default/setsockopt */
393 err = raw_enable_allfilters(dev, sk);
394 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800395 } else {
396 ifindex = 0;
397
398 /* filters set by default/setsockopt */
399 err = raw_enable_allfilters(NULL, sk);
400 }
401
402 if (!err) {
403 if (ro->bound) {
404 /* unregister old filters */
405 if (ro->ifindex) {
406 struct net_device *dev;
407
408 dev = dev_get_by_index(&init_net, ro->ifindex);
409 if (dev) {
410 raw_disable_allfilters(dev, sk);
411 dev_put(dev);
412 }
413 } else
414 raw_disable_allfilters(NULL, sk);
415 }
416 ro->ifindex = ifindex;
417 ro->bound = 1;
418 }
419
420 out:
421 release_sock(sk);
422
423 if (notify_enetdown) {
424 sk->sk_err = ENETDOWN;
425 if (!sock_flag(sk, SOCK_DEAD))
426 sk->sk_error_report(sk);
427 }
428
429 return err;
430}
431
432static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
433 int *len, int peer)
434{
435 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
436 struct sock *sk = sock->sk;
437 struct raw_sock *ro = raw_sk(sk);
438
439 if (peer)
440 return -EOPNOTSUPP;
441
Eric Dumazete84b90a2009-08-06 20:27:04 +0000442 memset(addr, 0, sizeof(*addr));
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800443 addr->can_family = AF_CAN;
444 addr->can_ifindex = ro->ifindex;
445
446 *len = sizeof(*addr);
447
448 return 0;
449}
450
451static int raw_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700452 char __user *optval, unsigned int optlen)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800453{
454 struct sock *sk = sock->sk;
455 struct raw_sock *ro = raw_sk(sk);
456 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
457 struct can_filter sfilter; /* single filter */
458 struct net_device *dev = NULL;
459 can_err_mask_t err_mask = 0;
460 int count = 0;
461 int err = 0;
462
463 if (level != SOL_CAN_RAW)
464 return -EINVAL;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800465
466 switch (optname) {
467
468 case CAN_RAW_FILTER:
469 if (optlen % sizeof(struct can_filter) != 0)
470 return -EINVAL;
471
472 count = optlen / sizeof(struct can_filter);
473
474 if (count > 1) {
475 /* filter does not fit into dfilter => alloc space */
Julia Lawall16dff912010-05-21 22:18:34 +0000476 filter = memdup_user(optval, optlen);
477 if (IS_ERR(filter))
478 return PTR_ERR(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800479 } else if (count == 1) {
Eric Dumazet4ffa8702010-04-09 23:47:31 +0000480 if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700481 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800482 }
483
484 lock_sock(sk);
485
486 if (ro->bound && ro->ifindex)
487 dev = dev_get_by_index(&init_net, ro->ifindex);
488
489 if (ro->bound) {
490 /* (try to) register the new filters */
491 if (count == 1)
492 err = raw_enable_filters(dev, sk, &sfilter, 1);
493 else
494 err = raw_enable_filters(dev, sk, filter,
495 count);
496 if (err) {
497 if (count > 1)
498 kfree(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800499 goto out_fil;
500 }
501
502 /* remove old filter registrations */
503 raw_disable_filters(dev, sk, ro->filter, ro->count);
504 }
505
506 /* remove old filter space */
507 if (ro->count > 1)
508 kfree(ro->filter);
509
510 /* link new filters to the socket */
511 if (count == 1) {
512 /* copy filter data for single filter */
513 ro->dfilter = sfilter;
514 filter = &ro->dfilter;
515 }
516 ro->filter = filter;
517 ro->count = count;
518
519 out_fil:
520 if (dev)
521 dev_put(dev);
522
523 release_sock(sk);
524
525 break;
526
527 case CAN_RAW_ERR_FILTER:
528 if (optlen != sizeof(err_mask))
529 return -EINVAL;
530
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700531 if (copy_from_user(&err_mask, optval, optlen))
532 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800533
534 err_mask &= CAN_ERR_MASK;
535
536 lock_sock(sk);
537
538 if (ro->bound && ro->ifindex)
539 dev = dev_get_by_index(&init_net, ro->ifindex);
540
541 /* remove current error mask */
542 if (ro->bound) {
543 /* (try to) register the new err_mask */
544 err = raw_enable_errfilter(dev, sk, err_mask);
545
546 if (err)
547 goto out_err;
548
549 /* remove old err_mask registration */
550 raw_disable_errfilter(dev, sk, ro->err_mask);
551 }
552
553 /* link new err_mask to the socket */
554 ro->err_mask = err_mask;
555
556 out_err:
557 if (dev)
558 dev_put(dev);
559
560 release_sock(sk);
561
562 break;
563
564 case CAN_RAW_LOOPBACK:
565 if (optlen != sizeof(ro->loopback))
566 return -EINVAL;
567
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700568 if (copy_from_user(&ro->loopback, optval, optlen))
569 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800570
571 break;
572
573 case CAN_RAW_RECV_OWN_MSGS:
574 if (optlen != sizeof(ro->recv_own_msgs))
575 return -EINVAL;
576
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700577 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
578 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800579
580 break;
581
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200582 case CAN_RAW_FD_FRAMES:
583 if (optlen != sizeof(ro->fd_frames))
584 return -EINVAL;
585
586 if (copy_from_user(&ro->fd_frames, optval, optlen))
587 return -EFAULT;
588
589 break;
590
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800591 default:
592 return -ENOPROTOOPT;
593 }
594 return err;
595}
596
597static int raw_getsockopt(struct socket *sock, int level, int optname,
598 char __user *optval, int __user *optlen)
599{
600 struct sock *sk = sock->sk;
601 struct raw_sock *ro = raw_sk(sk);
602 int len;
603 void *val;
604 int err = 0;
605
606 if (level != SOL_CAN_RAW)
607 return -EINVAL;
608 if (get_user(len, optlen))
609 return -EFAULT;
610 if (len < 0)
611 return -EINVAL;
612
613 switch (optname) {
614
615 case CAN_RAW_FILTER:
616 lock_sock(sk);
617 if (ro->count > 0) {
618 int fsize = ro->count * sizeof(struct can_filter);
619 if (len > fsize)
620 len = fsize;
Pavel Emelyanov653252c2008-04-25 01:49:48 -0700621 if (copy_to_user(optval, ro->filter, len))
622 err = -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800623 } else
624 len = 0;
625 release_sock(sk);
626
627 if (!err)
628 err = put_user(len, optlen);
629 return err;
630
631 case CAN_RAW_ERR_FILTER:
632 if (len > sizeof(can_err_mask_t))
633 len = sizeof(can_err_mask_t);
634 val = &ro->err_mask;
635 break;
636
637 case CAN_RAW_LOOPBACK:
638 if (len > sizeof(int))
639 len = sizeof(int);
640 val = &ro->loopback;
641 break;
642
643 case CAN_RAW_RECV_OWN_MSGS:
644 if (len > sizeof(int))
645 len = sizeof(int);
646 val = &ro->recv_own_msgs;
647 break;
648
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200649 case CAN_RAW_FD_FRAMES:
650 if (len > sizeof(int))
651 len = sizeof(int);
652 val = &ro->fd_frames;
653 break;
654
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800655 default:
656 return -ENOPROTOOPT;
657 }
658
659 if (put_user(len, optlen))
660 return -EFAULT;
661 if (copy_to_user(optval, val, len))
662 return -EFAULT;
663 return 0;
664}
665
666static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
667 struct msghdr *msg, size_t size)
668{
669 struct sock *sk = sock->sk;
670 struct raw_sock *ro = raw_sk(sk);
671 struct sk_buff *skb;
672 struct net_device *dev;
673 int ifindex;
674 int err;
675
676 if (msg->msg_name) {
677 struct sockaddr_can *addr =
678 (struct sockaddr_can *)msg->msg_name;
679
Kurt Van Dijck5e507322011-01-15 20:56:42 -0800680 if (msg->msg_namelen < sizeof(*addr))
681 return -EINVAL;
682
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800683 if (addr->can_family != AF_CAN)
684 return -EINVAL;
685
686 ifindex = addr->can_ifindex;
687 } else
688 ifindex = ro->ifindex;
689
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200690 if (ro->fd_frames) {
691 if (unlikely(size != CANFD_MTU && size != CAN_MTU))
692 return -EINVAL;
693 } else {
694 if (unlikely(size != CAN_MTU))
695 return -EINVAL;
696 }
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700697
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800698 dev = dev_get_by_index(&init_net, ifindex);
699 if (!dev)
700 return -ENXIO;
701
702 skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
703 &err);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800704 if (!skb)
705 goto put_dev;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800706
707 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800708 if (err < 0)
709 goto free_skb;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000710 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Patrick Ohly51f31ca2009-02-12 05:03:39 +0000711 if (err < 0)
712 goto free_skb;
Oliver Hartkoppcff0d6e2010-08-03 00:31:48 -0700713
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800714 skb->dev = dev;
715 skb->sk = sk;
716
717 err = can_send(skb, ro->loopback);
718
719 dev_put(dev);
720
721 if (err)
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800722 goto send_failed;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800723
724 return size;
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800725
726free_skb:
727 kfree_skb(skb);
728put_dev:
729 dev_put(dev);
730send_failed:
731 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800732}
733
734static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
735 struct msghdr *msg, size_t size, int flags)
736{
737 struct sock *sk = sock->sk;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200738 struct raw_sock *ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800739 struct sk_buff *skb;
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200740 int rxmtu;
Urs Thuermanna2199942008-02-07 18:05:04 -0800741 int err = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800742 int noblock;
743
744 noblock = flags & MSG_DONTWAIT;
745 flags &= ~MSG_DONTWAIT;
746
Urs Thuermanna2199942008-02-07 18:05:04 -0800747 skb = skb_recv_datagram(sk, flags, noblock, &err);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800748 if (!skb)
Urs Thuermanna2199942008-02-07 18:05:04 -0800749 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800750
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200751 /*
752 * when serving a legacy socket the DLC <= 8 is already checked inside
753 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
754 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
755 */
756 if (!ro->fd_frames)
757 rxmtu = CAN_MTU;
758 else
759 rxmtu = skb->len;
760
761 if (size < rxmtu)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800762 msg->msg_flags |= MSG_TRUNC;
763 else
Oliver Hartkoppe2d265d2012-06-13 20:41:31 +0200764 size = rxmtu;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800765
Urs Thuermanna2199942008-02-07 18:05:04 -0800766 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
767 if (err < 0) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800768 skb_free_datagram(sk, skb);
Urs Thuermanna2199942008-02-07 18:05:04 -0800769 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800770 }
771
Neil Horman3b885782009-10-12 13:26:31 -0700772 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800773
774 if (msg->msg_name) {
775 msg->msg_namelen = sizeof(struct sockaddr_can);
776 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
777 }
778
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000779 /* assign the flags that have been recorded in raw_rcv() */
780 msg->msg_flags |= *(raw_flags(skb));
781
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800782 skb_free_datagram(sk, skb);
783
784 return size;
785}
786
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000787static const struct proto_ops raw_ops = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800788 .family = PF_CAN,
789 .release = raw_release,
790 .bind = raw_bind,
791 .connect = sock_no_connect,
792 .socketpair = sock_no_socketpair,
793 .accept = sock_no_accept,
794 .getname = raw_getname,
795 .poll = datagram_poll,
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000796 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800797 .listen = sock_no_listen,
798 .shutdown = sock_no_shutdown,
799 .setsockopt = raw_setsockopt,
800 .getsockopt = raw_getsockopt,
801 .sendmsg = raw_sendmsg,
802 .recvmsg = raw_recvmsg,
803 .mmap = sock_no_mmap,
804 .sendpage = sock_no_sendpage,
805};
806
807static struct proto raw_proto __read_mostly = {
808 .name = "CAN_RAW",
809 .owner = THIS_MODULE,
810 .obj_size = sizeof(struct raw_sock),
811 .init = raw_init,
812};
813
Kurt Van Dijck16506292011-05-03 18:40:57 +0000814static const struct can_proto raw_can_proto = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800815 .type = SOCK_RAW,
816 .protocol = CAN_RAW,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800817 .ops = &raw_ops,
818 .prot = &raw_proto,
819};
820
821static __init int raw_module_init(void)
822{
823 int err;
824
825 printk(banner);
826
827 err = can_proto_register(&raw_can_proto);
828 if (err < 0)
829 printk(KERN_ERR "can: registration of raw protocol failed\n");
830
831 return err;
832}
833
834static __exit void raw_module_exit(void)
835{
836 can_proto_unregister(&raw_can_proto);
837}
838
839module_init(raw_module_init);
840module_exit(raw_module_exit);