]> rtime.felk.cvut.cz Git - socketcan-devel.git/commitdiff
Complete reworked and beautified the Broadcast Manager (bcm)
authorhartkopp <hartkopp@030b6a49-0b11-0410-94ab-b0dab22257f2>
Thu, 1 Feb 2007 09:15:00 +0000 (09:15 +0000)
committerhartkopp <hartkopp@030b6a49-0b11-0410-94ab-b0dab22257f2>
Thu, 1 Feb 2007 09:15:00 +0000 (09:15 +0000)
The BCM now supports the binding to ifindex 0 like the raw socket does.
The first implementation of the BCM was from September 2003 and therefore
many historic stuff (e.g. unusual error reporting) was removed.
The support of ifindex 0 leads to the possibility to read from several
interfaces via one BCM-socket using sendto() / recvfrom().
See new test-apps tst-bcm-?x-sendto.c
The update of RX/TX_SETUPs 'on the run' are only possible with a smaller or
eval size of parameters now, as it is very complex in locking to provide the
other case and there was really now use-case for it ...

git-svn-id: svn://svn.berlios.de//socketcan/trunk@138 030b6a49-0b11-0410-94ab-b0dab22257f2

kernel/2.6/include/linux/can/bcm.h
kernel/2.6/net/can/bcm.c
test/Makefile
test/tst-bcm-filter.c
test/tst-bcm-rx-sendto.c [new file with mode: 0644]
test/tst-bcm-tx-sendto.c [new file with mode: 0644]

index f405bf71aa549e34ace92a19a0cf09e6a05e8203..d587d94074da7674f6478787f0c11a37526b12d6 100644 (file)
@@ -73,6 +73,4 @@ enum {
 #define TX_RESET_MULTI_IDX  0x0200
 #define RX_RTR_FRAME        0x0400
 
-#define CMD_ERROR    0x8000
-
 #endif /* CAN_BCM_H */
index 4fcdf56d74f335cf5e49f10cece2f2c64a091e8f..40c695ff72b4f5649f5798e74de831e9e2294d51 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * bcm.c
+ * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
  *
  * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
  * All rights reserved.
@@ -57,7 +57,6 @@
 
 #include "af_can.h"
 
-
 RCSID("$Id$");
 
 #ifdef CONFIG_CAN_DEBUG_CORE
@@ -88,17 +87,19 @@ MODULE_DESCRIPTION(NAME);
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
 
-
-#define GET_U64(p) (*(unsigned long long*)(p)->data)
+#define GET_U64(p) (*(unsigned long long*)(p)->data) /* easy access */
 
 struct bcm_op {
        struct list_head list;
+       int ifindex;
        canid_t can_id;
        int flags;
        unsigned long j_ival1, j_ival2, j_lastmsg;
        unsigned long frames_abs, frames_filtered;
-       struct timeval ival1, ival2, stamp;
        struct timer_list timer, thrtimer;
+       struct timeval ival1, ival2;
+       struct timeval rx_stamp;
+       int rx_ifindex;
        int count;
        int nframes;
        int currframe;
@@ -118,39 +119,48 @@ struct bcm_opt {
 };
 
 static struct proc_dir_entry *proc_dir = NULL;
-static int bcm_init(struct sock *sk);
-static int bcm_read_proc(char *page, char **start, off_t off,
-                        int count, int *eof, void *data);
 
+static int  bcm_init(struct sock *sk);
 static void bcm_notifier(unsigned long msg, void *data);
-static int bcm_release(struct socket *sock);
-static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
-                      int flags);
+static int  bcm_release(struct socket *sock);
+static int  bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
+                       int flags);
+static int  bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
+                       struct msghdr *msg, size_t size);
+static int  bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
+                       struct msghdr *msg, size_t size, int flags);
 static unsigned int bcm_poll(struct file *file, struct socket *sock,
                             poll_table *wait);
-static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *msg, size_t size);
-static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *msg, size_t size, int flags);
+
+static int  bcm_read_proc(char *page, char **start, off_t off,
+                         int count, int *eof, void *data);
 
 static void bcm_tx_timeout_handler(unsigned long data);
+static int  bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
+static int  bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+                        int ifindex, struct sock *sk);
+static void bcm_can_tx(struct bcm_op *op);
+
+static int  bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+                        int ifindex, struct sock *sk);
 static void bcm_rx_handler(struct sk_buff *skb, void *op);
 static void bcm_rx_timeout_handler(unsigned long data);
 static void bcm_rx_thr_handler(unsigned long data);
-static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id);
-static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id);
-static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id);
-static void bcm_remove_op(struct bcm_op *op);
-static void bcm_can_tx(struct bcm_op *op);
-static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
-                            struct can_frame *frames, struct timeval *tv);
+static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
+                               struct can_frame *rxdata);
 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
 static void bcm_rx_starttimer(struct bcm_op *op);
 static void bcm_rx_update_and_send(struct bcm_op *op,
                                   struct can_frame *lastdata,
                                   struct can_frame *rxdata);
