blob: cde1b4a20f758fe8055f26a7f24b40f16767ae55 [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
58static __initdata const char banner[] =
59 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;
85 int count; /* number of active filters */
86 struct can_filter dfilter; /* default/single filter */
87 struct can_filter *filter; /* pointer to filter(s) */
88 can_err_mask_t err_mask;
89};
90
Oliver Hartkopp1e556592010-10-19 09:32:04 +000091/*
92 * Return pointer to store the extra msg flags for raw_recvmsg().
93 * We use the space of one unsigned int beyond the 'struct sockaddr_can'
94 * in skb->cb.
95 */
96static inline unsigned int *raw_flags(struct sk_buff *skb)
97{
98 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
99 sizeof(unsigned int)));
100
101 /* return pointer after struct sockaddr_can */
102 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
103}
104
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800105static inline struct raw_sock *raw_sk(const struct sock *sk)
106{
107 return (struct raw_sock *)sk;
108}
109
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000110static void raw_rcv(struct sk_buff *oskb, void *data)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800111{
112 struct sock *sk = (struct sock *)data;
113 struct raw_sock *ro = raw_sk(sk);
114 struct sockaddr_can *addr;
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000115 struct sk_buff *skb;
116 unsigned int *pflags;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800117
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800118 /* check the received tx sock reference */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000119 if (!ro->recv_own_msgs && oskb->sk == sk)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800120 return;
121
122 /* clone the given skb to be able to enqueue it into the rcv queue */
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000123 skb = skb_clone(oskb, GFP_ATOMIC);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800124 if (!skb)
125 return;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800126
127 /*
128 * Put the datagram to the queue so that raw_recvmsg() can
129 * get it from there. We need to pass the interface index to
130 * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
131 * containing the interface index.
132 */
133
134 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
135 addr = (struct sockaddr_can *)skb->cb;
136 memset(addr, 0, sizeof(*addr));
137 addr->can_family = AF_CAN;
138 addr->can_ifindex = skb->dev->ifindex;
139
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000140 /* add CAN specific message flags for raw_recvmsg() */
141 pflags = raw_flags(skb);
142 *pflags = 0;
143 if (oskb->sk)
144 *pflags |= MSG_DONTROUTE;
145 if (oskb->sk == sk)
146 *pflags |= MSG_CONFIRM;
147
Urs Thuermanna2199942008-02-07 18:05:04 -0800148 if (sock_queue_rcv_skb(sk, skb) < 0)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800149 kfree_skb(skb);
150}
151
152static int raw_enable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800153 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800154{
155 int err = 0;
156 int i;
157
158 for (i = 0; i < count; i++) {
159 err = can_rx_register(dev, filter[i].can_id,
160 filter[i].can_mask,
161 raw_rcv, sk, "raw");
162 if (err) {
163 /* clean up successfully registered filters */
164 while (--i >= 0)
165 can_rx_unregister(dev, filter[i].can_id,
166 filter[i].can_mask,
167 raw_rcv, sk);
168 break;
169 }
170 }
171
172 return err;
173}
174
175static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
176 can_err_mask_t err_mask)
177{
178 int err = 0;
179
180 if (err_mask)
181 err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
182 raw_rcv, sk, "raw");
183
184 return err;
185}
186
187static void raw_disable_filters(struct net_device *dev, struct sock *sk,
Urs Thuermanna2199942008-02-07 18:05:04 -0800188 struct can_filter *filter, int count)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800189{
190 int i;
191
192 for (i = 0; i < count; i++)
193 can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
194 raw_rcv, sk);
195}
196
197static inline void raw_disable_errfilter(struct net_device *dev,
198 struct sock *sk,
199 can_err_mask_t err_mask)
200
201{
202 if (err_mask)
203 can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
204 raw_rcv, sk);
205}
206
207static inline void raw_disable_allfilters(struct net_device *dev,
208 struct sock *sk)
209{
210 struct raw_sock *ro = raw_sk(sk);
211
212 raw_disable_filters(dev, sk, ro->filter, ro->count);
213 raw_disable_errfilter(dev, sk, ro->err_mask);
214}
215
216static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
217{
218 struct raw_sock *ro = raw_sk(sk);
219 int err;
220
221 err = raw_enable_filters(dev, sk, ro->filter, ro->count);
222 if (!err) {
223 err = raw_enable_errfilter(dev, sk, ro->err_mask);
224 if (err)
225 raw_disable_filters(dev, sk, ro->filter, ro->count);
226 }
227
228 return err;
229}
230
231static int raw_notifier(struct notifier_block *nb,
232 unsigned long msg, void *data)
233{
234 struct net_device *dev = (struct net_device *)data;
235 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
236 struct sock *sk = &ro->sk;
237
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700238 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800239 return NOTIFY_DONE;
240
241 if (dev->type != ARPHRD_CAN)
242 return NOTIFY_DONE;
243
244 if (ro->ifindex != dev->ifindex)
245 return NOTIFY_DONE;
246
247 switch (msg) {
248
249 case NETDEV_UNREGISTER:
250 lock_sock(sk);
251 /* remove current filters & unregister */
252 if (ro->bound)
253 raw_disable_allfilters(dev, sk);
254
255 if (ro->count > 1)
256 kfree(ro->filter);
257
258 ro->ifindex = 0;
259 ro->bound = 0;
260 ro->count = 0;
261 release_sock(sk);
262
263 sk->sk_err = ENODEV;
264 if (!sock_flag(sk, SOCK_DEAD))
265 sk->sk_error_report(sk);
266 break;
267
268 case NETDEV_DOWN:
269 sk->sk_err = ENETDOWN;
270 if (!sock_flag(sk, SOCK_DEAD))
271 sk->sk_error_report(sk);
272 break;
273 }
274
275 return NOTIFY_DONE;
276}
277
278static int raw_init(struct sock *sk)
279{
280 struct raw_sock *ro = raw_sk(sk);
281
282 ro->bound = 0;
283 ro->ifindex = 0;
284
285 /* set default filter to single entry dfilter */
286 ro->dfilter.can_id = 0;
287 ro->dfilter.can_mask = MASK_ALL;
288 ro->filter = &ro->dfilter;
289 ro->count = 1;
290
291 /* set default loopback behaviour */
292 ro->loopback = 1;
293 ro->recv_own_msgs = 0;
294
295 /* set notifier */
296 ro->notifier.notifier_call = raw_notifier;
297
298 register_netdevice_notifier(&ro->notifier);
299
300 return 0;
301}
302
303static int raw_release(struct socket *sock)
304{
305 struct sock *sk = sock->sk;
Oliver Hartkopp10022a62011-04-20 01:57:15 +0000306 struct raw_sock *ro;
307
308 if (!sk)
309 return 0;
310
311 ro = raw_sk(sk);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800312
313 unregister_netdevice_notifier(&ro->notifier);
314
315 lock_sock(sk);
316
317 /* remove current filters & unregister */
318 if (ro->bound) {
319 if (ro->ifindex) {
320 struct net_device *dev;
321
322 dev = dev_get_by_index(&init_net, ro->ifindex);
323 if (dev) {
324 raw_disable_allfilters(dev, sk);
325 dev_put(dev);
326 }
327 } else
328 raw_disable_allfilters(NULL, sk);
329 }
330
331 if (ro->count > 1)
332 kfree(ro->filter);
333
334 ro->ifindex = 0;
335 ro->bound = 0;
336 ro->count = 0;
337
Lothar Waßmannf7e5cc02009-07-14 23:10:21 +0000338 sock_orphan(sk);
339 sock->sk = NULL;
340
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800341 release_sock(sk);
342 sock_put(sk);
343
344 return 0;
345}
346
347static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
348{
349 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
350 struct sock *sk = sock->sk;
351 struct raw_sock *ro = raw_sk(sk);
352 int ifindex;
353 int err = 0;
354 int notify_enetdown = 0;
355
356 if (len < sizeof(*addr))
357 return -EINVAL;
358
359 lock_sock(sk);
360
361 if (ro->bound && addr->can_ifindex == ro->ifindex)
362 goto out;
363
364 if (addr->can_ifindex) {
365 struct net_device *dev;
366
367 dev = dev_get_by_index(&init_net, addr->can_ifindex);
368 if (!dev) {
369 err = -ENODEV;
370 goto out;
371 }
372 if (dev->type != ARPHRD_CAN) {
373 dev_put(dev);
374 err = -ENODEV;
375 goto out;
376 }
377 if (!(dev->flags & IFF_UP))
378 notify_enetdown = 1;
379
380 ifindex = dev->ifindex;
381
382 /* filters set by default/setsockopt */
383 err = raw_enable_allfilters(dev, sk);
384 dev_put(dev);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800385 } else {
386 ifindex = 0;
387
388 /* filters set by default/setsockopt */
389 err = raw_enable_allfilters(NULL, sk);
390 }
391
392 if (!err) {
393 if (ro->bound) {
394 /* unregister old filters */
395 if (ro->ifindex) {
396 struct net_device *dev;
397
398 dev = dev_get_by_index(&init_net, ro->ifindex);
399 if (dev) {
400 raw_disable_allfilters(dev, sk);
401 dev_put(dev);
402 }
403 } else
404 raw_disable_allfilters(NULL, sk);
405 }
406 ro->ifindex = ifindex;
407 ro->bound = 1;
408 }
409
410 out:
411 release_sock(sk);
412
413 if (notify_enetdown) {
414 sk->sk_err = ENETDOWN;
415 if (!sock_flag(sk, SOCK_DEAD))
416 sk->sk_error_report(sk);
417 }
418
419 return err;
420}
421
422static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
423 int *len, int peer)
424{
425 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
426 struct sock *sk = sock->sk;
427 struct raw_sock *ro = raw_sk(sk);
428
429 if (peer)
430 return -EOPNOTSUPP;
431
Eric Dumazete84b90a2009-08-06 20:27:04 +0000432 memset(addr, 0, sizeof(*addr));
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800433 addr->can_family = AF_CAN;
434 addr->can_ifindex = ro->ifindex;
435
436 *len = sizeof(*addr);
437
438 return 0;
439}
440
441static int raw_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700442 char __user *optval, unsigned int optlen)
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800443{
444 struct sock *sk = sock->sk;
445 struct raw_sock *ro = raw_sk(sk);
446 struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
447 struct can_filter sfilter; /* single filter */
448 struct net_device *dev = NULL;
449 can_err_mask_t err_mask = 0;
450 int count = 0;
451 int err = 0;
452
453 if (level != SOL_CAN_RAW)
454 return -EINVAL;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800455
456 switch (optname) {
457
458 case CAN_RAW_FILTER:
459 if (optlen % sizeof(struct can_filter) != 0)
460 return -EINVAL;
461
462 count = optlen / sizeof(struct can_filter);
463
464 if (count > 1) {
465 /* filter does not fit into dfilter => alloc space */
Julia Lawall16dff912010-05-21 22:18:34 +0000466 filter = memdup_user(optval, optlen);
467 if (IS_ERR(filter))
468 return PTR_ERR(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800469 } else if (count == 1) {
Eric Dumazet4ffa8702010-04-09 23:47:31 +0000470 if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700471 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800472 }
473
474 lock_sock(sk);
475
476 if (ro->bound && ro->ifindex)
477 dev = dev_get_by_index(&init_net, ro->ifindex);
478
479 if (ro->bound) {
480 /* (try to) register the new filters */
481 if (count == 1)
482 err = raw_enable_filters(dev, sk, &sfilter, 1);
483 else
484 err = raw_enable_filters(dev, sk, filter,
485 count);
486 if (err) {
487 if (count > 1)
488 kfree(filter);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800489 goto out_fil;
490 }
491
492 /* remove old filter registrations */
493 raw_disable_filters(dev, sk, ro->filter, ro->count);
494 }
495
496 /* remove old filter space */
497 if (ro->count > 1)
498 kfree(ro->filter);
499
500 /* link new filters to the socket */
501 if (count == 1) {
502 /* copy filter data for single filter */
503 ro->dfilter = sfilter;
504 filter = &ro->dfilter;
505 }
506 ro->filter = filter;
507 ro->count = count;
508
509 out_fil:
510 if (dev)
511 dev_put(dev);
512
513 release_sock(sk);
514
515 break;
516
517 case CAN_RAW_ERR_FILTER:
518 if (optlen != sizeof(err_mask))
519 return -EINVAL;
520
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700521 if (copy_from_user(&err_mask, optval, optlen))
522 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800523
524 err_mask &= CAN_ERR_MASK;
525
526 lock_sock(sk);
527
528 if (ro->bound && ro->ifindex)
529 dev = dev_get_by_index(&init_net, ro->ifindex);
530
531 /* remove current error mask */
532 if (ro->bound) {
533 /* (try to) register the new err_mask */
534 err = raw_enable_errfilter(dev, sk, err_mask);
535
536 if (err)
537 goto out_err;
538
539 /* remove old err_mask registration */
540 raw_disable_errfilter(dev, sk, ro->err_mask);
541 }
542
543 /* link new err_mask to the socket */
544 ro->err_mask = err_mask;
545
546 out_err:
547 if (dev)
548 dev_put(dev);
549
550 release_sock(sk);
551
552 break;
553
554 case CAN_RAW_LOOPBACK:
555 if (optlen != sizeof(ro->loopback))
556 return -EINVAL;
557
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700558 if (copy_from_user(&ro->loopback, optval, optlen))
559 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800560
561 break;
562
563 case CAN_RAW_RECV_OWN_MSGS:
564 if (optlen != sizeof(ro->recv_own_msgs))
565 return -EINVAL;
566
Sam Ravnborg3f91bd42008-04-26 22:57:25 -0700567 if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
568 return -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800569
570 break;
571
572 default:
573 return -ENOPROTOOPT;
574 }
575 return err;
576}
577
578static int raw_getsockopt(struct socket *sock, int level, int optname,
579 char __user *optval, int __user *optlen)
580{
581 struct sock *sk = sock->sk;
582 struct raw_sock *ro = raw_sk(sk);
583 int len;
584 void *val;
585 int err = 0;
586
587 if (level != SOL_CAN_RAW)
588 return -EINVAL;
589 if (get_user(len, optlen))
590 return -EFAULT;
591 if (len < 0)
592 return -EINVAL;
593
594 switch (optname) {
595
596 case CAN_RAW_FILTER:
597 lock_sock(sk);
598 if (ro->count > 0) {
599 int fsize = ro->count * sizeof(struct can_filter);
600 if (len > fsize)
601 len = fsize;
Pavel Emelyanov653252c2008-04-25 01:49:48 -0700602 if (copy_to_user(optval, ro->filter, len))
603 err = -EFAULT;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800604 } else
605 len = 0;
606 release_sock(sk);
607
608 if (!err)
609 err = put_user(len, optlen);
610 return err;
611
612 case CAN_RAW_ERR_FILTER:
613 if (len > sizeof(can_err_mask_t))
614 len = sizeof(can_err_mask_t);
615 val = &ro->err_mask;
616 break;
617
618 case CAN_RAW_LOOPBACK:
619 if (len > sizeof(int))
620 len = sizeof(int);
621 val = &ro->loopback;
622 break;
623
624 case CAN_RAW_RECV_OWN_MSGS:
625 if (len > sizeof(int))
626 len = sizeof(int);
627 val = &ro->recv_own_msgs;
628 break;
629
630 default:
631 return -ENOPROTOOPT;
632 }
633
634 if (put_user(len, optlen))
635 return -EFAULT;
636 if (copy_to_user(optval, val, len))
637 return -EFAULT;
638 return 0;
639}
640
641static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
642 struct msghdr *msg, size_t size)
643{
644 struct sock *sk = sock->sk;
645 struct raw_sock *ro = raw_sk(sk);
646 struct sk_buff *skb;
647 struct net_device *dev;
648 int ifindex;
649 int err;
650
651 if (msg->msg_name) {
652 struct sockaddr_can *addr =
653 (struct sockaddr_can *)msg->msg_name;
654
Kurt Van Dijck5e507322011-01-15 20:56:42 -0800655 if (msg->msg_namelen < sizeof(*addr))
656 return -EINVAL;
657
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800658 if (addr->can_family != AF_CAN)
659 return -EINVAL;
660
661 ifindex = addr->can_ifindex;
662 } else
663 ifindex = ro->ifindex;
664
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700665 if (size != sizeof(struct can_frame))
666 return -EINVAL;
667
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800668 dev = dev_get_by_index(&init_net, ifindex);
669 if (!dev)
670 return -ENXIO;
671
672 skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
673 &err);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800674 if (!skb)
675 goto put_dev;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800676
677 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800678 if (err < 0)
679 goto free_skb;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000680 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Patrick Ohly51f31ca2009-02-12 05:03:39 +0000681 if (err < 0)
682 goto free_skb;
Oliver Hartkoppcff0d6e2010-08-03 00:31:48 -0700683
684 /* to be able to check the received tx sock reference in raw_rcv() */
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000685 skb_shinfo(skb)->tx_flags |= SKBTX_DRV_NEEDS_SK_REF;
Oliver Hartkoppcff0d6e2010-08-03 00:31:48 -0700686
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800687 skb->dev = dev;
688 skb->sk = sk;
689
690 err = can_send(skb, ro->loopback);
691
692 dev_put(dev);
693
694 if (err)
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800695 goto send_failed;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800696
697 return size;
Ilpo Järvinenebad5c02008-12-14 23:16:58 -0800698
699free_skb:
700 kfree_skb(skb);
701put_dev:
702 dev_put(dev);
703send_failed:
704 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800705}
706
707static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
708 struct msghdr *msg, size_t size, int flags)
709{
710 struct sock *sk = sock->sk;
711 struct sk_buff *skb;
Urs Thuermanna2199942008-02-07 18:05:04 -0800712 int err = 0;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800713 int noblock;
714
715 noblock = flags & MSG_DONTWAIT;
716 flags &= ~MSG_DONTWAIT;
717
Urs Thuermanna2199942008-02-07 18:05:04 -0800718 skb = skb_recv_datagram(sk, flags, noblock, &err);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800719 if (!skb)
Urs Thuermanna2199942008-02-07 18:05:04 -0800720 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800721
722 if (size < skb->len)
723 msg->msg_flags |= MSG_TRUNC;
724 else
725 size = skb->len;
726
Urs Thuermanna2199942008-02-07 18:05:04 -0800727 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
728 if (err < 0) {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800729 skb_free_datagram(sk, skb);
Urs Thuermanna2199942008-02-07 18:05:04 -0800730 return err;
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800731 }
732
Neil Horman3b885782009-10-12 13:26:31 -0700733 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800734
735 if (msg->msg_name) {
736 msg->msg_namelen = sizeof(struct sockaddr_can);
737 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
738 }
739
Oliver Hartkopp1e556592010-10-19 09:32:04 +0000740 /* assign the flags that have been recorded in raw_rcv() */
741 msg->msg_flags |= *(raw_flags(skb));
742
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800743 skb_free_datagram(sk, skb);
744
745 return size;
746}
747
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000748static const struct proto_ops raw_ops = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800749 .family = PF_CAN,
750 .release = raw_release,
751 .bind = raw_bind,
752 .connect = sock_no_connect,
753 .socketpair = sock_no_socketpair,
754 .accept = sock_no_accept,
755 .getname = raw_getname,
756 .poll = datagram_poll,
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000757 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800758 .listen = sock_no_listen,
759 .shutdown = sock_no_shutdown,
760 .setsockopt = raw_setsockopt,
761 .getsockopt = raw_getsockopt,
762 .sendmsg = raw_sendmsg,
763 .recvmsg = raw_recvmsg,
764 .mmap = sock_no_mmap,
765 .sendpage = sock_no_sendpage,
766};
767
768static struct proto raw_proto __read_mostly = {
769 .name = "CAN_RAW",
770 .owner = THIS_MODULE,
771 .obj_size = sizeof(struct raw_sock),
772 .init = raw_init,
773};
774
Kurt Van Dijck16506292011-05-03 18:40:57 +0000775static const struct can_proto raw_can_proto = {
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800776 .type = SOCK_RAW,
777 .protocol = CAN_RAW,
Oliver Hartkoppc18ce102007-11-16 15:53:09 -0800778 .ops = &raw_ops,
779 .prot = &raw_proto,
780};
781
782static __init int raw_module_init(void)
783{
784 int err;
785
786 printk(banner);
787
788 err = can_proto_register(&raw_can_proto);
789 if (err < 0)
790 printk(KERN_ERR "can: registration of raw protocol failed\n");
791
792 return err;
793}
794
795static __exit void raw_module_exit(void)
796{
797 can_proto_unregister(&raw_can_proto);
798}
799
800module_init(raw_module_init);
801module_exit(raw_module_exit);