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