-static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
-                               struct can_frame *rxdata);
+static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
+                            struct can_frame *frames, struct timeval *tv);
+
+static int  bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex);
+static int  bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex);
+static void bcm_remove_op(struct bcm_op *op);
+static int  bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex);
+static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex);
 
 static struct proto_ops bcm_ops = {
        .family        = PF_CAN,
@@ -218,6 +228,9 @@ static struct can_proto bcm_can_proto = {
 
 #endif
 
+#define CFSIZ sizeof(struct can_frame)
+#define OPSIZ sizeof(struct bcm_op)
+#define MHSIZ sizeof(struct bcm_msg_head)
 
 static int __init bcm_module_init(void)
 {
@@ -243,6 +256,10 @@ static void __exit bcm_module_exit(void)
 
 }
 
+/**************************************************/
+/* initial settings at socket creation time       */
+/**************************************************/
+
 static int bcm_init(struct sock *sk)
 {
        struct bcm_opt *bo = bcm_sk(sk);
@@ -258,15 +275,21 @@ static int bcm_init(struct sock *sk)
        return 0;
 }
 
+/**************************************************/
+/* handling of netdevice problems                 */
+/**************************************************/
+
 static void bcm_notifier(unsigned long msg, void *data)
 {
        struct sock *sk = (struct sock *)data;
+       struct bcm_opt *bo = bcm_sk(sk);
 
        DBG("called for sock %p\n", sk);
 
        switch (msg) {
        case NETDEV_UNREGISTER:
-               sk->sk_bound_dev_if = 0;
+               bo->bound   = 0;
+               bo->ifindex = 0;
                /* fallthrough */
        case NETDEV_DOWN:
                sk->sk_err = ENETDOWN;
@@ -275,6 +298,10 @@ static void bcm_notifier(unsigned long msg, void *data)
        }
 }
 
+/**************************************************/
+/* standard socket functions                      */
+/**************************************************/
+
 static int bcm_release(struct socket *sock)
 {
        struct sock *sk = sock->sk;
@@ -293,14 +320,16 @@ static int bcm_release(struct socket *sock)
        list_for_each_entry_safe(op, n, &bo->rx_ops, list) {
                DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
 
-               if (sk->sk_bound_dev_if) {
-                       struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+               /* Don't care if we're bound or not (due to netdev problems) */
+               /* can_rx_unregister() is always a save thing to do here     */
+               if (op->ifindex) {
+                       struct net_device *dev = dev_get_by_index(op->ifindex);
                        if (dev) {
                                can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
                                dev_put(dev);
                        }
                } else
-                       DBG("sock %p not bound for can_rx_unregister()\n", sk);
+                       can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
 
                bcm_remove_op(op);
        }
@@ -312,14 +341,13 @@ static int bcm_release(struct socket *sock)
        }
 
        /* remove device notifier */
-       if (sk->sk_bound_dev_if) {
-               struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+       if (bo->ifindex) {
+               struct net_device *dev = dev_get_by_index(bo->ifindex);
                if (dev) {
                        can_dev_unregister(dev, bcm_notifier, sk);
                        dev_put(dev);
                }
-       } else
-               DBG("sock %p not bound for can_dev_unregister()\n", sk);
+       }
 
        sock_put(sk);
 
@@ -332,663 +360,732 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
        struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
        struct sock *sk = sock->sk;
        struct bcm_opt *bo = bcm_sk(sk);
-       struct net_device *dev;
+
+       if (bo->bound)
+               return -EISCONN;
 
        /* bind a device to this socket */
-       dev = dev_get_by_index(addr->can_ifindex);
-       if (!dev) {
-               DBG("could not find device %d\n", addr->can_ifindex);
-               return -ENODEV;
-       }
-       sk->sk_bound_dev_if = dev->ifindex;
-       can_dev_register(dev, bcm_notifier, sk);
-       dev_put(dev);
+       if (addr->can_ifindex) {
+               struct net_device *dev = dev_get_by_index(addr->can_ifindex);
+               if (!dev) {
+                       DBG("could not find device index %d\n", addr->can_ifindex);
+                       return -ENODEV;
+               }
+               bo->ifindex = dev->ifindex;
+               can_dev_register(dev, bcm_notifier, sk); /* register notifier */
+               dev_put(dev);
+
+               DBG("socket %p bound to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
+       } else
+               bo->ifindex = 0; /* no notifier for ifindex = 0 ('any' CAN device) */
 
-       DBG("socket %p to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
+       bo->bound = 1;
 
        if (proc_dir) {
-               sprintf(bo->procname, "%p", bo);
+               sprintf(bo->procname, "%p", sock); /* unique socket address as filename */
                bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
-                                                          proc_dir, bcm_read_proc, bo);
+                                                          proc_dir, bcm_read_proc, sk);
        }
 
        return 0;
 }
 
-static int bcm_read_proc(char *page, char **start, off_t off,
-                        int count, int *eof, void *data)
+static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
+                      struct msghdr *msg, size_t size)
 {
-       int len = 0;
-       struct bcm_opt *bo = (struct bcm_opt *) data;
-       struct bcm_op *op;
-       struct net_device *dev = NULL;
-
-       len += snprintf(page + len, PAGE_SIZE - len,">>> bo %p", bo);
-       len += snprintf(page + len, PAGE_SIZE - len," / dropped %lu", bo->dropped_usr_msgs);
-
-       if (!list_empty(&bo->rx_ops)) {
-               struct sock *sk;
-               op = list_entry(bo->rx_ops.next, struct bcm_op, list);
-               sk = op->sk;
-               if (sk->sk_bound_dev_if)
-                       dev = dev_get_by_index(sk->sk_bound_dev_if);
-               len += snprintf(page + len, PAGE_SIZE - len,
-                               " / sk %p / socket %p", sk, sk->sk_socket);
-       } else if (!list_empty(&bo->tx_ops)) {
-               struct sock *sk;
-               op = list_entry(bo->tx_ops.next, struct bcm_op, list);
-               sk = op->sk;
-               if (sk->sk_bound_dev_if)
-                       dev = dev_get_by_index(sk->sk_bound_dev_if);
-               len += snprintf(page + len, PAGE_SIZE - len,
-                               " / sk %p / socket %p", sk, sk->sk_socket);
+       struct sock *sk = sock->sk;
+       struct bcm_opt *bo = bcm_sk(sk);
+       int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
+       struct bcm_msg_head msg_head;
+       int ret; /* read bytes or error codes as return value */
+
+       if (!bo->bound) {
+               DBG("sock %p not bound\n", sk);
+               return -ENOTCONN;
        }
 
-       if (dev) {
-               len += snprintf(page + len, PAGE_SIZE - len, " / %s", dev->name);
-               dev_put(dev);
+       /* check for alternative ifindex for this bcm_op */
+
+       if (!ifindex && msg->msg_name) { /* no bound device as default */
+               struct sockaddr_can *addr = (struct sockaddr_can *)msg->msg_name;
+               if (addr->can_family != AF_CAN)
+                       return -EINVAL;
+               ifindex = addr->can_ifindex; /* ifindex from sendto() */
+
+               if (ifindex && !dev_get_by_index(ifindex)) {
+                       DBG("device %d not found\n", ifindex);
+                       return -ENODEV;
+               }
        }
 
-       len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
+       /* read message head information */
 
-       list_for_each_entry(op, &bo->rx_ops, list) {
+       if ((ret = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
+                                   MHSIZ)) < 0)
+               return ret;
 
-               unsigned long reduction;
+       DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
 
-               /* print only active entries & prevent division by zero */
-               if (!op->frames_abs)
-                       continue;
+       switch (msg_head.opcode) {
 
-               len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X [%d]%c ",
-                               op->can_id, op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
-               if (op->j_ival1)
-                       len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
+       case TX_SETUP:
 
-               if (op->j_ival2)
-                       len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
+               ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
+               break;
 
-               len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
-                               op->frames_filtered, op->frames_abs);
+       case RX_SETUP:
 
-               reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
+               ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
+               break;
 
-               len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
-                               (reduction == 100)?"near ":"", reduction);
+       case TX_DELETE:
 
-               if (len > PAGE_SIZE - 200) {
-                       /* mark output cut off */
-                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
-                       break;
-               }
-       }
+               if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
+                       ret = MHSIZ;
+               else
+                       ret = -EINVAL;
+               break;
+                   
+       case RX_DELETE:
 
-       list_for_each_entry(op, &bo->tx_ops, list) {
+               if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
+                       ret = MHSIZ;
+               else
+                       ret = -EINVAL;
+               break;
 
-               len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X [%d] ",
-                               op->can_id, op->nframes);
-               if (op->j_ival1)
-                       len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
+       case TX_READ:
 
-               if (op->j_ival2)
-                       len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
+               /* reuse msg_head for the reply */
+               msg_head.opcode  = TX_STATUS; /* reply to TX_READ */
+               ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
+               break;
 
-               len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
+       case RX_READ:
 
-               if (len > PAGE_SIZE - 100) {
-                       /* mark output cut off */
-                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
-                       break;
-               }
-       }
+               /* reuse msg_head for the reply */
+               msg_head.opcode  = RX_STATUS; /* reply to RX_READ */
+               ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
+               break;
 
-       len += snprintf(page + len, PAGE_SIZE - len, "\n");
+       case TX_SEND:
 
-       *eof = 1;
-       return len;
-}
+               if (msg_head.nframes < 1) /* we need at least one can_frame */
+                       return -EINVAL;
 
-static unsigned int bcm_poll(struct file *file, struct socket *sock,
-                            poll_table *wait)
-{
-       unsigned int mask = 0;
+               ret = bcm_tx_send(msg, ifindex, sk);
+               break;
 
-       DBG("socket %p\n", sock);
+       default:
 
-       mask = datagram_poll(file, sock, wait);
-       return mask;
+               DBG("Unknown opcode %d\n", msg_head.opcode);
+               ret = -EINVAL;
+               break;
+       }
+
+       return ret;
 }
 
-static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *msg, size_t size)
+static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
+                      struct msghdr *msg, size_t size, int flags)
 {
-       struct bcm_msg_head msg_head;
-       int i;
-       struct bcm_op *op;
-       int err;
        struct sock *sk = sock->sk;
-       struct bcm_opt *bo = bcm_sk(sk);
-       char c;
-       int rbytes = 0; /* read bytes as return value */
+       struct sk_buff *skb;
+       int error = 0;
+       int noblock;
+       int err;
 
-       /* read message head information */
+       DBG("socket %p, sk %p\n", sock, sk);
 
-       if ((err = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
-                                   sizeof(msg_head))) < 0)
-               return err;
+       noblock =  flags & MSG_DONTWAIT;
+       flags   &= ~MSG_DONTWAIT;
+       if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
+               return error;
+       }
 
