Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * raw.c - Raw sockets for protocol family CAN |
| 3 | * |
| 4 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of Volkswagen nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * Alternatively, provided that this notice is retained in full, this |
| 20 | * software may be distributed under the terms of the GNU General |
| 21 | * Public License ("GPL") version 2, in which case the provisions of the |
| 22 | * GPL apply INSTEAD OF those given above. |
| 23 | * |
| 24 | * The provided data structures and external interfaces from this code |
| 25 | * are not restricted to be used by modules with a GPL compatible license. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 38 | * DAMAGE. |
| 39 | * |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/uio.h> |
| 45 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 47 | #include <linux/netdevice.h> |
| 48 | #include <linux/socket.h> |
| 49 | #include <linux/if_arp.h> |
| 50 | #include <linux/skbuff.h> |
| 51 | #include <linux/can.h> |
| 52 | #include <linux/can/core.h> |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 53 | #include <linux/can/skb.h> |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 54 | #include <linux/can/raw.h> |
| 55 | #include <net/sock.h> |
| 56 | #include <net/net_namespace.h> |
| 57 | |
| 58 | #define CAN_RAW_VERSION CAN_VERSION |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 59 | |
| 60 | MODULE_DESCRIPTION("PF_CAN raw protocol"); |
| 61 | MODULE_LICENSE("Dual BSD/GPL"); |
| 62 | MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>"); |
Lothar Waßmann | b13bb2e | 2009-07-14 23:12:25 +0000 | [diff] [blame] | 63 | MODULE_ALIAS("can-proto-1"); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 64 | |
| 65 | #define MASK_ALL 0 |
| 66 | |
| 67 | /* |
| 68 | * A raw socket has a list of can_filters attached to it, each receiving |
| 69 | * the CAN frames matching that filter. If the filter list is empty, |
| 70 | * no CAN frames will be received by the socket. The default after |
| 71 | * opening the socket, is to have one filter which receives all frames. |
| 72 | * The filter list is allocated dynamically with the exception of the |
| 73 | * list containing only one item. This common case is optimized by |
| 74 | * storing the single filter in dfilter, to avoid using dynamic memory. |
| 75 | */ |
| 76 | |
| 77 | struct raw_sock { |
| 78 | struct sock sk; |
| 79 | int bound; |
| 80 | int ifindex; |
| 81 | struct notifier_block notifier; |
| 82 | int loopback; |
| 83 | int recv_own_msgs; |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 84 | int fd_frames; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 85 | int count; /* number of active filters */ |
| 86 | struct can_filter dfilter; /* default/single filter */ |
| 87 | struct can_filter *filter; /* pointer to filter(s) */ |
| 88 | can_err_mask_t err_mask; |
| 89 | }; |
| 90 | |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 91 | /* |
| 92 | * Return pointer to store the extra msg flags for raw_recvmsg(). |
| 93 | * We use the space of one unsigned int beyond the 'struct sockaddr_can' |
| 94 | * in skb->cb. |
| 95 | */ |
| 96 | static inline unsigned int *raw_flags(struct sk_buff *skb) |
| 97 | { |
| 98 | BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) + |
| 99 | sizeof(unsigned int))); |
| 100 | |
| 101 | /* return pointer after struct sockaddr_can */ |
| 102 | return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]); |
| 103 | } |
| 104 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 105 | static inline struct raw_sock *raw_sk(const struct sock *sk) |
| 106 | { |
| 107 | return (struct raw_sock *)sk; |
| 108 | } |
| 109 | |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 110 | static void raw_rcv(struct sk_buff *oskb, void *data) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 111 | { |
| 112 | struct sock *sk = (struct sock *)data; |
| 113 | struct raw_sock *ro = raw_sk(sk); |
| 114 | struct sockaddr_can *addr; |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 115 | struct sk_buff *skb; |
| 116 | unsigned int *pflags; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 117 | |
Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 118 | /* check the received tx sock reference */ |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 119 | if (!ro->recv_own_msgs && oskb->sk == sk) |
Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 120 | return; |
| 121 | |
Oliver Hartkopp | 821047c | 2014-03-01 15:31:53 +0100 | [diff] [blame] | 122 | /* do not pass non-CAN2.0 frames to a legacy socket */ |
| 123 | if (!ro->fd_frames && oskb->len != CAN_MTU) |
| 124 | return; |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 125 | |
Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 126 | /* clone the given skb to be able to enqueue it into the rcv queue */ |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 127 | skb = skb_clone(oskb, GFP_ATOMIC); |
Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 128 | if (!skb) |
| 129 | return; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Put the datagram to the queue so that raw_recvmsg() can |
| 133 | * get it from there. We need to pass the interface index to |
| 134 | * raw_recvmsg(). We pass a whole struct sockaddr_can in skb->cb |
| 135 | * containing the interface index. |
| 136 | */ |
| 137 | |
| 138 | BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); |
| 139 | addr = (struct sockaddr_can *)skb->cb; |
| 140 | memset(addr, 0, sizeof(*addr)); |
| 141 | addr->can_family = AF_CAN; |
| 142 | addr->can_ifindex = skb->dev->ifindex; |
| 143 | |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 144 | /* add CAN specific message flags for raw_recvmsg() */ |
| 145 | pflags = raw_flags(skb); |
| 146 | *pflags = 0; |
| 147 | if (oskb->sk) |
| 148 | *pflags |= MSG_DONTROUTE; |
| 149 | if (oskb->sk == sk) |
| 150 | *pflags |= MSG_CONFIRM; |
| 151 | |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 152 | if (sock_queue_rcv_skb(sk, skb) < 0) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 153 | kfree_skb(skb); |
| 154 | } |
| 155 | |
| 156 | static int raw_enable_filters(struct net_device *dev, struct sock *sk, |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 157 | struct can_filter *filter, int count) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 158 | { |
| 159 | int err = 0; |
| 160 | int i; |
| 161 | |
| 162 | for (i = 0; i < count; i++) { |
| 163 | err = can_rx_register(dev, filter[i].can_id, |
| 164 | filter[i].can_mask, |
| 165 | raw_rcv, sk, "raw"); |
| 166 | if (err) { |
| 167 | /* clean up successfully registered filters */ |
| 168 | while (--i >= 0) |
| 169 | can_rx_unregister(dev, filter[i].can_id, |
| 170 | filter[i].can_mask, |
| 171 | raw_rcv, sk); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | static int raw_enable_errfilter(struct net_device *dev, struct sock *sk, |
| 180 | can_err_mask_t err_mask) |
| 181 | { |
| 182 | int err = 0; |
| 183 | |
| 184 | if (err_mask) |
| 185 | err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG, |
| 186 | raw_rcv, sk, "raw"); |
| 187 | |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | static void raw_disable_filters(struct net_device *dev, struct sock *sk, |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 192 | struct can_filter *filter, int count) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 193 | { |
| 194 | int i; |
| 195 | |
| 196 | for (i = 0; i < count; i++) |
| 197 | can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask, |
| 198 | raw_rcv, sk); |
| 199 | } |
| 200 | |
| 201 | static inline void raw_disable_errfilter(struct net_device *dev, |
| 202 | struct sock *sk, |
| 203 | can_err_mask_t err_mask) |
| 204 | |
| 205 | { |
| 206 | if (err_mask) |
| 207 | can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG, |
| 208 | raw_rcv, sk); |
| 209 | } |
| 210 | |
| 211 | static inline void raw_disable_allfilters(struct net_device *dev, |
| 212 | struct sock *sk) |
| 213 | { |
| 214 | struct raw_sock *ro = raw_sk(sk); |
| 215 | |
| 216 | raw_disable_filters(dev, sk, ro->filter, ro->count); |
| 217 | raw_disable_errfilter(dev, sk, ro->err_mask); |
| 218 | } |
| 219 | |
| 220 | static int raw_enable_allfilters(struct net_device *dev, struct sock *sk) |
| 221 | { |
| 222 | struct raw_sock *ro = raw_sk(sk); |
| 223 | int err; |
| 224 | |
| 225 | err = raw_enable_filters(dev, sk, ro->filter, ro->count); |
| 226 | if (!err) { |
| 227 | err = raw_enable_errfilter(dev, sk, ro->err_mask); |
| 228 | if (err) |
| 229 | raw_disable_filters(dev, sk, ro->filter, ro->count); |
| 230 | } |
| 231 | |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static int raw_notifier(struct notifier_block *nb, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 236 | unsigned long msg, void *ptr) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 237 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 238 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 239 | struct raw_sock *ro = container_of(nb, struct raw_sock, notifier); |
| 240 | struct sock *sk = &ro->sk; |
| 241 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 242 | if (!net_eq(dev_net(dev), &init_net)) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 243 | return NOTIFY_DONE; |
| 244 | |
| 245 | if (dev->type != ARPHRD_CAN) |
| 246 | return NOTIFY_DONE; |
| 247 | |
| 248 | if (ro->ifindex != dev->ifindex) |
| 249 | return NOTIFY_DONE; |
| 250 | |
| 251 | switch (msg) { |
| 252 | |
| 253 | case NETDEV_UNREGISTER: |
| 254 | lock_sock(sk); |
| 255 | /* remove current filters & unregister */ |
| 256 | if (ro->bound) |
| 257 | raw_disable_allfilters(dev, sk); |
| 258 | |
| 259 | if (ro->count > 1) |
| 260 | kfree(ro->filter); |
| 261 | |
| 262 | ro->ifindex = 0; |
| 263 | ro->bound = 0; |
| 264 | ro->count = 0; |
| 265 | release_sock(sk); |
| 266 | |
| 267 | sk->sk_err = ENODEV; |
| 268 | if (!sock_flag(sk, SOCK_DEAD)) |
| 269 | sk->sk_error_report(sk); |
| 270 | break; |
| 271 | |
| 272 | case NETDEV_DOWN: |
| 273 | sk->sk_err = ENETDOWN; |
| 274 | if (!sock_flag(sk, SOCK_DEAD)) |
| 275 | sk->sk_error_report(sk); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | return NOTIFY_DONE; |
| 280 | } |
| 281 | |
| 282 | static int raw_init(struct sock *sk) |
| 283 | { |
| 284 | struct raw_sock *ro = raw_sk(sk); |
| 285 | |
| 286 | ro->bound = 0; |
| 287 | ro->ifindex = 0; |
| 288 | |
| 289 | /* set default filter to single entry dfilter */ |
| 290 | ro->dfilter.can_id = 0; |
| 291 | ro->dfilter.can_mask = MASK_ALL; |
| 292 | ro->filter = &ro->dfilter; |
| 293 | ro->count = 1; |
| 294 | |
| 295 | /* set default loopback behaviour */ |
| 296 | ro->loopback = 1; |
| 297 | ro->recv_own_msgs = 0; |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 298 | ro->fd_frames = 0; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 299 | |
| 300 | /* set notifier */ |
| 301 | ro->notifier.notifier_call = raw_notifier; |
| 302 | |
| 303 | register_netdevice_notifier(&ro->notifier); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int raw_release(struct socket *sock) |
| 309 | { |
| 310 | struct sock *sk = sock->sk; |
Oliver Hartkopp | 10022a6 | 2011-04-20 01:57:15 +0000 | [diff] [blame] | 311 | struct raw_sock *ro; |
| 312 | |
| 313 | if (!sk) |
| 314 | return 0; |
| 315 | |
| 316 | ro = raw_sk(sk); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 317 | |
| 318 | unregister_netdevice_notifier(&ro->notifier); |
| 319 | |
| 320 | lock_sock(sk); |
| 321 | |
| 322 | /* remove current filters & unregister */ |
| 323 | if (ro->bound) { |
| 324 | if (ro->ifindex) { |
| 325 | struct net_device *dev; |
| 326 | |
| 327 | dev = dev_get_by_index(&init_net, ro->ifindex); |
| 328 | if (dev) { |
| 329 | raw_disable_allfilters(dev, sk); |
| 330 | dev_put(dev); |
| 331 | } |
| 332 | } else |
| 333 | raw_disable_allfilters(NULL, sk); |
| 334 | } |
| 335 | |
| 336 | if (ro->count > 1) |
| 337 | kfree(ro->filter); |
| 338 | |
| 339 | ro->ifindex = 0; |
| 340 | ro->bound = 0; |
| 341 | ro->count = 0; |
| 342 | |
Lothar Waßmann | f7e5cc0 | 2009-07-14 23:10:21 +0000 | [diff] [blame] | 343 | sock_orphan(sk); |
| 344 | sock->sk = NULL; |
| 345 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 346 | release_sock(sk); |
| 347 | sock_put(sk); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) |
| 353 | { |
| 354 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; |
| 355 | struct sock *sk = sock->sk; |
| 356 | struct raw_sock *ro = raw_sk(sk); |
| 357 | int ifindex; |
| 358 | int err = 0; |
| 359 | int notify_enetdown = 0; |
| 360 | |
| 361 | if (len < sizeof(*addr)) |
| 362 | return -EINVAL; |
| 363 | |
| 364 | lock_sock(sk); |
| 365 | |
| 366 | if (ro->bound && addr->can_ifindex == ro->ifindex) |
| 367 | goto out; |
| 368 | |
| 369 | if (addr->can_ifindex) { |
| 370 | struct net_device *dev; |
| 371 | |
| 372 | dev = dev_get_by_index(&init_net, addr->can_ifindex); |
| 373 | if (!dev) { |
| 374 | err = -ENODEV; |
| 375 | goto out; |
| 376 | } |
| 377 | if (dev->type != ARPHRD_CAN) { |
| 378 | dev_put(dev); |
| 379 | err = -ENODEV; |
| 380 | goto out; |
| 381 | } |
| 382 | if (!(dev->flags & IFF_UP)) |
| 383 | notify_enetdown = 1; |
| 384 | |
| 385 | ifindex = dev->ifindex; |
| 386 | |
| 387 | /* filters set by default/setsockopt */ |
| 388 | err = raw_enable_allfilters(dev, sk); |
| 389 | dev_put(dev); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 390 | } else { |
| 391 | ifindex = 0; |
| 392 | |
| 393 | /* filters set by default/setsockopt */ |
| 394 | err = raw_enable_allfilters(NULL, sk); |
| 395 | } |
| 396 | |
| 397 | if (!err) { |
| 398 | if (ro->bound) { |
| 399 | /* unregister old filters */ |
| 400 | if (ro->ifindex) { |
| 401 | struct net_device *dev; |
| 402 | |
| 403 | dev = dev_get_by_index(&init_net, ro->ifindex); |
| 404 | if (dev) { |
| 405 | raw_disable_allfilters(dev, sk); |
| 406 | dev_put(dev); |
| 407 | } |
| 408 | } else |
| 409 | raw_disable_allfilters(NULL, sk); |
| 410 | } |
| 411 | ro->ifindex = ifindex; |
| 412 | ro->bound = 1; |
| 413 | } |
| 414 | |
| 415 | out: |
| 416 | release_sock(sk); |
| 417 | |
| 418 | if (notify_enetdown) { |
| 419 | sk->sk_err = ENETDOWN; |
| 420 | if (!sock_flag(sk, SOCK_DEAD)) |
| 421 | sk->sk_error_report(sk); |
| 422 | } |
| 423 | |
| 424 | return err; |
| 425 | } |
| 426 | |
| 427 | static int raw_getname(struct socket *sock, struct sockaddr *uaddr, |
| 428 | int *len, int peer) |
| 429 | { |
| 430 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; |
| 431 | struct sock *sk = sock->sk; |
| 432 | struct raw_sock *ro = raw_sk(sk); |
| 433 | |
| 434 | if (peer) |
| 435 | return -EOPNOTSUPP; |
| 436 | |
Eric Dumazet | e84b90a | 2009-08-06 20:27:04 +0000 | [diff] [blame] | 437 | memset(addr, 0, sizeof(*addr)); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 438 | addr->can_family = AF_CAN; |
| 439 | addr->can_ifindex = ro->ifindex; |
| 440 | |
| 441 | *len = sizeof(*addr); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int raw_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 447 | char __user *optval, unsigned int optlen) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 448 | { |
| 449 | struct sock *sk = sock->sk; |
| 450 | struct raw_sock *ro = raw_sk(sk); |
| 451 | struct can_filter *filter = NULL; /* dyn. alloc'ed filters */ |
| 452 | struct can_filter sfilter; /* single filter */ |
| 453 | struct net_device *dev = NULL; |
| 454 | can_err_mask_t err_mask = 0; |
| 455 | int count = 0; |
| 456 | int err = 0; |
| 457 | |
| 458 | if (level != SOL_CAN_RAW) |
| 459 | return -EINVAL; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 460 | |
| 461 | switch (optname) { |
| 462 | |
| 463 | case CAN_RAW_FILTER: |
| 464 | if (optlen % sizeof(struct can_filter) != 0) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | count = optlen / sizeof(struct can_filter); |
| 468 | |
| 469 | if (count > 1) { |
| 470 | /* filter does not fit into dfilter => alloc space */ |
Julia Lawall | 16dff91 | 2010-05-21 22:18:34 +0000 | [diff] [blame] | 471 | filter = memdup_user(optval, optlen); |
| 472 | if (IS_ERR(filter)) |
| 473 | return PTR_ERR(filter); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 474 | } else if (count == 1) { |
Eric Dumazet | 4ffa870 | 2010-04-09 23:47:31 +0000 | [diff] [blame] | 475 | if (copy_from_user(&sfilter, optval, sizeof(sfilter))) |
Sam Ravnborg | 3f91bd4 | 2008-04-26 22:57:25 -0700 | [diff] [blame] | 476 | return -EFAULT; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | lock_sock(sk); |
| 480 | |
| 481 | if (ro->bound && ro->ifindex) |
| 482 | dev = dev_get_by_index(&init_net, ro->ifindex); |
| 483 | |
| 484 | if (ro->bound) { |
| 485 | /* (try to) register the new filters */ |
| 486 | if (count == 1) |
| 487 | err = raw_enable_filters(dev, sk, &sfilter, 1); |
| 488 | else |
| 489 | err = raw_enable_filters(dev, sk, filter, |
| 490 | count); |
| 491 | if (err) { |
| 492 | if (count > 1) |
| 493 | kfree(filter); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 494 | goto out_fil; |
| 495 | } |
| 496 | |
| 497 | /* remove old filter registrations */ |
| 498 | raw_disable_filters(dev, sk, ro->filter, ro->count); |
| 499 | } |
| 500 | |
| 501 | /* remove old filter space */ |
| 502 | if (ro->count > 1) |
| 503 | kfree(ro->filter); |
| 504 | |
| 505 | /* link new filters to the socket */ |
| 506 | if (count == 1) { |
| 507 | /* copy filter data for single filter */ |
| 508 | ro->dfilter = sfilter; |
| 509 | filter = &ro->dfilter; |
| 510 | } |
| 511 | ro->filter = filter; |
| 512 | ro->count = count; |
| 513 | |
| 514 | out_fil: |
| 515 | if (dev) |
| 516 | dev_put(dev); |
| 517 | |
| 518 | release_sock(sk); |
| 519 | |
| 520 | break; |
| 521 | |
| 522 | case CAN_RAW_ERR_FILTER: |
| 523 | if (optlen != sizeof(err_mask)) |
| 524 | return -EINVAL; |
| 525 | |
Sam Ravnborg | 3f91bd4 | 2008-04-26 22:57:25 -0700 | [diff] [blame] | 526 | if (copy_from_user(&err_mask, optval, optlen)) |
| 527 | return -EFAULT; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 528 | |
| 529 | err_mask &= CAN_ERR_MASK; |
| 530 | |
| 531 | lock_sock(sk); |
| 532 | |
| 533 | if (ro->bound && ro->ifindex) |
| 534 | dev = dev_get_by_index(&init_net, ro->ifindex); |
| 535 | |
| 536 | /* remove current error mask */ |
| 537 | if (ro->bound) { |
| 538 | /* (try to) register the new err_mask */ |
| 539 | err = raw_enable_errfilter(dev, sk, err_mask); |
| 540 | |
| 541 | if (err) |
| 542 | goto out_err; |
| 543 | |
| 544 | /* remove old err_mask registration */ |
| 545 | raw_disable_errfilter(dev, sk, ro->err_mask); |
| 546 | } |
| 547 | |
| 548 | /* link new err_mask to the socket */ |
| 549 | ro->err_mask = err_mask; |
| 550 | |
| 551 | out_err: |
| 552 | if (dev) |
| 553 | dev_put(dev); |
| 554 | |
| 555 | release_sock(sk); |
| 556 | |
| 557 | break; |
| 558 | |
| 559 | case CAN_RAW_LOOPBACK: |
| 560 | if (optlen != sizeof(ro->loopback)) |
| 561 | return -EINVAL; |
| 562 | |
Sam Ravnborg | 3f91bd4 | 2008-04-26 22:57:25 -0700 | [diff] [blame] | 563 | if (copy_from_user(&ro->loopback, optval, optlen)) |
| 564 | return -EFAULT; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 565 | |
| 566 | break; |
| 567 | |
| 568 | case CAN_RAW_RECV_OWN_MSGS: |
| 569 | if (optlen != sizeof(ro->recv_own_msgs)) |
| 570 | return -EINVAL; |
| 571 | |
Sam Ravnborg | 3f91bd4 | 2008-04-26 22:57:25 -0700 | [diff] [blame] | 572 | if (copy_from_user(&ro->recv_own_msgs, optval, optlen)) |
| 573 | return -EFAULT; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 574 | |
| 575 | break; |
| 576 | |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 577 | case CAN_RAW_FD_FRAMES: |
| 578 | if (optlen != sizeof(ro->fd_frames)) |
| 579 | return -EINVAL; |
| 580 | |
| 581 | if (copy_from_user(&ro->fd_frames, optval, optlen)) |
| 582 | return -EFAULT; |
| 583 | |
| 584 | break; |
| 585 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 586 | default: |
| 587 | return -ENOPROTOOPT; |
| 588 | } |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | static int raw_getsockopt(struct socket *sock, int level, int optname, |
| 593 | char __user *optval, int __user *optlen) |
| 594 | { |
| 595 | struct sock *sk = sock->sk; |
| 596 | struct raw_sock *ro = raw_sk(sk); |
| 597 | int len; |
| 598 | void *val; |
| 599 | int err = 0; |
| 600 | |
| 601 | if (level != SOL_CAN_RAW) |
| 602 | return -EINVAL; |
| 603 | if (get_user(len, optlen)) |
| 604 | return -EFAULT; |
| 605 | if (len < 0) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | switch (optname) { |
| 609 | |
| 610 | case CAN_RAW_FILTER: |
| 611 | lock_sock(sk); |
| 612 | if (ro->count > 0) { |
| 613 | int fsize = ro->count * sizeof(struct can_filter); |
| 614 | if (len > fsize) |
| 615 | len = fsize; |
Pavel Emelyanov | 653252c | 2008-04-25 01:49:48 -0700 | [diff] [blame] | 616 | if (copy_to_user(optval, ro->filter, len)) |
| 617 | err = -EFAULT; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 618 | } else |
| 619 | len = 0; |
| 620 | release_sock(sk); |
| 621 | |
| 622 | if (!err) |
| 623 | err = put_user(len, optlen); |
| 624 | return err; |
| 625 | |
| 626 | case CAN_RAW_ERR_FILTER: |
| 627 | if (len > sizeof(can_err_mask_t)) |
| 628 | len = sizeof(can_err_mask_t); |
| 629 | val = &ro->err_mask; |
| 630 | break; |
| 631 | |
| 632 | case CAN_RAW_LOOPBACK: |
| 633 | if (len > sizeof(int)) |
| 634 | len = sizeof(int); |
| 635 | val = &ro->loopback; |
| 636 | break; |
| 637 | |
| 638 | case CAN_RAW_RECV_OWN_MSGS: |
| 639 | if (len > sizeof(int)) |
| 640 | len = sizeof(int); |
| 641 | val = &ro->recv_own_msgs; |
| 642 | break; |
| 643 | |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 644 | case CAN_RAW_FD_FRAMES: |
| 645 | if (len > sizeof(int)) |
| 646 | len = sizeof(int); |
| 647 | val = &ro->fd_frames; |
| 648 | break; |
| 649 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 650 | default: |
| 651 | return -ENOPROTOOPT; |
| 652 | } |
| 653 | |
| 654 | if (put_user(len, optlen)) |
| 655 | return -EFAULT; |
| 656 | if (copy_to_user(optval, val, len)) |
| 657 | return -EFAULT; |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int raw_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 662 | struct msghdr *msg, size_t size) |
| 663 | { |
| 664 | struct sock *sk = sock->sk; |
| 665 | struct raw_sock *ro = raw_sk(sk); |
| 666 | struct sk_buff *skb; |
| 667 | struct net_device *dev; |
| 668 | int ifindex; |
| 669 | int err; |
| 670 | |
| 671 | if (msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 672 | DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 673 | |
Kurt Van Dijck | 5e50732 | 2011-01-15 20:56:42 -0800 | [diff] [blame] | 674 | if (msg->msg_namelen < sizeof(*addr)) |
| 675 | return -EINVAL; |
| 676 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 677 | if (addr->can_family != AF_CAN) |
| 678 | return -EINVAL; |
| 679 | |
| 680 | ifindex = addr->can_ifindex; |
| 681 | } else |
| 682 | ifindex = ro->ifindex; |
| 683 | |
Oliver Hartkopp | e2d265d | 2012-06-13 20:41:31 +0200 | [diff] [blame] | 684 | if (ro->fd_frames) { |
| 685 | if (unlikely(size != CANFD_MTU && size != CAN_MTU)) |
| 686 | return -EINVAL; |
| 687 | } else { |
| 688 | if (unlikely(size != CAN_MTU)) |
| 689 | return -EINVAL; |
| 690 | } |
Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 691 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 692 | dev = dev_get_by_index(&init_net, ifindex); |
| 693 | if (!dev) |
| 694 | return -ENXIO; |
| 695 | |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 696 | skb = sock_alloc_send_skb(sk, size + sizeof(struct can_skb_priv), |
| 697 | msg->msg_flags & MSG_DONTWAIT, &err); |
Ilpo Järvinen | ebad5c0 | 2008-12-14 23:16:58 -0800 | [diff] [blame] | 698 | if (!skb) |
| 699 | goto put_dev; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 700 | |
Oliver Hartkopp | 2bf3440 | 2013-01-28 08:33:33 +0000 | [diff] [blame] | 701 | can_skb_reserve(skb); |
| 702 | can_skb_prv(skb)->ifindex = dev->ifindex; |
Oliver Hartkopp | 156c2bb | 2013-01-17 18:43:39 +0100 | [diff] [blame] | 703 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 704 | err = memcpy_from_msg(skb_put(skb, size), msg, size); |
Ilpo Järvinen | ebad5c0 | 2008-12-14 23:16:58 -0800 | [diff] [blame] | 705 | if (err < 0) |
| 706 | goto free_skb; |
Daniel Borkmann | bf84a01 | 2013-04-14 08:08:13 +0000 | [diff] [blame] | 707 | |
| 708 | sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); |
Oliver Hartkopp | cff0d6e | 2010-08-03 00:31:48 -0700 | [diff] [blame] | 709 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 710 | skb->dev = dev; |
| 711 | skb->sk = sk; |
Rostislav Lisovy | bb5ecb0 | 2014-01-24 13:17:37 +0100 | [diff] [blame] | 712 | skb->priority = sk->sk_priority; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 713 | |
| 714 | err = can_send(skb, ro->loopback); |
| 715 | |
| 716 | dev_put(dev); |
| 717 | |
| 718 | if (err) |
Ilpo Järvinen | ebad5c0 | 2008-12-14 23:16:58 -0800 | [diff] [blame] | 719 | goto send_failed; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 720 | |
| 721 | return size; |
Ilpo Järvinen | ebad5c0 | 2008-12-14 23:16:58 -0800 | [diff] [blame] | 722 | |
| 723 | free_skb: |
| 724 | kfree_skb(skb); |
| 725 | put_dev: |
| 726 | dev_put(dev); |
| 727 | send_failed: |
| 728 | return err; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static int raw_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 732 | struct msghdr *msg, size_t size, int flags) |
| 733 | { |
| 734 | struct sock *sk = sock->sk; |
| 735 | struct sk_buff *skb; |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 736 | int err = 0; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 737 | int noblock; |
| 738 | |
| 739 | noblock = flags & MSG_DONTWAIT; |
| 740 | flags &= ~MSG_DONTWAIT; |
| 741 | |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 742 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 743 | if (!skb) |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 744 | return err; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 745 | |
Oliver Hartkopp | 821047c | 2014-03-01 15:31:53 +0100 | [diff] [blame] | 746 | if (size < skb->len) |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 747 | msg->msg_flags |= MSG_TRUNC; |
| 748 | else |
Oliver Hartkopp | 821047c | 2014-03-01 15:31:53 +0100 | [diff] [blame] | 749 | size = skb->len; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 750 | |
Al Viro | 7eab8d9 | 2014-04-06 21:51:23 -0400 | [diff] [blame] | 751 | err = memcpy_to_msg(msg, skb->data, size); |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 752 | if (err < 0) { |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 753 | skb_free_datagram(sk, skb); |
Urs Thuermann | a219994 | 2008-02-07 18:05:04 -0800 | [diff] [blame] | 754 | return err; |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 757 | sock_recv_ts_and_drops(msg, sk, skb); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 758 | |
| 759 | if (msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 760 | __sockaddr_check_size(sizeof(struct sockaddr_can)); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 761 | msg->msg_namelen = sizeof(struct sockaddr_can); |
| 762 | memcpy(msg->msg_name, skb->cb, msg->msg_namelen); |
| 763 | } |
| 764 | |
Oliver Hartkopp | 1e55659 | 2010-10-19 09:32:04 +0000 | [diff] [blame] | 765 | /* assign the flags that have been recorded in raw_rcv() */ |
| 766 | msg->msg_flags |= *(raw_flags(skb)); |
| 767 | |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 768 | skb_free_datagram(sk, skb); |
| 769 | |
| 770 | return size; |
| 771 | } |
| 772 | |
Oliver Hartkopp | 53914b6 | 2011-03-22 08:27:25 +0000 | [diff] [blame] | 773 | static const struct proto_ops raw_ops = { |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 774 | .family = PF_CAN, |
| 775 | .release = raw_release, |
| 776 | .bind = raw_bind, |
| 777 | .connect = sock_no_connect, |
| 778 | .socketpair = sock_no_socketpair, |
| 779 | .accept = sock_no_accept, |
| 780 | .getname = raw_getname, |
| 781 | .poll = datagram_poll, |
Oliver Hartkopp | 53914b6 | 2011-03-22 08:27:25 +0000 | [diff] [blame] | 782 | .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */ |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 783 | .listen = sock_no_listen, |
| 784 | .shutdown = sock_no_shutdown, |
| 785 | .setsockopt = raw_setsockopt, |
| 786 | .getsockopt = raw_getsockopt, |
| 787 | .sendmsg = raw_sendmsg, |
| 788 | .recvmsg = raw_recvmsg, |
| 789 | .mmap = sock_no_mmap, |
| 790 | .sendpage = sock_no_sendpage, |
| 791 | }; |
| 792 | |
| 793 | static struct proto raw_proto __read_mostly = { |
| 794 | .name = "CAN_RAW", |
| 795 | .owner = THIS_MODULE, |
| 796 | .obj_size = sizeof(struct raw_sock), |
| 797 | .init = raw_init, |
| 798 | }; |
| 799 | |
Kurt Van Dijck | 1650629 | 2011-05-03 18:40:57 +0000 | [diff] [blame] | 800 | static const struct can_proto raw_can_proto = { |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 801 | .type = SOCK_RAW, |
| 802 | .protocol = CAN_RAW, |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 803 | .ops = &raw_ops, |
| 804 | .prot = &raw_proto, |
| 805 | }; |
| 806 | |
| 807 | static __init int raw_module_init(void) |
| 808 | { |
| 809 | int err; |
| 810 | |
Jeremiah Mahler | b111b78 | 2014-11-21 23:42:35 -0800 | [diff] [blame] | 811 | pr_info("can: raw protocol (rev " CAN_RAW_VERSION ")\n"); |
Oliver Hartkopp | c18ce10 | 2007-11-16 15:53:09 -0800 | [diff] [blame] | 812 | |
| 813 | err = can_proto_register(&raw_can_proto); |
| 814 | if (err < 0) |
| 815 | printk(KERN_ERR "can: registration of raw protocol failed\n"); |
| 816 | |
| 817 | return err; |
| 818 | } |
| 819 | |
| 820 | static __exit void raw_module_exit(void) |
| 821 | { |
| 822 | can_proto_unregister(&raw_can_proto); |
| 823 | } |
| 824 | |
| 825 | module_init(raw_module_init); |
| 826 | module_exit(raw_module_exit); |