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