-       DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
+       DBG("delivering skbuff %p\n", skb);
+       DBG_SKB(skb);
 
-       if (!sk->sk_bound_dev_if) {
-               DBG("sock %p not bound\n", sk);
-               return -ENOTCONN;
+       if (skb->len < size)
+               size = skb->len;
+       if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
+               skb_free_datagram(sk, skb);
+               return err;
        }
 
-       switch (msg_head.opcode) {
+       sock_recv_timestamp(msg, sk, skb);
 
-       case TX_SETUP:
+       if (msg->msg_name) {
+               msg->msg_namelen = sizeof(struct sockaddr_can);
+               memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
+       }
 
-               if (msg_head.nframes < 1) /* we need at least one can_frame */
-                       return -EINVAL;
+       DBG("freeing sock %p, skbuff %p\n", sk, skb);
+       skb_free_datagram(sk, skb);
 
-               /* check the given can_id */
+       return size;
+}
 
-               if (!(op = bcm_find_op(&bo->tx_ops, msg_head.can_id))) {
+static unsigned int bcm_poll(struct file *file, struct socket *sock,
+                            poll_table *wait)
+{
+       unsigned int mask = 0;
 
-                       /* insert new BCM operation for the given can_id */
+       DBG("socket %p\n", sock);
 
-                       if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
-                               return -ENOMEM;
+       mask = datagram_poll(file, sock, wait);
+       return mask;
+}
 
-                       memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
+/**************************************************/
+/* helper functions for bcm_sendmsg()             */
+/**************************************************/
 
-                       DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
-                           op, msg_head.can_id);
+static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+                       int ifindex, struct sock *sk)
+{
+       struct bcm_opt *bo = bcm_sk(sk);
+       struct bcm_op *op;
+       int i, err;
 
-                       op->can_id    = msg_head.can_id;
+       if (!ifindex) /* we need a real device to send frames */
+               return -ENODEV;
 
-                       /* create array for can_frames and copy the data */
-                       if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
-                               kfree(op);
-                               return -ENOMEM;
-                       }
+       if (msg_head->nframes < 1) /* we need at least one can_frame */
+               return -EINVAL;
 
-                       for (i = 0; i < msg_head.nframes; i++) {
-                               if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame))) < 0) {
-                                       kfree(op->frames);
-                                       kfree(op);
-                                       return err;
-                               }
+       /* check the given can_id */
 
-                               if (msg_head.flags & TX_CP_CAN_ID)
-                                       op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
-                       }
+       if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
 
-                       op->last_frames = NULL; /* tx_ops never compare with previous received messages */
+               /* update existing BCM operation */
 
-                       op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
+               DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
+                   op, msg_head->can_id);
 
-                       init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
-                       init_timer(&op->thrtimer); /* currently unused in tx_ops */
+               /* Do we need more space for the can_frames than currently */
+               /* allocated? -> This is a _really_ unusual use-case and   */
+               /* therefore (complexity / locking) it is not supported.   */
+               if (msg_head->nframes > op->nframes)
+                       return -E2BIG;
 
-                       op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
-                       op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
+               /* update can_frames content */
+               for (i = 0; i < msg_head->nframes; i++) {
+                       if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0)
+                               return err;
 
-                       /* add this bcm_op to the list of the tx_ops */
-                       list_add(&op->list, &bo->tx_ops);
+                       if (msg_head->flags & TX_CP_CAN_ID)
+                               op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
+               }
 
-               } else {
-                       /* update existing BCM operation */
+       } else {
+               /* insert new BCM operation for the given can_id */
 
-                       DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
-                           op, msg_head.can_id);
+               if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
+                       return -ENOMEM;
 
-                       /* do we need more space for the can_frames? */
-                       if (msg_head.nframes > op->nframes) {
+               memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
 
-                               /* yes => create new array */
+               DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
+                   op, msg_head->can_id);
 
-                               struct can_frame *p;
-                               if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
-                                       return -ENOMEM;
+               op->can_id    = msg_head->can_id;
 
-                               kfree (op->frames);
-                               op->frames = p;
-                       }
+               /* create array for can_frames and copy the data */
+               if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+                       kfree(op);
+                       return -ENOMEM;
+               }
 
-                       /* update can_frames content */
-                       for (i = 0; i < msg_head.nframes; i++) {
-                               memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
-                               if (msg_head.flags & TX_CP_CAN_ID)
-                                       op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
+               for (i = 0; i < msg_head->nframes; i++) {
+                       if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, CFSIZ)) < 0) {
+                               kfree(op->frames);
+                               kfree(op);
+                               return err;
                        }
 
+                       if (msg_head->flags & TX_CP_CAN_ID)
+                               op->frames[i].can_id = msg_head->can_id; /* copy can_id into frame */
                }
 
-               if (op->nframes != msg_head.nframes) {
-                       op->nframes   = msg_head.nframes;
-                       op->currframe = 0; /* start multiple frame transmission with index 0 */
-               }
+               op->last_frames = NULL; /* tx_ops never compare with previous received messages */
 
-               /* check flags */
+               op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
+               op->ifindex = ifindex;
 
-               op->flags = msg_head.flags;
+               init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
+               init_timer(&op->thrtimer); /* currently unused in tx_ops */
 
-               if (op->flags & TX_RESET_MULTI_IDX)
-                       op->currframe = 0; /* start multiple frame transmission with index 0 */
+               op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
+               op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
 
-               if (op->flags & SETTIMER) {
+               /* add this bcm_op to the list of the tx_ops */
+               list_add(&op->list, &bo->tx_ops);
 
-                       /* set timer values */
+       } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
 
-                       op->count   = msg_head.count;
-                       op->ival1   = msg_head.ival1;
-                       op->ival2   = msg_head.ival2;
-                       op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
-                       op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
+       if (op->nframes != msg_head->nframes) {
+               op->nframes   = msg_head->nframes;
+               op->currframe = 0; /* start multiple frame transmission with index 0 */
+       }
 
-                       DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
-                           op->count, op->j_ival1, op->j_ival2);
+       /* check flags */
 
-                       /* disable an active timer due to zero values? */
-                       if (!op->j_ival1 && !op->j_ival2) {
-                               del_timer(&op->timer);
-                               DBG("TX_SETUP: SETTIMER disabled timer.\n");
-                       }
+       op->flags = msg_head->flags;
 
-               }
+       if (op->flags & TX_RESET_MULTI_IDX)
+               op->currframe = 0; /* start multiple frame transmission with index 0 */
 
-               if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
+       if (op->flags & SETTIMER) {
 
-                       del_timer(&op->timer);
+               /* set timer values */
 
-                       op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
-                       if (op->j_ival1 && (op->count > 0)){
-                               op->timer.expires = jiffies + op->j_ival1;
-                               /* op->count-- is done in bcm_tx_timeout_handler */
-                               DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
-                                   op->timer.function,
-                                   (char*) op->timer.data,
-                                   (unsigned int) op->timer.expires);
-                       } else{
-                               op->timer.expires = jiffies + op->j_ival2;
-                               DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
-                                   op->timer.function,
-                                   (char*) op->timer.data,
-                                   (unsigned int) op->timer.expires);
-                       }
+               op->count   = msg_head->count;
+               op->ival1   = msg_head->ival1;
+               op->ival2   = msg_head->ival2;
+               op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
+               op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
 
-                       add_timer(&op->timer);
-               }
+               DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
+                   op->count, op->j_ival1, op->j_ival2);
 
-               if (op->flags & TX_ANNOUNCE)
-                       bcm_can_tx(op);
+               /* disable an active timer due to zero values? */
+               if (!op->j_ival1 && !op->j_ival2) {
+                       del_timer(&op->timer);
+                       DBG("TX_SETUP: SETTIMER disabled timer.\n");
+               }
+       }
 
