1fba76a58SOliver Hartkopp // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 2d6ada83bSMarc Kleine-Budde /* raw.c - Raw sockets for protocol family CAN 3c18ce101SOliver Hartkopp * 4c18ce101SOliver Hartkopp * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 5c18ce101SOliver Hartkopp * All rights reserved. 6c18ce101SOliver Hartkopp * 7c18ce101SOliver Hartkopp * Redistribution and use in source and binary forms, with or without 8c18ce101SOliver Hartkopp * modification, are permitted provided that the following conditions 9c18ce101SOliver Hartkopp * are met: 10c18ce101SOliver Hartkopp * 1. Redistributions of source code must retain the above copyright 11c18ce101SOliver Hartkopp * notice, this list of conditions and the following disclaimer. 12c18ce101SOliver Hartkopp * 2. Redistributions in binary form must reproduce the above copyright 13c18ce101SOliver Hartkopp * notice, this list of conditions and the following disclaimer in the 14c18ce101SOliver Hartkopp * documentation and/or other materials provided with the distribution. 15c18ce101SOliver Hartkopp * 3. Neither the name of Volkswagen nor the names of its contributors 16c18ce101SOliver Hartkopp * may be used to endorse or promote products derived from this software 17c18ce101SOliver Hartkopp * without specific prior written permission. 18c18ce101SOliver Hartkopp * 19c18ce101SOliver Hartkopp * Alternatively, provided that this notice is retained in full, this 20c18ce101SOliver Hartkopp * software may be distributed under the terms of the GNU General 21c18ce101SOliver Hartkopp * Public License ("GPL") version 2, in which case the provisions of the 22c18ce101SOliver Hartkopp * GPL apply INSTEAD OF those given above. 23c18ce101SOliver Hartkopp * 24c18ce101SOliver Hartkopp * The provided data structures and external interfaces from this code 25c18ce101SOliver Hartkopp * are not restricted to be used by modules with a GPL compatible license. 26c18ce101SOliver Hartkopp * 27c18ce101SOliver Hartkopp * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28c18ce101SOliver Hartkopp * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29c18ce101SOliver Hartkopp * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30c18ce101SOliver Hartkopp * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31c18ce101SOliver Hartkopp * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32c18ce101SOliver Hartkopp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33c18ce101SOliver Hartkopp * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34c18ce101SOliver Hartkopp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35c18ce101SOliver Hartkopp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36c18ce101SOliver Hartkopp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37c18ce101SOliver Hartkopp * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 38c18ce101SOliver Hartkopp * DAMAGE. 39c18ce101SOliver Hartkopp * 40c18ce101SOliver Hartkopp */ 41c18ce101SOliver Hartkopp 42c18ce101SOliver Hartkopp #include <linux/module.h> 43c18ce101SOliver Hartkopp #include <linux/init.h> 44c18ce101SOliver Hartkopp #include <linux/uio.h> 45c18ce101SOliver Hartkopp #include <linux/net.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47c18ce101SOliver Hartkopp #include <linux/netdevice.h> 48c18ce101SOliver Hartkopp #include <linux/socket.h> 49c18ce101SOliver Hartkopp #include <linux/if_arp.h> 50c18ce101SOliver Hartkopp #include <linux/skbuff.h> 51c18ce101SOliver Hartkopp #include <linux/can.h> 52c18ce101SOliver Hartkopp #include <linux/can/core.h> 53156c2bb9SOliver Hartkopp #include <linux/can/skb.h> 54c18ce101SOliver Hartkopp #include <linux/can/raw.h> 55c18ce101SOliver Hartkopp #include <net/sock.h> 56c18ce101SOliver Hartkopp #include <net/net_namespace.h> 57c18ce101SOliver Hartkopp 58c18ce101SOliver Hartkopp #define CAN_RAW_VERSION CAN_VERSION 59c18ce101SOliver Hartkopp 60c18ce101SOliver Hartkopp MODULE_DESCRIPTION("PF_CAN raw protocol"); 61c18ce101SOliver Hartkopp MODULE_LICENSE("Dual BSD/GPL"); 62c18ce101SOliver Hartkopp MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>"); 63b13bb2e9SLothar Waßmann MODULE_ALIAS("can-proto-1"); 64c18ce101SOliver Hartkopp 65c18ce101SOliver Hartkopp #define MASK_ALL 0 66c18ce101SOliver Hartkopp 67d6ada83bSMarc Kleine-Budde /* A raw socket has a list of can_filters attached to it, each receiving 68c18ce101SOliver Hartkopp * the CAN frames matching that filter. If the filter list is empty, 69c18ce101SOliver Hartkopp * no CAN frames will be received by the socket. The default after 70c18ce101SOliver Hartkopp * opening the socket, is to have one filter which receives all frames. 71c18ce101SOliver Hartkopp * The filter list is allocated dynamically with the exception of the 72c18ce101SOliver Hartkopp * list containing only one item. This common case is optimized by 73c18ce101SOliver Hartkopp * storing the single filter in dfilter, to avoid using dynamic memory. 74c18ce101SOliver Hartkopp */ 75c18ce101SOliver Hartkopp 76514ac99cSOliver Hartkopp struct uniqframe { 77d3b58c47SOliver Hartkopp int skbcnt; 78514ac99cSOliver Hartkopp const struct sk_buff *skb; 79a5581ef4SOliver Hartkopp unsigned int join_rx_count; 80514ac99cSOliver Hartkopp }; 81514ac99cSOliver Hartkopp 82c18ce101SOliver Hartkopp struct raw_sock { 83c18ce101SOliver Hartkopp struct sock sk; 84c18ce101SOliver Hartkopp int bound; 85c18ce101SOliver Hartkopp int ifindex; 86c18ce101SOliver Hartkopp struct notifier_block notifier; 87c18ce101SOliver Hartkopp int loopback; 88c18ce101SOliver Hartkopp int recv_own_msgs; 89e2d265d3SOliver Hartkopp int fd_frames; 90a5581ef4SOliver Hartkopp int join_filters; 91c18ce101SOliver Hartkopp int count; /* number of active filters */ 92c18ce101SOliver Hartkopp struct can_filter dfilter; /* default/single filter */ 93c18ce101SOliver Hartkopp struct can_filter *filter; /* pointer to filter(s) */ 94c18ce101SOliver Hartkopp can_err_mask_t err_mask; 95514ac99cSOliver Hartkopp struct uniqframe __percpu *uniq; 96c18ce101SOliver Hartkopp }; 97c18ce101SOliver Hartkopp 98d6ada83bSMarc Kleine-Budde /* Return pointer to store the extra msg flags for raw_recvmsg(). 991e55659cSOliver Hartkopp * We use the space of one unsigned int beyond the 'struct sockaddr_can' 1001e55659cSOliver Hartkopp * in skb->cb. 1011e55659cSOliver Hartkopp */ 1021e55659cSOliver Hartkopp static inline unsigned int *raw_flags(struct sk_buff *skb) 1031e55659cSOliver Hartkopp { 104b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sockaddr_can) + 105b4772ef8SEyal Birger sizeof(unsigned int)); 1061e55659cSOliver Hartkopp 1071e55659cSOliver Hartkopp /* return pointer after struct sockaddr_can */ 1081e55659cSOliver Hartkopp return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]); 1091e55659cSOliver Hartkopp } 1101e55659cSOliver Hartkopp 111c18ce101SOliver Hartkopp static inline struct raw_sock *raw_sk(const struct sock *sk) 112c18ce101SOliver Hartkopp { 113c18ce101SOliver Hartkopp return (struct raw_sock *)sk; 114c18ce101SOliver Hartkopp } 115c18ce101SOliver Hartkopp 1161e55659cSOliver Hartkopp static void raw_rcv(struct sk_buff *oskb, void *data) 117c18ce101SOliver Hartkopp { 118c18ce101SOliver Hartkopp struct sock *sk = (struct sock *)data; 119c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 120c18ce101SOliver Hartkopp struct sockaddr_can *addr; 1211e55659cSOliver Hartkopp struct sk_buff *skb; 1221e55659cSOliver Hartkopp unsigned int *pflags; 123c18ce101SOliver Hartkopp 124c18ce101SOliver Hartkopp /* check the received tx sock reference */ 1251e55659cSOliver Hartkopp if (!ro->recv_own_msgs && oskb->sk == sk) 126c18ce101SOliver Hartkopp return; 1271fa17d4bSOliver Hartkopp 128821047c4SOliver Hartkopp /* do not pass non-CAN2.0 frames to a legacy socket */ 129821047c4SOliver Hartkopp if (!ro->fd_frames && oskb->len != CAN_MTU) 130e2d265d3SOliver Hartkopp return; 131e2d265d3SOliver Hartkopp 132514ac99cSOliver Hartkopp /* eliminate multiple filter matches for the same skb */ 133514ac99cSOliver Hartkopp if (this_cpu_ptr(ro->uniq)->skb == oskb && 134d3b58c47SOliver Hartkopp this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) { 135a5581ef4SOliver Hartkopp if (ro->join_filters) { 136a5581ef4SOliver Hartkopp this_cpu_inc(ro->uniq->join_rx_count); 137a5581ef4SOliver Hartkopp /* drop frame until all enabled filters matched */ 138a5581ef4SOliver Hartkopp if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count) 139514ac99cSOliver Hartkopp return; 140514ac99cSOliver Hartkopp } else { 141a5581ef4SOliver Hartkopp return; 142a5581ef4SOliver Hartkopp } 143a5581ef4SOliver Hartkopp } else { 144514ac99cSOliver Hartkopp this_cpu_ptr(ro->uniq)->skb = oskb; 145d3b58c47SOliver Hartkopp this_cpu_ptr(ro->uniq)->skbcnt = can_skb_prv(oskb)->skbcnt; 146a5581ef4SOliver Hartkopp this_cpu_ptr(ro->uniq)->join_rx_count = 1; 147a5581ef4SOliver Hartkopp /* drop first frame to check all enabled filters? */ 148a5581ef4SOliver Hartkopp if (ro->join_filters && ro->count > 1) 149a5581ef4SOliver Hartkopp return; 150514ac99cSOliver Hartkopp } 151514ac99cSOliver Hartkopp 1521fa17d4bSOliver Hartkopp /* clone the given skb to be able to enqueue it into the rcv queue */ 1531e55659cSOliver Hartkopp skb = skb_clone(oskb, GFP_ATOMIC); 1541fa17d4bSOliver Hartkopp if (!skb) 1551fa17d4bSOliver Hartkopp return; 156c18ce101SOliver Hartkopp 157d6ada83bSMarc Kleine-Budde /* Put the datagram to the queue so that raw_recvmsg() can 158c18ce101SOliver Hartkopp * get it from there. We need to pass the interface index to 159c18ce101SOliver Hartkopp * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb 160c18ce101SOliver Hartkopp * containing the interface index. 161c18ce101SOliver Hartkopp */ 162c18ce101SOliver Hartkopp 163b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sockaddr_can)); 164c18ce101SOliver Hartkopp addr = (struct sockaddr_can *)skb->cb; 165c18ce101SOliver Hartkopp memset(addr, 0, sizeof(*addr)); 166c18ce101SOliver Hartkopp addr->can_family = AF_CAN; 167c18ce101SOliver Hartkopp addr->can_ifindex = skb->dev->ifindex; 168c18ce101SOliver Hartkopp 1691e55659cSOliver Hartkopp /* add CAN specific message flags for raw_recvmsg() */ 1701e55659cSOliver Hartkopp pflags = raw_flags(skb); 1711e55659cSOliver Hartkopp *pflags = 0; 1721e55659cSOliver Hartkopp if (oskb->sk) 1731e55659cSOliver Hartkopp *pflags |= MSG_DONTROUTE; 1741e55659cSOliver Hartkopp if (oskb->sk == sk) 1751e55659cSOliver Hartkopp *pflags |= MSG_CONFIRM; 1761e55659cSOliver Hartkopp 177a219994bSUrs Thuermann if (sock_queue_rcv_skb(sk, skb) < 0) 178c18ce101SOliver Hartkopp kfree_skb(skb); 179c18ce101SOliver Hartkopp } 180c18ce101SOliver Hartkopp 1818e8cda6dSMario Kicherer static int raw_enable_filters(struct net *net, struct net_device *dev, 1828e8cda6dSMario Kicherer struct sock *sk, struct can_filter *filter, 1838e8cda6dSMario Kicherer int count) 184c18ce101SOliver Hartkopp { 185c18ce101SOliver Hartkopp int err = 0; 186c18ce101SOliver Hartkopp int i; 187c18ce101SOliver Hartkopp 188c18ce101SOliver Hartkopp for (i = 0; i < count; i++) { 1898e8cda6dSMario Kicherer err = can_rx_register(net, dev, filter[i].can_id, 190c18ce101SOliver Hartkopp filter[i].can_mask, 191f1712c73SEric Dumazet raw_rcv, sk, "raw", sk); 192c18ce101SOliver Hartkopp if (err) { 193c18ce101SOliver Hartkopp /* clean up successfully registered filters */ 194c18ce101SOliver Hartkopp while (--i >= 0) 1958e8cda6dSMario Kicherer can_rx_unregister(net, dev, filter[i].can_id, 196c18ce101SOliver Hartkopp filter[i].can_mask, 197c18ce101SOliver Hartkopp raw_rcv, sk); 198c18ce101SOliver Hartkopp break; 199c18ce101SOliver Hartkopp } 200c18ce101SOliver Hartkopp } 201c18ce101SOliver Hartkopp 202c18ce101SOliver Hartkopp return err; 203c18ce101SOliver Hartkopp } 204c18ce101SOliver Hartkopp 2058e8cda6dSMario Kicherer static int raw_enable_errfilter(struct net *net, struct net_device *dev, 2068e8cda6dSMario Kicherer struct sock *sk, can_err_mask_t err_mask) 207c18ce101SOliver Hartkopp { 208c18ce101SOliver Hartkopp int err = 0; 209c18ce101SOliver Hartkopp 210c18ce101SOliver Hartkopp if (err_mask) 2118e8cda6dSMario Kicherer err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG, 212f1712c73SEric Dumazet raw_rcv, sk, "raw", sk); 213c18ce101SOliver Hartkopp 214c18ce101SOliver Hartkopp return err; 215c18ce101SOliver Hartkopp } 216c18ce101SOliver Hartkopp 2178e8cda6dSMario Kicherer static void raw_disable_filters(struct net *net, struct net_device *dev, 2188e8cda6dSMario Kicherer struct sock *sk, struct can_filter *filter, 2198e8cda6dSMario Kicherer int count) 220c18ce101SOliver Hartkopp { 221c18ce101SOliver Hartkopp int i; 222c18ce101SOliver Hartkopp 223c18ce101SOliver Hartkopp for (i = 0; i < count; i++) 2248e8cda6dSMario Kicherer can_rx_unregister(net, dev, filter[i].can_id, 2258e8cda6dSMario Kicherer filter[i].can_mask, raw_rcv, sk); 226c18ce101SOliver Hartkopp } 227c18ce101SOliver Hartkopp 2288e8cda6dSMario Kicherer static inline void raw_disable_errfilter(struct net *net, 2298e8cda6dSMario Kicherer struct net_device *dev, 230c18ce101SOliver Hartkopp struct sock *sk, 231c18ce101SOliver Hartkopp can_err_mask_t err_mask) 232c18ce101SOliver Hartkopp 233c18ce101SOliver Hartkopp { 234c18ce101SOliver Hartkopp if (err_mask) 2358e8cda6dSMario Kicherer can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG, 236c18ce101SOliver Hartkopp raw_rcv, sk); 237c18ce101SOliver Hartkopp } 238c18ce101SOliver Hartkopp 2398e8cda6dSMario Kicherer static inline void raw_disable_allfilters(struct net *net, 2408e8cda6dSMario Kicherer struct net_device *dev, 241c18ce101SOliver Hartkopp struct sock *sk) 242c18ce101SOliver Hartkopp { 243c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 244c18ce101SOliver Hartkopp 2458e8cda6dSMario Kicherer raw_disable_filters(net, dev, sk, ro->filter, ro->count); 2468e8cda6dSMario Kicherer raw_disable_errfilter(net, dev, sk, ro->err_mask); 247c18ce101SOliver Hartkopp } 248c18ce101SOliver Hartkopp 2498e8cda6dSMario Kicherer static int raw_enable_allfilters(struct net *net, struct net_device *dev, 2508e8cda6dSMario Kicherer struct sock *sk) 251c18ce101SOliver Hartkopp { 252c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 253c18ce101SOliver Hartkopp int err; 254c18ce101SOliver Hartkopp 2558e8cda6dSMario Kicherer err = raw_enable_filters(net, dev, sk, ro->filter, ro->count); 256c18ce101SOliver Hartkopp if (!err) { 2578e8cda6dSMario Kicherer err = raw_enable_errfilter(net, dev, sk, ro->err_mask); 258c18ce101SOliver Hartkopp if (err) 2598e8cda6dSMario Kicherer raw_disable_filters(net, dev, sk, ro->filter, 2608e8cda6dSMario Kicherer ro->count); 261c18ce101SOliver Hartkopp } 262c18ce101SOliver Hartkopp 263c18ce101SOliver Hartkopp return err; 264c18ce101SOliver Hartkopp } 265c18ce101SOliver Hartkopp 266c18ce101SOliver Hartkopp static int raw_notifier(struct notifier_block *nb, 267351638e7SJiri Pirko unsigned long msg, void *ptr) 268c18ce101SOliver Hartkopp { 269351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 270c18ce101SOliver Hartkopp struct raw_sock *ro = container_of(nb, struct raw_sock, notifier); 271c18ce101SOliver Hartkopp struct sock *sk = &ro->sk; 272c18ce101SOliver Hartkopp 2738e8cda6dSMario Kicherer if (!net_eq(dev_net(dev), sock_net(sk))) 274c18ce101SOliver Hartkopp return NOTIFY_DONE; 275c18ce101SOliver Hartkopp 276c18ce101SOliver Hartkopp if (dev->type != ARPHRD_CAN) 277c18ce101SOliver Hartkopp return NOTIFY_DONE; 278c18ce101SOliver Hartkopp 279c18ce101SOliver Hartkopp if (ro->ifindex != dev->ifindex) 280c18ce101SOliver Hartkopp return NOTIFY_DONE; 281c18ce101SOliver Hartkopp 282c18ce101SOliver Hartkopp switch (msg) { 283c18ce101SOliver Hartkopp case NETDEV_UNREGISTER: 284c18ce101SOliver Hartkopp lock_sock(sk); 285c18ce101SOliver Hartkopp /* remove current filters & unregister */ 286c18ce101SOliver Hartkopp if (ro->bound) 2878e8cda6dSMario Kicherer raw_disable_allfilters(dev_net(dev), dev, sk); 288c18ce101SOliver Hartkopp 289c18ce101SOliver Hartkopp if (ro->count > 1) 290c18ce101SOliver Hartkopp kfree(ro->filter); 291c18ce101SOliver Hartkopp 292c18ce101SOliver Hartkopp ro->ifindex = 0; 293c18ce101SOliver Hartkopp ro->bound = 0; 294c18ce101SOliver Hartkopp ro->count = 0; 295c18ce101SOliver Hartkopp release_sock(sk); 296c18ce101SOliver Hartkopp 297c18ce101SOliver Hartkopp sk->sk_err = ENODEV; 298c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 299c18ce101SOliver Hartkopp sk->sk_error_report(sk); 300c18ce101SOliver Hartkopp break; 301c18ce101SOliver Hartkopp 302c18ce101SOliver Hartkopp case NETDEV_DOWN: 303c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN; 304c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 305c18ce101SOliver Hartkopp sk->sk_error_report(sk); 306c18ce101SOliver Hartkopp break; 307c18ce101SOliver Hartkopp } 308c18ce101SOliver Hartkopp 309c18ce101SOliver Hartkopp return NOTIFY_DONE; 310c18ce101SOliver Hartkopp } 311c18ce101SOliver Hartkopp 312c18ce101SOliver Hartkopp static int raw_init(struct sock *sk) 313c18ce101SOliver Hartkopp { 314c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 315c18ce101SOliver Hartkopp 316c18ce101SOliver Hartkopp ro->bound = 0; 317c18ce101SOliver Hartkopp ro->ifindex = 0; 318c18ce101SOliver Hartkopp 319c18ce101SOliver Hartkopp /* set default filter to single entry dfilter */ 320c18ce101SOliver Hartkopp ro->dfilter.can_id = 0; 321c18ce101SOliver Hartkopp ro->dfilter.can_mask = MASK_ALL; 322c18ce101SOliver Hartkopp ro->filter = &ro->dfilter; 323c18ce101SOliver Hartkopp ro->count = 1; 324c18ce101SOliver Hartkopp 325c18ce101SOliver Hartkopp /* set default loopback behaviour */ 326c18ce101SOliver Hartkopp ro->loopback = 1; 327c18ce101SOliver Hartkopp ro->recv_own_msgs = 0; 328e2d265d3SOliver Hartkopp ro->fd_frames = 0; 329a5581ef4SOliver Hartkopp ro->join_filters = 0; 330c18ce101SOliver Hartkopp 331514ac99cSOliver Hartkopp /* alloc_percpu provides zero'ed memory */ 332514ac99cSOliver Hartkopp ro->uniq = alloc_percpu(struct uniqframe); 333514ac99cSOliver Hartkopp if (unlikely(!ro->uniq)) 334514ac99cSOliver Hartkopp return -ENOMEM; 335514ac99cSOliver Hartkopp 336c18ce101SOliver Hartkopp /* set notifier */ 337c18ce101SOliver Hartkopp ro->notifier.notifier_call = raw_notifier; 338c18ce101SOliver Hartkopp 339c18ce101SOliver Hartkopp register_netdevice_notifier(&ro->notifier); 340c18ce101SOliver Hartkopp 341c18ce101SOliver Hartkopp return 0; 342c18ce101SOliver Hartkopp } 343c18ce101SOliver Hartkopp 344c18ce101SOliver Hartkopp static int raw_release(struct socket *sock) 345c18ce101SOliver Hartkopp { 346c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 34710022a6cSOliver Hartkopp struct raw_sock *ro; 34810022a6cSOliver Hartkopp 34910022a6cSOliver Hartkopp if (!sk) 35010022a6cSOliver Hartkopp return 0; 35110022a6cSOliver Hartkopp 35210022a6cSOliver Hartkopp ro = raw_sk(sk); 353c18ce101SOliver Hartkopp 354c18ce101SOliver Hartkopp unregister_netdevice_notifier(&ro->notifier); 355c18ce101SOliver Hartkopp 356c18ce101SOliver Hartkopp lock_sock(sk); 357c18ce101SOliver Hartkopp 358c18ce101SOliver Hartkopp /* remove current filters & unregister */ 359c18ce101SOliver Hartkopp if (ro->bound) { 360c18ce101SOliver Hartkopp if (ro->ifindex) { 361c18ce101SOliver Hartkopp struct net_device *dev; 362c18ce101SOliver Hartkopp 3638e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), ro->ifindex); 364c18ce101SOliver Hartkopp if (dev) { 3658e8cda6dSMario Kicherer raw_disable_allfilters(dev_net(dev), dev, sk); 366c18ce101SOliver Hartkopp dev_put(dev); 367c18ce101SOliver Hartkopp } 368bff10040SMarc Kleine-Budde } else { 3698e8cda6dSMario Kicherer raw_disable_allfilters(sock_net(sk), NULL, sk); 370c18ce101SOliver Hartkopp } 371bff10040SMarc Kleine-Budde } 372c18ce101SOliver Hartkopp 373c18ce101SOliver Hartkopp if (ro->count > 1) 374c18ce101SOliver Hartkopp kfree(ro->filter); 375c18ce101SOliver Hartkopp 376c18ce101SOliver Hartkopp ro->ifindex = 0; 377c18ce101SOliver Hartkopp ro->bound = 0; 378c18ce101SOliver Hartkopp ro->count = 0; 379514ac99cSOliver Hartkopp free_percpu(ro->uniq); 380c18ce101SOliver Hartkopp 381f7e5cc0cSLothar Waßmann sock_orphan(sk); 382f7e5cc0cSLothar Waßmann sock->sk = NULL; 383f7e5cc0cSLothar Waßmann 384c18ce101SOliver Hartkopp release_sock(sk); 385c18ce101SOliver Hartkopp sock_put(sk); 386c18ce101SOliver Hartkopp 387c18ce101SOliver Hartkopp return 0; 388c18ce101SOliver Hartkopp } 389c18ce101SOliver Hartkopp 390c18ce101SOliver Hartkopp static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) 391c18ce101SOliver Hartkopp { 392c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 393c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 394c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 395c18ce101SOliver Hartkopp int ifindex; 396c18ce101SOliver Hartkopp int err = 0; 397c18ce101SOliver Hartkopp int notify_enetdown = 0; 398c18ce101SOliver Hartkopp 399c18ce101SOliver Hartkopp if (len < sizeof(*addr)) 400c18ce101SOliver Hartkopp return -EINVAL; 401adb552c3SMarc Kleine-Budde if (addr->can_family != AF_CAN) 402adb552c3SMarc Kleine-Budde return -EINVAL; 403c18ce101SOliver Hartkopp 404c18ce101SOliver Hartkopp lock_sock(sk); 405c18ce101SOliver Hartkopp 406c18ce101SOliver Hartkopp if (ro->bound && addr->can_ifindex == ro->ifindex) 407c18ce101SOliver Hartkopp goto out; 408c18ce101SOliver Hartkopp 409c18ce101SOliver Hartkopp if (addr->can_ifindex) { 410c18ce101SOliver Hartkopp struct net_device *dev; 411c18ce101SOliver Hartkopp 4128e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), addr->can_ifindex); 413c18ce101SOliver Hartkopp if (!dev) { 414c18ce101SOliver Hartkopp err = -ENODEV; 415c18ce101SOliver Hartkopp goto out; 416c18ce101SOliver Hartkopp } 417c18ce101SOliver Hartkopp if (dev->type != ARPHRD_CAN) { 418c18ce101SOliver Hartkopp dev_put(dev); 419c18ce101SOliver Hartkopp err = -ENODEV; 420c18ce101SOliver Hartkopp goto out; 421c18ce101SOliver Hartkopp } 422c18ce101SOliver Hartkopp if (!(dev->flags & IFF_UP)) 423c18ce101SOliver Hartkopp notify_enetdown = 1; 424c18ce101SOliver Hartkopp 425c18ce101SOliver Hartkopp ifindex = dev->ifindex; 426c18ce101SOliver Hartkopp 427c18ce101SOliver Hartkopp /* filters set by default/setsockopt */ 4288e8cda6dSMario Kicherer err = raw_enable_allfilters(sock_net(sk), dev, sk); 429c18ce101SOliver Hartkopp dev_put(dev); 430c18ce101SOliver Hartkopp } else { 431c18ce101SOliver Hartkopp ifindex = 0; 432c18ce101SOliver Hartkopp 433c18ce101SOliver Hartkopp /* filters set by default/setsockopt */ 4348e8cda6dSMario Kicherer err = raw_enable_allfilters(sock_net(sk), NULL, sk); 435c18ce101SOliver Hartkopp } 436c18ce101SOliver Hartkopp 437c18ce101SOliver Hartkopp if (!err) { 438c18ce101SOliver Hartkopp if (ro->bound) { 439c18ce101SOliver Hartkopp /* unregister old filters */ 440c18ce101SOliver Hartkopp if (ro->ifindex) { 441c18ce101SOliver Hartkopp struct net_device *dev; 442c18ce101SOliver Hartkopp 4438e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), 4448e8cda6dSMario Kicherer ro->ifindex); 445c18ce101SOliver Hartkopp if (dev) { 4468e8cda6dSMario Kicherer raw_disable_allfilters(dev_net(dev), 4478e8cda6dSMario Kicherer dev, sk); 448c18ce101SOliver Hartkopp dev_put(dev); 449c18ce101SOliver Hartkopp } 450bff10040SMarc Kleine-Budde } else { 4518e8cda6dSMario Kicherer raw_disable_allfilters(sock_net(sk), NULL, sk); 452c18ce101SOliver Hartkopp } 453bff10040SMarc Kleine-Budde } 454c18ce101SOliver Hartkopp ro->ifindex = ifindex; 455c18ce101SOliver Hartkopp ro->bound = 1; 456c18ce101SOliver Hartkopp } 457c18ce101SOliver Hartkopp 458c18ce101SOliver Hartkopp out: 459c18ce101SOliver Hartkopp release_sock(sk); 460c18ce101SOliver Hartkopp 461c18ce101SOliver Hartkopp if (notify_enetdown) { 462c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN; 463c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 464c18ce101SOliver Hartkopp sk->sk_error_report(sk); 465c18ce101SOliver Hartkopp } 466c18ce101SOliver Hartkopp 467c18ce101SOliver Hartkopp return err; 468c18ce101SOliver Hartkopp } 469c18ce101SOliver Hartkopp 470c18ce101SOliver Hartkopp static int raw_getname(struct socket *sock, struct sockaddr *uaddr, 4719b2c45d4SDenys Vlasenko int peer) 472c18ce101SOliver Hartkopp { 473c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 474c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 475c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 476c18ce101SOliver Hartkopp 477c18ce101SOliver Hartkopp if (peer) 478c18ce101SOliver Hartkopp return -EOPNOTSUPP; 479c18ce101SOliver Hartkopp 480e84b90aeSEric Dumazet memset(addr, 0, sizeof(*addr)); 481c18ce101SOliver Hartkopp addr->can_family = AF_CAN; 482c18ce101SOliver Hartkopp addr->can_ifindex = ro->ifindex; 483c18ce101SOliver Hartkopp 4849b2c45d4SDenys Vlasenko return sizeof(*addr); 485c18ce101SOliver Hartkopp } 486c18ce101SOliver Hartkopp 487c18ce101SOliver Hartkopp static int raw_setsockopt(struct socket *sock, int level, int optname, 488b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 489c18ce101SOliver Hartkopp { 490c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 491c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 492c18ce101SOliver Hartkopp struct can_filter *filter = NULL; /* dyn. alloc'ed filters */ 493c18ce101SOliver Hartkopp struct can_filter sfilter; /* single filter */ 494c18ce101SOliver Hartkopp struct net_device *dev = NULL; 495c18ce101SOliver Hartkopp can_err_mask_t err_mask = 0; 496c18ce101SOliver Hartkopp int count = 0; 497c18ce101SOliver Hartkopp int err = 0; 498c18ce101SOliver Hartkopp 499c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW) 500c18ce101SOliver Hartkopp return -EINVAL; 501c18ce101SOliver Hartkopp 502c18ce101SOliver Hartkopp switch (optname) { 503c18ce101SOliver Hartkopp case CAN_RAW_FILTER: 504c18ce101SOliver Hartkopp if (optlen % sizeof(struct can_filter) != 0) 505c18ce101SOliver Hartkopp return -EINVAL; 506c18ce101SOliver Hartkopp 507332b05caSMarc Kleine-Budde if (optlen > CAN_RAW_FILTER_MAX * sizeof(struct can_filter)) 508332b05caSMarc Kleine-Budde return -EINVAL; 509332b05caSMarc Kleine-Budde 510c18ce101SOliver Hartkopp count = optlen / sizeof(struct can_filter); 511c18ce101SOliver Hartkopp 512c18ce101SOliver Hartkopp if (count > 1) { 513c18ce101SOliver Hartkopp /* filter does not fit into dfilter => alloc space */ 51416dff918SJulia Lawall filter = memdup_user(optval, optlen); 51516dff918SJulia Lawall if (IS_ERR(filter)) 51616dff918SJulia Lawall return PTR_ERR(filter); 517c18ce101SOliver Hartkopp } else if (count == 1) { 5184ffa8701SEric Dumazet if (copy_from_user(&sfilter, optval, sizeof(sfilter))) 5193f91bd42SSam Ravnborg return -EFAULT; 520c18ce101SOliver Hartkopp } 521c18ce101SOliver Hartkopp 522c18ce101SOliver Hartkopp lock_sock(sk); 523c18ce101SOliver Hartkopp 524c18ce101SOliver Hartkopp if (ro->bound && ro->ifindex) 5258e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), ro->ifindex); 526c18ce101SOliver Hartkopp 527c18ce101SOliver Hartkopp if (ro->bound) { 528c18ce101SOliver Hartkopp /* (try to) register the new filters */ 529c18ce101SOliver Hartkopp if (count == 1) 5308e8cda6dSMario Kicherer err = raw_enable_filters(sock_net(sk), dev, sk, 5318e8cda6dSMario Kicherer &sfilter, 1); 532c18ce101SOliver Hartkopp else 5338e8cda6dSMario Kicherer err = raw_enable_filters(sock_net(sk), dev, sk, 5348e8cda6dSMario Kicherer filter, count); 535c18ce101SOliver Hartkopp if (err) { 536c18ce101SOliver Hartkopp if (count > 1) 537c18ce101SOliver Hartkopp kfree(filter); 538c18ce101SOliver Hartkopp goto out_fil; 539c18ce101SOliver Hartkopp } 540c18ce101SOliver Hartkopp 541c18ce101SOliver Hartkopp /* remove old filter registrations */ 5428e8cda6dSMario Kicherer raw_disable_filters(sock_net(sk), dev, sk, ro->filter, 5438e8cda6dSMario Kicherer ro->count); 544c18ce101SOliver Hartkopp } 545c18ce101SOliver Hartkopp 546c18ce101SOliver Hartkopp /* remove old filter space */ 547c18ce101SOliver Hartkopp if (ro->count > 1) 548c18ce101SOliver Hartkopp kfree(ro->filter); 549c18ce101SOliver Hartkopp 550c18ce101SOliver Hartkopp /* link new filters to the socket */ 551c18ce101SOliver Hartkopp if (count == 1) { 552c18ce101SOliver Hartkopp /* copy filter data for single filter */ 553c18ce101SOliver Hartkopp ro->dfilter = sfilter; 554c18ce101SOliver Hartkopp filter = &ro->dfilter; 555c18ce101SOliver Hartkopp } 556c18ce101SOliver Hartkopp ro->filter = filter; 557c18ce101SOliver Hartkopp ro->count = count; 558c18ce101SOliver Hartkopp 559c18ce101SOliver Hartkopp out_fil: 560c18ce101SOliver Hartkopp if (dev) 561c18ce101SOliver Hartkopp dev_put(dev); 562c18ce101SOliver Hartkopp 563c18ce101SOliver Hartkopp release_sock(sk); 564c18ce101SOliver Hartkopp 565c18ce101SOliver Hartkopp break; 566c18ce101SOliver Hartkopp 567c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER: 568c18ce101SOliver Hartkopp if (optlen != sizeof(err_mask)) 569c18ce101SOliver Hartkopp return -EINVAL; 570c18ce101SOliver Hartkopp 5713f91bd42SSam Ravnborg if (copy_from_user(&err_mask, optval, optlen)) 5723f91bd42SSam Ravnborg return -EFAULT; 573c18ce101SOliver Hartkopp 574c18ce101SOliver Hartkopp err_mask &= CAN_ERR_MASK; 575c18ce101SOliver Hartkopp 576c18ce101SOliver Hartkopp lock_sock(sk); 577c18ce101SOliver Hartkopp 578c18ce101SOliver Hartkopp if (ro->bound && ro->ifindex) 5798e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), ro->ifindex); 580c18ce101SOliver Hartkopp 581c18ce101SOliver Hartkopp /* remove current error mask */ 582c18ce101SOliver Hartkopp if (ro->bound) { 583c18ce101SOliver Hartkopp /* (try to) register the new err_mask */ 5848e8cda6dSMario Kicherer err = raw_enable_errfilter(sock_net(sk), dev, sk, 5858e8cda6dSMario Kicherer err_mask); 586c18ce101SOliver Hartkopp 587c18ce101SOliver Hartkopp if (err) 588c18ce101SOliver Hartkopp goto out_err; 589c18ce101SOliver Hartkopp 590c18ce101SOliver Hartkopp /* remove old err_mask registration */ 5918e8cda6dSMario Kicherer raw_disable_errfilter(sock_net(sk), dev, sk, 5928e8cda6dSMario Kicherer ro->err_mask); 593c18ce101SOliver Hartkopp } 594c18ce101SOliver Hartkopp 595c18ce101SOliver Hartkopp /* link new err_mask to the socket */ 596c18ce101SOliver Hartkopp ro->err_mask = err_mask; 597c18ce101SOliver Hartkopp 598c18ce101SOliver Hartkopp out_err: 599c18ce101SOliver Hartkopp if (dev) 600c18ce101SOliver Hartkopp dev_put(dev); 601c18ce101SOliver Hartkopp 602c18ce101SOliver Hartkopp release_sock(sk); 603c18ce101SOliver Hartkopp 604c18ce101SOliver Hartkopp break; 605c18ce101SOliver Hartkopp 606c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK: 607c18ce101SOliver Hartkopp if (optlen != sizeof(ro->loopback)) 608c18ce101SOliver Hartkopp return -EINVAL; 609c18ce101SOliver Hartkopp 6103f91bd42SSam Ravnborg if (copy_from_user(&ro->loopback, optval, optlen)) 6113f91bd42SSam Ravnborg return -EFAULT; 612c18ce101SOliver Hartkopp 613c18ce101SOliver Hartkopp break; 614c18ce101SOliver Hartkopp 615c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS: 616c18ce101SOliver Hartkopp if (optlen != sizeof(ro->recv_own_msgs)) 617c18ce101SOliver Hartkopp return -EINVAL; 618c18ce101SOliver Hartkopp 6193f91bd42SSam Ravnborg if (copy_from_user(&ro->recv_own_msgs, optval, optlen)) 6203f91bd42SSam Ravnborg return -EFAULT; 621c18ce101SOliver Hartkopp 622c18ce101SOliver Hartkopp break; 623c18ce101SOliver Hartkopp 624e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES: 625e2d265d3SOliver Hartkopp if (optlen != sizeof(ro->fd_frames)) 626e2d265d3SOliver Hartkopp return -EINVAL; 627e2d265d3SOliver Hartkopp 628e2d265d3SOliver Hartkopp if (copy_from_user(&ro->fd_frames, optval, optlen)) 629e2d265d3SOliver Hartkopp return -EFAULT; 630e2d265d3SOliver Hartkopp 631e2d265d3SOliver Hartkopp break; 632e2d265d3SOliver Hartkopp 633a5581ef4SOliver Hartkopp case CAN_RAW_JOIN_FILTERS: 634a5581ef4SOliver Hartkopp if (optlen != sizeof(ro->join_filters)) 635a5581ef4SOliver Hartkopp return -EINVAL; 636a5581ef4SOliver Hartkopp 637a5581ef4SOliver Hartkopp if (copy_from_user(&ro->join_filters, optval, optlen)) 638a5581ef4SOliver Hartkopp return -EFAULT; 639a5581ef4SOliver Hartkopp 640a5581ef4SOliver Hartkopp break; 641a5581ef4SOliver Hartkopp 642c18ce101SOliver Hartkopp default: 643c18ce101SOliver Hartkopp return -ENOPROTOOPT; 644c18ce101SOliver Hartkopp } 645c18ce101SOliver Hartkopp return err; 646c18ce101SOliver Hartkopp } 647c18ce101SOliver Hartkopp 648c18ce101SOliver Hartkopp static int raw_getsockopt(struct socket *sock, int level, int optname, 649c18ce101SOliver Hartkopp char __user *optval, int __user *optlen) 650c18ce101SOliver Hartkopp { 651c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 652c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 653c18ce101SOliver Hartkopp int len; 654c18ce101SOliver Hartkopp void *val; 655c18ce101SOliver Hartkopp int err = 0; 656c18ce101SOliver Hartkopp 657c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW) 658c18ce101SOliver Hartkopp return -EINVAL; 659c18ce101SOliver Hartkopp if (get_user(len, optlen)) 660c18ce101SOliver Hartkopp return -EFAULT; 661c18ce101SOliver Hartkopp if (len < 0) 662c18ce101SOliver Hartkopp return -EINVAL; 663c18ce101SOliver Hartkopp 664c18ce101SOliver Hartkopp switch (optname) { 665c18ce101SOliver Hartkopp case CAN_RAW_FILTER: 666c18ce101SOliver Hartkopp lock_sock(sk); 667c18ce101SOliver Hartkopp if (ro->count > 0) { 668c18ce101SOliver Hartkopp int fsize = ro->count * sizeof(struct can_filter); 669d5e4ecacSMarc Kleine-Budde 670c18ce101SOliver Hartkopp if (len > fsize) 671c18ce101SOliver Hartkopp len = fsize; 672653252c2SPavel Emelyanov if (copy_to_user(optval, ro->filter, len)) 673653252c2SPavel Emelyanov err = -EFAULT; 674bff10040SMarc Kleine-Budde } else { 675c18ce101SOliver Hartkopp len = 0; 676bff10040SMarc Kleine-Budde } 677c18ce101SOliver Hartkopp release_sock(sk); 678c18ce101SOliver Hartkopp 679c18ce101SOliver Hartkopp if (!err) 680c18ce101SOliver Hartkopp err = put_user(len, optlen); 681c18ce101SOliver Hartkopp return err; 682c18ce101SOliver Hartkopp 683c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER: 684c18ce101SOliver Hartkopp if (len > sizeof(can_err_mask_t)) 685c18ce101SOliver Hartkopp len = sizeof(can_err_mask_t); 686c18ce101SOliver Hartkopp val = &ro->err_mask; 687c18ce101SOliver Hartkopp break; 688c18ce101SOliver Hartkopp 689c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK: 690c18ce101SOliver Hartkopp if (len > sizeof(int)) 691c18ce101SOliver Hartkopp len = sizeof(int); 692c18ce101SOliver Hartkopp val = &ro->loopback; 693c18ce101SOliver Hartkopp break; 694c18ce101SOliver Hartkopp 695c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS: 696c18ce101SOliver Hartkopp if (len > sizeof(int)) 697c18ce101SOliver Hartkopp len = sizeof(int); 698c18ce101SOliver Hartkopp val = &ro->recv_own_msgs; 699c18ce101SOliver Hartkopp break; 700c18ce101SOliver Hartkopp 701e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES: 702e2d265d3SOliver Hartkopp if (len > sizeof(int)) 703e2d265d3SOliver Hartkopp len = sizeof(int); 704e2d265d3SOliver Hartkopp val = &ro->fd_frames; 705e2d265d3SOliver Hartkopp break; 706e2d265d3SOliver Hartkopp 707a5581ef4SOliver Hartkopp case CAN_RAW_JOIN_FILTERS: 708a5581ef4SOliver Hartkopp if (len > sizeof(int)) 709a5581ef4SOliver Hartkopp len = sizeof(int); 710a5581ef4SOliver Hartkopp val = &ro->join_filters; 711a5581ef4SOliver Hartkopp break; 712a5581ef4SOliver Hartkopp 713c18ce101SOliver Hartkopp default: 714c18ce101SOliver Hartkopp return -ENOPROTOOPT; 715c18ce101SOliver Hartkopp } 716c18ce101SOliver Hartkopp 717c18ce101SOliver Hartkopp if (put_user(len, optlen)) 718c18ce101SOliver Hartkopp return -EFAULT; 719c18ce101SOliver Hartkopp if (copy_to_user(optval, val, len)) 720c18ce101SOliver Hartkopp return -EFAULT; 721c18ce101SOliver Hartkopp return 0; 722c18ce101SOliver Hartkopp } 723c18ce101SOliver Hartkopp 7241b784140SYing Xue static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 725c18ce101SOliver Hartkopp { 726c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 727c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 728c18ce101SOliver Hartkopp struct sk_buff *skb; 729c18ce101SOliver Hartkopp struct net_device *dev; 730c18ce101SOliver Hartkopp int ifindex; 731c18ce101SOliver Hartkopp int err; 732c18ce101SOliver Hartkopp 733c18ce101SOliver Hartkopp if (msg->msg_name) { 734342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); 735c18ce101SOliver Hartkopp 7365e507328SKurt Van Dijck if (msg->msg_namelen < sizeof(*addr)) 7375e507328SKurt Van Dijck return -EINVAL; 7385e507328SKurt Van Dijck 739c18ce101SOliver Hartkopp if (addr->can_family != AF_CAN) 740c18ce101SOliver Hartkopp return -EINVAL; 741c18ce101SOliver Hartkopp 742c18ce101SOliver Hartkopp ifindex = addr->can_ifindex; 743bff10040SMarc Kleine-Budde } else { 744c18ce101SOliver Hartkopp ifindex = ro->ifindex; 745bff10040SMarc Kleine-Budde } 746c18ce101SOliver Hartkopp 7478e8cda6dSMario Kicherer dev = dev_get_by_index(sock_net(sk), ifindex); 748c18ce101SOliver Hartkopp if (!dev) 749c18ce101SOliver Hartkopp return -ENXIO; 750c18ce101SOliver Hartkopp 751a43608faSOliver Hartkopp err = -EINVAL; 752a43608faSOliver Hartkopp if (ro->fd_frames && dev->mtu == CANFD_MTU) { 753a43608faSOliver Hartkopp if (unlikely(size != CANFD_MTU && size != CAN_MTU)) 754a43608faSOliver Hartkopp goto put_dev; 755a43608faSOliver Hartkopp } else { 756a43608faSOliver Hartkopp if (unlikely(size != CAN_MTU)) 757a43608faSOliver Hartkopp goto put_dev; 758a43608faSOliver Hartkopp } 759a43608faSOliver Hartkopp 760156c2bb9SOliver Hartkopp skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv), 761156c2bb9SOliver Hartkopp msg->msg_flags & MSG_DONTWAIT, &err); 762ebad5c09SIlpo Järvinen if (!skb) 763ebad5c09SIlpo Järvinen goto put_dev; 764c18ce101SOliver Hartkopp 7652bf3440dSOliver Hartkopp can_skb_reserve(skb); 7662bf3440dSOliver Hartkopp can_skb_prv(skb)->ifindex = dev->ifindex; 767d3b58c47SOliver Hartkopp can_skb_prv(skb)->skbcnt = 0; 768156c2bb9SOliver Hartkopp 7696ce8e9ceSAl Viro err = memcpy_from_msg(skb_put(skb, size), msg, size); 770ebad5c09SIlpo Järvinen if (err < 0) 771ebad5c09SIlpo Järvinen goto free_skb; 772bf84a010SDaniel Borkmann 7738f932f76SWillem de Bruijn skb_setup_tx_timestamp(skb, sk->sk_tsflags); 774cff0d6e6SOliver Hartkopp 775c18ce101SOliver Hartkopp skb->dev = dev; 776c18ce101SOliver Hartkopp skb->sk = sk; 777bb5ecb0cSRostislav Lisovy skb->priority = sk->sk_priority; 778c18ce101SOliver Hartkopp 779c18ce101SOliver Hartkopp err = can_send(skb, ro->loopback); 780c18ce101SOliver Hartkopp 781c18ce101SOliver Hartkopp dev_put(dev); 782c18ce101SOliver Hartkopp 783c18ce101SOliver Hartkopp if (err) 784ebad5c09SIlpo Järvinen goto send_failed; 785c18ce101SOliver Hartkopp 786c18ce101SOliver Hartkopp return size; 787ebad5c09SIlpo Järvinen 788ebad5c09SIlpo Järvinen free_skb: 789ebad5c09SIlpo Järvinen kfree_skb(skb); 790ebad5c09SIlpo Järvinen put_dev: 791ebad5c09SIlpo Järvinen dev_put(dev); 792ebad5c09SIlpo Järvinen send_failed: 793ebad5c09SIlpo Järvinen return err; 794c18ce101SOliver Hartkopp } 795c18ce101SOliver Hartkopp 7961b784140SYing Xue static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 7971b784140SYing Xue int flags) 798c18ce101SOliver Hartkopp { 799c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 800c18ce101SOliver Hartkopp struct sk_buff *skb; 801a219994bSUrs Thuermann int err = 0; 802c18ce101SOliver Hartkopp int noblock; 803c18ce101SOliver Hartkopp 804c18ce101SOliver Hartkopp noblock = flags & MSG_DONTWAIT; 805c18ce101SOliver Hartkopp flags &= ~MSG_DONTWAIT; 806c18ce101SOliver Hartkopp 807a219994bSUrs Thuermann skb = skb_recv_datagram(sk, flags, noblock, &err); 808c18ce101SOliver Hartkopp if (!skb) 809a219994bSUrs Thuermann return err; 810c18ce101SOliver Hartkopp 811821047c4SOliver Hartkopp if (size < skb->len) 812c18ce101SOliver Hartkopp msg->msg_flags |= MSG_TRUNC; 813c18ce101SOliver Hartkopp else 814821047c4SOliver Hartkopp size = skb->len; 815c18ce101SOliver Hartkopp 8167eab8d9eSAl Viro err = memcpy_to_msg(msg, skb->data, size); 817a219994bSUrs Thuermann if (err < 0) { 818c18ce101SOliver Hartkopp skb_free_datagram(sk, skb); 819a219994bSUrs Thuermann return err; 820c18ce101SOliver Hartkopp } 821c18ce101SOliver Hartkopp 8223b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 823c18ce101SOliver Hartkopp 824c18ce101SOliver Hartkopp if (msg->msg_name) { 825342dfc30SSteffen Hurrle __sockaddr_check_size(sizeof(struct sockaddr_can)); 826c18ce101SOliver Hartkopp msg->msg_namelen = sizeof(struct sockaddr_can); 827c18ce101SOliver Hartkopp memcpy(msg->msg_name, skb->cb, msg->msg_namelen); 828c18ce101SOliver Hartkopp } 829c18ce101SOliver Hartkopp 8301e55659cSOliver Hartkopp /* assign the flags that have been recorded in raw_rcv() */ 8311e55659cSOliver Hartkopp msg->msg_flags |= *(raw_flags(skb)); 8321e55659cSOliver Hartkopp 833c18ce101SOliver Hartkopp skb_free_datagram(sk, skb); 834c18ce101SOliver Hartkopp 835c18ce101SOliver Hartkopp return size; 836c18ce101SOliver Hartkopp } 837c18ce101SOliver Hartkopp 838473d924dSOliver Hartkopp int raw_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd, 839473d924dSOliver Hartkopp unsigned long arg) 840473d924dSOliver Hartkopp { 841473d924dSOliver Hartkopp /* no ioctls for socket layer -> hand it down to NIC layer */ 842473d924dSOliver Hartkopp return -ENOIOCTLCMD; 843473d924dSOliver Hartkopp } 844473d924dSOliver Hartkopp 84553914b67SOliver Hartkopp static const struct proto_ops raw_ops = { 846c18ce101SOliver Hartkopp .family = PF_CAN, 847c18ce101SOliver Hartkopp .release = raw_release, 848c18ce101SOliver Hartkopp .bind = raw_bind, 849c18ce101SOliver Hartkopp .connect = sock_no_connect, 850c18ce101SOliver Hartkopp .socketpair = sock_no_socketpair, 851c18ce101SOliver Hartkopp .accept = sock_no_accept, 852c18ce101SOliver Hartkopp .getname = raw_getname, 853a11e1d43SLinus Torvalds .poll = datagram_poll, 854473d924dSOliver Hartkopp .ioctl = raw_sock_no_ioctlcmd, 855c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 856c18ce101SOliver Hartkopp .listen = sock_no_listen, 857c18ce101SOliver Hartkopp .shutdown = sock_no_shutdown, 858c18ce101SOliver Hartkopp .setsockopt = raw_setsockopt, 859c18ce101SOliver Hartkopp .getsockopt = raw_getsockopt, 860c18ce101SOliver Hartkopp .sendmsg = raw_sendmsg, 861c18ce101SOliver Hartkopp .recvmsg = raw_recvmsg, 862c18ce101SOliver Hartkopp .mmap = sock_no_mmap, 863c18ce101SOliver Hartkopp .sendpage = sock_no_sendpage, 864c18ce101SOliver Hartkopp }; 865c18ce101SOliver Hartkopp 866c18ce101SOliver Hartkopp static struct proto raw_proto __read_mostly = { 867c18ce101SOliver Hartkopp .name = "CAN_RAW", 868c18ce101SOliver Hartkopp .owner = THIS_MODULE, 869c18ce101SOliver Hartkopp .obj_size = sizeof(struct raw_sock), 870c18ce101SOliver Hartkopp .init = raw_init, 871c18ce101SOliver Hartkopp }; 872c18ce101SOliver Hartkopp 8731650629dSKurt Van Dijck static const struct can_proto raw_can_proto = { 874c18ce101SOliver Hartkopp .type = SOCK_RAW, 875c18ce101SOliver Hartkopp .protocol = CAN_RAW, 876c18ce101SOliver Hartkopp .ops = &raw_ops, 877c18ce101SOliver Hartkopp .prot = &raw_proto, 878c18ce101SOliver Hartkopp }; 879c18ce101SOliver Hartkopp 880c18ce101SOliver Hartkopp static __init int raw_module_init(void) 881c18ce101SOliver Hartkopp { 882c18ce101SOliver Hartkopp int err; 883c18ce101SOliver Hartkopp 884b111b78cSJeremiah Mahler pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n"); 885c18ce101SOliver Hartkopp 886c18ce101SOliver Hartkopp err = can_proto_register(&raw_can_proto); 887c18ce101SOliver Hartkopp if (err < 0) 888d956b1a8SMarc Kleine-Budde pr_err("can: registration of raw protocol failed\n"); 889c18ce101SOliver Hartkopp 890c18ce101SOliver Hartkopp return err; 891c18ce101SOliver Hartkopp } 892c18ce101SOliver Hartkopp 893c18ce101SOliver Hartkopp static __exit void raw_module_exit(void) 894c18ce101SOliver Hartkopp { 895c18ce101SOliver Hartkopp can_proto_unregister(&raw_can_proto); 896c18ce101SOliver Hartkopp } 897c18ce101SOliver Hartkopp 898c18ce101SOliver Hartkopp module_init(raw_module_init); 899c18ce101SOliver Hartkopp module_exit(raw_module_exit); 900