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