-               rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
+       if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
 
-               break; /* TX_SETUP */
+               del_timer(&op->timer);
 
-       case TX_DELETE:
+               op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
+               if (op->j_ival1 && (op->count > 0)){
+                       op->timer.expires = jiffies + op->j_ival1;
+                       /* op->count-- is done in bcm_tx_timeout_handler */
+                       DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
+                           op->timer.function,
+                           (char*) op->timer.data,
+                           (unsigned int) op->timer.expires);
+               } else{
+                       op->timer.expires = jiffies + op->j_ival2;
+                       DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
+                           op->timer.function,
+                           (char*) op->timer.data,
+                           (unsigned int) op->timer.expires);
+               }
 
-               bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id);
+               add_timer(&op->timer);
+       }
 
-               rbytes = sizeof(struct bcm_msg_head);
+       if (op->flags & TX_ANNOUNCE)
+               bcm_can_tx(op);
 
-               break; /* TX_DELETE */
+       return msg_head->nframes * CFSIZ + MHSIZ;
+}
 
-       case TX_READ:
+static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
+                       int ifindex, struct sock *sk)
+{
+       struct bcm_opt *bo = bcm_sk(sk);
+       struct bcm_op *op;
+       int do_rx_register;
+       int err;
 
-               /* reuse msg_head for the reply */
-               msg_head.opcode  = TX_STATUS; /* reply to TX_READ */
-               op = bcm_find_op(&bo->tx_ops, msg_head.can_id);
-               c  = 'T'; /* for nice debug output ... */
+       if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
+               /* be robust against wrong usage ... */
+               msg_head->flags |= RX_FILTER_ID;
+               msg_head->nframes = 0; /* ignore trailing garbage */
+       }
 
-               goto TRX_READ;
+       if ((msg_head->flags & RX_RTR_FRAME) &&
+           ((msg_head->nframes != 1) || (!(msg_head->can_id & CAN_RTR_FLAG)))) {
 
-       case RX_READ:
+               DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
+               return -EINVAL;
+       }
 
-               /* reuse msg_head for the reply */
-               msg_head.opcode  = RX_STATUS; /* reply to RX_READ */
-               op = bcm_find_op(&bo->rx_ops, msg_head.can_id);
-               c  = 'R'; /* for nice debug output ... */
+       /* check the given can_id */
 
-       TRX_READ:
+       if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
 
-               /* check the given can_id */
+               /* update existing BCM operation */
 
-               if (!op) {
-                       DBG("%cX_READ: did not find op for can_id <%03X>\n",
-                           c, msg_head.can_id);
+               DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
+                   op, msg_head->can_id);
 
-                       msg_head.flags   |= CMD_ERROR;
-                       msg_head.nframes  = 0;
-                       bcm_send_to_user(sk, &msg_head, NULL, NULL);
-               }
-               else {
-                       DBG("%cX_READ: sending status for can_id <%03X>\n",
-                           c, msg_head.can_id);
+               /* Do we need more space for the can_frames than currently */
+               /* allocated? -> This is a _really_ unusual use-case and   */
+               /* therefore (complexity / locking) it is not supported.   */
+               if (msg_head->nframes > op->nframes)
+                       return -E2BIG;
 
-                       /* put current values into msg_head */
-                       msg_head.flags   = op->flags;
-                       msg_head.count   = op->count;
-                       msg_head.ival1   = op->ival1;
-                       msg_head.ival2   = op->ival2;
-                       msg_head.nframes = op->nframes;
+               if (msg_head->nframes) {
+                       /* update can_frames content */
+                       if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ) < 0))
+                               return err;
 
-                       bcm_send_to_user(sk, &msg_head, op->frames, NULL);
+                       /* clear received can_frames to indicate 'nothing received' */
+                       memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
                }
 
-               rbytes = sizeof(struct bcm_msg_head);
+               op->nframes = msg_head->nframes;
+               /* Only an update -> do not call can_rx_register() at end of RX_SETUP */
+               do_rx_register = 0;
 
-               break; /* [T|R]X_READ */
+       } else {
+               /* insert new BCM operation for the given can_id */
 
-       case TX_SEND:
-       {
-               struct sk_buff *skb;
-               struct net_device *dev;
+               if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
+                       return -ENOMEM;
 
-               /* just copy and send one can_frame */
+               memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
 
-               if (msg_head.nframes < 1) /* we need at least one can_frame */
-                       return -EINVAL;
+               DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
+                   op, msg_head->can_id);
 
-               skb = alloc_skb(sizeof(struct can_frame), GFP_KERNEL);
+               op->can_id    = msg_head->can_id;
+               op->nframes   = msg_head->nframes;
 
-               if (!skb)
-                       return -ENOMEM;
+               if (msg_head->nframes) {
 
-               if ((err = memcpy_fromiovec(skb_put(skb, sizeof(struct can_frame)), msg->msg_iov, sizeof(struct can_frame))) < 0) {
-                       kfree_skb(skb);
-                       return err;
-               }
+                       /* create array for can_frames and copy the data */
+                       if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+                               kfree(op);
+                               return -ENOMEM;
+                       }
 
-               DBG_FRAME("BCM: TX_SEND: sending frame",
-                         (struct can_frame *)skb->data);
-               dev = dev_get_by_index(sk->sk_bound_dev_if);
+                       if ((err = memcpy_fromiovec((unsigned char*)op->frames, msg->msg_iov, msg_head->nframes * CFSIZ)) < 0) {
+                               kfree(op->frames);
+                               kfree(op);
+                               return err;
+                       }
 
-               if (dev) {
-                       skb->dev = dev;
-                       skb->sk  = sk;
-                       can_send(skb, 1); /* send with loopback */
-                       dev_put(dev);
-               }
+                       /* create array for received can_frames */
+                       if (!(op->last_frames = kmalloc(msg_head->nframes * CFSIZ, GFP_KERNEL))) {
+                               kfree(op->frames);
+                               kfree(op);
+                               return -ENOMEM;
+                       }
 
-               rbytes = sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
-       }
-       break;
+                       /* clear received can_frames to indicate 'nothing received' */
+                       memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
+               } else {
+                       /* op->frames = NULL due to memset */
 
-       case RX_SETUP:
+                       /* even when we have the RX_FILTER_ID case, we need to store the last frame */
+                       /* for the throttle functionality */
 
-               if ((msg_head.flags & RX_FILTER_ID) || (!(msg_head.nframes))) {
-                       /* be robust against wrong usage ... */
-                       msg_head.flags |= RX_FILTER_ID;
-                       msg_head.nframes = 0; /* ignore trailing garbage */
-               }
+                       /* create array for received can_frames */
+                       if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
+                               kfree(op);
+                               return -ENOMEM;
+                       }
 
-               if ((msg_head.flags & RX_RTR_FRAME) &&
-                   ((msg_head.nframes != 1) || (!(msg_head.can_id & CAN_RTR_FLAG)))) {
+                       /* clear received can_frames to indicate 'nothing received' */
+                       memset(op->last_frames, 0, CFSIZ);
+               }
 
-                       DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
+               op->sk = sk; /* bcm_delete_rx_op() needs this */
+               op->ifindex = ifindex;
 
-                       msg_head.flags   |= CMD_ERROR; /* return msg_head back to sender */
-                       msg_head.nframes  = 0;
-                       bcm_send_to_user(sk, &msg_head, NULL, NULL);
+               init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
+               init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
 
-                       rbytes = sizeof(struct bcm_msg_head);
+               op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
+               op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
 
-                       break;
-               }
+               op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
+               op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
+               op->thrtimer.expires = 0; /* mark disabled timer */
 
-               /* check the given can_id */
+               /* add this bcm_op to the list of the tx_ops */
+               list_add(&op->list, &bo->rx_ops);
 
