linux/net/can/bcm.c
<<
>>
Prefs
   1/*
   2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
   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/hrtimer.h>
  47#include <linux/list.h>
  48#include <linux/proc_fs.h>
  49#include <linux/uio.h>
  50#include <linux/net.h>
  51#include <linux/netdevice.h>
  52#include <linux/socket.h>
  53#include <linux/if_arp.h>
  54#include <linux/skbuff.h>
  55#include <linux/can.h>
  56#include <linux/can/core.h>
  57#include <linux/can/bcm.h>
  58#include <net/sock.h>
  59#include <net/net_namespace.h>
  60
  61/*
  62 * To send multiple CAN frame content within TX_SETUP or to filter
  63 * CAN messages with multiplex index within RX_SETUP, the number of
  64 * different filters is limited to 256 due to the one byte index value.
  65 */
  66#define MAX_NFRAMES 256
  67
  68/* use of last_frames[index].can_dlc */
  69#define RX_RECV    0x40 /* received data for this element */
  70#define RX_THR     0x80 /* element not been sent due to throttle feature */
  71#define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
  72
  73/* get best masking value for can_rx_register() for a given single can_id */
  74#define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
  75                     (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
  76                     (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
  77
  78#define CAN_BCM_VERSION CAN_VERSION
  79static __initdata const char banner[] = KERN_INFO
  80        "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
  81
  82MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
  83MODULE_LICENSE("Dual BSD/GPL");
  84MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  85
  86/* easy access to can_frame payload */
  87static inline u64 GET_U64(const struct can_frame *cp)
  88{
  89        return *(u64 *)cp->data;
  90}
  91
  92struct bcm_op {
  93        struct list_head list;
  94        int ifindex;
  95        canid_t can_id;
  96        u32 flags;
  97        unsigned long frames_abs, frames_filtered;
  98        struct timeval ival1, ival2;
  99        struct hrtimer timer, thrtimer;
 100        ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
 101        int rx_ifindex;
 102        u32 count;
 103        u32 nframes;
 104        u32 currframe;
 105        struct can_frame *frames;
 106        struct can_frame *last_frames;
 107        struct can_frame sframe;
 108        struct can_frame last_sframe;
 109        struct sock *sk;
 110        struct net_device *rx_reg_dev;
 111};
 112
 113static struct proc_dir_entry *proc_dir;
 114
 115struct bcm_sock {
 116        struct sock sk;
 117        int bound;
 118        int ifindex;
 119        struct notifier_block notifier;
 120        struct list_head rx_ops;
 121        struct list_head tx_ops;
 122        unsigned long dropped_usr_msgs;
 123        struct proc_dir_entry *bcm_proc_read;
 124        char procname [32]; /* inode number in decimal with \0 */
 125};
 126
 127static inline struct bcm_sock *bcm_sk(const struct sock *sk)
 128{
 129        return (struct bcm_sock *)sk;
 130}
 131
 132#define CFSIZ sizeof(struct can_frame)
 133#define OPSIZ sizeof(struct bcm_op)
 134#define MHSIZ sizeof(struct bcm_msg_head)
 135
 136/*
 137 * procfs functions
 138 */
 139static char *bcm_proc_getifname(int ifindex)
 140{
 141        struct net_device *dev;
 142
 143        if (!ifindex)
 144                return "any";
 145
 146        /* no usage counting */
 147        dev = __dev_get_by_index(&init_net, ifindex);
 148        if (dev)
 149                return dev->name;
 150
 151        return "???";
 152}
 153
 154static int bcm_read_proc(char *page, char **start, off_t off,
 155                         int count, int *eof, void *data)
 156{
 157        int len = 0;
 158        struct sock *sk = (struct sock *)data;
 159        struct bcm_sock *bo = bcm_sk(sk);
 160        struct bcm_op *op;
 161
 162        len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
 163                        sk->sk_socket);
 164        len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
 165        len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
 166        len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
 167                        bo->dropped_usr_msgs);
 168        len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
 169                        bcm_proc_getifname(bo->ifindex));
 170        len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
 171
 172        list_for_each_entry(op, &bo->rx_ops, list) {
 173
 174                unsigned long reduction;
 175
 176                /* print only active entries & prevent division by zero */
 177                if (!op->frames_abs)
 178                        continue;
 179
 180                len += snprintf(page + len, PAGE_SIZE - len,
 181                                "rx_op: %03X %-5s ",
 182                                op->can_id, bcm_proc_getifname(op->ifindex));
 183                len += snprintf(page + len, PAGE_SIZE - len, "[%u]%c ",
 184                                op->nframes,
 185                                (op->flags & RX_CHECK_DLC)?'d':' ');
 186                if (op->kt_ival1.tv64)
 187                        len += snprintf(page + len, PAGE_SIZE - len,
 188                                        "timeo=%lld ",
 189                                        (long long)
 190                                        ktime_to_us(op->kt_ival1));
 191
 192                if (op->kt_ival2.tv64)
 193                        len += snprintf(page + len, PAGE_SIZE - len,
 194                                        "thr=%lld ",
 195                                        (long long)
 196                                        ktime_to_us(op->kt_ival2));
 197
 198                len += snprintf(page + len, PAGE_SIZE - len,
 199                                "# recv %ld (%ld) => reduction: ",
 200                                op->frames_filtered, op->frames_abs);
 201
 202                reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
 203
 204                len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
 205                                (reduction == 100)?"near ":"", reduction);
 206
 207                if (len > PAGE_SIZE - 200) {
 208                        /* mark output cut off */
 209                        len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
 210                        break;
 211                }
 212        }
 213
 214        list_for_each_entry(op, &bo->tx_ops, list) {
 215
 216                len += snprintf(page + len, PAGE_SIZE - len,
 217                                "tx_op: %03X %s [%u] ",
 218                                op->can_id, bcm_proc_getifname(op->ifindex),
 219                                op->nframes);
 220
 221                if (op->kt_ival1.tv64)
 222                        len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
 223                                        (long long) ktime_to_us(op->kt_ival1));
 224
 225                if (op->kt_ival2.tv64)
 226                        len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
 227                                        (long long) ktime_to_us(op->kt_ival2));
 228
 229                len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
 230                                op->frames_abs);
 231
 232                if (len > PAGE_SIZE - 100) {
 233                        /* mark output cut off */
 234                        len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
 235                        break;
 236                }
 237        }
 238
 239        len += snprintf(page + len, PAGE_SIZE - len, "\n");
 240
 241        *eof = 1;
 242        return len;
 243}
 244
 245/*
 246 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
 247 *              of the given bcm tx op
 248 */
 249static void bcm_can_tx(struct bcm_op *op)
 250{
 251        struct sk_buff *skb;
 252        struct net_device *dev;
 253        struct can_frame *cf = &op->frames[op->currframe];
 254
 255        /* no target device? => exit */
 256        if (!op->ifindex)
 257                return;
 258
 259        dev = dev_get_by_index(&init_net, op->ifindex);
 260        if (!dev) {
 261                /* RFC: should this bcm_op remove itself here? */
 262                return;
 263        }
 264
 265        skb = alloc_skb(CFSIZ, gfp_any());
 266        if (!skb)
 267                goto out;
 268
 269        memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
 270
 271        /* send with loopback */
 272        skb->dev = dev;
 273        skb->sk = op->sk;
 274        can_send(skb, 1);
 275
 276        /* update statistics */
 277        op->currframe++;
 278        op->frames_abs++;
 279
 280        /* reached last frame? */
 281        if (op->currframe >= op->nframes)
 282                op->currframe = 0;
 283 out:
 284        dev_put(dev);
 285}
 286
 287/*
 288 * bcm_send_to_user - send a BCM message to the userspace
 289 *                    (consisting of bcm_msg_head + x CAN frames)
 290 */
 291static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
 292                             struct can_frame *frames, int has_timestamp)
 293{
 294        struct sk_buff *skb;
 295        struct can_frame *firstframe;
 296        struct sockaddr_can *addr;
 297        struct sock *sk = op->sk;
 298        unsigned int datalen = head->nframes * CFSIZ;
 299        int err;
 300
 301        skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
 302        if (!skb)
 303                return;
 304
 305        memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
 306
 307        if (head->nframes) {
 308                /* can_frames starting here */
 309                firstframe = (struct can_frame *)skb_tail_pointer(skb);
 310
 311                memcpy(skb_put(skb, datalen), frames, datalen);
 312
 313                /*
 314                 * the BCM uses the can_dlc-element of the can_frame
 315                 * structure for internal purposes. This is only
 316                 * relevant for updates that are generated by the
 317                 * BCM, where nframes is 1
 318                 */
 319                if (head->nframes == 1)
 320                        firstframe->can_dlc &= BCM_CAN_DLC_MASK;
 321        }
 322
 323        if (has_timestamp) {
 324                /* restore rx timestamp */
 325                skb->tstamp = op->rx_stamp;
 326        }
 327
 328        /*
 329         *  Put the datagram to the queue so that bcm_recvmsg() can
 330         *  get it from there.  We need to pass the interface index to
 331         *  bcm_recvmsg().  We pass a whole struct sockaddr_can in skb->cb
 332         *  containing the interface index.
 333         */
 334
 335        BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
 336        addr = (struct sockaddr_can *)skb->cb;
 337        memset(addr, 0, sizeof(*addr));
 338        addr->can_family  = AF_CAN;
 339        addr->can_ifindex = op->rx_ifindex;
 340
 341        err = sock_queue_rcv_skb(sk, skb);
 342        if (err < 0) {
 343                struct bcm_sock *bo = bcm_sk(sk);
 344
 345                kfree_skb(skb);
 346                /* don't care about overflows in this statistic */
 347                bo->dropped_usr_msgs++;
 348        }
 349}
 350
 351/*
 352 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
 353 */
 354static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
 355{
 356        struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
 357        enum hrtimer_restart ret = HRTIMER_NORESTART;
 358
 359        if (op->kt_ival1.tv64 && (op->count > 0)) {
 360
 361                op->count--;
 362                if (!op->count && (op->flags & TX_COUNTEVT)) {
 363                        struct bcm_msg_head msg_head;
 364
 365                        /* create notification to user */
 366                        msg_head.opcode  = TX_EXPIRED;
 367                        msg_head.flags   = op->flags;
 368                        msg_head.count   = op->count;
 369                        msg_head.ival1   = op->ival1;
 370                        msg_head.ival2   = op->ival2;
 371                        msg_head.can_id  = op->can_id;
 372                        msg_head.nframes = 0;
 373
 374                        bcm_send_to_user(op, &msg_head, NULL, 0);
 375                }
 376        }
 377
 378        if (op->kt_ival1.tv64 && (op->count > 0)) {
 379
 380                /* send (next) frame */
 381                bcm_can_tx(op);
 382                hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
 383                ret = HRTIMER_RESTART;
 384
 385        } else {
 386                if (op->kt_ival2.tv64) {
 387
 388                        /* send (next) frame */
 389                        bcm_can_tx(op);
 390                        hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
 391                        ret = HRTIMER_RESTART;
 392                }
 393        }
 394
 395        return ret;
 396}
 397
 398/*
 399 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
 400 */
 401static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
 402{
 403        struct bcm_msg_head head;
 404
 405        /* update statistics */
 406        op->frames_filtered++;
 407
 408        /* prevent statistics overflow */
 409        if (op->frames_filtered > ULONG_MAX/100)
 410                op->frames_filtered = op->frames_abs = 0;
 411
 412        head.opcode  = RX_CHANGED;
 413        head.flags   = op->flags;
 414        head.count   = op->count;
 415        head.ival1   = op->ival1;
 416        head.ival2   = op->ival2;
 417        head.can_id  = op->can_id;
 418        head.nframes = 1;
 419
 420        bcm_send_to_user(op, &head, data, 1);
 421}
 422
 423/*
 424 * bcm_rx_update_and_send - process a detected relevant receive content change
 425 *                          1. update the last received data
 426 *                          2. send a notification to the user (if possible)
 427 */
 428static void bcm_rx_update_and_send(struct bcm_op *op,
 429                                   struct can_frame *lastdata,
 430                                   struct can_frame *rxdata)
 431{
 432        memcpy(lastdata, rxdata, CFSIZ);
 433
 434        /* mark as used */
 435        lastdata->can_dlc |= RX_RECV;
 436
 437        /* throtteling mode inactive OR data update already on the run ? */
 438        if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
 439                /* send RX_CHANGED to the user immediately */
 440                bcm_rx_changed(op, rxdata);
 441                return;
 442        }
 443
 444        if (hrtimer_active(&op->thrtimer)) {
 445                /* mark as 'throttled' */
 446                lastdata->can_dlc |= RX_THR;
 447                return;
 448        }
 449
 450        if (!op->kt_lastmsg.tv64) {
 451                /* send first RX_CHANGED to the user immediately */
 452                bcm_rx_changed(op, rxdata);
 453                op->kt_lastmsg = ktime_get();
 454                return;
 455        }
 456
 457        if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
 458            ktime_to_us(op->kt_ival2)) {
 459                /* mark as 'throttled' and start timer */
 460                lastdata->can_dlc |= RX_THR;
 461                hrtimer_start(&op->thrtimer,
 462                              ktime_add(op->kt_lastmsg, op->kt_ival2),
 463                              HRTIMER_MODE_ABS);
 464                return;
 465        }
 466
 467        /* the gap was that big, that throttling was not needed here */
 468        bcm_rx_changed(op, rxdata);
 469        op->kt_lastmsg = ktime_get();
 470}
 471
 472/*
 473 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
 474 *                       received data stored in op->last_frames[]
 475 */
 476static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
 477                                struct can_frame *rxdata)
 478{
 479        /*
 480         * no one uses the MSBs of can_dlc for comparation,
 481         * so we use it here to detect the first time of reception
 482         */
 483
 484        if (!(op->last_frames[index].can_dlc & RX_RECV)) {
 485                /* received data for the first time => send update to user */
 486                bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
 487                return;
 488        }
 489
 490        /* do a real check in can_frame data section */
 491
 492        if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
 493            (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
 494                bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
 495                return;
 496        }
 497
 498        if (op->flags & RX_CHECK_DLC) {
 499                /* do a real check in can_frame dlc */
 500                if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
 501                                        BCM_CAN_DLC_MASK)) {
 502                        bcm_rx_update_and_send(op, &op->last_frames[index],
 503                                               rxdata);
 504                        return;
 505                }
 506        }
 507}
 508
 509/*
 510 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
 511 */
 512static void bcm_rx_starttimer(struct bcm_op *op)
 513{
 514        if (op->flags & RX_NO_AUTOTIMER)
 515                return;
 516
 517        if (op->kt_ival1.tv64)
 518                hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
 519}
 520
 521/*
 522 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
 523 */
 524static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
 525{
 526        struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
 527        struct bcm_msg_head msg_head;
 528
 529        msg_head.opcode  = RX_TIMEOUT;
 530        msg_head.flags   = op->flags;
 531        msg_head.count   = op->count;
 532        msg_head.ival1   = op->ival1;
 533        msg_head.ival2   = op->ival2;
 534        msg_head.can_id  = op->can_id;
 535        msg_head.nframes = 0;
 536
 537        bcm_send_to_user(op, &msg_head, NULL, 0);
 538
 539        /* no restart of the timer is done here! */
 540
 541        /* if user wants to be informed, when cyclic CAN-Messages come back */
 542        if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
 543                /* clear received can_frames to indicate 'nothing received' */
 544                memset(op->last_frames, 0, op->nframes * CFSIZ);
 545        }
 546
 547        return HRTIMER_NORESTART;
 548}
 549
 550/*
 551 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
 552 */
 553static int bcm_rx_thr_flush(struct bcm_op *op)
 554{
 555        int updated = 0;
 556
 557        if (op->nframes > 1) {
 558                unsigned int i;
 559
 560                /* for MUX filter we start at index 1 */
 561                for (i = 1; i < op->nframes; i++) {
 562                        if ((op->last_frames) &&
 563                            (op->last_frames[i].can_dlc & RX_THR)) {
 564                                op->last_frames[i].can_dlc &= ~RX_THR;
 565                                bcm_rx_changed(op, &op->last_frames[i]);
 566                                updated++;
 567                        }
 568                }
 569
 570        } else {
 571                /* for RX_FILTER_ID and simple filter */
 572                if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
 573                        op->last_frames[0].can_dlc &= ~RX_THR;
 574                        bcm_rx_changed(op, &op->last_frames[0]);
 575                        updated++;
 576                }
 577        }
 578
 579        return updated;
 580}
 581
 582/*
 583 * bcm_rx_thr_handler - the time for blocked content updates is over now:
 584 *                      Check for throttled data and send it to the userspace
 585 */
 586static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
 587{
 588        struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
 589
 590        if (bcm_rx_thr_flush(op)) {
 591                hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
 592                return HRTIMER_RESTART;
 593        } else {
 594                /* rearm throttle handling */
 595                op->kt_lastmsg = ktime_set(0, 0);
 596                return HRTIMER_NORESTART;
 597        }
 598}
 599
 600/*
 601 * bcm_rx_handler - handle a CAN frame receiption
 602 */
 603static void bcm_rx_handler(struct sk_buff *skb, void *data)
 604{
 605        struct bcm_op *op = (struct bcm_op *)data;
 606        struct can_frame rxframe;
 607        unsigned int i;
 608
 609        /* disable timeout */
 610        hrtimer_cancel(&op->timer);
 611
 612        if (skb->len == sizeof(rxframe)) {
 613                memcpy(&rxframe, skb->data, sizeof(rxframe));
 614                /* save rx timestamp */
 615                op->rx_stamp = skb->tstamp;
 616                /* save originator for recvfrom() */
 617                op->rx_ifindex = skb->dev->ifindex;
 618                /* update statistics */
 619                op->frames_abs++;
 620                kfree_skb(skb);
 621
 622        } else {
 623                kfree_skb(skb);
 624                return;
 625        }
 626
 627        if (op->can_id != rxframe.can_id)
 628                return;
 629
 630        if (op->flags & RX_RTR_FRAME) {
 631                /* send reply for RTR-request (placed in op->frames[0]) */
 632                bcm_can_tx(op);
 633                return;
 634        }
 635
 636        if (op->flags & RX_FILTER_ID) {
 637                /* the easiest case */
 638                bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
 639                bcm_rx_starttimer(op);
 640                return;
 641        }
 642
 643        if (op->nframes == 1) {
 644                /* simple compare with index 0 */
 645                bcm_rx_cmp_to_index(op, 0, &rxframe);
 646                bcm_rx_starttimer(op);
 647                return;
 648        }
 649
 650        if (op->nframes > 1) {
 651                /*
 652                 * multiplex compare
 653                 *
 654                 * find the first multiplex mask that fits.
 655                 * Remark: The MUX-mask is stored in index 0
 656                 */
 657
 658                for (i = 1; i < op->nframes; i++) {
 659                        if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
 660                            (GET_U64(&op->frames[0]) &
 661                             GET_U64(&op->frames[i]))) {
 662                                bcm_rx_cmp_to_index(op, i, &rxframe);
 663                                break;
 664                        }
 665                }
 666                bcm_rx_starttimer(op);
 667        }
 668}
 669
 670/*
 671 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
 672 */
 673static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
 674                                  int ifindex)
 675{
 676        struct bcm_op *op;
 677
 678        list_for_each_entry(op, ops, list) {
 679                if ((op->can_id == can_id) && (op->ifindex == ifindex))
 680                        return op;
 681        }
 682
 683        return NULL;
 684}
 685
 686static void bcm_remove_op(struct bcm_op *op)
 687{
 688        hrtimer_cancel(&op->timer);
 689        hrtimer_cancel(&op->thrtimer);
 690
 691        if ((op->frames) && (op->frames != &op->sframe))
 692                kfree(op->frames);
 693
 694        if ((op->last_frames) && (op->last_frames != &op->last_sframe))
 695                kfree(op->last_frames);
 696
 697        kfree(op);
 698
 699        return;
 700}
 701
 702static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
 703{
 704        if (op->rx_reg_dev == dev) {
 705                can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
 706                                  bcm_rx_handler, op);
 707
 708                /* mark as removed subscription */
 709                op->rx_reg_dev = NULL;
 710        } else
 711                printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
 712                       "mismatch %p %p\n", op->rx_reg_dev, dev);
 713}
 714
 715/*
 716 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
 717 */
 718static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
 719{
 720        struct bcm_op *op, *n;
 721
 722        list_for_each_entry_safe(op, n, ops, list) {
 723                if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
 724
 725                        /*
 726                         * Don't care if we're bound or not (due to netdev
 727                         * problems) can_rx_unregister() is always a save
 728                         * thing to do here.
 729                         */
 730                        if (op->ifindex) {
 731                                /*
 732                                 * Only remove subscriptions that had not
 733                                 * been removed due to NETDEV_UNREGISTER
 734                                 * in bcm_notifier()
 735                                 */
 736                                if (op->rx_reg_dev) {
 737                                        struct net_device *dev;
 738
 739                                        dev = dev_get_by_index(&init_net,
 740                                                               op->ifindex);
 741                                        if (dev) {
 742                                                bcm_rx_unreg(dev, op);
 743                                                dev_put(dev);
 744                                        }
 745                                }
 746                        } else
 747                                can_rx_unregister(NULL, op->can_id,
 748                                                  REGMASK(op->can_id),
 749                                                  bcm_rx_handler, op);
 750
 751                        list_del(&op->list);
 752                        bcm_remove_op(op);
 753                        return 1; /* done */
 754                }
 755        }
 756
 757        return 0; /* not found */
 758}
 759
 760/*
 761 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
 762 */
 763static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
 764{
 765        struct bcm_op *op, *n;
 766
 767        list_for_each_entry_safe(op, n, ops, list) {
 768                if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
 769                        list_del(&op->list);
 770                        bcm_remove_op(op);
 771                        return 1; /* done */
 772                }
 773        }
 774
 775        return 0; /* not found */
 776}
 777
 778/*
 779 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
 780 */
 781static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
 782                       int ifindex)
 783{
 784        struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
 785
 786        if (!op)
 787                return -EINVAL;
 788
 789        /* put current values into msg_head */
 790        msg_head->flags   = op->flags;
 791        msg_head->count   = op->count;
 792        msg_head->ival1   = op->ival1;
 793        msg_head->ival2   = op->ival2;
 794        msg_head->nframes = op->nframes;
 795
 796        bcm_send_to_user(op, msg_head, op->frames, 0);
 797
 798        return MHSIZ;
 799}
 800
 801/*
 802 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
 803 */
 804static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 805                        int ifindex, struct sock *sk)
 806{
 807        struct bcm_sock *bo = bcm_sk(sk);
 808        struct bcm_op *op;
 809        unsigned int i;
 810        int err;
 811
 812        /* we need a real device to send frames */
 813        if (!ifindex)
 814                return -ENODEV;
 815
 816        /* check nframes boundaries - we need at least one can_frame */
 817        if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
 818                return -EINVAL;
 819
 820        /* check the given can_id */
 821        op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
 822
 823        if (op) {
 824                /* update existing BCM operation */
 825
 826                /*
 827                 * Do we need more space for the can_frames than currently
 828                 * allocated? -> This is a _really_ unusual use-case and
 829                 * therefore (complexity / locking) it is not supported.
 830                 */
 831                if (msg_head->nframes > op->nframes)
 832                        return -E2BIG;
 833
 834                /* update can_frames content */
 835                for (i = 0; i < msg_head->nframes; i++) {
 836                        err = memcpy_fromiovec((u8 *)&op->frames[i],
 837                                               msg->msg_iov, CFSIZ);
 838
 839                        if (op->frames[i].can_dlc > 8)
 840                                err = -EINVAL;
 841
 842                        if (err < 0)
 843                                return err;
 844
 845                        if (msg_head->flags & TX_CP_CAN_ID) {
 846                                /* copy can_id into frame */
 847                                op->frames[i].can_id = msg_head->can_id;
 848                        }
 849                }
 850
 851        } else {
 852                /* insert new BCM operation for the given can_id */
 853
 854                op = kzalloc(OPSIZ, GFP_KERNEL);
 855                if (!op)
 856                        return -ENOMEM;
 857
 858                op->can_id    = msg_head->can_id;
 859
 860                /* create array for can_frames and copy the data */
 861                if (msg_head->nframes > 1) {
 862                        op->frames = kmalloc(msg_head->nframes * CFSIZ,
 863                                             GFP_KERNEL);
 864                        if (!op->frames) {
 865                                kfree(op);
 866                                return -ENOMEM;
 867                        }
 868                } else
 869                        op->frames = &op->sframe;
 870
 871                for (i = 0; i < msg_head->nframes; i++) {
 872                        err = memcpy_fromiovec((u8 *)&op->frames[i],
 873                                               msg->msg_iov, CFSIZ);
 874
 875                        if (op->frames[i].can_dlc > 8)
 876                                err = -EINVAL;
 877
 878                        if (err < 0) {
 879                                if (op->frames != &op->sframe)
 880                                        kfree(op->frames);
 881                                kfree(op);
 882                                return err;
 883                        }
 884
 885                        if (msg_head->flags & TX_CP_CAN_ID) {
 886                                /* copy can_id into frame */
 887                                op->frames[i].can_id = msg_head->can_id;
 888                        }
 889                }
 890
 891                /* tx_ops never compare with previous received messages */
 892                op->last_frames = NULL;
 893
 894                /* bcm_can_tx / bcm_tx_timeout_handler needs this */
 895                op->sk = sk;
 896                op->ifindex = ifindex;
 897
 898                /* initialize uninitialized (kzalloc) structure */
 899                hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 900                op->timer.function = bcm_tx_timeout_handler;
 901
 902                /* currently unused in tx_ops */
 903                hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 904
 905                /* add this bcm_op to the list of the tx_ops */
 906                list_add(&op->list, &bo->tx_ops);
 907
 908        } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
 909
 910        if (op->nframes != msg_head->nframes) {
 911                op->nframes   = msg_head->nframes;
 912                /* start multiple frame transmission with index 0 */
 913                op->currframe = 0;
 914        }
 915
 916        /* check flags */
 917
 918        op->flags = msg_head->flags;
 919
 920        if (op->flags & TX_RESET_MULTI_IDX) {
 921                /* start multiple frame transmission with index 0 */
 922                op->currframe = 0;
 923        }
 924
 925        if (op->flags & SETTIMER) {
 926                /* set timer values */
 927                op->count = msg_head->count;
 928                op->ival1 = msg_head->ival1;
 929                op->ival2 = msg_head->ival2;
 930                op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
 931                op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
 932
 933                /* disable an active timer due to zero values? */
 934                if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
 935                        hrtimer_cancel(&op->timer);
 936        }
 937
 938        if ((op->flags & STARTTIMER) &&
 939            ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
 940
 941                /* spec: send can_frame when starting timer */
 942                op->flags |= TX_ANNOUNCE;
 943
 944                if (op->kt_ival1.tv64 && (op->count > 0)) {
 945                        /* op->count-- is done in bcm_tx_timeout_handler */
 946                        hrtimer_start(&op->timer, op->kt_ival1,
 947                                      HRTIMER_MODE_REL);
 948                } else
 949                        hrtimer_start(&op->timer, op->kt_ival2,
 950                                      HRTIMER_MODE_REL);
 951        }
 952
 953        if (op->flags & TX_ANNOUNCE)
 954                bcm_can_tx(op);
 955
 956        return msg_head->nframes * CFSIZ + MHSIZ;
 957}
 958
 959/*
 960 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
 961 */
 962static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 963                        int ifindex, struct sock *sk)
 964{
 965        struct bcm_sock *bo = bcm_sk(sk);
 966        struct bcm_op *op;
 967        int do_rx_register;
 968        int err = 0;
 969
 970        if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
 971                /* be robust against wrong usage ... */
 972                msg_head->flags |= RX_FILTER_ID;
 973                /* ignore trailing garbage */
 974                msg_head->nframes = 0;
 975        }
 976
 977        /* the first element contains the mux-mask => MAX_NFRAMES + 1  */
 978        if (msg_head->nframes > MAX_NFRAMES + 1)
 979                return -EINVAL;
 980
 981        if ((msg_head->flags & RX_RTR_FRAME) &&
 982            ((msg_head->nframes != 1) ||
 983             (!(msg_head->can_id & CAN_RTR_FLAG))))
 984                return -EINVAL;
 985
 986        /* check the given can_id */
 987        op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
 988        if (op) {
 989                /* update existing BCM operation */
 990
 991                /*
 992                 * Do we need more space for the can_frames than currently
 993                 * allocated? -> This is a _really_ unusual use-case and
 994                 * therefore (complexity / locking) it is not supported.
 995                 */
 996                if (msg_head->nframes > op->nframes)
 997                        return -E2BIG;
 998
 999                if (msg_head->nframes) {
1000                        /* update can_frames content */
1001                        err = memcpy_fromiovec((u8 *)op->frames,
1002                                               msg->msg_iov,
1003                                               msg_head->nframes * CFSIZ);
1004                        if (err < 0)
1005                                return err;
1006
1007                        /* clear last_frames to indicate 'nothing received' */
1008                        memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1009                }
1010
1011                op->nframes = msg_head->nframes;
1012
1013                /* Only an update -> do not call can_rx_register() */
1014                do_rx_register = 0;
1015
1016        } else {
1017                /* insert new BCM operation for the given can_id */
1018                op = kzalloc(OPSIZ, GFP_KERNEL);
1019                if (!op)
1020                        return -ENOMEM;
1021
1022                op->can_id    = msg_head->can_id;
1023                op->nframes   = msg_head->nframes;
1024
1025                if (msg_head->nframes > 1) {
1026                        /* create array for can_frames and copy the data */
1027                        op->frames = kmalloc(msg_head->nframes * CFSIZ,
1028                                             GFP_KERNEL);
1029                        if (!op->frames) {
1030                                kfree(op);
1031                                return -ENOMEM;
1032                        }
1033
1034                        /* create and init array for received can_frames */
1035                        op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1036                                                  GFP_KERNEL);
1037                        if (!op->last_frames) {
1038                                kfree(op->frames);
1039                                kfree(op);
1040                                return -ENOMEM;
1041                        }
1042
1043                } else {
1044                        op->frames = &op->sframe;
1045                        op->last_frames = &op->last_sframe;
1046                }
1047
1048                if (msg_head->nframes) {
1049                        err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1050                                               msg_head->nframes * CFSIZ);
1051                        if (err < 0) {
1052                                if (op->frames != &op->sframe)
1053                                        kfree(op->frames);
1054                                if (op->last_frames != &op->last_sframe)
1055                                        kfree(op->last_frames);
1056                                kfree(op);
1057                                return err;
1058                        }
1059                }
1060
1061                /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1062                op->sk = sk;
1063                op->ifindex = ifindex;
1064
1065                /* initialize uninitialized (kzalloc) structure */
1066                hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1067                op->timer.function = bcm_rx_timeout_handler;
1068
1069                hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1070                op->thrtimer.function = bcm_rx_thr_handler;
1071
1072                /* add this bcm_op to the list of the rx_ops */
1073                list_add(&op->list, &bo->rx_ops);
1074
1075                /* call can_rx_register() */
1076                do_rx_register = 1;
1077
1078        } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1079
1080        /* check flags */
1081        op->flags = msg_head->flags;
1082
1083        if (op->flags & RX_RTR_FRAME) {
1084
1085                /* no timers in RTR-mode */
1086                hrtimer_cancel(&op->thrtimer);
1087                hrtimer_cancel(&op->timer);
1088
1089                /*
1090                 * funny feature in RX(!)_SETUP only for RTR-mode:
1091                 * copy can_id into frame BUT without RTR-flag to
1092                 * prevent a full-load-loopback-test ... ;-]
1093                 */
1094                if ((op->flags & TX_CP_CAN_ID) ||
1095                    (op->frames[0].can_id == op->can_id))
1096                        op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1097
1098        } else {
1099                if (op->flags & SETTIMER) {
1100
1101                        /* set timer value */
1102                        op->ival1 = msg_head->ival1;
1103                        op->ival2 = msg_head->ival2;
1104                        op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1105                        op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1106
1107                        /* disable an active timer due to zero value? */
1108                        if (!op->kt_ival1.tv64)
1109                                hrtimer_cancel(&op->timer);
1110
1111                        /*
1112                         * In any case cancel the throttle timer, flush
1113                         * potentially blocked msgs and reset throttle handling
1114                         */
1115                        op->kt_lastmsg = ktime_set(0, 0);
1116                        hrtimer_cancel(&op->thrtimer);
1117                        bcm_rx_thr_flush(op);
1118                }
1119
1120                if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1121                        hrtimer_start(&op->timer, op->kt_ival1,
1122                                      HRTIMER_MODE_REL);
1123        }
1124
1125        /* now we can register for can_ids, if we added a new bcm_op */
1126        if (do_rx_register) {
1127                if (ifindex) {
1128                        struct net_device *dev;
1129
1130                        dev = dev_get_by_index(&init_net, ifindex);
1131                        if (dev) {
1132                                err = can_rx_register(dev, op->can_id,
1133                                                      REGMASK(op->can_id),
1134                                                      bcm_rx_handler, op,
1135                                                      "bcm");
1136
1137                                op->rx_reg_dev = dev;
1138                                dev_put(dev);
1139                        }
1140
1141                } else
1142                        err = can_rx_register(NULL, op->can_id,
1143                                              REGMASK(op->can_id),
1144                                              bcm_rx_handler, op, "bcm");
1145                if (err) {
1146                        /* this bcm rx op is broken -> remove it */
1147                        list_del(&op->list);
1148                        bcm_remove_op(op);
1149                        return err;
1150                }
1151        }
1152
1153        return msg_head->nframes * CFSIZ + MHSIZ;
1154}
1155
1156/*
1157 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1158 */
1159static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1160{
1161        struct sk_buff *skb;
1162        struct net_device *dev;
1163        int err;
1164
1165        /* we need a real device to send frames */
1166        if (!ifindex)
1167                return -ENODEV;
1168
1169        skb = alloc_skb(CFSIZ, GFP_KERNEL);
1170
1171        if (!skb)
1172                return -ENOMEM;
1173
1174        err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1175        if (err < 0) {
1176                kfree_skb(skb);
1177                return err;
1178        }
1179
1180        dev = dev_get_by_index(&init_net, ifindex);
1181        if (!dev) {
1182                kfree_skb(skb);
1183                return -ENODEV;
1184        }
1185
1186        skb->dev = dev;
1187        skb->sk  = sk;
1188        err = can_send(skb, 1); /* send with loopback */
1189        dev_put(dev);
1190
1191        if (err)
1192                return err;
1193
1194        return CFSIZ + MHSIZ;
1195}
1196
1197/*
1198 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1199 */
1200static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1201                       struct msghdr *msg, size_t size)
1202{
1203        struct sock *sk = sock->sk;
1204        struct bcm_sock *bo = bcm_sk(sk);
1205        int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1206        struct bcm_msg_head msg_head;
1207        int ret; /* read bytes or error codes as return value */
1208
1209        if (!bo->bound)
1210                return -ENOTCONN;
1211
1212        /* check for valid message length from userspace */
1213        if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1214                return -EINVAL;
1215
1216        /* check for alternative ifindex for this bcm_op */
1217
1218        if (!ifindex && msg->msg_name) {
1219                /* no bound device as default => check msg_name */
1220                struct sockaddr_can *addr =
1221                        (struct sockaddr_can *)msg->msg_name;
1222
1223                if (addr->can_family != AF_CAN)
1224                        return -EINVAL;
1225
1226                /* ifindex from sendto() */
1227                ifindex = addr->can_ifindex;
1228
1229                if (ifindex) {
1230                        struct net_device *dev;
1231
1232                        dev = dev_get_by_index(&init_net, ifindex);
1233                        if (!dev)
1234                                return -ENODEV;
1235
1236                        if (dev->type != ARPHRD_CAN) {
1237                                dev_put(dev);
1238                                return -ENODEV;
1239                        }
1240
1241                        dev_put(dev);
1242                }
1243        }
1244
1245        /* read message head information */
1246
1247        ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1248        if (ret < 0)
1249                return ret;
1250
1251        lock_sock(sk);
1252
1253        switch (msg_head.opcode) {
1254
1255        case TX_SETUP:
1256                ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1257                break;
1258
1259        case RX_SETUP:
1260                ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1261                break;
1262
1263        case TX_DELETE:
1264                if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1265                        ret = MHSIZ;
1266                else
1267                        ret = -EINVAL;
1268                break;
1269
1270        case RX_DELETE:
1271                if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1272                        ret = MHSIZ;
1273                else
1274                        ret = -EINVAL;
1275                break;
1276
1277        case TX_READ:
1278                /* reuse msg_head for the reply to TX_READ */
1279                msg_head.opcode  = TX_STATUS;
1280                ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1281                break;
1282
1283        case RX_READ:
1284                /* reuse msg_head for the reply to RX_READ */
1285                msg_head.opcode  = RX_STATUS;
1286                ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1287                break;
1288
1289        case TX_SEND:
1290                /* we need exactly one can_frame behind the msg head */
1291                if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1292                        ret = -EINVAL;
1293                else
1294                        ret = bcm_tx_send(msg, ifindex, sk);
1295                break;
1296
1297        default:
1298                ret = -EINVAL;
1299                break;
1300        }
1301
1302        release_sock(sk);
1303
1304        return ret;
1305}
1306
1307/*
1308 * notification handler for netdevice status changes
1309 */
1310static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1311                        void *data)
1312{
1313        struct net_device *dev = (struct net_device *)data;
1314        struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1315        struct sock *sk = &bo->sk;
1316        struct bcm_op *op;
1317        int notify_enodev = 0;
1318
1319        if (!net_eq(dev_net(dev), &init_net))
1320                return NOTIFY_DONE;
1321
1322        if (dev->type != ARPHRD_CAN)
1323                return NOTIFY_DONE;
1324
1325        switch (msg) {
1326
1327        case NETDEV_UNREGISTER:
1328                lock_sock(sk);
1329
1330                /* remove device specific receive entries */
1331                list_for_each_entry(op, &bo->rx_ops, list)
1332                        if (op->rx_reg_dev == dev)
1333                                bcm_rx_unreg(dev, op);
1334
1335                /* remove device reference, if this is our bound device */
1336                if (bo->bound && bo->ifindex == dev->ifindex) {
1337                        bo->bound   = 0;
1338                        bo->ifindex = 0;
1339                        notify_enodev = 1;
1340                }
1341
1342                release_sock(sk);
1343
1344                if (notify_enodev) {
1345                        sk->sk_err = ENODEV;
1346                        if (!sock_flag(sk, SOCK_DEAD))
1347                                sk->sk_error_report(sk);
1348                }
1349                break;
1350
1351        case NETDEV_DOWN:
1352                if (bo->bound && bo->ifindex == dev->ifindex) {
1353                        sk->sk_err = ENETDOWN;
1354                        if (!sock_flag(sk, SOCK_DEAD))
1355                                sk->sk_error_report(sk);
1356                }
1357        }
1358
1359        return NOTIFY_DONE;
1360}
1361
1362/*
1363 * initial settings for all BCM sockets to be set at socket creation time
1364 */
1365static int bcm_init(struct sock *sk)
1366{
1367        struct bcm_sock *bo = bcm_sk(sk);
1368
1369        bo->bound            = 0;
1370        bo->ifindex          = 0;
1371        bo->dropped_usr_msgs = 0;
1372        bo->bcm_proc_read    = NULL;
1373
1374        INIT_LIST_HEAD(&bo->tx_ops);
1375        INIT_LIST_HEAD(&bo->rx_ops);
1376
1377        /* set notifier */
1378        bo->notifier.notifier_call = bcm_notifier;
1379
1380        register_netdevice_notifier(&bo->notifier);
1381
1382        return 0;
1383}
1384
1385/*
1386 * standard socket functions
1387 */
1388static int bcm_release(struct socket *sock)
1389{
1390        struct sock *sk = sock->sk;
1391        struct bcm_sock *bo = bcm_sk(sk);
1392        struct bcm_op *op, *next;
1393
1394        /* remove bcm_ops, timer, rx_unregister(), etc. */
1395
1396        unregister_netdevice_notifier(&bo->notifier);
1397
1398        lock_sock(sk);
1399
1400        list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1401                bcm_remove_op(op);
1402
1403        list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1404                /*
1405                 * Don't care if we're bound or not (due to netdev problems)
1406                 * can_rx_unregister() is always a save thing to do here.
1407                 */
1408                if (op->ifindex) {
1409                        /*
1410                         * Only remove subscriptions that had not
1411                         * been removed due to NETDEV_UNREGISTER
1412                         * in bcm_notifier()
1413                         */
1414                        if (op->rx_reg_dev) {
1415                                struct net_device *dev;
1416
1417                                dev = dev_get_by_index(&init_net, op->ifindex);
1418                                if (dev) {
1419                                        bcm_rx_unreg(dev, op);
1420                                        dev_put(dev);
1421                                }
1422                        }
1423                } else
1424                        can_rx_unregister(NULL, op->can_id,
1425                                          REGMASK(op->can_id),
1426                                          bcm_rx_handler, op);
1427
1428                bcm_remove_op(op);
1429        }
1430
1431        /* remove procfs entry */
1432        if (proc_dir && bo->bcm_proc_read)
1433                remove_proc_entry(bo->procname, proc_dir);
1434
1435        /* remove device reference */
1436        if (bo->bound) {
1437                bo->bound   = 0;
1438                bo->ifindex = 0;
1439        }
1440
1441        release_sock(sk);
1442        sock_put(sk);
1443
1444        return 0;
1445}
1446
1447static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1448                       int flags)
1449{
1450        struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1451        struct sock *sk = sock->sk;
1452        struct bcm_sock *bo = bcm_sk(sk);
1453
1454        if (bo->bound)
1455                return -EISCONN;
1456
1457        /* bind a device to this socket */
1458        if (addr->can_ifindex) {
1459                struct net_device *dev;
1460
1461                dev = dev_get_by_index(&init_net, addr->can_ifindex);
1462                if (!dev)
1463                        return -ENODEV;
1464
1465                if (dev->type != ARPHRD_CAN) {
1466                        dev_put(dev);
1467                        return -ENODEV;
1468                }
1469
1470                bo->ifindex = dev->ifindex;
1471                dev_put(dev);
1472
1473        } else {
1474                /* no interface reference for ifindex = 0 ('any' CAN device) */
1475                bo->ifindex = 0;
1476        }
1477
1478        bo->bound = 1;
1479
1480        if (proc_dir) {
1481                /* unique socket address as filename */
1482                sprintf(bo->procname, "%lu", sock_i_ino(sk));
1483                bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1484                                                           proc_dir,
1485                                                           bcm_read_proc, sk);
1486        }
1487
1488        return 0;
1489}
1490
1491static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1492                       struct msghdr *msg, size_t size, int flags)
1493{
1494        struct sock *sk = sock->sk;
1495        struct sk_buff *skb;
1496        int error = 0;
1497        int noblock;
1498        int err;
1499
1500        noblock =  flags & MSG_DONTWAIT;
1501        flags   &= ~MSG_DONTWAIT;
1502        skb = skb_recv_datagram(sk, flags, noblock, &error);
1503        if (!skb)
1504                return error;
1505
1506        if (skb->len < size)
1507                size = skb->len;
1508
1509        err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1510        if (err < 0) {
1511                skb_free_datagram(sk, skb);
1512                return err;
1513        }
1514
1515        sock_recv_timestamp(msg, sk, skb);
1516
1517        if (msg->msg_name) {
1518                msg->msg_namelen = sizeof(struct sockaddr_can);
1519                memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1520        }
1521
1522        skb_free_datagram(sk, skb);
1523
1524        return size;
1525}
1526
1527static struct proto_ops bcm_ops __read_mostly = {
1528        .family        = PF_CAN,
1529        .release       = bcm_release,
1530        .bind          = sock_no_bind,
1531        .connect       = bcm_connect,
1532        .socketpair    = sock_no_socketpair,
1533        .accept        = sock_no_accept,
1534        .getname       = sock_no_getname,
1535        .poll          = datagram_poll,
1536        .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
1537        .listen        = sock_no_listen,
1538        .shutdown      = sock_no_shutdown,
1539        .setsockopt    = sock_no_setsockopt,
1540        .getsockopt    = sock_no_getsockopt,
1541        .sendmsg       = bcm_sendmsg,
1542        .recvmsg       = bcm_recvmsg,
1543        .mmap          = sock_no_mmap,
1544        .sendpage      = sock_no_sendpage,
1545};
1546
1547static struct proto bcm_proto __read_mostly = {
1548        .name       = "CAN_BCM",
1549        .owner      = THIS_MODULE,
1550        .obj_size   = sizeof(struct bcm_sock),
1551        .init       = bcm_init,
1552};
1553
1554static struct can_proto bcm_can_proto __read_mostly = {
1555        .type       = SOCK_DGRAM,
1556        .protocol   = CAN_BCM,
1557        .capability = -1,
1558        .ops        = &bcm_ops,
1559        .prot       = &bcm_proto,
1560};
1561
1562static int __init bcm_module_init(void)
1563{
1564        int err;
1565
1566        printk(banner);
1567
1568        err = can_proto_register(&bcm_can_proto);
1569        if (err < 0) {
1570                printk(KERN_ERR "can: registration of bcm protocol failed\n");
1571                return err;
1572        }
1573
1574        /* create /proc/net/can-bcm directory */
1575        proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1576
1577        if (proc_dir)
1578                proc_dir->owner = THIS_MODULE;
1579
1580        return 0;
1581}
1582
1583static void __exit bcm_module_exit(void)
1584{
1585        can_proto_unregister(&bcm_can_proto);
1586
1587        if (proc_dir)
1588                proc_net_remove(&init_net, "can-bcm");
1589}
1590
1591module_init(bcm_module_init);
1592module_exit(bcm_module_exit);
1593