linux/net/can/raw.c
<<
>>
Prefs
   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>
  48#include <linux/slab.h>
  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
  60static __initdata const char banner[] =
  61        KERN_INFO "can: raw protocol (rev " CAN_RAW_VERSION ")\n";
  62
  63MODULE_DESCRIPTION("PF_CAN raw protocol");
  64MODULE_LICENSE("Dual BSD/GPL");
  65MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>");
  66MODULE_ALIAS("can-proto-1");
  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
  80struct 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
  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 */
  98static 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
 107static inline struct raw_sock *raw_sk(const struct sock *sk)
 108{
 109        return (struct raw_sock *)sk;
 110}
 111
 112static void raw_rcv(struct sk_buff *oskb, void *data)
 113{
 114        struct sock *sk = (struct sock *)data;
 115        struct raw_sock *ro = raw_sk(sk);
 116        struct sockaddr_can *addr;
 117        struct sk_buff *skb;
 118        unsigned int *pflags;
 119
 120        /* check the received tx sock reference */
 121        if (!ro->recv_own_msgs && oskb->sk == sk)
 122                return;
 123
 124        /* clone the given skb to be able to enqueue it into the rcv queue */
 125        skb = skb_clone(oskb, GFP_ATOMIC);
 126        if (!skb)
 127                return;
 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
 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
 150        if (sock_queue_rcv_skb(sk, skb) < 0)
 151                kfree_skb(skb);
 152}
 153
 154static int raw_enable_filters(struct net_device *dev, struct sock *sk,
 155                              struct can_filter *filter, int count)
 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
 177static 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
 189static void raw_disable_filters(struct net_device *dev, struct sock *sk,
 190                              struct can_filter *filter, int count)
 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
 199static 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
 209static 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
 218static 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
 233static 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
 240        if (!net_eq(dev_net(dev), &init_net))
 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
 280static 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
 305static 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
 335        sock_orphan(sk);
 336        sock->sk = NULL;
 337
 338        release_sock(sk);
 339        sock_put(sk);
 340
 341        return 0;
 342}
 343
 344static 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);
 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
 419static 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
 429        memset(addr, 0, sizeof(*addr));
 430        addr->can_family  = AF_CAN;
 431        addr->can_ifindex = ro->ifindex;
 432
 433        *len = sizeof(*addr);
 434
 435        return 0;
 436}
 437
 438static int raw_setsockopt(struct socket *sock, int level, int optname,
 439                          char __user *optval, unsigned int optlen)
 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;
 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 */
 463                        filter = memdup_user(optval, optlen);
 464                        if (IS_ERR(filter))
 465                                return PTR_ERR(filter);
 466                } else if (count == 1) {
 467                        if (copy_from_user(&sfilter, optval, sizeof(sfilter)))
 468                                return -EFAULT;
 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);
 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
 518                if (copy_from_user(&err_mask, optval, optlen))
 519                        return -EFAULT;
 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
 555                if (copy_from_user(&ro->loopback, optval, optlen))
 556                        return -EFAULT;
 557
 558                break;
 559
 560        case CAN_RAW_RECV_OWN_MSGS:
 561                if (optlen != sizeof(ro->recv_own_msgs))
 562                        return -EINVAL;
 563
 564                if (copy_from_user(&ro->recv_own_msgs, optval, optlen))
 565                        return -EFAULT;
 566
 567                break;
 568
 569        default:
 570                return -ENOPROTOOPT;
 571        }
 572        return err;
 573}
 574
 575static 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;
 599                        if (copy_to_user(optval, ro->filter, len))
 600                                err = -EFAULT;
 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
 638static 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
 652                if (addr->can_family != AF_CAN)
 653                        return -EINVAL;
 654
 655                ifindex = addr->can_ifindex;
 656        } else
 657                ifindex = ro->ifindex;
 658
 659        if (size != sizeof(struct can_frame))
 660                return -EINVAL;
 661
 662        dev = dev_get_by_index(&init_net, ifindex);
 663        if (!dev)
 664                return -ENXIO;
 665
 666        skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT,
 667                                  &err);
 668        if (!skb)
 669                goto put_dev;
 670
 671        err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
 672        if (err < 0)
 673                goto free_skb;
 674        err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
 675        if (err < 0)
 676                goto free_skb;
 677
 678        /* to be able to check the received tx sock reference in raw_rcv() */
 679        skb_shinfo(skb)->tx_flags |= SKBTX_DRV_NEEDS_SK_REF;
 680
 681        skb->dev = dev;
 682        skb->sk  = sk;
 683
 684        err = can_send(skb, ro->loopback);
 685
 686        dev_put(dev);
 687
 688        if (err)
 689                goto send_failed;
 690
 691        return size;
 692
 693free_skb:
 694        kfree_skb(skb);
 695put_dev:
 696        dev_put(dev);
 697send_failed:
 698        return err;
 699}
 700
 701static int raw_recvmsg(struct kiocb *iocb, struct socket *sock,
 702                       struct msghdr *msg, size_t size, int flags)
 703{
 704        struct sock *sk = sock->sk;
 705        struct sk_buff *skb;
 706        int err = 0;
 707        int noblock;
 708
 709        noblock =  flags & MSG_DONTWAIT;
 710        flags   &= ~MSG_DONTWAIT;
 711
 712        skb = skb_recv_datagram(sk, flags, noblock, &err);
 713        if (!skb)
 714                return err;
 715
 716        if (size < skb->len)
 717                msg->msg_flags |= MSG_TRUNC;
 718        else
 719                size = skb->len;
 720
 721        err = memcpy_toiovec(msg->msg_iov, skb->data, size);
 722        if (err < 0) {
 723                skb_free_datagram(sk, skb);
 724                return err;
 725        }
 726
 727        sock_recv_ts_and_drops(msg, sk, skb);
 728
 729        if (msg->msg_name) {
 730                msg->msg_namelen = sizeof(struct sockaddr_can);
 731                memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
 732        }
 733
 734        /* assign the flags that have been recorded in raw_rcv() */
 735        msg->msg_flags |= *(raw_flags(skb));
 736
 737        skb_free_datagram(sk, skb);
 738
 739        return size;
 740}
 741
 742static struct proto_ops raw_ops __read_mostly = {
 743        .family        = PF_CAN,
 744        .release       = raw_release,
 745        .bind          = raw_bind,
 746        .connect       = sock_no_connect,
 747        .socketpair    = sock_no_socketpair,
 748        .accept        = sock_no_accept,
 749        .getname       = raw_getname,
 750        .poll          = datagram_poll,
 751        .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
 752        .listen        = sock_no_listen,
 753        .shutdown      = sock_no_shutdown,
 754        .setsockopt    = raw_setsockopt,
 755        .getsockopt    = raw_getsockopt,
 756        .sendmsg       = raw_sendmsg,
 757        .recvmsg       = raw_recvmsg,
 758        .mmap          = sock_no_mmap,
 759        .sendpage      = sock_no_sendpage,
 760};
 761
 762static struct proto raw_proto __read_mostly = {
 763        .name       = "CAN_RAW",
 764        .owner      = THIS_MODULE,
 765        .obj_size   = sizeof(struct raw_sock),
 766        .init       = raw_init,
 767};
 768
 769static struct can_proto raw_can_proto __read_mostly = {
 770        .type       = SOCK_RAW,
 771        .protocol   = CAN_RAW,
 772        .ops        = &raw_ops,
 773        .prot       = &raw_proto,
 774};
 775
 776static __init int raw_module_init(void)
 777{
 778        int err;
 779
 780        printk(banner);
 781
 782        err = can_proto_register(&raw_can_proto);
 783        if (err < 0)
 784                printk(KERN_ERR "can: registration of raw protocol failed\n");
 785
 786        return err;
 787}
 788
 789static __exit void raw_module_exit(void)
 790{
 791        can_proto_unregister(&raw_can_proto);
 792}
 793
 794module_init(raw_module_init);
 795module_exit(raw_module_exit);
 796