-               if (!(op = bcm_find_op(&bo->rx_ops, msg_head.can_id))) {
+               do_rx_register = 1; /* call can_rx_register() at end of RX_SETUP */
 
-                       /* insert new BCM operation for the given can_id */
+       } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
 
-                       if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
-                               return -ENOMEM;
 
-                       memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
+       /* check flags */
 
-                       DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
-                           op, msg_head.can_id);
+       op->flags = msg_head->flags;
 
-                       op->can_id    = msg_head.can_id;
-                       op->nframes   = msg_head.nframes;
+       if (op->flags & RX_RTR_FRAME) {
 
-                       if (op->nframes) {
+               /* no timers in RTR-mode */
+               del_timer(&op->thrtimer);
+               del_timer(&op->timer);
 
-                               /* create array for can_frames and copy the data */
-                               if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
-                                       kfree(op);
-                                       return -ENOMEM;
-                               }
+               /* funny feature in RX(!)_SETUP only for RTR-mode: */
+               /* copy can_id into frame BUT without RTR-flag to  */
+               /* prevent a full-load-loopback-test ... ;-]       */
+               if ((op->flags & TX_CP_CAN_ID) ||
+                   (op->frames[0].can_id == op->can_id))
+                       op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
 
-                               for (i = 0; i < msg_head.nframes; i++)
-                                       if ((err = memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame))) < 0) {
-                                               kfree(op->frames);
-                                               kfree(op);
-                                               return err;
-                                       }
-
-                               /* create array for received can_frames */
-                               if (!(op->last_frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
-                                       kfree(op->frames);
-                                       kfree(op);
-                                       return -ENOMEM;
-                               }
+       } else {
+               if (op->flags & SETTIMER) {
 
-                               /* clear received can_frames to indicate 'nothing received' */
-                               memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
-                       } else {
-                               /* op->frames = NULL due to memset */
+                       /* set timer value */
 
-                               /* even when we have the RX_FILTER_ID case, we need to store the last frame */
-                               /* for the throttle functionality */
+                       op->ival1   = msg_head->ival1;
+                       op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
+                       op->ival2   = msg_head->ival2;
+                       op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
 
-                               /* create array for received can_frames */
-                               if (!(op->last_frames = kmalloc(sizeof(struct can_frame), GFP_KERNEL))) {
-                                       kfree(op);
-                                       return -ENOMEM;
-                               }
+                       DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
+                           op->j_ival1, op->j_ival2);
 
-                               /* clear received can_frames to indicate 'nothing received' */
-                               memset(op->last_frames, 0, sizeof(struct can_frame));
+                       /* disable an active timer due to zero value? */
+                       if (!op->j_ival1) {
+                               del_timer(&op->timer);
+                               DBG("RX_SETUP: disabled timer for rx timeouts.\n");
                        }
 
-                       op->sk = sk; /* bcm_delete_rx_op() needs this */
+                       /* free currently blocked msgs ? */
+                       if (op->thrtimer.expires) { /* running throttle timer? */
+                               DBG("RX_SETUP: unblocking throttled msgs.\n");
+                               del_timer(&op->thrtimer);
+                               op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
+                               add_timer(&op->thrtimer);
+                       }
+                       /* if (op->j_ival2) is zero, no (new) throttling will happen */
+                       /* see bcm_rx_update_and_send() and bcm_rx_thr_handler()     */
+               }
 
-                       init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
-                       init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
+               if ((op->flags & STARTTIMER) && op->j_ival1) {
 
-                       op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
-                       op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
+                       del_timer(&op->timer);
 
-                       op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
-                       op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
-                       op->thrtimer.expires = 0; /* mark disabled timer */
+                       op->timer.expires = jiffies + op->j_ival1;
 
-                       /* add this bcm_op to the list of the tx_ops */
-                       list_add(&op->list, &bo->rx_ops);
+                       DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
+                           (char *) op->timer.function,
+                           (char *) op->timer.data,
+                           (unsigned int) op->timer.expires);
 
-                       c=1; /* call can_rx_register() at end of RX_SETUP */
+                       add_timer(&op->timer);
+               }
+       }
 
-               } else {
-                       /* update existing BCM operation */
+       /* now we can register for can_ids, if we added a new bcm_op */
+       if (do_rx_register) {
+               DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
 
-                       DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
-                           op, msg_head.can_id);
+               if (ifindex) {
+                       struct net_device *dev = dev_get_by_index(ifindex);
 
-                       /* do we need more space for the can_frames? */
-                       if (msg_head.nframes > op->nframes) {
+                       if (dev) {
+                               can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
+                               dev_put(dev);
+                       }
+               } else 
+                       can_rx_register(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
+       }
 
-                               /* yes => create new arrays */
+       return msg_head->nframes * CFSIZ + MHSIZ;
+}
 
-                               struct can_frame *p;
+static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
+{
+       struct sk_buff *skb;
+       struct net_device *dev;
+       int err;
 
-                               if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
-                                       return -ENOMEM;
+       /* just copy and send one can_frame */
 
-                               if (op->frames)
-                                       kfree (op->frames);
-                               op->frames = p;
+       if (!ifindex) /* we need a real device to send frames */
+               return -ENODEV;
 
-                               if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
-                                       return -ENOMEM;
-                               if (op->last_frames)
-                                       kfree (op->last_frames);
-                               op->last_frames = p;
-                       }
+       skb = alloc_skb(CFSIZ, GFP_KERNEL);
 
-                       if (msg_head.nframes) {
-                               /* update can_frames content */
-                               for (i = 0; i < msg_head.nframes; i++)
-                                       memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
+       if (!skb)
+               return -ENOMEM;
 
-                               /* clear received can_frames to indicate 'nothing received' */
-                               memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
-                       }
+       if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ)) < 0) {
+               kfree_skb(skb);
+               return err;
+       }
 
-                       op->nframes = msg_head.nframes;
-                       c=0; /* do not call can_rx_register() at end of RX_SETUP */
+       DBG_FRAME("BCM: TX_SEND: sending frame",
+                 (struct can_frame *)skb->data);
+       dev = dev_get_by_index(ifindex);
 
-               } /* if (!bcm_find_op(&bo->tx_ops, msg_head.can_id)) */
+       if (!dev) {
+               kfree_skb(skb);
+               return -ENODEV;
+       }
 
+       skb->dev = dev;
+       skb->sk  = sk;
+       can_send(skb, 1); /* send with loopback */
+       dev_put(dev);
 
-               /* check flags */
+       return CFSIZ + MHSIZ;
+}
 
-               op->flags = msg_head.flags;
+static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, int ifindex)
+{
+       struct bcm_op *op;
+       int ret;
 
-               if (op->flags & RX_RTR_FRAME) {
+       if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
 
-                       /* no timers in RTR-mode */
-                       del_timer(&op->thrtimer);
-                       del_timer(&op->timer);
+               DBG("TRX_READ: sending status for can_id <%03X>\n", msg_head->can_id);
+               /* put current values into msg_head */
+               msg_head->flags   = op->flags;
+               msg_head->count   = op->count;
+               msg_head->ival1   = op->ival1;
+               msg_head->ival2   = op->ival2;
+               msg_head->nframes = op->nframes;
 
-                       /* funny feature in RX(!)_SETUP only for RTR-mode: */
-                       /* copy can_id into frame BUT without RTR-flag to  */
-                       /* prevent a full-load-loopback-test ... ;-]       */
-                       if ((op->flags & TX_CP_CAN_ID) ||
-                           (op->frames[0].can_id == op->can_id))
-                               op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
+               bcm_send_to_user(op, msg_head, op->frames, NULL);
 
-               } else {
-                       if (op->flags & SETTIMER) {
+               ret = MHSIZ;
 
-                               /* set timer value */
+       } else {
 
-                               op->ival1   = msg_head.ival1;
-                               op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
-                               op->ival2   = msg_head.ival2;
-                               op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
+               DBG("TRX_READ: did not find op for can_id <%03X>\n",
+                   msg_head->can_id);
+               ret = -EINVAL;
+       }
 
-                               DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
-                                   op->j_ival1, op->j_ival2);
+       return ret;
+}
 
