xref: /openbmc/linux/net/can/raw.c (revision f6723b56)
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/skb.h>
54 #include <linux/can/raw.h>
55 #include <net/sock.h>
56 #include <net/net_namespace.h>
57 
58 #define CAN_RAW_VERSION CAN_VERSION
59 static __initconst const char banner[] =
60 	KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
61 
62 MODULE_DESCRIPTION("PF_CAN raw protocol");
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
65 MODULE_ALIAS("can-proto-1");
66 
67 #define MASK_ALL 0
68 
69 /*
70  * A raw socket has a list of can_filters attached to it, each receiving
71  * the CAN frames matching that filter.  If the filter list is empty,
72  * no CAN frames will be received by the socket.  The default after
73  * opening the socket, is to have one filter which receives all frames.
74  * The filter list is allocated dynamically with the exception of the
75  * list containing only one item.  This common case is optimized by
76  * storing the single filter in dfilter, to avoid using dynamic memory.
77  */
78 
79 struct raw_sock {
80 	struct sock sk;
81 	int bound;
82 	int ifindex;
83 	struct notifier_block notifier;
84 	int loopback;
85 	int recv_own_msgs;
86 	int fd_frames;
87 	int count;                 /* number of active filters */
88 	struct can_filter dfilter; /* default/single filter */
89 	struct can_filter *filter; /* pointer to filter(s) */
90 	can_err_mask_t err_mask;
91 };
92 
93 /*
94  * Return pointer to store the extra msg flags for raw_recvmsg().
95  * We use the space of one unsigned int beyond the 'struct sockaddr_can'
96  * in skb->cb.
97  */
98 static inline unsigned int *raw_flags(struct sk_buff *skb)
99 {
100 	BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) +
101 					 sizeof(unsigned int)));
102 
103 	/* return pointer after struct sockaddr_can */
104 	return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
105 }
106 
107 static inline struct raw_sock *raw_sk(const struct sock *sk)
108 {
109 	return (struct raw_sock *)sk;
110 }
111 
112 static void raw_rcv(struct sk_buff *oskb, void *data)
113 {
114 	struct sock *sk = (struct sock *)data;
115 	struct raw_sock *ro = raw_sk(sk);
116 	struct sockaddr_can *addr;
117 	struct sk_buff *skb;
118 	unsigned int *pflags;
119 
120 	/* check the received tx sock reference */
121 	if (!ro->recv_own_msgs && oskb->sk == sk)
122 		return;
123 
124 	/* do not pass frames with DLC > 8 to a legacy socket */
125 	if (!ro->fd_frames) {
126 		struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;
127 
128 		if (unlikely(cfd->len > CAN_MAX_DLEN))
129 			return;
130 	}
131 
132 	/* clone the given skb to be able to enqueue it into the rcv queue */
133 	skb = skb_clone(oskb, GFP_ATOMIC);
134 	if (!skb)
135 		return;
136 
137 	/*
138 	 *  Put the datagram to the queue so that raw_recvmsg() can
139 	 *  get it from there.  We need to pass the interface index to
140 	 *  raw_recvmsg().  We pass a whole struct sockaddr_can in skb->cb
141 	 *  containing the interface index.
142 	 */
143 
144 	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
145 	addr = (struct sockaddr_can *)skb->cb;
146 	memset(addr, 0, sizeof(*addr));
147 	addr->can_family  = AF_CAN;
148 	addr->can_ifindex = skb->dev->ifindex;
149 
150 	/* add CAN specific message flags for raw_recvmsg() */
151 	pflags = raw_flags(skb);
152 	*pflags = 0;
153 	if (oskb->sk)
154 		*pflags |= MSG_DONTROUTE;
155 	if (oskb->sk == sk)
156 		*pflags |= MSG_CONFIRM;
157 
158 	if (sock_queue_rcv_skb(sk, skb) < 0)
159 		kfree_skb(skb);
160 }
161 
162 static int raw_enable_filters(struct net_device *dev, struct sock *sk,
163 			      struct can_filter *filter, int count)
164 {
165 	int err = 0;
166 	int i;
167 
168 	for (i = 0; i < count; i++) {
169 		err = can_rx_register(dev, filter[i].can_id,
170 				      filter[i].can_mask,
171 				      raw_rcv, sk, "raw");
172 		if (err) {
173 			/* clean up successfully registered filters */
174 			while (--i >= 0)
175 				can_rx_unregister(dev, filter[i].can_id,
176 						  filter[i].can_mask,
177 						  raw_rcv, sk);
178 			break;
179 		}
180 	}
181 
182 	return err;
183 }
184 
185 static int raw_enable_errfilter(struct net_device *dev, struct sock *sk,
186 				can_err_mask_t err_mask)
187 {
188 	int err = 0;
189 
190 	if (err_mask)
191 		err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG,
192 				      raw_rcv, sk, "raw");
193 
194 	return err;
195 }
196 
197 static void raw_disable_filters(struct net_device *dev, struct sock *sk,
198 			      struct can_filter *filter, int count)
199 {
200 	int i;
201 
202 	for (i = 0; i < count; i++)
203 		can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask,
204 				  raw_rcv, sk);
205 }
206 
207 static inline void raw_disable_errfilter(struct net_device *dev,
208 					 struct sock *sk,
209 					 can_err_mask_t err_mask)
210 
211 {
212 	if (err_mask)
213 		can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG,
214 				  raw_rcv, sk);
215 }
216 
217 static inline void raw_disable_allfilters(struct net_device *dev,
218 					  struct sock *sk)
219 {
220 	struct raw_sock *ro = raw_sk(sk);
221 
222 	raw_disable_filters(dev, sk, ro->filter, ro->count);
223 	raw_disable_errfilter(dev, sk, ro->err_mask);
224 }
225 
226 static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
227 {
228 	struct raw_sock *ro = raw_sk(sk);
229 	int err;
230 
231 	err = raw_enable_filters(dev, sk, ro->filter, ro->count);
232 	if (!err) {
233 		err = raw_enable_errfilter(dev, sk, ro->err_mask);
234 		if (err)
235 			raw_disable_filters(dev, sk, ro->filter, ro->count);
236 	}
237 
238 	return err;
239 }
240 
241 static int raw_notifier(struct notifier_block *nb,
242 			unsigned long msg, void *ptr)
243 {
244 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
245 	struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
246 	struct sock *sk = &ro->sk;
247 
248 	if (!net_eq(dev_net(dev), &init_net))
249 		return NOTIFY_DONE;
250 
251 	if (dev->type != ARPHRD_CAN)
252 		return NOTIFY_DONE;
253 
254 	if (ro->ifindex != dev->ifindex)
255 		return NOTIFY_DONE;
256 
257 	switch (msg) {
258 
259 	case NETDEV_UNREGISTER:
260 		lock_sock(sk);
261 		/* remove current filters & unregister */
262 		if (ro->bound)
263 			raw_disable_allfilters(dev, sk);
264 
265 		if (ro->count > 1)
266 			kfree(ro->filter);
267 
268 		ro->ifindex = 0;
269 		ro->bound   = 0;
270 		ro->count   = 0;
271 		release_sock(sk);
272 
273 		sk->sk_err = ENODEV;
274 		if (!sock_flag(sk, SOCK_DEAD))
275 			sk->sk_error_report(sk);
276 		break;
277 
278 	case NETDEV_DOWN:
279 		sk->sk_err = ENETDOWN;
280 		if (!sock_flag(sk, SOCK_DEAD))
281 			sk->sk_error_report(sk);
282 		break;
283 	}
284 
285 	return NOTIFY_DONE;
286 }
287 
288 static int raw_init(struct sock *sk)
289 {
290 	struct raw_sock *ro = raw_sk(sk);
291 
292 	ro->bound            = 0;
293 	ro->ifindex          = 0;
294 
295 	/* set default filter to single entry dfilter */
296 	ro->dfilter.can_id   = 0;
297 	ro->dfilter.can_mask = MASK_ALL;
298 	ro->filter           = &ro->dfilter;
299 	ro->count            = 1;
300 
301 	/* set default loopback behaviour */
302 	ro->loopback         = 1;
303 	ro->recv_own_msgs    = 0;
304 	ro->fd_frames        = 0;
305 
306 	/* set notifier */
307 	ro->notifier.notifier_call = raw_notifier;
308 
309 	register_netdevice_notifier(&ro->notifier);
310 
311 	return 0;
312 }
313 
314 static int raw_release(struct socket *sock)
315 {
316 	struct sock *sk = sock->sk;
317 	struct raw_sock *ro;
318 
319 	if (!sk)
320 		return 0;
321 
322 	ro = raw_sk(sk);
323 
324 	unregister_netdevice_notifier(&ro->notifier);
325 
326 	lock_sock(sk);
327 
328 	/* remove current filters & unregister */
329 	if (ro->bound) {
330 		if (ro->ifindex) {
331 			struct net_device *dev;
332 
333 			dev = dev_get_by_index(&init_net, ro->ifindex);
334 			if (dev) {
335 				raw_disable_allfilters(dev, sk);
336 				dev_put(dev);
337 			}
338 		} else
339 			raw_disable_allfilters(NULL, sk);
340 	}
341 
342 	if (ro->count > 1)
343 		kfree(ro->filter);
344 
345 	ro->ifindex = 0;
346 	ro->bound   = 0;
347 	ro->count   = 0;
348 
349 	sock_orphan(sk);
350 	sock->sk = NULL;
351 
352 	release_sock(sk);
353 	sock_put(sk);
354 
355 	return 0;
356 }
357 
358 static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
359 {
360 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
361 	struct sock *sk = sock->sk;
362 	struct raw_sock *ro = raw_sk(sk);
363 	int ifindex;
364 	int err = 0;
365 	int notify_enetdown = 0;
366 
367 	if (len < sizeof(*addr))
368 		return -EINVAL;
369 
370 	lock_sock(sk);
371 
372 	if (ro->bound && addr->can_ifindex == ro->ifindex)
373 		goto out;
374 
375 	if (addr->can_ifindex) {
376 		struct net_device *dev;
377 
378 		dev = dev_get_by_index(&init_net, addr->can_ifindex);
379 		if (!dev) {
380 			err = -ENODEV;
381 			goto out;
382 		}
383 		if (dev->type != ARPHRD_CAN) {
384 			dev_put(dev);
385 			err = -ENODEV;
386 			goto out;
387 		}
388 		if (!(dev->flags & IFF_UP))
389 			notify_enetdown = 1;
390 
391 		ifindex = dev->ifindex;
392 
393 		/* filters set by default/setsockopt */
394 		err = raw_enable_allfilters(dev, sk);
395 		dev_put(dev);
396 	} else {
397 		ifindex = 0;
398 
399 		/* filters set by default/setsockopt */
400 		err = raw_enable_allfilters(NULL, sk);
401 	}
402 
403 	if (!err) {
404 		if (ro->bound) {
405 			/* unregister old filters */
406 			if (ro->ifindex) {
407 				struct net_device *dev;
408 
409 				dev = dev_get_by_index(&init_net, ro->ifindex);
410 				if (dev) {
411 					raw_disable_allfilters(dev, sk);
412 					dev_put(dev);
413 				}
414 			} else
415 				raw_disable_allfilters(NULL, sk);
416 		}
417 		ro->ifindex = ifindex;
418 		ro->bound = 1;
419 	}
420 
421  out:
422 	release_sock(sk);
423 
424 	if (notify_enetdown) {
425 		sk->sk_err = ENETDOWN;
426 		if (!sock_flag(sk, SOCK_DEAD))
427 			sk->sk_error_report(sk);
428 	}
429 
430 	return err;
431 }
432 
433 static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
434 		       int *len, int peer)
435 {
436 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
437 	struct sock *sk = sock->sk;
438 	struct raw_sock *ro = raw_sk(sk);
439 
440 	if (peer)
441 		return -EOPNOTSUPP;
442 
443 	memset(addr, 0, sizeof(*addr));
444 	addr->can_family  = AF_CAN;
445 	addr->can_ifindex = ro->ifindex;
446 
447 	*len = sizeof(*addr);
448 
449 	return 0;
450 }
451 
452 static int raw_setsockopt(struct socket *sock, int level, int optname,
453 			  char __user *optval, unsigned int optlen)
454 {
455 	struct sock *sk = sock->sk;
456 	struct raw_sock *ro = raw_sk(sk);
457 	struct can_filter *filter = NULL;  /* dyn. alloc'ed filters */
458 	struct can_filter sfilter;         /* single filter */
459 	struct net_device *dev = NULL;
460 	can_err_mask_t err_mask = 0;
461 	int count = 0;
462 	int err = 0;
463 
464 	if (level != SOL_CAN_RAW)
465 		return -EINVAL;
466 
467 	switch (optname) {
468 
469 	case CAN_RAW_FILTER:
470 		if (optlen % sizeof(struct can_filter) != 0)
471 			return -EINVAL;
472 
473 		count = optlen / sizeof(struct can_filter);
474 
475 		if (count > 1) {
476 			/* filter does not fit into dfilter => alloc space */
477 			filter = memdup_user(optval, optlen);
478 			if (IS_ERR(filter))
479 				return PTR_ERR(filter);
480 		} else if (count == 1) {
481 			if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
482 				return -EFAULT;
483 		}
484 
485 		lock_sock(sk);
486 
487 		if (ro->bound && ro->ifindex)
488 			dev = dev_get_by_index(&init_net, ro->ifindex);
489 
490 		if (ro->bound) {
491 			/* (try to) register the new filters */
492 			if (count == 1)
493 				err = raw_enable_filters(dev, sk, &sfilter, 1);
494 			else
495 				err = raw_enable_filters(dev, sk, filter,
496 							 count);
497 			if (err) {
498 				if (count > 1)
499 					kfree(filter);
500 				goto out_fil;
501 			}
502 
503 			/* remove old filter registrations */
504 			raw_disable_filters(dev, sk, ro->filter, ro->count);
505 		}
506 
507 		/* remove old filter space */
508 		if (ro->count > 1)
509 			kfree(ro->filter);
510 
511 		/* link new filters to the socket */
512 		if (count == 1) {
513 			/* copy filter data for single filter */
514 			ro->dfilter = sfilter;
515 			filter = &ro->dfilter;
516 		}
517 		ro->filter = filter;
518 		ro->count  = count;
519 
520  out_fil:
521 		if (dev)
522 			dev_put(dev);
523 
524 		release_sock(sk);
525 
526 		break;
527 
528 	case CAN_RAW_ERR_FILTER:
529 		if (optlen != sizeof(err_mask))
530 			return -EINVAL;
531 
532 		if (copy_from_user(&err_mask, optval, optlen))
533 			return -EFAULT;
534 
535 		err_mask &= CAN_ERR_MASK;
536 
537 		lock_sock(sk);
538 
539 		if (ro->bound && ro->ifindex)
540 			dev = dev_get_by_index(&init_net, ro->ifindex);
541 
542 		/* remove current error mask */
543 		if (ro->bound) {
544 			/* (try to) register the new err_mask */
545 			err = raw_enable_errfilter(dev, sk, err_mask);
546 
547 			if (err)
548 				goto out_err;
549 
550 			/* remove old err_mask registration */
551 			raw_disable_errfilter(dev, sk, ro->err_mask);
552 		}
553 
554 		/* link new err_mask to the socket */
555 		ro->err_mask = err_mask;
556 
557  out_err:
558 		if (dev)
559 			dev_put(dev);
560 
561 		release_sock(sk);
562 
563 		break;
564 
565 	case CAN_RAW_LOOPBACK:
566 		if (optlen != sizeof(ro->loopback))
567 			return -EINVAL;
568 
569 		if (copy_from_user(&ro->loopback, optval, optlen))
570 			return -EFAULT;
571 
572 		break;
573 
574 	case CAN_RAW_RECV_OWN_MSGS:
575 		if (optlen != sizeof(ro->recv_own_msgs))
576 			return -EINVAL;
577 
578 		if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
579 			return -EFAULT;
580 
581 		break;
582 
583 	case CAN_RAW_FD_FRAMES:
584 		if (optlen != sizeof(ro->fd_frames))
585 			return -EINVAL;
586 
587 		if (copy_from_user(&ro->fd_frames, optval, optlen))
588 			return -EFAULT;
589 
590 		break;
591 
592 	default:
593 		return -ENOPROTOOPT;
594 	}
595 	return err;
596 }
597 
598 static int raw_getsockopt(struct socket *sock, int level, int optname,
599 			  char __user *optval, int __user *optlen)
600 {
601 	struct sock *sk = sock->sk;
602 	struct raw_sock *ro = raw_sk(sk);
603 	int len;
604 	void *val;
605 	int err = 0;
606 
607 	if (level != SOL_CAN_RAW)
608 		return -EINVAL;
609 	if (get_user(len, optlen))
610 		return -EFAULT;
611 	if (len < 0)
612 		return -EINVAL;
613 
614 	switch (optname) {
615 
616 	case CAN_RAW_FILTER:
617 		lock_sock(sk);
618 		if (ro->count > 0) {
619 			int fsize = ro->count * sizeof(struct can_filter);
620 			if (len > fsize)
621 				len = fsize;
622 			if (copy_to_user(optval, ro->filter, len))
623 				err = -EFAULT;
624 		} else
625 			len = 0;
626 		release_sock(sk);
627 
628 		if (!err)
629 			err = put_user(len, optlen);
630 		return err;
631 
632 	case CAN_RAW_ERR_FILTER:
633 		if (len > sizeof(can_err_mask_t))
634 			len = sizeof(can_err_mask_t);
635 		val = &ro->err_mask;
636 		break;
637 
638 	case CAN_RAW_LOOPBACK:
639 		if (len > sizeof(int))
640 			len = sizeof(int);
641 		val = &ro->loopback;
642 		break;
643 
644 	case CAN_RAW_RECV_OWN_MSGS:
645 		if (len > sizeof(int))
646 			len = sizeof(int);
647 		val = &ro->recv_own_msgs;
648 		break;
649 
650 	case CAN_RAW_FD_FRAMES:
651 		if (len > sizeof(int))
652 			len = sizeof(int);
653 		val = &ro->fd_frames;
654 		break;
655 
656 	default:
657 		return -ENOPROTOOPT;
658 	}
659 
660 	if (put_user(len, optlen))
661 		return -EFAULT;
662 	if (copy_to_user(optval, val, len))
663 		return -EFAULT;
664 	return 0;
665 }
666 
667 static int raw_sendmsg(struct kiocb *iocb, struct socket *sock,
668 		       struct msghdr *msg, size_t size)
669 {
670 	struct sock *sk = sock->sk;
671 	struct raw_sock *ro = raw_sk(sk);
672 	struct sk_buff *skb;
673 	struct net_device *dev;
674 	int ifindex;
675 	int err;
676 
677 	if (msg->msg_name) {
678 		DECLARE_SOCKADDR(struct sockaddr_can *, addr, 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 + sizeof(struct can_skb_priv),
703 				  msg->msg_flags & MSG_DONTWAIT, &err);
704 	if (!skb)
705 		goto put_dev;
706 
707 	can_skb_reserve(skb);
708 	can_skb_prv(skb)->ifindex = dev->ifindex;
709 
710 	err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
711 	if (err < 0)
712 		goto free_skb;
713 
714 	sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
715 
716 	skb->dev = dev;
717 	skb->sk  = sk;
718 	skb->priority = sk->sk_priority;
719 
720 	err = can_send(skb, ro->loopback);
721 
722 	dev_put(dev);
723 
724 	if (err)
725 		goto send_failed;
726 
727 	return size;
728 
729 free_skb:
730 	kfree_skb(skb);
731 put_dev:
732 	dev_put(dev);
733 send_failed:
734 	return err;
735 }
736 
737 static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
738 		       struct msghdr *msg, size_t size, int flags)
739 {
740 	struct sock *sk = sock->sk;
741 	struct raw_sock *ro = raw_sk(sk);
742 	struct sk_buff *skb;
743 	int rxmtu;
744 	int err = 0;
745 	int noblock;
746 
747 	noblock =  flags & MSG_DONTWAIT;
748 	flags   &= ~MSG_DONTWAIT;
749 
750 	skb = skb_recv_datagram(sk, flags, noblock, &err);
751 	if (!skb)
752 		return err;
753 
754 	/*
755 	 * when serving a legacy socket the DLC <= 8 is already checked inside
756 	 * raw_rcv(). Now check if we need to pass a canfd_frame to a legacy
757 	 * socket and cut the possible CANFD_MTU/CAN_MTU length to CAN_MTU
758 	 */
759 	if (!ro->fd_frames)
760 		rxmtu = CAN_MTU;
761 	else
762 		rxmtu = skb->len;
763 
764 	if (size < rxmtu)
765 		msg->msg_flags |= MSG_TRUNC;
766 	else
767 		size = rxmtu;
768 
769 	err = memcpy_toiovec(msg->msg_iov, skb->data, size);
770 	if (err < 0) {
771 		skb_free_datagram(sk, skb);
772 		return err;
773 	}
774 
775 	sock_recv_ts_and_drops(msg, sk, skb);
776 
777 	if (msg->msg_name) {
778 		__sockaddr_check_size(sizeof(struct sockaddr_can));
779 		msg->msg_namelen = sizeof(struct sockaddr_can);
780 		memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
781 	}
782 
783 	/* assign the flags that have been recorded in raw_rcv() */
784 	msg->msg_flags |= *(raw_flags(skb));
785 
786 	skb_free_datagram(sk, skb);
787 
788 	return size;
789 }
790 
791 static const struct proto_ops raw_ops = {
792 	.family        = PF_CAN,
793 	.release       = raw_release,
794 	.bind          = raw_bind,
795 	.connect       = sock_no_connect,
796 	.socketpair    = sock_no_socketpair,
797 	.accept        = sock_no_accept,
798 	.getname       = raw_getname,
799 	.poll          = datagram_poll,
800 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
801 	.listen        = sock_no_listen,
802 	.shutdown      = sock_no_shutdown,
803 	.setsockopt    = raw_setsockopt,
804 	.getsockopt    = raw_getsockopt,
805 	.sendmsg       = raw_sendmsg,
806 	.recvmsg       = raw_recvmsg,
807 	.mmap          = sock_no_mmap,
808 	.sendpage      = sock_no_sendpage,
809 };
810 
811 static struct proto raw_proto __read_mostly = {
812 	.name       = "CAN_RAW",
813 	.owner      = THIS_MODULE,
814 	.obj_size   = sizeof(struct raw_sock),
815 	.init       = raw_init,
816 };
817 
818 static const struct can_proto raw_can_proto = {
819 	.type       = SOCK_RAW,
820 	.protocol   = CAN_RAW,
821 	.ops        = &raw_ops,
822 	.prot       = &raw_proto,
823 };
824 
825 static __init int raw_module_init(void)
826 {
827 	int err;
828 
829 	printk(banner);
830 
831 	err = can_proto_register(&raw_can_proto);
832 	if (err < 0)
833 		printk(KERN_ERR "can: registration of raw protocol failed\n");
834 
835 	return err;
836 }
837 
838 static __exit void raw_module_exit(void)
839 {
840 	can_proto_unregister(&raw_can_proto);
841 }
842 
843 module_init(raw_module_init);
844 module_exit(raw_module_exit);
845