]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - patch-series/2.6.22/03-can-raw-proto.diff
The two options "CAN bit-timing calculation" and
[socketcan-devel.git] / patch-series / 2.6.22 / 03-can-raw-proto.diff
1 DESC
2 CAN: Add raw protocol
3 EDESC
4 This patch adds the CAN raw protocol.
5
6 Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
7 Signed-off-by: Urs Thuermann <urs.thuermann@volkswagen.de>
8
9 ---
10  include/linux/can/raw.h |   31 +
11  net/can/Kconfig         |   26 +
12  net/can/Makefile        |    3 
13  net/can/raw.c           |  757 ++++++++++++++++++++++++++++++++++++++++++++++++
14  4 files changed, 817 insertions(+)
15
16 Index: linux-2.6.22/include/linux/can/raw.h
17 ===================================================================
18 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
19 +++ linux-2.6.22/include/linux/can/raw.h        2007-07-16 21:48:14.000000000 +0200
20 @@ -0,0 +1,31 @@
21 +/*
22 + * linux/can/raw.h
23 + *
24 + * Definitions for raw CAN sockets
25 + *
26 + * Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
27 + *          Urs Thuermann   <urs.thuermann@volkswagen.de>
28 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
29 + * All rights reserved.
30 + *
31 + * Send feedback to <socketcan-users@lists.berlios.de>
32 + *
33 + */
34 +
35 +#ifndef CAN_RAW_H
36 +#define CAN_RAW_H
37 +
38 +#include <linux/can.h>
39 +
40 +#define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW)
41 +
42 +/* for socket options affecting the socket (not the global system) */
43 +
44 +enum {
45 +       CAN_RAW_FILTER = 1,     /* set 0 .. n can_filter(s)          */
46 +       CAN_RAW_ERR_FILTER,     /* set filter for error frames       */
47 +       CAN_RAW_LOOPBACK,       /* local loopback (default:on)       */
48 +       CAN_RAW_RECV_OWN_MSGS   /* receive my own msgs (default:off) */
49 +};
50 +
51 +#endif
52 Index: linux-2.6.22/net/can/Kconfig
53 ===================================================================
54 --- linux-2.6.22.orig/net/can/Kconfig   2007-07-16 21:42:28.000000000 +0200
55 +++ linux-2.6.22/net/can/Kconfig        2007-07-16 21:48:14.000000000 +0200
56 @@ -16,6 +16,32 @@
57           If you want CAN support, you should say Y here and also to the
58           specific driver for your controller(s) below.
59  
60 +config CAN_RAW
61 +       tristate "Raw CAN Protocol (raw access with CAN-ID filtering)"
62 +       depends on CAN
63 +       default N
64 +       ---help---
65 +         The Raw CAN protocol option offers access to the CAN bus via
66 +         the BSD socket API. You probably want to use the raw socket in
67 +         most cases where no higher level protocol is being used. The raw
68 +         socket has several filter options e.g. ID-Masking / Errorframes.
69 +         To receive/send raw CAN messages, use AF_CAN with protocol CAN_RAW.
70 +
71 +config CAN_RAW_USER
72 +       bool "Allow non-root users to access Raw CAN Protocol sockets"
73 +       depends on CAN_RAW
74 +       default N
75 +       ---help---
76 +         The Controller Area Network is a local field bus transmitting only
77 +         broadcast messages without any routing and security concepts.
78 +         In the majority of cases the user application has to deal with
79 +         raw CAN frames. Therefore it might be reasonable NOT to restrict
80 +         the CAN access only to the user root, as known from other networks.
81 +         Since CAN_RAW sockets can only send and receive frames to/from CAN
82 +         interfaces this does not affect security of others networks.
83 +         Say Y here if you want non-root users to be able to access CAN_RAW
84 +         sockets.
85 +
86  config CAN_DEBUG_CORE
87         bool "CAN Core debugging messages"
88         depends on CAN
89 Index: linux-2.6.22/net/can/Makefile
90 ===================================================================
91 --- linux-2.6.22.orig/net/can/Makefile  2007-07-16 21:42:28.000000000 +0200
92 +++ linux-2.6.22/net/can/Makefile       2007-07-16 21:48:14.000000000 +0200
93 @@ -4,3 +4,6 @@
94  
95  obj-$(CONFIG_CAN)      += can.o
96  can-objs               := af_can.o proc.o
97 +
98 +obj-$(CONFIG_CAN_RAW)  += can-raw.o
99 +can-raw-objs           := raw.o
100 Index: linux-2.6.22/net/can/raw.c
101 ===================================================================
102 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
103 +++ linux-2.6.22/net/can/raw.c  2007-07-16 21:49:00.000000000 +0200
104 @@ -0,0 +1,757 @@
105 +/*
106 + * raw.c - Raw sockets for protocol family CAN
107 + *
108 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
109 + * All rights reserved.
110 + *
111 + * Redistribution and use in source and binary forms, with or without
112 + * modification, are permitted provided that the following conditions
113 + * are met:
114 + * 1. Redistributions of source code must retain the above copyright
115 + *    notice, this list of conditions, the following disclaimer and
116 + *    the referenced file 'COPYING'.
117 + * 2. Redistributions in binary form must reproduce the above copyright
118 + *    notice, this list of conditions and the following disclaimer in the
119 + *    documentation and/or other materials provided with the distribution.
120 + * 3. Neither the name of Volkswagen nor the names of its contributors
121 + *    may be used to endorse or promote products derived from this software
122 + *    without specific prior written permission.
123 + *
124 + * Alternatively, provided that this notice is retained in full, this
125 + * software may be distributed under the terms of the GNU General
126 + * Public License ("GPL") version 2 as distributed in the 'COPYING'
127 + * file from the main directory of the linux kernel source.
128 + *
129 + * The provided data structures and external interfaces from this code
130 + * are not restricted to be used by modules with a GPL compatible license.
131 + *
132 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
133 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
134 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
135 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
136 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
137 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
138 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
139 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
140 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
141 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
142 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
143 + * DAMAGE.
144 + *
145 + * Send feedback to <socketcan-users@lists.berlios.de>
146 + *
147 + */
148 +
149 +#include <linux/module.h>
150 +#include <linux/init.h>
151 +#include <linux/uio.h>
152 +#include <linux/poll.h>
153 +#include <linux/net.h>
154 +#include <linux/netdevice.h>
155 +#include <linux/socket.h>
156 +#include <linux/if_arp.h>
157 +#include <linux/skbuff.h>
158 +#include <linux/can.h>
159 +#include <linux/can/core.h>
160 +#include <linux/can/raw.h>
161 +#include <net/sock.h>
162 +
163 +#define IDENT "raw"
164 +#define CAN_RAW_VERSION CAN_VERSION
165 +static __initdata const char banner[] =
166 +       KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
167 +
168 +MODULE_DESCRIPTION("PF_CAN raw protocol");
169 +MODULE_LICENSE("Dual BSD/GPL");
170 +MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
171 +
172 +#ifdef CONFIG_CAN_DEBUG_CORE
173 +static int debug;
174 +module_param(debug, int, S_IRUGO);
175 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
176 +#endif
177 +
178 +#ifdef CONFIG_CAN_RAW_USER
179 +#define RAW_CAP (-1)
180 +#else
181 +#define RAW_CAP CAP_NET_RAW
182 +#endif
183 +
184 +#define MASK_ALL 0
185 +
186 +/*
187 + * A raw socket has a list of can_filters attached to it, each receiving
188 + * the CAN frames matching that filter.  If the filter list is empty,
189 + * no CAN frames will be received by the socket.  The default after
190 + * opening the socket, is to have one filter which receives all frames.
191 + * The filter list is allocated dynamically with the exception of the
192 + * list containing only one item.  This common case is optimized by
193 + * storing the single filter in dfilter, to avoid using dynamic memory.
194 + */
195 +
196 +struct raw_sock {
197 +       struct sock sk;
198 +       int bound;
199 +       int ifindex;
200 +       struct notifier_block notifier;
201 +       int loopback;
202 +       int recv_own_msgs;
203 +       int count;                 /* number of active filters */
204 +       struct can_filter dfilter; /* default/single filter */
205 +       struct can_filter *filter; /* pointer to filter(s) */
206 +       can_err_mask_t err_mask;
207 +};
208 +
209 +static inline struct raw_sock *raw_sk(const struct sock *sk)
210 +{
211 +       return (struct raw_sock *)sk;
212 +}
213 +
214 +static void raw_rcv(struct sk_buff *skb, void *data)
215 +{
216 +       struct sock *sk = (struct sock *)data;
217 +       struct raw_sock *ro = raw_sk(sk);
218 +       struct sockaddr_can *addr;
219 +       int error;
220 +
221 +       DBG("received skbuff %p, sk %p\n", skb, sk);
222 +       DBG_SKB(skb);
223 +
224 +       if (!ro->recv_own_msgs) {
225 +               /* check the received tx sock reference */
226 +               if (skb->sk == sk) {
227 +                       DBG("trashed own tx msg\n");
228 +                       kfree_skb(skb);
229 +                       return;
230 +               }
231 +       }
232 +
233 +       addr = (struct sockaddr_can *)skb->cb;
234 +       memset(addr, 0, sizeof(*addr));
235 +       addr->can_family  = AF_CAN;
236 +       addr->can_ifindex = skb->dev->ifindex;
237 +
238 +       error = sock_queue_rcv_skb(sk, skb);
239 +       if (error < 0) {
240 +               DBG("sock_queue_rcv_skb failed: %d\n", error);
241 +               DBG("freeing skbuff %p\n", skb);
242 +               kfree_skb(skb);
243 +       }
244 +}
245 +
246 +static void raw_enable_filters(struct net_device *dev, struct sock *sk)
247 +{
248 +       struct raw_sock *ro = raw_sk(sk);
249 +       struct can_filter *filter = ro->filter;
250 +       int i;
251 +
252 +       for (i = 0; i < ro->count; i++) {
253 +               DBG("filter can_id %08X, can_mask %08X%s, sk %p\n",
254 +                   filter[i].can_id, filter[i].can_mask,
255 +                   filter[i].can_id & CAN_INV_FILTER ? " (inv)" : "", sk);
256 +
257 +               can_rx_register(dev, filter[i].can_id, filter[i].can_mask,
258 +                               raw_rcv, sk, IDENT);
259 +       }
260 +}
261 +
262 +static void raw_enable_errfilter(struct net_device *dev, struct sock *sk)
263 +{
264 +       struct raw_sock *ro = raw_sk(sk);
265 +
266 +       if (ro->err_mask)
267 +               can_rx_register(dev, 0, ro->err_mask | CAN_ERR_FLAG,
268 +                               raw_rcv, sk, IDENT);
269 +}
270 +
271 +static void raw_disable_filters(struct net_device *dev, struct sock *sk)
272 +{
273 +       struct raw_sock *ro = raw_sk(sk);
274 +       struct can_filter *filter = ro->filter;
275 +       int i;
276 +
277 +       for (i = 0; i < ro->count; i++) {
278 +               DBG("filter can_id %08X, can_mask %08X%s, sk %p\n",
279 +                   filter[i].can_id, filter[i].can_mask,
280 +                   filter[i].can_id & CAN_INV_FILTER ? " (inv)" : "", sk);
281 +
282 +               can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
283 +                                 raw_rcv, sk);
284 +       }
285 +}
286 +
287 +static void raw_disable_errfilter(struct net_device *dev, struct sock *sk)
288 +{
289 +       struct raw_sock *ro = raw_sk(sk);
290 +
291 +       if (ro->err_mask)
292 +               can_rx_unregister(dev, 0, ro->err_mask | CAN_ERR_FLAG,
293 +                                 raw_rcv, sk);
294 +}
295 +
296 +static int raw_notifier(struct notifier_block *nb,
297 +                       unsigned long msg, void *data)
298 +{
299 +       struct net_device *dev = (struct net_device *)data;
300 +       struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
301 +       struct sock *sk = &ro->sk;
302 +
303 +       DBG("msg %ld for dev %p (%s idx %d) sk %p ro->ifindex %d\n",
304 +           msg, dev, dev->name, dev->ifindex, sk, ro->ifindex);
305 +
306 +       if (dev->type != ARPHRD_CAN)
307 +               return NOTIFY_DONE;
308 +
309 +       if (ro->ifindex != dev->ifindex)
310 +               return NOTIFY_DONE;
311 +
312 +       switch (msg) {
313 +
314 +       case NETDEV_UNREGISTER:
315 +               lock_sock(sk);
316 +               /* remove current filters & unregister */
317 +               if (ro->bound) {
318 +                       raw_disable_filters(dev, sk);
319 +                       raw_disable_errfilter(dev, sk);
320 +               }
321 +
322 +               if (ro->count > 1)
323 +                       kfree(ro->filter);
324 +
325 +               ro->ifindex = 0;
326 +               ro->bound   = 0;
327 +               ro->count   = 0;
328 +               release_sock(sk);
329 +
330 +               sk->sk_err = ENODEV;
331 +               if (!sock_flag(sk, SOCK_DEAD))
332 +                       sk->sk_error_report(sk);
333 +               break;
334 +
335 +       case NETDEV_DOWN:
336 +               sk->sk_err = ENETDOWN;
337 +               if (!sock_flag(sk, SOCK_DEAD))
338 +                       sk->sk_error_report(sk);
339 +               break;
340 +       }
341 +
342 +       return NOTIFY_DONE;
343 +}
344 +
345 +static int raw_init(struct sock *sk)
346 +{
347 +       struct raw_sock *ro = raw_sk(sk);
348 +
349 +       ro->bound            = 0;
350 +       ro->ifindex          = 0;
351 +
352 +       /* set default filter to single entry dfilter */
353 +       ro->dfilter.can_id   = 0;
354 +       ro->dfilter.can_mask = MASK_ALL;
355 +       ro->filter           = &ro->dfilter;
356 +       ro->count            = 1;
357 +
358 +       /* set default loopback behaviour */
359 +       ro->loopback         = 1;
360 +       ro->recv_own_msgs    = 0;
361 +
362 +       /* set notifier */
363 +       ro->notifier.notifier_call = raw_notifier;
364 +
365 +       register_netdevice_notifier(&ro->notifier);
366 +
367 +       return 0;
368 +}
369 +
370 +static int raw_release(struct socket *sock)
371 +{
372 +       struct sock *sk = sock->sk;
373 +       struct raw_sock *ro = raw_sk(sk);
374 +
375 +       DBG("socket %p, sk %p, refcnt %d\n", sock, sk,
376 +           atomic_read(&sk->sk_refcnt));
377 +
378 +       unregister_netdevice_notifier(&ro->notifier);
379 +
380 +       lock_sock(sk);
381 +
382 +       /* remove current filters & unregister */
383 +       if (ro->bound) {
384 +               if (ro->ifindex) {
385 +                       struct net_device *dev = dev_get_by_index(ro->ifindex);
386 +                       if (dev) {
387 +                               raw_disable_filters(dev, sk);
388 +                               raw_disable_errfilter(dev, sk);
389 +                               dev_put(dev);
390 +                       }
391 +               } else {
392 +                       raw_disable_filters(NULL, sk);
393 +                       raw_disable_errfilter(NULL, sk);
394 +               }
395 +       }
396 +
397 +       if (ro->count > 1)
398 +               kfree(ro->filter);
399 +
400 +       ro->ifindex = 0;
401 +       ro->bound   = 0;
402 +       ro->count   = 0;
403 +
404 +       release_sock(sk);
405 +       sock_put(sk);
406 +
407 +       return 0;
408 +}
409 +
410 +static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
411 +{
412 +       struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
413 +       struct sock *sk = sock->sk;
414 +       struct raw_sock *ro = raw_sk(sk);
415 +       int err = 0;
416 +       int notify_enetdown = 0;
417 +
418 +       DBG("socket %p to device %d\n", sock, addr->can_ifindex);
419 +
420 +       if (len < sizeof(*addr))
421 +               return -EINVAL;
422 +
423 +       lock_sock(sk);
424 +
425 +       if (ro->bound) {
426 +               /* unregister current filters for this device */
427 +               if (ro->ifindex) {
428 +                       struct net_device *dev = dev_get_by_index(ro->ifindex);
429 +                       if (dev) {
430 +                               raw_disable_filters(dev, sk);
431 +                               raw_disable_errfilter(dev, sk);
432 +                               dev_put(dev);
433 +                       }
434 +                       ro->ifindex = 0;
435 +
436 +               } else {
437 +                       raw_disable_filters(NULL, sk);
438 +                       raw_disable_errfilter(NULL, sk);
439 +               }
440 +
441 +               ro->bound = 0;
442 +       }
443 +
444 +       if (addr->can_ifindex) {
445 +               struct net_device *dev = dev_get_by_index(addr->can_ifindex);
446 +               if (!dev) {
447 +                       DBG("could not find device %d\n", addr->can_ifindex);
448 +                       err = -ENODEV;
449 +                       goto out;
450 +               }
451 +               if (dev->type != ARPHRD_CAN) {
452 +                       DBG("device %d no CAN device\n", addr->can_ifindex);
453 +                       dev_put(dev);
454 +                       err = -ENODEV;
455 +                       goto out;
456 +               }
457 +               if (!(dev->flags & IFF_UP))
458 +                       notify_enetdown = 1;
459 +
460 +               ro->ifindex = dev->ifindex;
461 +
462 +               /* filters set by default/setsockopt */
463 +               raw_enable_filters(dev, sk);
464 +               raw_enable_errfilter(dev, sk);
465 +               dev_put(dev);
466 +
467 +       } else {
468 +               ro->ifindex = 0;
469 +
470 +               /* filters set by default/setsockopt */
471 +               raw_enable_filters(NULL, sk);
472 +               raw_enable_errfilter(NULL, sk);
473 +       }
474 +
475 +       ro->bound = 1;
476 +
477 + out:
478 +       release_sock(sk);
479 +
480 +       if (notify_enetdown) {
481 +               sk->sk_err = ENETDOWN;
482 +               if (!sock_flag(sk, SOCK_DEAD))
483 +                       sk->sk_error_report(sk);
484 +       }
485 +
486 +       return err;
487 +}
488 +
489 +static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
490 +                      int *len, int peer)
491 +{
492 +       struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
493 +       struct sock *sk = sock->sk;
494 +       struct raw_sock *ro = raw_sk(sk);
495 +
496 +       if (peer)
497 +               return -EOPNOTSUPP;
498 +
499 +       addr->can_family  = AF_CAN;
500 +       addr->can_ifindex = ro->ifindex;
501 +
502 +       *len = sizeof(*addr);
503 +
504 +       return 0;
505 +}
506 +
507 +static unsigned int raw_poll(struct file *file, struct socket *sock,
508 +                            poll_table *wait)
509 +{
510 +       unsigned int mask = 0;
511 +
512 +       DBG("socket %p\n", sock);
513 +
514 +       mask = datagram_poll(file, sock, wait);
515 +       return mask;
516 +}
517 +
518 +static int raw_setsockopt(struct socket *sock, int level, int optname,
519 +                         char __user *optval, int optlen)
520 +{
521 +       struct sock *sk = sock->sk;
522 +       struct raw_sock *ro = raw_sk(sk);
523 +       struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
524 +       struct can_filter sfilter;         /* single filter */
525 +       struct net_device *dev = NULL;
526 +       can_err_mask_t err_mask = 0;
527 +       int count = 0;
528 +       int err;
529 +
530 +       if (level != SOL_CAN_RAW)
531 +               return -EINVAL;
532 +       if (optlen < 0)
533 +               return -EINVAL;
534 +
535 +       switch (optname) {
536 +
537 +       case CAN_RAW_FILTER:
538 +               if (optlen % sizeof(struct can_filter) != 0)
539 +                       return -EINVAL;
540 +
541 +               count = optlen / sizeof(struct can_filter);
542 +
543 +               if (count > 1) {
544 +                       /* filter does not fit into dfilter => alloc space */
545 +                       filter = kmalloc(optlen, GFP_KERNEL);
546 +                       if (!filter)
547 +                               return -ENOMEM;
548 +
549 +                       err = copy_from_user(filter, optval, optlen);
550 +                       if (err) {
551 +                               kfree(filter);
552 +                               return err;
553 +                       }
554 +               } else if (count == 1) {
555 +                       err = copy_from_user(&sfilter, optval, optlen);
556 +                       if (err)
557 +                               return err;
558 +               }
559 +
560 +               lock_sock(sk);
561 +
562 +               if (ro->bound && ro->ifindex)
563 +                       dev = dev_get_by_index(ro->ifindex);
564 +
565 +               /* remove current filters & unregister */
566 +               if (ro->bound)
567 +                       raw_disable_filters(dev, sk);
568 +
569 +               if (ro->count > 1)
570 +                       kfree(ro->filter);
571 +
572 +               if (count == 1) {
573 +                       /* copy filter data for single filter */
574 +                       ro->dfilter = sfilter;
575 +                       filter = &ro->dfilter;
576 +               }
577 +
578 +               /* add new filters & register */
579 +               ro->filter = filter;
580 +               ro->count  = count;
581 +               if (ro->bound)
582 +                       raw_enable_filters(dev, sk);
583 +
584 +               if (dev)
585 +                       dev_put(dev);
586 +
587 +               release_sock(sk);
588 +
589 +               break;
590 +
591 +       case CAN_RAW_ERR_FILTER:
592 +               if (optlen != sizeof(err_mask))
593 +                       return -EINVAL;
594 +
595 +               err = copy_from_user(&err_mask, optval, optlen);
596 +               if (err)
597 +                       return err;
598 +
599 +               err_mask &= CAN_ERR_MASK;
600 +
601 +               lock_sock(sk);
602 +
603 +               if (ro->bound && ro->ifindex)
604 +                       dev = dev_get_by_index(ro->ifindex);
605 +
606 +               /* remove current error mask */
607 +               if (ro->bound)
608 +                       raw_disable_errfilter(dev, sk);
609 +
610 +               ro->err_mask = err_mask;
611 +
612 +               /* add new error mask */
613 +               if (ro->bound)
614 +                       raw_enable_errfilter(dev, sk);
615 +
616 +               if (dev)
617 +                       dev_put(dev);
618 +
619 +               release_sock(sk);
620 +
621 +               break;
622 +
623 +       case CAN_RAW_LOOPBACK:
624 +               if (optlen != sizeof(ro->loopback))
625 +                       return -EINVAL;
626 +
627 +               err = copy_from_user(&ro->loopback, optval, optlen);
628 +               if (err)
629 +                       return err;
630 +
631 +               break;
632 +
633 +       case CAN_RAW_RECV_OWN_MSGS:
634 +               if (optlen != sizeof(ro->recv_own_msgs))
635 +                       return -EINVAL;
636 +
637 +               err = copy_from_user(&ro->recv_own_msgs, optval, optlen);
638 +               if (err)
639 +                       return err;
640 +
641 +               break;
642 +
643 +       default:
644 +               return -ENOPROTOOPT;
645 +       }
646 +       return 0;
647 +}
648 +
649 +static int raw_getsockopt(struct socket *sock, int level, int optname,
650 +                         char __user *optval, int __user *optlen)
651 +{
652 +       struct sock *sk = sock->sk;
653 +       struct raw_sock *ro = raw_sk(sk);
654 +       int len;
655 +       void *val;
656 +       int err = 0;
657 +
658 +       if (level != SOL_CAN_RAW)
659 +               return -EINVAL;
660 +       if (get_user(len, optlen))
661 +               return -EFAULT;
662 +       if (len < 0)
663 +               return -EINVAL;
664 +
665 +       switch (optname) {
666 +
667 +       case CAN_RAW_FILTER:
668 +               lock_sock(sk);
669 +               if (ro->count > 0) {
670 +                       int fsize = ro->count * sizeof(struct can_filter);
671 +                       if (len > fsize)
672 +                               len = fsize;
673 +                       err = copy_to_user(optval, ro->filter, len);
674 +               } else
675 +                       len = 0;
676 +               release_sock(sk);
677 +
678 +               if (!err)
679 +                       err = put_user(len, optlen);
680 +               return err;
681 +
682 +       case CAN_RAW_ERR_FILTER:
683 +               if (len > sizeof(can_err_mask_t))
684 +                       len = sizeof(can_err_mask_t);
685 +               val = &ro->err_mask;
686 +               break;
687 +
688 +       case CAN_RAW_LOOPBACK:
689 +               if (len > sizeof(int))
690 +                       len = sizeof(int);
691 +               val = &ro->loopback;
692 +               break;
693 +
694 +       case CAN_RAW_RECV_OWN_MSGS:
695 +               if (len > sizeof(int))
696 +                       len = sizeof(int);
697 +               val = &ro->recv_own_msgs;
698 +               break;
699 +
700 +       default:
701 +               return -ENOPROTOOPT;
702 +       }
703 +
704 +       if (put_user(len, optlen))
705 +               return -EFAULT;
706 +       if (copy_to_user(optval, val, len))
707 +               return -EFAULT;
708 +       return 0;
709 +}
710 +
711 +static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
712 +                      struct msghdr *msg, size_t size)
713 +{
714 +       struct sock *sk = sock->sk;
715 +       struct raw_sock *ro = raw_sk(sk);
716 +       struct sk_buff *skb;
717 +       struct net_device *dev;
718 +       int ifindex;
719 +       int err;
720 +
721 +       DBG("socket %p, sk %p\n", sock, sk);
722 +
723 +       if (msg->msg_name) {
724 +               struct sockaddr_can *addr =
725 +                       (struct sockaddr_can *)msg->msg_name;
726 +
727 +               if (addr->can_family != AF_CAN)
728 +                       return -EINVAL;
729 +
730 +               ifindex = addr->can_ifindex;
731 +       } else
732 +               ifindex = ro->ifindex;
733 +
734 +       dev = dev_get_by_index(ifindex);
735 +       if (!dev) {
736 +               DBG("device %d not found\n", ifindex);
737 +               return -ENXIO;
738 +       }
739 +
740 +       skb = alloc_skb(size, GFP_KERNEL);
741 +       if (!skb) {
742 +               dev_put(dev);
743 +               return -ENOMEM;
744 +       }
745 +
746 +       err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
747 +       if (err < 0) {
748 +               kfree_skb(skb);
749 +               dev_put(dev);
750 +               return err;
751 +       }
752 +       skb->dev = dev;
753 +       skb->sk  = sk;
754 +
755 +       DBG("sending skbuff to interface %d\n", ifindex);
756 +       DBG_SKB(skb);
757 +
758 +       err = can_send(skb, ro->loopback);
759 +
760 +       dev_put(dev);
761 +
762 +       if (err)
763 +               return err;
764 +
765 +       return size;
766 +}
767 +
768 +static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
769 +                      struct msghdr *msg, size_t size, int flags)
770 +{
771 +       struct sock *sk = sock->sk;
772 +       struct sk_buff *skb;
773 +       int error = 0;
774 +       int noblock;
775 +
776 +       DBG("socket %p, sk %p\n", sock, sk);
777 +
778 +       noblock =  flags & MSG_DONTWAIT;
779 +       flags   &= ~MSG_DONTWAIT;
780 +
781 +       skb = skb_recv_datagram(sk, flags, noblock, &error);
782 +       if (!skb)
783 +               return error;
784 +
785 +       DBG("delivering skbuff %p\n", skb);
786 +       DBG_SKB(skb);
787 +
788 +       if (size < skb->len)
789 +               msg->msg_flags |= MSG_TRUNC;
790 +       else
791 +               size = skb->len;
792 +
793 +       error = memcpy_toiovec(msg->msg_iov, skb->data, size);
794 +       if (error < 0) {
795 +               skb_free_datagram(sk, skb);
796 +               return error;
797 +       }
798 +
799 +       sock_recv_timestamp(msg, sk, skb);
800 +
801 +       if (msg->msg_name) {
802 +               msg->msg_namelen = sizeof(struct sockaddr_can);
803 +               memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
804 +       }
805 +
806 +       DBG("freeing sock %p, skbuff %p\n", sk, skb);
807 +       skb_free_datagram(sk, skb);
808 +
809 +       return size;
810 +}
811 +
812 +static struct proto_ops raw_ops = {
813 +       .family        = PF_CAN,
814 +       .release       = raw_release,
815 +       .bind          = raw_bind,
816 +       .connect       = sock_no_connect,
817 +       .socketpair    = sock_no_socketpair,
818 +       .accept        = sock_no_accept,
819 +       .getname       = raw_getname,
820 +       .poll          = raw_poll,
821 +       .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
822 +       .listen        = sock_no_listen,
823 +       .shutdown      = sock_no_shutdown,
824 +       .setsockopt    = raw_setsockopt,
825 +       .getsockopt    = raw_getsockopt,
826 +       .sendmsg       = raw_sendmsg,
827 +       .recvmsg       = raw_recvmsg,
828 +       .mmap          = sock_no_mmap,
829 +       .sendpage      = sock_no_sendpage,
830 +};
831 +
832 +static struct proto raw_proto = {
833 +       .name       = "CAN_RAW",
834 +       .owner      = THIS_MODULE,
835 +       .obj_size   = sizeof(struct raw_sock),
836 +       .init       = raw_init,
837 +};
838 +
839 +static struct can_proto raw_can_proto = {
840 +       .type       = SOCK_RAW,
841 +       .protocol   = CAN_RAW,
842 +       .capability = RAW_CAP,
843 +       .ops        = &raw_ops,
844 +       .prot       = &raw_proto,
845 +};
846 +
847 +static __init int raw_module_init(void)
848 +{
849 +       printk(banner);
850 +
851 +       can_proto_register(&raw_can_proto);
852 +       return 0;
853 +}
854 +
855 +static __exit void raw_module_exit(void)
856 +{
857 +       can_proto_unregister(&raw_can_proto);
858 +}
859 +
860 +module_init(raw_module_init);
861 +module_exit(raw_module_exit);