-                               /* disable an active timer due to zero value? */
-                               if (!op->j_ival1) {
-                                       del_timer(&op->timer);
-                                       DBG("RX_SETUP: disabled timer for rx timeouts.\n");
-                               }
+/**************************************************/
+/* procfs functions                               */
+/**************************************************/
 
-                               /* free currently blocked msgs ? */
-                               if (op->thrtimer.expires) { /* running throttle timer? */
-                                       DBG("RX_SETUP: unblocking throttled msgs.\n");
-                                       del_timer(&op->thrtimer);
-                                       op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
-                                       add_timer(&op->thrtimer);
-                               }
-                               /* if (op->j_ival2) is zero, no (new) throttling will happen */
-                               /* see bcm_rx_update_and_send() and bcm_rx_thr_handler()     */
-                       }
+static char *bcm_proc_getifname(int ifindex)
+{
+       struct net_device *dev;
 
-                       if ((op->flags & STARTTIMER) && op->j_ival1) {
+       if (!ifindex)
+               return "any";
 
-                               del_timer(&op->timer);
+       dev = __dev_get_by_index(ifindex); /* no usage counting */
+       if (dev)
+               return dev->name;
 
-                               op->timer.expires = jiffies + op->j_ival1;
+       return "???";
+}
 
-                               DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
-                                   (char *) op->timer.function,
-                                   (char *) op->timer.data,
-                                   (unsigned int) op->timer.expires);
+static int bcm_read_proc(char *page, char **start, off_t off,
+                        int count, int *eof, void *data)
+{
+       int len = 0;
+       struct sock *sk = (struct sock *)data;
+       struct bcm_opt *bo = bcm_sk(sk);
+       struct bcm_op *op;
 
-                               add_timer(&op->timer);
-                       }
-               }
+       len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p", sk->sk_socket);
+       len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
+       len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
+       len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu", bo->dropped_usr_msgs);
+       len += snprintf(page + len, PAGE_SIZE - len, " / bound %s", bcm_proc_getifname(bo->ifindex));
+       len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
 
-               /* now we can register for can_ids, if we added a new bcm_op */
-               if (c) {
-                       struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
+       list_for_each_entry(op, &bo->rx_ops, list) {
 
-                       DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
+               unsigned long reduction;
 
-                       if (dev) {
-                               can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
-                               dev_put(dev);
-                       }
-               }
+               /* print only active entries & prevent division by zero */
+               if (!op->frames_abs)
+                       continue;
 
-               rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
+               len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X %-5s ",
+                               op->can_id, bcm_proc_getifname(op->ifindex));
+               len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
+                               op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
+               if (op->j_ival1)
+                       len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
 
-               break; /* RX_SETUP */
+               if (op->j_ival2)
+                       len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
 
-       case RX_DELETE:
+               len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
+                               op->frames_filtered, op->frames_abs);
 
-               bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id);
+               reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
 
-               rbytes = sizeof(struct bcm_msg_head);
+               len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
+                               (reduction == 100)?"near ":"", reduction);
 
-               break; /* RX_DELETE */
+               if (len > PAGE_SIZE - 200) {
+                       /* mark output cut off */
+                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
+                       break;
+               }
+       }
 
-       default:
+       list_for_each_entry(op, &bo->tx_ops, list) {
 
-               DBG("Unknown opcode %d\n", msg_head.opcode);
+               len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X %s [%d] ",
+                               op->can_id, bcm_proc_getifname(op->ifindex), op->nframes);
+               if (op->j_ival1)
+                       len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
 
-               msg_head.flags   |= CMD_ERROR; /* return msg_head back to sender */
-               msg_head.nframes  = 0;
-               bcm_send_to_user(sk, &msg_head, NULL, NULL);
+               if (op->j_ival2)
+                       len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
 
-               rbytes = sizeof(struct bcm_msg_head);
+               len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
 
-               break;
+               if (len > PAGE_SIZE - 100) {
+                       /* mark output cut off */
+                       len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
+                       break;
+               }
        }
 
-       return rbytes;
+       len += snprintf(page + len, PAGE_SIZE - len, "\n");
+
+       *eof = 1;
+       return len;
 }
 
-static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
-                      struct msghdr *msg, size_t size, int flags)
+/**************************************************/
+/* bcm_op handling tx path                        */
+/**************************************************/
+
+static void bcm_can_tx(struct bcm_op *op)
 {
-       struct sock *sk = sock->sk;
        struct sk_buff *skb;
-       int error = 0;
-       int noblock;
-       int err;
+       struct net_device *dev;
+       struct can_frame *cf = &op->frames[op->currframe];
 
-       DBG("socket %p, sk %p\n", sock, sk);
+       DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
 
-       noblock =  flags & MSG_DONTWAIT;
-       flags   &= ~MSG_DONTWAIT;
-       if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
-               return error;
-       }
+       if (!op->ifindex)
+               return; /* no target device -> exit */
 
-       DBG("delivering skbuff %p\n", skb);
-       DBG_SKB(skb);
+       dev = dev_get_by_index(op->ifindex);
 
-       if (skb->len < size)
-               size = skb->len;
-       if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
-               skb_free_datagram(sk, skb);
-               return err;
-       }
+       if (!dev)
+               return; /* should this bcm_op remove itself here? */
 
-       sock_recv_timestamp(msg, sk, skb);
+       skb = alloc_skb(CFSIZ,
+                       in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
 
-       DBG("freeing sock %p, skbuff %p\n", sk, skb);
-       skb_free_datagram(sk, skb);
+       if (!skb)
+               goto out; /* no memory */
 
-       return size;
+       memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
+
+       skb->dev = dev;
+       skb->sk = op->sk;
+       can_send(skb, 1); /* send with loopback */
+
+       op->currframe++;
+       op->frames_abs++; /* statistics */
+
+       /* reached last frame? */
+       if (op->currframe >= op->nframes)
+               op->currframe = 0;
+ out:
+       dev_put(dev);
 }
 
 static void bcm_tx_timeout_handler(unsigned long data)
@@ -1015,7 +1112,7 @@ static void bcm_tx_timeout_handler(unsigned long data)
                        msg_head.can_id  = op->can_id;
                        msg_head.nframes = 0;
 
-                       bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
+                       bcm_send_to_user(op, &msg_head, NULL, NULL);
                }
        }
 
@@ -1052,6 +1149,10 @@ static void bcm_tx_timeout_handler(unsigned long data)
 
 }
 
+/**************************************************/
+/* bcm_op handling rx path                        */
+/**************************************************/
+
 static void bcm_rx_handler(struct sk_buff *skb, void *data)
 {
        struct bcm_op *op = (struct bcm_op*)data;
@@ -1064,7 +1165,8 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
 
        if (skb->len == sizeof(rxframe)) {
                memcpy(&rxframe, skb->data, sizeof(rxframe));
-               skb_get_timestamp(skb, &op->stamp); /* save rx timestamp */
+               skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
+               op->rx_ifindex = skb->dev->ifindex; /* save originator for recvfrom() */
                op->frames_abs++; /* statistics */
                kfree_skb(skb);
                DBG("got can_frame with can_id <%03X>\n", rxframe.can_id);
@@ -1167,7 +1269,7 @@ static void bcm_rx_update_and_send(struct bcm_op *op,
 {
        unsigned long nexttx = op->j_lastmsg + op->j_ival2;
 
-       memcpy(lastdata, rxdata, sizeof(struct can_frame));
+       memcpy(lastdata, rxdata, CFSIZ);
        lastdata->can_dlc |= RX_RECV; /* mark as used */
 
        /* throttle bcm_rx_changed ? */
@@ -1230,7 +1332,7 @@ static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
        head.can_id  = op->can_id;
        head.nframes = 1;
 
-       bcm_send_to_user(op->sk, &head, data, &op->stamp);
+       bcm_send_to_user(op, &head, data, &op->rx_stamp);
 }
 
 
@@ -1249,14 +1351,14 @@ static void bcm_rx_timeout_handler(unsigned long data)
        msg_head.can_id  = op->can_id;
        msg_head.nframes = 0;
 
-       bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
+       bcm_send_to_user(op, &msg_head, NULL, NULL);
 
        /* no restart of the timer is done here! */
 
        /* if the user wants to be informed, when cyclic CAN-Messages come back ... */
        if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
                /* clear received can_frames to indicate 'nothing received' */
-               memset(op->last_frames, 0, op->nframes * sizeof(struct can_frame));
+               memset(op->last_frames, 0, op->nframes * CFSIZ);
                DBG("RX_ANNOUNCE_RESTART\n");
        }
 
@@ -1292,55 +1394,16 @@ static void bcm_rx_thr_handler(unsigned long data)
        }
 }
 
