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