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