1c18ce101SOliver Hartkopp /* 2c18ce101SOliver Hartkopp * 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 67c18ce101SOliver Hartkopp /* 68c18ce101SOliver Hartkopp * A raw socket has a list of can_filters attached to it, each receiving 69c18ce101SOliver Hartkopp * the CAN frames matching that filter. If the filter list is empty, 70c18ce101SOliver Hartkopp * no CAN frames will be received by the socket. The default after 71c18ce101SOliver Hartkopp * opening the socket, is to have one filter which receives all frames. 72c18ce101SOliver Hartkopp * The filter list is allocated dynamically with the exception of the 73c18ce101SOliver Hartkopp * list containing only one item. This common case is optimized by 74c18ce101SOliver Hartkopp * storing the single filter in dfilter, to avoid using dynamic memory. 75c18ce101SOliver Hartkopp */ 76c18ce101SOliver Hartkopp 77514ac99cSOliver Hartkopp struct uniqframe { 78514ac99cSOliver Hartkopp ktime_t tstamp; 79514ac99cSOliver Hartkopp const struct sk_buff *skb; 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; 90c18ce101SOliver Hartkopp int count; /* number of active filters */ 91c18ce101SOliver Hartkopp struct can_filter dfilter; /* default/single filter */ 92c18ce101SOliver Hartkopp struct can_filter *filter; /* pointer to filter(s) */ 93c18ce101SOliver Hartkopp can_err_mask_t err_mask; 94514ac99cSOliver Hartkopp struct uniqframe __percpu *uniq; 95c18ce101SOliver Hartkopp }; 96c18ce101SOliver Hartkopp 971e55659cSOliver Hartkopp /* 981e55659cSOliver Hartkopp * 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 && 134514ac99cSOliver Hartkopp ktime_equal(this_cpu_ptr(ro->uniq)->tstamp, oskb->tstamp)) { 135514ac99cSOliver Hartkopp return; 136514ac99cSOliver Hartkopp } else { 137514ac99cSOliver Hartkopp this_cpu_ptr(ro->uniq)->skb = oskb; 138514ac99cSOliver Hartkopp this_cpu_ptr(ro->uniq)->tstamp = oskb->tstamp; 139514ac99cSOliver Hartkopp } 140514ac99cSOliver Hartkopp 1411fa17d4bSOliver Hartkopp /* clone the given skb to be able to enqueue it into the rcv queue */ 1421e55659cSOliver Hartkopp skb = skb_clone(oskb, GFP_ATOMIC); 1431fa17d4bSOliver Hartkopp if (!skb) 1441fa17d4bSOliver Hartkopp return; 145c18ce101SOliver Hartkopp 146c18ce101SOliver Hartkopp /* 147c18ce101SOliver Hartkopp * Put the datagram to the queue so that raw_recvmsg() can 148c18ce101SOliver Hartkopp * get it from there. We need to pass the interface index to 149c18ce101SOliver Hartkopp * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb 150c18ce101SOliver Hartkopp * containing the interface index. 151c18ce101SOliver Hartkopp */ 152c18ce101SOliver Hartkopp 153b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sockaddr_can)); 154c18ce101SOliver Hartkopp addr = (struct sockaddr_can *)skb->cb; 155c18ce101SOliver Hartkopp memset(addr, 0, sizeof(*addr)); 156c18ce101SOliver Hartkopp addr->can_family = AF_CAN; 157c18ce101SOliver Hartkopp addr->can_ifindex = skb->dev->ifindex; 158c18ce101SOliver Hartkopp 1591e55659cSOliver Hartkopp /* add CAN specific message flags for raw_recvmsg() */ 1601e55659cSOliver Hartkopp pflags = raw_flags(skb); 1611e55659cSOliver Hartkopp *pflags = 0; 1621e55659cSOliver Hartkopp if (oskb->sk) 1631e55659cSOliver Hartkopp *pflags |= MSG_DONTROUTE; 1641e55659cSOliver Hartkopp if (oskb->sk == sk) 1651e55659cSOliver Hartkopp *pflags |= MSG_CONFIRM; 1661e55659cSOliver Hartkopp 167a219994bSUrs Thuermann if (sock_queue_rcv_skb(sk, skb) < 0) 168c18ce101SOliver Hartkopp kfree_skb(skb); 169c18ce101SOliver Hartkopp } 170c18ce101SOliver Hartkopp 171c18ce101SOliver Hartkopp static int raw_enable_filters(struct net_device *dev, struct sock *sk, 172a219994bSUrs Thuermann struct can_filter *filter, int count) 173c18ce101SOliver Hartkopp { 174c18ce101SOliver Hartkopp int err = 0; 175c18ce101SOliver Hartkopp int i; 176c18ce101SOliver Hartkopp 177c18ce101SOliver Hartkopp for (i = 0; i < count; i++) { 178c18ce101SOliver Hartkopp err = can_rx_register(dev, filter[i].can_id, 179c18ce101SOliver Hartkopp filter[i].can_mask, 180c18ce101SOliver Hartkopp raw_rcv, sk, "raw"); 181c18ce101SOliver Hartkopp if (err) { 182c18ce101SOliver Hartkopp /* clean up successfully registered filters */ 183c18ce101SOliver Hartkopp while (--i >= 0) 184c18ce101SOliver Hartkopp can_rx_unregister(dev, filter[i].can_id, 185c18ce101SOliver Hartkopp filter[i].can_mask, 186c18ce101SOliver Hartkopp raw_rcv, sk); 187c18ce101SOliver Hartkopp break; 188c18ce101SOliver Hartkopp } 189c18ce101SOliver Hartkopp } 190c18ce101SOliver Hartkopp 191c18ce101SOliver Hartkopp return err; 192c18ce101SOliver Hartkopp } 193c18ce101SOliver Hartkopp 194c18ce101SOliver Hartkopp static int raw_enable_errfilter(struct net_device *dev, struct sock *sk, 195c18ce101SOliver Hartkopp can_err_mask_t err_mask) 196c18ce101SOliver Hartkopp { 197c18ce101SOliver Hartkopp int err = 0; 198c18ce101SOliver Hartkopp 199c18ce101SOliver Hartkopp if (err_mask) 200c18ce101SOliver Hartkopp err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG, 201c18ce101SOliver Hartkopp raw_rcv, sk, "raw"); 202c18ce101SOliver Hartkopp 203c18ce101SOliver Hartkopp return err; 204c18ce101SOliver Hartkopp } 205c18ce101SOliver Hartkopp 206c18ce101SOliver Hartkopp static void raw_disable_filters(struct net_device *dev, struct sock *sk, 207a219994bSUrs Thuermann struct can_filter *filter, int count) 208c18ce101SOliver Hartkopp { 209c18ce101SOliver Hartkopp int i; 210c18ce101SOliver Hartkopp 211c18ce101SOliver Hartkopp for (i = 0; i < count; i++) 212c18ce101SOliver Hartkopp can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask, 213c18ce101SOliver Hartkopp raw_rcv, sk); 214c18ce101SOliver Hartkopp } 215c18ce101SOliver Hartkopp 216c18ce101SOliver Hartkopp static inline void raw_disable_errfilter(struct net_device *dev, 217c18ce101SOliver Hartkopp struct sock *sk, 218c18ce101SOliver Hartkopp can_err_mask_t err_mask) 219c18ce101SOliver Hartkopp 220c18ce101SOliver Hartkopp { 221c18ce101SOliver Hartkopp if (err_mask) 222c18ce101SOliver Hartkopp can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG, 223c18ce101SOliver Hartkopp raw_rcv, sk); 224c18ce101SOliver Hartkopp } 225c18ce101SOliver Hartkopp 226c18ce101SOliver Hartkopp static inline void raw_disable_allfilters(struct net_device *dev, 227c18ce101SOliver Hartkopp struct sock *sk) 228c18ce101SOliver Hartkopp { 229c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 230c18ce101SOliver Hartkopp 231c18ce101SOliver Hartkopp raw_disable_filters(dev, sk, ro->filter, ro->count); 232c18ce101SOliver Hartkopp raw_disable_errfilter(dev, sk, ro->err_mask); 233c18ce101SOliver Hartkopp } 234c18ce101SOliver Hartkopp 235c18ce101SOliver Hartkopp static int raw_enable_allfilters(struct net_device *dev, struct sock *sk) 236c18ce101SOliver Hartkopp { 237c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 238c18ce101SOliver Hartkopp int err; 239c18ce101SOliver Hartkopp 240c18ce101SOliver Hartkopp err = raw_enable_filters(dev, sk, ro->filter, ro->count); 241c18ce101SOliver Hartkopp if (!err) { 242c18ce101SOliver Hartkopp err = raw_enable_errfilter(dev, sk, ro->err_mask); 243c18ce101SOliver Hartkopp if (err) 244c18ce101SOliver Hartkopp raw_disable_filters(dev, sk, ro->filter, ro->count); 245c18ce101SOliver Hartkopp } 246c18ce101SOliver Hartkopp 247c18ce101SOliver Hartkopp return err; 248c18ce101SOliver Hartkopp } 249c18ce101SOliver Hartkopp 250c18ce101SOliver Hartkopp static int raw_notifier(struct notifier_block *nb, 251351638e7SJiri Pirko unsigned long msg, void *ptr) 252c18ce101SOliver Hartkopp { 253351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 254c18ce101SOliver Hartkopp struct raw_sock *ro = container_of(nb, struct raw_sock, notifier); 255c18ce101SOliver Hartkopp struct sock *sk = &ro->sk; 256c18ce101SOliver Hartkopp 257721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 258c18ce101SOliver Hartkopp return NOTIFY_DONE; 259c18ce101SOliver Hartkopp 260c18ce101SOliver Hartkopp if (dev->type != ARPHRD_CAN) 261c18ce101SOliver Hartkopp return NOTIFY_DONE; 262c18ce101SOliver Hartkopp 263c18ce101SOliver Hartkopp if (ro->ifindex != dev->ifindex) 264c18ce101SOliver Hartkopp return NOTIFY_DONE; 265c18ce101SOliver Hartkopp 266c18ce101SOliver Hartkopp switch (msg) { 267c18ce101SOliver Hartkopp 268c18ce101SOliver Hartkopp case NETDEV_UNREGISTER: 269c18ce101SOliver Hartkopp lock_sock(sk); 270c18ce101SOliver Hartkopp /* remove current filters & unregister */ 271c18ce101SOliver Hartkopp if (ro->bound) 272c18ce101SOliver Hartkopp raw_disable_allfilters(dev, sk); 273c18ce101SOliver Hartkopp 274c18ce101SOliver Hartkopp if (ro->count > 1) 275c18ce101SOliver Hartkopp kfree(ro->filter); 276c18ce101SOliver Hartkopp 277c18ce101SOliver Hartkopp ro->ifindex = 0; 278c18ce101SOliver Hartkopp ro->bound = 0; 279c18ce101SOliver Hartkopp ro->count = 0; 280c18ce101SOliver Hartkopp release_sock(sk); 281c18ce101SOliver Hartkopp 282c18ce101SOliver Hartkopp sk->sk_err = ENODEV; 283c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 284c18ce101SOliver Hartkopp sk->sk_error_report(sk); 285c18ce101SOliver Hartkopp break; 286c18ce101SOliver Hartkopp 287c18ce101SOliver Hartkopp case NETDEV_DOWN: 288c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN; 289c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 290c18ce101SOliver Hartkopp sk->sk_error_report(sk); 291c18ce101SOliver Hartkopp break; 292c18ce101SOliver Hartkopp } 293c18ce101SOliver Hartkopp 294c18ce101SOliver Hartkopp return NOTIFY_DONE; 295c18ce101SOliver Hartkopp } 296c18ce101SOliver Hartkopp 297c18ce101SOliver Hartkopp static int raw_init(struct sock *sk) 298c18ce101SOliver Hartkopp { 299c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 300c18ce101SOliver Hartkopp 301c18ce101SOliver Hartkopp ro->bound = 0; 302c18ce101SOliver Hartkopp ro->ifindex = 0; 303c18ce101SOliver Hartkopp 304c18ce101SOliver Hartkopp /* set default filter to single entry dfilter */ 305c18ce101SOliver Hartkopp ro->dfilter.can_id = 0; 306c18ce101SOliver Hartkopp ro->dfilter.can_mask = MASK_ALL; 307c18ce101SOliver Hartkopp ro->filter = &ro->dfilter; 308c18ce101SOliver Hartkopp ro->count = 1; 309c18ce101SOliver Hartkopp 310c18ce101SOliver Hartkopp /* set default loopback behaviour */ 311c18ce101SOliver Hartkopp ro->loopback = 1; 312c18ce101SOliver Hartkopp ro->recv_own_msgs = 0; 313e2d265d3SOliver Hartkopp ro->fd_frames = 0; 314c18ce101SOliver Hartkopp 315514ac99cSOliver Hartkopp /* alloc_percpu provides zero'ed memory */ 316514ac99cSOliver Hartkopp ro->uniq = alloc_percpu(struct uniqframe); 317514ac99cSOliver Hartkopp if (unlikely(!ro->uniq)) 318514ac99cSOliver Hartkopp return -ENOMEM; 319514ac99cSOliver Hartkopp 320c18ce101SOliver Hartkopp /* set notifier */ 321c18ce101SOliver Hartkopp ro->notifier.notifier_call = raw_notifier; 322c18ce101SOliver Hartkopp 323c18ce101SOliver Hartkopp register_netdevice_notifier(&ro->notifier); 324c18ce101SOliver Hartkopp 325c18ce101SOliver Hartkopp return 0; 326c18ce101SOliver Hartkopp } 327c18ce101SOliver Hartkopp 328c18ce101SOliver Hartkopp static int raw_release(struct socket *sock) 329c18ce101SOliver Hartkopp { 330c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 33110022a6cSOliver Hartkopp struct raw_sock *ro; 33210022a6cSOliver Hartkopp 33310022a6cSOliver Hartkopp if (!sk) 33410022a6cSOliver Hartkopp return 0; 33510022a6cSOliver Hartkopp 33610022a6cSOliver Hartkopp ro = raw_sk(sk); 337c18ce101SOliver Hartkopp 338c18ce101SOliver Hartkopp unregister_netdevice_notifier(&ro->notifier); 339c18ce101SOliver Hartkopp 340c18ce101SOliver Hartkopp lock_sock(sk); 341c18ce101SOliver Hartkopp 342c18ce101SOliver Hartkopp /* remove current filters & unregister */ 343c18ce101SOliver Hartkopp if (ro->bound) { 344c18ce101SOliver Hartkopp if (ro->ifindex) { 345c18ce101SOliver Hartkopp struct net_device *dev; 346c18ce101SOliver Hartkopp 347c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, ro->ifindex); 348c18ce101SOliver Hartkopp if (dev) { 349c18ce101SOliver Hartkopp raw_disable_allfilters(dev, sk); 350c18ce101SOliver Hartkopp dev_put(dev); 351c18ce101SOliver Hartkopp } 352c18ce101SOliver Hartkopp } else 353c18ce101SOliver Hartkopp raw_disable_allfilters(NULL, sk); 354c18ce101SOliver Hartkopp } 355c18ce101SOliver Hartkopp 356c18ce101SOliver Hartkopp if (ro->count > 1) 357c18ce101SOliver Hartkopp kfree(ro->filter); 358c18ce101SOliver Hartkopp 359c18ce101SOliver Hartkopp ro->ifindex = 0; 360c18ce101SOliver Hartkopp ro->bound = 0; 361c18ce101SOliver Hartkopp ro->count = 0; 362514ac99cSOliver Hartkopp free_percpu(ro->uniq); 363c18ce101SOliver Hartkopp 364f7e5cc0cSLothar Waßmann sock_orphan(sk); 365f7e5cc0cSLothar Waßmann sock->sk = NULL; 366f7e5cc0cSLothar Waßmann 367c18ce101SOliver Hartkopp release_sock(sk); 368c18ce101SOliver Hartkopp sock_put(sk); 369c18ce101SOliver Hartkopp 370c18ce101SOliver Hartkopp return 0; 371c18ce101SOliver Hartkopp } 372c18ce101SOliver Hartkopp 373c18ce101SOliver Hartkopp static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) 374c18ce101SOliver Hartkopp { 375c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 376c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 377c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 378c18ce101SOliver Hartkopp int ifindex; 379c18ce101SOliver Hartkopp int err = 0; 380c18ce101SOliver Hartkopp int notify_enetdown = 0; 381c18ce101SOliver Hartkopp 382c18ce101SOliver Hartkopp if (len < sizeof(*addr)) 383c18ce101SOliver Hartkopp return -EINVAL; 384c18ce101SOliver Hartkopp 385c18ce101SOliver Hartkopp lock_sock(sk); 386c18ce101SOliver Hartkopp 387c18ce101SOliver Hartkopp if (ro->bound && addr->can_ifindex == ro->ifindex) 388c18ce101SOliver Hartkopp goto out; 389c18ce101SOliver Hartkopp 390c18ce101SOliver Hartkopp if (addr->can_ifindex) { 391c18ce101SOliver Hartkopp struct net_device *dev; 392c18ce101SOliver Hartkopp 393c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, addr->can_ifindex); 394c18ce101SOliver Hartkopp if (!dev) { 395c18ce101SOliver Hartkopp err = -ENODEV; 396c18ce101SOliver Hartkopp goto out; 397c18ce101SOliver Hartkopp } 398c18ce101SOliver Hartkopp if (dev->type != ARPHRD_CAN) { 399c18ce101SOliver Hartkopp dev_put(dev); 400c18ce101SOliver Hartkopp err = -ENODEV; 401c18ce101SOliver Hartkopp goto out; 402c18ce101SOliver Hartkopp } 403c18ce101SOliver Hartkopp if (!(dev->flags & IFF_UP)) 404c18ce101SOliver Hartkopp notify_enetdown = 1; 405c18ce101SOliver Hartkopp 406c18ce101SOliver Hartkopp ifindex = dev->ifindex; 407c18ce101SOliver Hartkopp 408c18ce101SOliver Hartkopp /* filters set by default/setsockopt */ 409c18ce101SOliver Hartkopp err = raw_enable_allfilters(dev, sk); 410c18ce101SOliver Hartkopp dev_put(dev); 411c18ce101SOliver Hartkopp } else { 412c18ce101SOliver Hartkopp ifindex = 0; 413c18ce101SOliver Hartkopp 414c18ce101SOliver Hartkopp /* filters set by default/setsockopt */ 415c18ce101SOliver Hartkopp err = raw_enable_allfilters(NULL, sk); 416c18ce101SOliver Hartkopp } 417c18ce101SOliver Hartkopp 418c18ce101SOliver Hartkopp if (!err) { 419c18ce101SOliver Hartkopp if (ro->bound) { 420c18ce101SOliver Hartkopp /* unregister old filters */ 421c18ce101SOliver Hartkopp if (ro->ifindex) { 422c18ce101SOliver Hartkopp struct net_device *dev; 423c18ce101SOliver Hartkopp 424c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, ro->ifindex); 425c18ce101SOliver Hartkopp if (dev) { 426c18ce101SOliver Hartkopp raw_disable_allfilters(dev, sk); 427c18ce101SOliver Hartkopp dev_put(dev); 428c18ce101SOliver Hartkopp } 429c18ce101SOliver Hartkopp } else 430c18ce101SOliver Hartkopp raw_disable_allfilters(NULL, sk); 431c18ce101SOliver Hartkopp } 432c18ce101SOliver Hartkopp ro->ifindex = ifindex; 433c18ce101SOliver Hartkopp ro->bound = 1; 434c18ce101SOliver Hartkopp } 435c18ce101SOliver Hartkopp 436c18ce101SOliver Hartkopp out: 437c18ce101SOliver Hartkopp release_sock(sk); 438c18ce101SOliver Hartkopp 439c18ce101SOliver Hartkopp if (notify_enetdown) { 440c18ce101SOliver Hartkopp sk->sk_err = ENETDOWN; 441c18ce101SOliver Hartkopp if (!sock_flag(sk, SOCK_DEAD)) 442c18ce101SOliver Hartkopp sk->sk_error_report(sk); 443c18ce101SOliver Hartkopp } 444c18ce101SOliver Hartkopp 445c18ce101SOliver Hartkopp return err; 446c18ce101SOliver Hartkopp } 447c18ce101SOliver Hartkopp 448c18ce101SOliver Hartkopp static int raw_getname(struct socket *sock, struct sockaddr *uaddr, 449c18ce101SOliver Hartkopp int *len, int peer) 450c18ce101SOliver Hartkopp { 451c18ce101SOliver Hartkopp struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 452c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 453c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 454c18ce101SOliver Hartkopp 455c18ce101SOliver Hartkopp if (peer) 456c18ce101SOliver Hartkopp return -EOPNOTSUPP; 457c18ce101SOliver Hartkopp 458e84b90aeSEric Dumazet memset(addr, 0, sizeof(*addr)); 459c18ce101SOliver Hartkopp addr->can_family = AF_CAN; 460c18ce101SOliver Hartkopp addr->can_ifindex = ro->ifindex; 461c18ce101SOliver Hartkopp 462c18ce101SOliver Hartkopp *len = sizeof(*addr); 463c18ce101SOliver Hartkopp 464c18ce101SOliver Hartkopp return 0; 465c18ce101SOliver Hartkopp } 466c18ce101SOliver Hartkopp 467c18ce101SOliver Hartkopp static int raw_setsockopt(struct socket *sock, int level, int optname, 468b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 469c18ce101SOliver Hartkopp { 470c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 471c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 472c18ce101SOliver Hartkopp struct can_filter *filter = NULL; /* dyn. alloc'ed filters */ 473c18ce101SOliver Hartkopp struct can_filter sfilter; /* single filter */ 474c18ce101SOliver Hartkopp struct net_device *dev = NULL; 475c18ce101SOliver Hartkopp can_err_mask_t err_mask = 0; 476c18ce101SOliver Hartkopp int count = 0; 477c18ce101SOliver Hartkopp int err = 0; 478c18ce101SOliver Hartkopp 479c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW) 480c18ce101SOliver Hartkopp return -EINVAL; 481c18ce101SOliver Hartkopp 482c18ce101SOliver Hartkopp switch (optname) { 483c18ce101SOliver Hartkopp 484c18ce101SOliver Hartkopp case CAN_RAW_FILTER: 485c18ce101SOliver Hartkopp if (optlen % sizeof(struct can_filter) != 0) 486c18ce101SOliver Hartkopp return -EINVAL; 487c18ce101SOliver Hartkopp 488c18ce101SOliver Hartkopp count = optlen / sizeof(struct can_filter); 489c18ce101SOliver Hartkopp 490c18ce101SOliver Hartkopp if (count > 1) { 491c18ce101SOliver Hartkopp /* filter does not fit into dfilter => alloc space */ 49216dff918SJulia Lawall filter = memdup_user(optval, optlen); 49316dff918SJulia Lawall if (IS_ERR(filter)) 49416dff918SJulia Lawall return PTR_ERR(filter); 495c18ce101SOliver Hartkopp } else if (count == 1) { 4964ffa8701SEric Dumazet if (copy_from_user(&sfilter, optval, sizeof(sfilter))) 4973f91bd42SSam Ravnborg return -EFAULT; 498c18ce101SOliver Hartkopp } 499c18ce101SOliver Hartkopp 500c18ce101SOliver Hartkopp lock_sock(sk); 501c18ce101SOliver Hartkopp 502c18ce101SOliver Hartkopp if (ro->bound && ro->ifindex) 503c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, ro->ifindex); 504c18ce101SOliver Hartkopp 505c18ce101SOliver Hartkopp if (ro->bound) { 506c18ce101SOliver Hartkopp /* (try to) register the new filters */ 507c18ce101SOliver Hartkopp if (count == 1) 508c18ce101SOliver Hartkopp err = raw_enable_filters(dev, sk, &sfilter, 1); 509c18ce101SOliver Hartkopp else 510c18ce101SOliver Hartkopp err = raw_enable_filters(dev, sk, filter, 511c18ce101SOliver Hartkopp count); 512c18ce101SOliver Hartkopp if (err) { 513c18ce101SOliver Hartkopp if (count > 1) 514c18ce101SOliver Hartkopp kfree(filter); 515c18ce101SOliver Hartkopp goto out_fil; 516c18ce101SOliver Hartkopp } 517c18ce101SOliver Hartkopp 518c18ce101SOliver Hartkopp /* remove old filter registrations */ 519c18ce101SOliver Hartkopp raw_disable_filters(dev, sk, ro->filter, ro->count); 520c18ce101SOliver Hartkopp } 521c18ce101SOliver Hartkopp 522c18ce101SOliver Hartkopp /* remove old filter space */ 523c18ce101SOliver Hartkopp if (ro->count > 1) 524c18ce101SOliver Hartkopp kfree(ro->filter); 525c18ce101SOliver Hartkopp 526c18ce101SOliver Hartkopp /* link new filters to the socket */ 527c18ce101SOliver Hartkopp if (count == 1) { 528c18ce101SOliver Hartkopp /* copy filter data for single filter */ 529c18ce101SOliver Hartkopp ro->dfilter = sfilter; 530c18ce101SOliver Hartkopp filter = &ro->dfilter; 531c18ce101SOliver Hartkopp } 532c18ce101SOliver Hartkopp ro->filter = filter; 533c18ce101SOliver Hartkopp ro->count = count; 534c18ce101SOliver Hartkopp 535c18ce101SOliver Hartkopp out_fil: 536c18ce101SOliver Hartkopp if (dev) 537c18ce101SOliver Hartkopp dev_put(dev); 538c18ce101SOliver Hartkopp 539c18ce101SOliver Hartkopp release_sock(sk); 540c18ce101SOliver Hartkopp 541c18ce101SOliver Hartkopp break; 542c18ce101SOliver Hartkopp 543c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER: 544c18ce101SOliver Hartkopp if (optlen != sizeof(err_mask)) 545c18ce101SOliver Hartkopp return -EINVAL; 546c18ce101SOliver Hartkopp 5473f91bd42SSam Ravnborg if (copy_from_user(&err_mask, optval, optlen)) 5483f91bd42SSam Ravnborg return -EFAULT; 549c18ce101SOliver Hartkopp 550c18ce101SOliver Hartkopp err_mask &= CAN_ERR_MASK; 551c18ce101SOliver Hartkopp 552c18ce101SOliver Hartkopp lock_sock(sk); 553c18ce101SOliver Hartkopp 554c18ce101SOliver Hartkopp if (ro->bound && ro->ifindex) 555c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, ro->ifindex); 556c18ce101SOliver Hartkopp 557c18ce101SOliver Hartkopp /* remove current error mask */ 558c18ce101SOliver Hartkopp if (ro->bound) { 559c18ce101SOliver Hartkopp /* (try to) register the new err_mask */ 560c18ce101SOliver Hartkopp err = raw_enable_errfilter(dev, sk, err_mask); 561c18ce101SOliver Hartkopp 562c18ce101SOliver Hartkopp if (err) 563c18ce101SOliver Hartkopp goto out_err; 564c18ce101SOliver Hartkopp 565c18ce101SOliver Hartkopp /* remove old err_mask registration */ 566c18ce101SOliver Hartkopp raw_disable_errfilter(dev, sk, ro->err_mask); 567c18ce101SOliver Hartkopp } 568c18ce101SOliver Hartkopp 569c18ce101SOliver Hartkopp /* link new err_mask to the socket */ 570c18ce101SOliver Hartkopp ro->err_mask = err_mask; 571c18ce101SOliver Hartkopp 572c18ce101SOliver Hartkopp out_err: 573c18ce101SOliver Hartkopp if (dev) 574c18ce101SOliver Hartkopp dev_put(dev); 575c18ce101SOliver Hartkopp 576c18ce101SOliver Hartkopp release_sock(sk); 577c18ce101SOliver Hartkopp 578c18ce101SOliver Hartkopp break; 579c18ce101SOliver Hartkopp 580c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK: 581c18ce101SOliver Hartkopp if (optlen != sizeof(ro->loopback)) 582c18ce101SOliver Hartkopp return -EINVAL; 583c18ce101SOliver Hartkopp 5843f91bd42SSam Ravnborg if (copy_from_user(&ro->loopback, optval, optlen)) 5853f91bd42SSam Ravnborg return -EFAULT; 586c18ce101SOliver Hartkopp 587c18ce101SOliver Hartkopp break; 588c18ce101SOliver Hartkopp 589c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS: 590c18ce101SOliver Hartkopp if (optlen != sizeof(ro->recv_own_msgs)) 591c18ce101SOliver Hartkopp return -EINVAL; 592c18ce101SOliver Hartkopp 5933f91bd42SSam Ravnborg if (copy_from_user(&ro->recv_own_msgs, optval, optlen)) 5943f91bd42SSam Ravnborg return -EFAULT; 595c18ce101SOliver Hartkopp 596c18ce101SOliver Hartkopp break; 597c18ce101SOliver Hartkopp 598e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES: 599e2d265d3SOliver Hartkopp if (optlen != sizeof(ro->fd_frames)) 600e2d265d3SOliver Hartkopp return -EINVAL; 601e2d265d3SOliver Hartkopp 602e2d265d3SOliver Hartkopp if (copy_from_user(&ro->fd_frames, optval, optlen)) 603e2d265d3SOliver Hartkopp return -EFAULT; 604e2d265d3SOliver Hartkopp 605e2d265d3SOliver Hartkopp break; 606e2d265d3SOliver Hartkopp 607c18ce101SOliver Hartkopp default: 608c18ce101SOliver Hartkopp return -ENOPROTOOPT; 609c18ce101SOliver Hartkopp } 610c18ce101SOliver Hartkopp return err; 611c18ce101SOliver Hartkopp } 612c18ce101SOliver Hartkopp 613c18ce101SOliver Hartkopp static int raw_getsockopt(struct socket *sock, int level, int optname, 614c18ce101SOliver Hartkopp char __user *optval, int __user *optlen) 615c18ce101SOliver Hartkopp { 616c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 617c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 618c18ce101SOliver Hartkopp int len; 619c18ce101SOliver Hartkopp void *val; 620c18ce101SOliver Hartkopp int err = 0; 621c18ce101SOliver Hartkopp 622c18ce101SOliver Hartkopp if (level != SOL_CAN_RAW) 623c18ce101SOliver Hartkopp return -EINVAL; 624c18ce101SOliver Hartkopp if (get_user(len, optlen)) 625c18ce101SOliver Hartkopp return -EFAULT; 626c18ce101SOliver Hartkopp if (len < 0) 627c18ce101SOliver Hartkopp return -EINVAL; 628c18ce101SOliver Hartkopp 629c18ce101SOliver Hartkopp switch (optname) { 630c18ce101SOliver Hartkopp 631c18ce101SOliver Hartkopp case CAN_RAW_FILTER: 632c18ce101SOliver Hartkopp lock_sock(sk); 633c18ce101SOliver Hartkopp if (ro->count > 0) { 634c18ce101SOliver Hartkopp int fsize = ro->count * sizeof(struct can_filter); 635c18ce101SOliver Hartkopp if (len > fsize) 636c18ce101SOliver Hartkopp len = fsize; 637653252c2SPavel Emelyanov if (copy_to_user(optval, ro->filter, len)) 638653252c2SPavel Emelyanov err = -EFAULT; 639c18ce101SOliver Hartkopp } else 640c18ce101SOliver Hartkopp len = 0; 641c18ce101SOliver Hartkopp release_sock(sk); 642c18ce101SOliver Hartkopp 643c18ce101SOliver Hartkopp if (!err) 644c18ce101SOliver Hartkopp err = put_user(len, optlen); 645c18ce101SOliver Hartkopp return err; 646c18ce101SOliver Hartkopp 647c18ce101SOliver Hartkopp case CAN_RAW_ERR_FILTER: 648c18ce101SOliver Hartkopp if (len > sizeof(can_err_mask_t)) 649c18ce101SOliver Hartkopp len = sizeof(can_err_mask_t); 650c18ce101SOliver Hartkopp val = &ro->err_mask; 651c18ce101SOliver Hartkopp break; 652c18ce101SOliver Hartkopp 653c18ce101SOliver Hartkopp case CAN_RAW_LOOPBACK: 654c18ce101SOliver Hartkopp if (len > sizeof(int)) 655c18ce101SOliver Hartkopp len = sizeof(int); 656c18ce101SOliver Hartkopp val = &ro->loopback; 657c18ce101SOliver Hartkopp break; 658c18ce101SOliver Hartkopp 659c18ce101SOliver Hartkopp case CAN_RAW_RECV_OWN_MSGS: 660c18ce101SOliver Hartkopp if (len > sizeof(int)) 661c18ce101SOliver Hartkopp len = sizeof(int); 662c18ce101SOliver Hartkopp val = &ro->recv_own_msgs; 663c18ce101SOliver Hartkopp break; 664c18ce101SOliver Hartkopp 665e2d265d3SOliver Hartkopp case CAN_RAW_FD_FRAMES: 666e2d265d3SOliver Hartkopp if (len > sizeof(int)) 667e2d265d3SOliver Hartkopp len = sizeof(int); 668e2d265d3SOliver Hartkopp val = &ro->fd_frames; 669e2d265d3SOliver Hartkopp break; 670e2d265d3SOliver Hartkopp 671c18ce101SOliver Hartkopp default: 672c18ce101SOliver Hartkopp return -ENOPROTOOPT; 673c18ce101SOliver Hartkopp } 674c18ce101SOliver Hartkopp 675c18ce101SOliver Hartkopp if (put_user(len, optlen)) 676c18ce101SOliver Hartkopp return -EFAULT; 677c18ce101SOliver Hartkopp if (copy_to_user(optval, val, len)) 678c18ce101SOliver Hartkopp return -EFAULT; 679c18ce101SOliver Hartkopp return 0; 680c18ce101SOliver Hartkopp } 681c18ce101SOliver Hartkopp 6821b784140SYing Xue static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 683c18ce101SOliver Hartkopp { 684c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 685c18ce101SOliver Hartkopp struct raw_sock *ro = raw_sk(sk); 686c18ce101SOliver Hartkopp struct sk_buff *skb; 687c18ce101SOliver Hartkopp struct net_device *dev; 688c18ce101SOliver Hartkopp int ifindex; 689c18ce101SOliver Hartkopp int err; 690c18ce101SOliver Hartkopp 691c18ce101SOliver Hartkopp if (msg->msg_name) { 692342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); 693c18ce101SOliver Hartkopp 6945e507328SKurt Van Dijck if (msg->msg_namelen < sizeof(*addr)) 6955e507328SKurt Van Dijck return -EINVAL; 6965e507328SKurt Van Dijck 697c18ce101SOliver Hartkopp if (addr->can_family != AF_CAN) 698c18ce101SOliver Hartkopp return -EINVAL; 699c18ce101SOliver Hartkopp 700c18ce101SOliver Hartkopp ifindex = addr->can_ifindex; 701c18ce101SOliver Hartkopp } else 702c18ce101SOliver Hartkopp ifindex = ro->ifindex; 703c18ce101SOliver Hartkopp 704e2d265d3SOliver Hartkopp if (ro->fd_frames) { 705e2d265d3SOliver Hartkopp if (unlikely(size != CANFD_MTU && size != CAN_MTU)) 7067f2d38ebSOliver Hartkopp return -EINVAL; 707e2d265d3SOliver Hartkopp } else { 708e2d265d3SOliver Hartkopp if (unlikely(size != CAN_MTU)) 709e2d265d3SOliver Hartkopp return -EINVAL; 710e2d265d3SOliver Hartkopp } 7117f2d38ebSOliver Hartkopp 712c18ce101SOliver Hartkopp dev = dev_get_by_index(&init_net, ifindex); 713c18ce101SOliver Hartkopp if (!dev) 714c18ce101SOliver Hartkopp return -ENXIO; 715c18ce101SOliver Hartkopp 716156c2bb9SOliver Hartkopp skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv), 717156c2bb9SOliver Hartkopp msg->msg_flags & MSG_DONTWAIT, &err); 718ebad5c09SIlpo Järvinen if (!skb) 719ebad5c09SIlpo Järvinen goto put_dev; 720c18ce101SOliver Hartkopp 7212bf3440dSOliver Hartkopp can_skb_reserve(skb); 7222bf3440dSOliver Hartkopp can_skb_prv(skb)->ifindex = dev->ifindex; 723156c2bb9SOliver Hartkopp 7246ce8e9ceSAl Viro err = memcpy_from_msg(skb_put(skb, size), msg, size); 725ebad5c09SIlpo Järvinen if (err < 0) 726ebad5c09SIlpo Järvinen goto free_skb; 727bf84a010SDaniel Borkmann 728bf84a010SDaniel Borkmann sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); 729cff0d6e6SOliver Hartkopp 730c18ce101SOliver Hartkopp skb->dev = dev; 731c18ce101SOliver Hartkopp skb->sk = sk; 732bb5ecb0cSRostislav Lisovy skb->priority = sk->sk_priority; 733c18ce101SOliver Hartkopp 734c18ce101SOliver Hartkopp err = can_send(skb, ro->loopback); 735c18ce101SOliver Hartkopp 736c18ce101SOliver Hartkopp dev_put(dev); 737c18ce101SOliver Hartkopp 738c18ce101SOliver Hartkopp if (err) 739ebad5c09SIlpo Järvinen goto send_failed; 740c18ce101SOliver Hartkopp 741c18ce101SOliver Hartkopp return size; 742ebad5c09SIlpo Järvinen 743ebad5c09SIlpo Järvinen free_skb: 744ebad5c09SIlpo Järvinen kfree_skb(skb); 745ebad5c09SIlpo Järvinen put_dev: 746ebad5c09SIlpo Järvinen dev_put(dev); 747ebad5c09SIlpo Järvinen send_failed: 748ebad5c09SIlpo Järvinen return err; 749c18ce101SOliver Hartkopp } 750c18ce101SOliver Hartkopp 7511b784140SYing Xue static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 7521b784140SYing Xue int flags) 753c18ce101SOliver Hartkopp { 754c18ce101SOliver Hartkopp struct sock *sk = sock->sk; 755c18ce101SOliver Hartkopp struct sk_buff *skb; 756a219994bSUrs Thuermann int err = 0; 757c18ce101SOliver Hartkopp int noblock; 758c18ce101SOliver Hartkopp 759c18ce101SOliver Hartkopp noblock = flags & MSG_DONTWAIT; 760c18ce101SOliver Hartkopp flags &= ~MSG_DONTWAIT; 761c18ce101SOliver Hartkopp 762a219994bSUrs Thuermann skb = skb_recv_datagram(sk, flags, noblock, &err); 763c18ce101SOliver Hartkopp if (!skb) 764a219994bSUrs Thuermann return err; 765c18ce101SOliver Hartkopp 766821047c4SOliver Hartkopp if (size < skb->len) 767c18ce101SOliver Hartkopp msg->msg_flags |= MSG_TRUNC; 768c18ce101SOliver Hartkopp else 769821047c4SOliver Hartkopp size = skb->len; 770c18ce101SOliver Hartkopp 7717eab8d9eSAl Viro err = memcpy_to_msg(msg, skb->data, size); 772a219994bSUrs Thuermann if (err < 0) { 773c18ce101SOliver Hartkopp skb_free_datagram(sk, skb); 774a219994bSUrs Thuermann return err; 775c18ce101SOliver Hartkopp } 776c18ce101SOliver Hartkopp 7773b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 778c18ce101SOliver Hartkopp 779c18ce101SOliver Hartkopp if (msg->msg_name) { 780342dfc30SSteffen Hurrle __sockaddr_check_size(sizeof(struct sockaddr_can)); 781c18ce101SOliver Hartkopp msg->msg_namelen = sizeof(struct sockaddr_can); 782c18ce101SOliver Hartkopp memcpy(msg->msg_name, skb->cb, msg->msg_namelen); 783c18ce101SOliver Hartkopp } 784c18ce101SOliver Hartkopp 7851e55659cSOliver Hartkopp /* assign the flags that have been recorded in raw_rcv() */ 7861e55659cSOliver Hartkopp msg->msg_flags |= *(raw_flags(skb)); 7871e55659cSOliver Hartkopp 788c18ce101SOliver Hartkopp skb_free_datagram(sk, skb); 789c18ce101SOliver Hartkopp 790c18ce101SOliver Hartkopp return size; 791c18ce101SOliver Hartkopp } 792c18ce101SOliver Hartkopp 79353914b67SOliver Hartkopp static const struct proto_ops raw_ops = { 794c18ce101SOliver Hartkopp .family = PF_CAN, 795c18ce101SOliver Hartkopp .release = raw_release, 796c18ce101SOliver Hartkopp .bind = raw_bind, 797c18ce101SOliver Hartkopp .connect = sock_no_connect, 798c18ce101SOliver Hartkopp .socketpair = sock_no_socketpair, 799c18ce101SOliver Hartkopp .accept = sock_no_accept, 800c18ce101SOliver Hartkopp .getname = raw_getname, 801c18ce101SOliver Hartkopp .poll = datagram_poll, 80253914b67SOliver Hartkopp .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */ 803c18ce101SOliver Hartkopp .listen = sock_no_listen, 804c18ce101SOliver Hartkopp .shutdown = sock_no_shutdown, 805c18ce101SOliver Hartkopp .setsockopt = raw_setsockopt, 806c18ce101SOliver Hartkopp .getsockopt = raw_getsockopt, 807c18ce101SOliver Hartkopp .sendmsg = raw_sendmsg, 808c18ce101SOliver Hartkopp .recvmsg = raw_recvmsg, 809c18ce101SOliver Hartkopp .mmap = sock_no_mmap, 810c18ce101SOliver Hartkopp .sendpage = sock_no_sendpage, 811c18ce101SOliver Hartkopp }; 812c18ce101SOliver Hartkopp 813c18ce101SOliver Hartkopp static struct proto raw_proto __read_mostly = { 814c18ce101SOliver Hartkopp .name = "CAN_RAW", 815c18ce101SOliver Hartkopp .owner = THIS_MODULE, 816c18ce101SOliver Hartkopp .obj_size = sizeof(struct raw_sock), 817c18ce101SOliver Hartkopp .init = raw_init, 818c18ce101SOliver Hartkopp }; 819c18ce101SOliver Hartkopp 8201650629dSKurt Van Dijck static const struct can_proto raw_can_proto = { 821c18ce101SOliver Hartkopp .type = SOCK_RAW, 822c18ce101SOliver Hartkopp .protocol = CAN_RAW, 823c18ce101SOliver Hartkopp .ops = &raw_ops, 824c18ce101SOliver Hartkopp .prot = &raw_proto, 825c18ce101SOliver Hartkopp }; 826c18ce101SOliver Hartkopp 827c18ce101SOliver Hartkopp static __init int raw_module_init(void) 828c18ce101SOliver Hartkopp { 829c18ce101SOliver Hartkopp int err; 830c18ce101SOliver Hartkopp 831b111b78cSJeremiah Mahler pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n"); 832c18ce101SOliver Hartkopp 833c18ce101SOliver Hartkopp err = can_proto_register(&raw_can_proto); 834c18ce101SOliver Hartkopp if (err < 0) 835c18ce101SOliver Hartkopp printk(KERN_ERR "can: registration of raw protocol failed\n"); 836c18ce101SOliver Hartkopp 837c18ce101SOliver Hartkopp return err; 838c18ce101SOliver Hartkopp } 839c18ce101SOliver Hartkopp 840c18ce101SOliver Hartkopp static __exit void raw_module_exit(void) 841c18ce101SOliver Hartkopp { 842c18ce101SOliver Hartkopp can_proto_unregister(&raw_can_proto); 843c18ce101SOliver Hartkopp } 844c18ce101SOliver Hartkopp 845c18ce101SOliver Hartkopp module_init(raw_module_init); 846c18ce101SOliver Hartkopp module_exit(raw_module_exit); 847