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