-static void bcm_can_tx(struct bcm_op *op)
-{
-       struct sk_buff *skb;
-       struct net_device *dev;
-       struct can_frame *cf = &op->frames[op->currframe];
-
-       DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
-
-       skb = alloc_skb(sizeof(struct can_frame),
-                       in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
-
-       if (!skb)
-               return;
-
-       memcpy(skb_put(skb, sizeof(struct can_frame)), cf, sizeof(struct can_frame));
-
-       if (op->sk->sk_bound_dev_if) {
-               dev = dev_get_by_index(op->sk->sk_bound_dev_if);
-
-               if (dev) {
-                       skb->dev = dev;
-                       skb->sk = op->sk;
-                       can_send(skb, 1); /* send with loopback */
-                       dev_put(dev);
-               }
-       }
-
-       op->currframe++;
-       op->frames_abs++; /* statistics */
-
-       /* reached last frame? */
-       if (op->currframe >= op->nframes)
-               op->currframe = 0;
-
-}
-
-static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
+static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
                             struct can_frame *frames, struct timeval *tv)
 {
        struct sk_buff *skb;
        struct can_frame *firstframe;
-       int datalen = head->nframes * sizeof(struct can_frame);
+       struct sock *sk = op->sk;
+       int datalen = head->nframes * CFSIZ;
+       struct sockaddr_can *addr;
        int err;
 
-       if (!sk) {
-               DBG("no sk available\n");
-               return;
-       }
-
        skb = alloc_skb(sizeof(*head) + datalen,
                        in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
        if (!skb)
@@ -1350,7 +1413,12 @@ static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
        firstframe = (struct can_frame *) skb->tail; /* can_frames starting here */
 
        if (tv)
-               skb_set_timestamp(skb, tv);
+               skb_set_timestamp(skb, tv); /* restore timestamp */
+
+       addr = (struct sockaddr_can *)skb->cb;
+       memset(addr, 0, sizeof(*addr));
+       addr->can_family  = AF_CAN;
+       addr->can_ifindex = op->rx_ifindex; /* restore originator for recvfrom() */
 
        if (head->nframes){
                memcpy(skb_put(skb, datalen), frames, datalen);
@@ -1370,54 +1438,64 @@ static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
        }
 }
 
-static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id)
+/**************************************************/
+/* bcm_op handling: find & delete bcm_op elements */
+/**************************************************/
+
+static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, int ifindex)
 {
        struct bcm_op *op;
 
        list_for_each_entry(op, ops, list)
-               if (op->can_id == can_id)
+               if ((op->can_id == can_id) && (op->ifindex == ifindex))
                        return op;
 
        return NULL;
 }
 
-static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id)
+static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
 {
        struct bcm_op *op, *n;
 
        list_for_each_entry_safe(op, n, ops, list) {
-               if (op->can_id == can_id) {
+               if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
                        DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
 
-                       if (op->sk->sk_bound_dev_if) {
-                               struct net_device *dev = dev_get_by_index(op->sk->sk_bound_dev_if);
+                       /* Don't care if we're bound or not (due to netdev problems) */
+                       /* can_rx_unregister() is always a save thing to do here     */
+                       if (op->ifindex) {
+                               struct net_device *dev = dev_get_by_index(op->ifindex);
                                if (dev) {
                                        can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
                                        dev_put(dev);
                                }
                        } else
-                               DBG("sock %p not bound for can_rx_unregister()\n", op->sk);
+                               can_rx_unregister(NULL, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
 
                        list_del(&op->list);
                        bcm_remove_op(op);
-                       return;
+                       return 1; /* done */
                }
        }
+
+       return 0; /* not found */
 }
 
-static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id)
+static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
 {
        struct bcm_op *op, *n;
 
        list_for_each_entry_safe(op, n, ops, list) {
-               if (op->can_id == can_id) {
+               if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
                        DBG("removing rx_op (%p) for can_id <%03X>\n",
                            op, op->can_id);
                        list_del(&op->list);
                        bcm_remove_op(op);
-                       return;
+                       return 1; /* done */
                }
        }
+
+       return 0; /* not found */
 }
 
 static void bcm_remove_op(struct bcm_op *op)
index bbbbe285c3104f4e98ac5e27ab4a52c8becfb025..8367fbf6a47c6bfcc18b0396dfd536c05c44563f 100644 (file)
@@ -1,7 +1,7 @@
 #
 #  $Id$
 #
-#  Copyright (c) 2002-2005 Volkswagen Group Electronic Research
+#  Copyright (c) 2002-2007 Volkswagen Group Electronic Research
 #  All rights reserved.
 #
 #  Redistribution and use in source and binary forms, with or without
 CFLAGS    = -O2 -Wall -Wno-parentheses -I../kernel/2.6/include \
            -fno-strict-aliasing
 
-PROGRAMS =     tst-raw         \
-               tst-raw-filter  \
-               tst-err         \
-               tst-raw-sendto  \
-               tst-bcm-cycle   \
-               tst-bcm-tx_read \
-               tst-bcm-rtr     \
-               tst-bcm-single  \
-               tst-bcm-filter  \
-               tst-bcm-throttle\
-               tst-proc        \
+PROGRAMS =     tst-raw           \
+               tst-raw-filter    \
+               tst-err           \
+               tst-raw-sendto    \
+               tst-bcm-cycle     \
+               tst-bcm-tx_read   \
+               tst-bcm-rtr       \
+               tst-bcm-single    \
+               tst-bcm-filter    \
+               tst-bcm-throttle  \
+               tst-bcm-rx-sendto \
+               tst-bcm-tx-sendto \
+               tst-proc          \
                canecho
 
 all: $(PROGRAMS)
index 93b3afe4599d0c3335eb8a2ccbbd458bf383e088..715a5b27f545a9ee9a0c74cd01391255b2033a0a 100644 (file)
@@ -176,6 +176,15 @@ int main(int argc, char **argv)
             rxmsg.frame[0].can_id);
     }
 
+    txmsg.msg_head.opcode  = RX_DELETE;
+    txmsg.msg_head.can_id  = 0x042; /* everything we need for RX_DELETE */
+
+    printf("<*>Writing RX_DELETE for can_id <%03X> for RX_SETUP with growing nframes\n",
+          txmsg.msg_head.can_id);
+
+    if (write(s, &txmsg, sizeof(txmsg)) < 0)
+      perror("write");
+
     txmsg.msg_head.opcode  = RX_SETUP;
     txmsg.msg_head.can_id  = 0x042;
     txmsg.msg_head.flags   = SETTIMER|RX_CHECK_DLC;
@@ -298,6 +307,15 @@ int main(int argc, char **argv)
             rxmsg.frame[0].can_id);
     }
 
