xref: /linux/net/can/raw.c (revision 4413e16d9d21673bb5048a2e542f1aaa00015c2e)
1 /*
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  *
40  */
41 
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/uio.h>
45 #include <linux/net.h>
46 #include <linux/slab.h>
47 #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
58 static __initdata const char banner[] =
59 	KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
60 
61 MODULE_DESCRIPTION("PF_CAN raw protocol");
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
64 MODULE_ALIAS("can-proto-1");
65 
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 
78 struct 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 fd_frames;
86 	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 
92 /*
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  */
97 static 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 
106 static inline struct raw_sock *raw_sk(const struct sock *sk)
107 {
108 	return (struct raw_sock *)sk;
109 }
110 
111 static void raw_rcv(struct sk_buff *oskb, void *data)
112 {
113 	struct sock *sk = (struct sock *)data;
114 	struct raw_sock *ro = raw_sk(sk);
115 	struct sockaddr_can *addr;
116 	struct sk_buff *skb;
117 	unsigned int *pflags;
118 
119 	/* check the received tx sock reference */
120 	if (!ro->recv_own_msgs && oskb->sk == sk)
121 		return;
122 
123 	/* 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 
131 	/* clone the given skb to be able to enqueue it into the rcv queue */
132 	skb = skb_clone(oskb, GFP_ATOMIC);
133 	if (!skb)
134 		return;
135 
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 
149 	/* 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 
157 	if (sock_queue_rcv_skb(sk, skb) < 0)
158 		kfree_skb(skb);
159 }
160 
161 static int raw_enable_filters(struct net_device *dev, struct sock *sk,
162 			      struct can_filter *filter, int count)
163 {
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 
184 static 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 
196 static void raw_disable_filters(struct net_device *dev, struct sock *sk,
197 			      struct can_filter *filter, int count)
198 {
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 
206 static 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 
216 static 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 
225 static 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 
240 static 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 
247 	if (!net_eq(dev_net(dev), &init_net))
248 		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 
287 static 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;
303 	ro->fd_frames        = 0;
304 
305 	/* set notifier */
306 	ro->notifier.notifier_call = raw_notifier;
307 
308 	register_netdevice_notifier(&ro->notifier);
309 
310 	return 0;
311 }
312 
313 static int raw_release(struct socket *sock)
314 {
315 	struct sock *sk = sock->sk;
316 	struct raw_sock *ro;
317 
318 	if (!sk)
319 		return 0;
320 
321 	ro = raw_sk(sk);
322 
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 
348 	sock_orphan(sk);
349 	sock->sk = NULL;
350 
351 	release_sock(sk);
352 	sock_put(sk);
353 
354 	return 0;
355 }
356 
357 static 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);
395 	} 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 
432 static 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 
442 	memset(addr, 0, sizeof(*addr));
443 	addr->can_family  = AF_CAN;
444 	addr->can_ifindex = ro->ifindex;
445 
446 	*len = sizeof(*addr);
447 
448 	return 0;
449 }
450 
451 static int raw_setsockopt(struct socket *sock, int level, int optname,
452 			  char __user *optval, unsigned int optlen)
453 {
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;
465 
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 */
476 			filter = memdup_user(optval, optlen);
477 			if (IS_ERR(filter))
478 				return PTR_ERR(filter);
479 		} else if (count == 1) {
480 			if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
481 				return -EFAULT;
482 		}
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);
499 				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 
531 		if (copy_from_user(&err_mask, optval, optlen))
532 			return -EFAULT;
533 
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 
568 		if (copy_from_user(&ro->loopback, optval, optlen))
569 			return -EFAULT;
570 
571 		break;
572 
573 	case CAN_RAW_RECV_OWN_MSGS:
574 		if (optlen != sizeof(ro->recv_own_msgs))
575 			return -EINVAL;
576 
577 		if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
578 			return -EFAULT;
579 
580 		break;
581 
582 	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 
591 	default:
592 		return -ENOPROTOOPT;
593 	}
594 	return err;
595 }
596 
597 static 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;
621 			if (copy_to_user(optval, ro->filter, len))
622 				err = -EFAULT;
623 		} 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 
649 	case CAN_RAW_FD_FRAMES:
650 		if (len > sizeof(int))
651 			len = sizeof(int);
652 		val = &ro->fd_frames;
653 		break;
654 
655 	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 
666 static 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 
680 		if (msg->msg_namelen < sizeof(*addr))
681 			return -EINVAL;
682 
683 		if (addr->can_family != AF_CAN)
684 			return -EINVAL;
685 
686 		ifindex = addr->can_ifindex;
687 	} else
688 		ifindex = ro->ifindex;
689 
690 	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 	}
697 
698 	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);
704 	if (!skb)
705 		goto put_dev;
706 
707 	err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
708 	if (err < 0)
709 		goto free_skb;
710 	err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
711 	if (err < 0)
712 		goto free_skb;
713 
714 	skb->dev = dev;
715 	skb->sk  = sk;
716 
717 	err = can_send(skb, ro->loopback);
718 
719 	dev_put(dev);
720 
721 	if (err)
722 		goto send_failed;
723 
724 	return size;
725 
726 free_skb:
727 	kfree_skb(skb);
728 put_dev:
729 	dev_put(dev);
730 send_failed:
731 	return err;
732 }
733 
734 static 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;
738 	struct raw_sock *ro = raw_sk(sk);
739 	struct sk_buff *skb;
740 	int rxmtu;
741 	int err = 0;
742 	int noblock;
743 
744 	noblock =  flags & MSG_DONTWAIT;
745 	flags   &= ~MSG_DONTWAIT;
746 
747 	skb = skb_recv_datagram(sk, flags, noblock, &err);
748 	if (!skb)
749 		return err;
750 
751 	/*
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)
762 		msg->msg_flags |= MSG_TRUNC;
763 	else
764 		size = rxmtu;
765 
766 	err = memcpy_toiovec(msg->msg_iov, skb->data, size);
767 	if (err < 0) {
768 		skb_free_datagram(sk, skb);
769 		return err;
770 	}
771 
772 	sock_recv_ts_and_drops(msg, sk, skb);
773 
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 
779 	/* assign the flags that have been recorded in raw_rcv() */
780 	msg->msg_flags |= *(raw_flags(skb));
781 
782 	skb_free_datagram(sk, skb);
783 
784 	return size;
785 }
786 
787 static const struct proto_ops raw_ops = {
788 	.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,
796 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
797 	.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 
807 static 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 
814 static const struct can_proto raw_can_proto = {
815 	.type       = SOCK_RAW,
816 	.protocol   = CAN_RAW,
817 	.ops        = &raw_ops,
818 	.prot       = &raw_proto,
819 };
820 
821 static __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 
834 static __exit void raw_module_exit(void)
835 {
836 	can_proto_unregister(&raw_can_proto);
837 }
838 
839 module_init(raw_module_init);
840 module_exit(raw_module_exit);
841