+    txmsg.msg_head.opcode  = RX_DELETE;
+    txmsg.msg_head.can_id  = 0x042; /* everything we need for RX_DELETE */
+
+    printf("<*>Writing RX_DELETE for can_id <%03X> for RX_SETUP with growing nframes\n",
+          txmsg.msg_head.can_id);
+
+    if (write(s, &txmsg, sizeof(txmsg)) < 0)
+      perror("write");
+
     /* no problems ;-) but NOW we try MUX messages ... and timeouts */
 
     txmsg.msg_head.opcode  = RX_SETUP;
diff --git a/test/tst-bcm-rx-sendto.c b/test/tst-bcm-rx-sendto.c
new file mode 100644 (file)
index 0000000..2827a37
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ *  $Id$
+ */
+
+/*
+ * tst-bcm-rx-sendto.c
+ *
+ * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, the following disclaimer and
+ *    the referenced file 'COPYING'.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Volkswagen nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * Alternatively, provided that this notice is retained in full, this
+ * software may be distributed under the terms of the GNU General
+ * Public License ("GPL") version 2 as distributed in the 'COPYING'
+ * file from the main directory of the linux kernel source.
+ *
+ * The provided data structures and external interfaces from this code
+ * are not restricted to be used by modules with a GPL compatible license.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Send feedback to <socketcan-users@lists.berlios.de>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/uio.h>
+#include <net/if.h>
+
+#include <linux/can.h>
+#include <linux/can/bcm.h>
+
+int main(int argc, char **argv)
+{
+    int s,nbytes;
+    struct sockaddr_can addr;
+    struct ifreq ifr;
+
+    struct timeval tv;
+
+    struct {
+      struct bcm_msg_head msg_head;
+      struct can_frame frame;
+    } txmsg, rxmsg;
+
+    if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
+       perror("socket");
+       return 1;
+    }
+
+    addr.can_family = PF_CAN;
+    addr.can_ifindex = 0; /* bind to 'any' device */
+
+    if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+       perror("connect");
+       return 1;
+    }
+
+    memset(&txmsg, 0, sizeof(txmsg)); /* clear timers, nframes, etc. */
+
+    txmsg.msg_head.opcode  = RX_SETUP;
+    txmsg.msg_head.can_id  = 0x123;
+    txmsg.msg_head.flags   = RX_FILTER_ID;
+
+    if (write(s, &txmsg, sizeof(txmsg)) < 0)
+      perror("write");
+
+    addr.can_family = PF_CAN;
+    strcpy(ifr.ifr_name, "vcan2");
+    ioctl(s, SIOCGIFINDEX, &ifr);
+    addr.can_ifindex = ifr.ifr_ifindex;
+
+    txmsg.msg_head.opcode  = RX_SETUP;
+    txmsg.msg_head.can_id  = 0x321;
+    txmsg.msg_head.flags   = RX_FILTER_ID;
+
+    if (sendto(s, &txmsg, sizeof(txmsg), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0)
+      perror("sendto");
+
+    addr.can_family = PF_CAN;
+    strcpy(ifr.ifr_name, "vcan1");
+    ioctl(s, SIOCGIFINDEX, &ifr);
+    addr.can_ifindex = ifr.ifr_ifindex;
+
+    txmsg.msg_head.opcode  = RX_SETUP;
+    txmsg.msg_head.can_id  = 0x424;
+    txmsg.msg_head.flags   = RX_FILTER_ID;
+
+    if (sendto(s, &txmsg, sizeof(txmsg), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0)
+      perror("sendto");
+
+    while (1) {
+       socklen_t len = sizeof(addr);
+       nbytes = recvfrom(s, &rxmsg, sizeof(rxmsg),
+                         0, (struct sockaddr*)&addr, &len);
+       if (nbytes < 0) {
+           perror("recvfrom");
+           return 1;
+       } else if (nbytes < sizeof(rxmsg)) {
+           fprintf(stderr, "recvfrom: incomplete BCM message from iface %d\n",
+                   addr.can_ifindex);
+           return 1;
+       } else {
+           int i;
+
+           ioctl(s, SIOCGSTAMP, &tv);
+           printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
+
+           ifr.ifr_ifindex = addr.can_ifindex;
+           ioctl(s, SIOCGIFNAME, &ifr);
+
+           printf(" %-5s ", ifr.ifr_name);
+           if (rxmsg.frame.can_id & CAN_EFF_FLAG)
+               printf("%8X  ", rxmsg.frame.can_id & CAN_EFF_MASK);
+           else
+               printf("%3X  ", rxmsg.frame.can_id & CAN_SFF_MASK);
+           
+           printf("[%d] ", rxmsg.frame.can_dlc);
+           
+           for (i = 0; i < rxmsg.frame.can_dlc; i++) {
+               printf("%02X ", rxmsg.frame.data[i]);
+           }
+           if (rxmsg.frame.can_id & CAN_RTR_FLAG)
+               printf("remote request");
+           printf("\n");
+           fflush(stdout);
+       }
+    }
+
+    close(s);
+
+    return 0;
+}
diff --git a/test/tst-bcm-tx-sendto.c b/test/tst-bcm-tx-sendto.c
new file mode 100644 (file)
index 0000000..72a8480
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ *  $Id$
+ */
+
+/*
+ * tst-bcm-tx-sendto.c
+ *
+ * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, the following disclaimer and
+ *    the referenced file 'COPYING'.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Volkswagen nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * Alternatively, provided that this notice is retained in full, this
+ * software may be distributed under the terms of the GNU General
+ * Public License ("GPL") version 2 as distributed in the 'COPYING'
+ * file from the main directory of the linux kernel source.
+ *
+ * The provided data structures and external interfaces from this code
+ * are not restricted to be used by modules with a GPL compatible license.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Send feedback to <socketcan-users@lists.berlios.de>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/uio.h>
+#include <net/if.h>
+
+#include <linux/can.h>
+#include <linux/can/bcm.h>
+
+#define U64_DATA(p) (*(unsigned long long*)(p)->data)
+
+int main(int argc, char **argv)
+{
+    int s;
+    struct sockaddr_can addr;
+    struct ifreq ifr;
+
+    struct {
+      struct bcm_msg_head msg_head;
+      struct can_frame frame;
+    } txmsg;
+
+    if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
+       perror("socket");
+       return 1;
+    }
+
+    addr.can_family = PF_CAN;
+    addr.can_ifindex = 0; /* bind to 'any' device */
+
+    if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+       perror("connect");
+       return 1;
+    }
+
+    txmsg.msg_head.opcode  = TX_SETUP;
+    txmsg.msg_head.can_id  = 0x42;
+    txmsg.msg_head.flags   = SETTIMER|STARTTIMER;
+    txmsg.msg_head.nframes = 1;
+    txmsg.msg_head.count = 10;
+    txmsg.msg_head.ival1.tv_sec = 1;
+    txmsg.msg_head.ival1.tv_usec = 0;
+    txmsg.msg_head.ival2.tv_sec = 0;
+    txmsg.msg_head.ival2.tv_usec = 0;
+    txmsg.frame.can_id    = 0x42;
+    txmsg.frame.can_dlc   = 8;
+    U64_DATA(&txmsg.frame) = (__u64) 0xdeadbeefdeadbeefULL;
+
+    /* should cause an error due to ifindex = 0 */
+    if (write(s, &txmsg, sizeof(txmsg)) < 0)
+      perror("write");
+
+    printf("Press any key to send on valid device ...\n");
+    getchar();
+
+    addr.can_family = PF_CAN;
+    strcpy(ifr.ifr_name, "vcan2");
+    ioctl(s, SIOCGIFINDEX, &ifr);
+    addr.can_ifindex = ifr.ifr_ifindex;
+
+    if (sendto(s, &txmsg, sizeof(txmsg), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0)
+      perror("sendto");
+
+    printf("Press any key to close the socket ...\n");
+    getchar();
+
+    close(s);
+
+    return 0;
+}