net: Make setsockopt() optlen be unsigned.
[safe/jmp/linux-2.6] / net / can / af_can.c
index 5158e88..6068321 100644 (file)
@@ -118,7 +118,6 @@ static int can_create(struct net *net, struct socket *sock, int protocol)
 {
        struct sock *sk;
        struct can_proto *cp;
-       char module_name[sizeof("can-proto-000")];
        int err = 0;
 
        sock->state = SS_UNCONNECTED;
@@ -129,26 +128,21 @@ static int can_create(struct net *net, struct socket *sock, int protocol)
        if (net != &init_net)
                return -EAFNOSUPPORT;
 
-       /* try to load protocol module, when CONFIG_KMOD is defined */
+#ifdef CONFIG_MODULES
+       /* try to load protocol module kernel is modular */
        if (!proto_tab[protocol]) {
-               sprintf(module_name, "can-proto-%d", protocol);
-               err = request_module(module_name);
+               err = request_module("can-proto-%d", protocol);
 
                /*
                 * In case of error we only print a message but don't
                 * return the error code immediately.  Below we will
                 * return -EPROTONOSUPPORT
                 */
-               if (err == -ENOSYS) {
-                       if (printk_ratelimit())
-                               printk(KERN_INFO "can: request_module(%s)"
-                                      " not implemented.\n", module_name);
-               } else if (err) {
-                       if (printk_ratelimit())
-                               printk(KERN_ERR "can: request_module(%s)"
-                                      " failed.\n", module_name);
-               }
+               if (err && printk_ratelimit())
+                       printk(KERN_ERR "can: request_module "
+                              "(can-proto-%d) failed.\n", protocol);
        }
+#endif
 
        spin_lock(&proto_tab_lock);
        cp = proto_tab[protocol];
@@ -205,17 +199,27 @@ static int can_create(struct net *net, struct socket *sock, int protocol)
  * @skb: pointer to socket buffer with CAN frame in data section
  * @loop: loopback for listeners on local CAN sockets (recommended default!)
  *
+ * Due to the loopback this routine must not be called from hardirq context.
+ *
  * Return:
  *  0 on success
  *  -ENETDOWN when the selected interface is down
  *  -ENOBUFS on full driver queue (see net_xmit_errno())
  *  -ENOMEM when local loopback failed at calling skb_clone()
  *  -EPERM when trying to send on a non-CAN interface
+ *  -EINVAL when the skb->data does not contain a valid CAN frame
  */
 int can_send(struct sk_buff *skb, int loop)
 {
+       struct sk_buff *newskb = NULL;
+       struct can_frame *cf = (struct can_frame *)skb->data;
        int err;
 
+       if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
+               kfree_skb(skb);
+               return -EINVAL;
+       }
+
        if (skb->dev->type != ARPHRD_CAN) {
                kfree_skb(skb);
                return -EPERM;
@@ -250,8 +254,7 @@ int can_send(struct sk_buff *skb, int loop)
                         * If the interface is not capable to do loopback
                         * itself, we do it here.
                         */
-                       struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
-
+                       newskb = skb_clone(skb, GFP_ATOMIC);
                        if (!newskb) {
                                kfree_skb(skb);
                                return -ENOMEM;
@@ -260,7 +263,6 @@ int can_send(struct sk_buff *skb, int loop)
                        newskb->sk = skb->sk;
                        newskb->ip_summed = CHECKSUM_UNNECESSARY;
                        newskb->pkt_type = PACKET_BROADCAST;
-                       netif_rx(newskb);
                }
        } else {
                /* indication for the CAN driver: no loopback required */
@@ -272,11 +274,19 @@ int can_send(struct sk_buff *skb, int loop)
        if (err > 0)
                err = net_xmit_errno(err);
 
+       if (err) {
+               kfree_skb(newskb);
+               return err;
+       }
+
+       if (newskb)
+               netif_rx_ni(newskb);
+
        /* update statistics */
        can_stats.tx_frames++;
        can_stats.tx_frames_delta++;
 
-       return err;
+       return 0;
 }
 EXPORT_SYMBOL(can_send);
 
@@ -310,23 +320,52 @@ static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
        return n ? d : NULL;
 }
 
+/**
+ * find_rcv_list - determine optimal filterlist inside device filter struct
+ * @can_id: pointer to CAN identifier of a given can_filter
+ * @mask: pointer to CAN mask of a given can_filter
+ * @d: pointer to the device filter struct
+ *
+ * Description:
+ *  Returns the optimal filterlist to reduce the filter handling in the
+ *  receive path. This function is called by service functions that need
+ *  to register or unregister a can_filter in the filter lists.
+ *
+ *  A filter matches in general, when
+ *
+ *          <received_can_id> & mask == can_id & mask
+ *
+ *  so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
+ *  relevant bits for the filter.
+ *
+ *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
+ *  filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
+ *  there is a special filterlist and a special rx path filter handling.
+ *
+ * Return:
+ *  Pointer to optimal filterlist for the given can_id/mask pair.
+ *  Constistency checked mask.
+ *  Reduced can_id to have a preprocessed filter compare value.
+ */
 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
                                        struct dev_rcv_lists *d)
 {
        canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
 
-       /* filter error frames */
+       /* filter for error frames in extra filterlist */
        if (*mask & CAN_ERR_FLAG) {
-               /* clear CAN_ERR_FLAG in list entry */
+               /* clear CAN_ERR_FLAG in filter entry */
                *mask &= CAN_ERR_MASK;
                return &d->rx[RX_ERR];
        }
 
-       /* ensure valid values in can_mask */
-       if (*mask & CAN_EFF_FLAG)
-               *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
-       else
-               *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
+       /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
+
+#define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
+
+       /* ensure valid values in can_mask for 'SFF only' frame filtering */
+       if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
+               *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
 
        /* reduce condition testing at receive time */
        *can_id &= *mask;
@@ -339,15 +378,19 @@ static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
        if (!(*mask))
                return &d->rx[RX_ALL];
 
-       /* use extra filterset for the subscription of exactly *ONE* can_id */
-       if (*can_id & CAN_EFF_FLAG) {
-               if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
-                       /* RFC: a use-case for hash-tables in the future? */
-                       return &d->rx[RX_EFF];
+       /* extra filterlists for the subscription of a single non-RTR can_id */
+       if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
+           && !(*can_id & CAN_RTR_FLAG)) {
+
+               if (*can_id & CAN_EFF_FLAG) {
+                       if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
+                               /* RFC: a future use-case for hash-tables? */
+                               return &d->rx[RX_EFF];
+                       }
+               } else {
+                       if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
+                               return &d->rx_sff[*can_id];
                }
-       } else {
-               if (*mask == CAN_SFF_MASK)
-                       return &d->rx_sff[*can_id];
        }
 
        /* default: filter via can_id/can_mask */
@@ -372,6 +415,12 @@ static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
  *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
  *  filter for error frames (CAN_ERR_FLAG bit set in mask).
  *
+ *  The provided pointer to the sk_buff is guaranteed to be valid as long as
+ *  the callback function is running. The callback function must *not* free
+ *  the given sk_buff while processing it's task. When the given sk_buff is
+ *  needed after the end of the callback function it must be cloned inside
+ *  the callback function with skb_clone().
+ *
  * Return:
  *  0 on success
  *  -ENOMEM on missing cache mem to create subscription entry
@@ -527,13 +576,8 @@ EXPORT_SYMBOL(can_rx_unregister);
 
 static inline void deliver(struct sk_buff *skb, struct receiver *r)
 {
-       struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
-
-       if (clone) {
-               clone->sk = skb->sk;
-               r->func(clone, r->data);
-               r->matches++;
-       }
+       r->func(skb, r->data);
+       r->matches++;
 }
 
 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
@@ -580,7 +624,10 @@ static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
                }
        }
 
-       /* check CAN_ID specific entries */
+       /* check filterlists for single non-RTR can_ids */
+       if (can_id & CAN_RTR_FLAG)
+               return matches;
+
        if (can_id & CAN_EFF_FLAG) {
                hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
                        if (r->can_id == can_id) {
@@ -603,12 +650,19 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
                   struct packet_type *pt, struct net_device *orig_dev)
 {
        struct dev_rcv_lists *d;
+       struct can_frame *cf = (struct can_frame *)skb->data;
        int matches;
 
-       if (dev->type != ARPHRD_CAN || dev->nd_net != &init_net) {
-               kfree_skb(skb);
-               return 0;
-       }
+       if (!net_eq(dev_net(dev), &init_net))
+               goto drop;
+
+       if (WARN_ONCE(dev->type != ARPHRD_CAN ||
+                     skb->len != sizeof(struct can_frame) ||
+                     cf->can_dlc > 8,
+                     "PF_CAN: dropped non conform skbuf: "
+                     "dev type %d, len %d, can_dlc %d\n",
+                     dev->type, skb->len, cf->can_dlc))
+               goto drop;
 
        /* update statistics */
        can_stats.rx_frames++;
@@ -626,15 +680,19 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
 
        rcu_read_unlock();
 
-       /* free the skbuff allocated by the netdevice driver */
-       kfree_skb(skb);
+       /* consume the skbuff allocated by the netdevice driver */
+       consume_skb(skb);
 
        if (matches > 0) {
                can_stats.matches++;
                can_stats.matches_delta++;
        }
 
-       return 0;
+       return NET_RX_SUCCESS;
+
+drop:
+       kfree_skb(skb);
+       return NET_RX_DROP;
 }
 
 /*
@@ -662,26 +720,26 @@ int can_proto_register(struct can_proto *cp)
                return -EINVAL;
        }
 
+       err = proto_register(cp->prot, 0);
+       if (err < 0)
+               return err;
+
        spin_lock(&proto_tab_lock);
        if (proto_tab[proto]) {
                printk(KERN_ERR "can: protocol %d already registered\n",
                       proto);
                err = -EBUSY;
-               goto errout;
+       } else {
+               proto_tab[proto] = cp;
+
+               /* use generic ioctl function if not defined by module */
+               if (!cp->ops->ioctl)
+                       cp->ops->ioctl = can_ioctl;
        }
+       spin_unlock(&proto_tab_lock);
 
-       err = proto_register(cp->prot, 0);
        if (err < 0)
-               goto errout;
-
-       proto_tab[proto] = cp;
-
-       /* use generic ioctl function if the module doesn't bring its own */
-       if (!cp->ops->ioctl)
-               cp->ops->ioctl = can_ioctl;
-
- errout:
-       spin_unlock(&proto_tab_lock);
+               proto_unregister(cp->prot);
 
        return err;
 }
@@ -700,9 +758,10 @@ void can_proto_unregister(struct can_proto *cp)
                printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
                       proto);
        }
-       proto_unregister(cp->prot);
        proto_tab[proto] = NULL;
        spin_unlock(&proto_tab_lock);
+
+       proto_unregister(cp->prot);
 }
 EXPORT_SYMBOL(can_proto_unregister);
 
@@ -715,7 +774,7 @@ static int can_notifier(struct notifier_block *nb, unsigned long msg,
        struct net_device *dev = (struct net_device *)data;
        struct dev_rcv_lists *d;
 
-       if (dev->nd_net != &init_net)
+       if (!net_eq(dev_net(dev), &init_net))
                return NOTIFY_DONE;
 
        if (dev->type != ARPHRD_CAN)
@@ -778,7 +837,7 @@ static int can_notifier(struct notifier_block *nb, unsigned long msg,
  */
 
 static struct packet_type can_packet __read_mostly = {
-       .type = __constant_htons(ETH_P_CAN),
+       .type = cpu_to_be16(ETH_P_CAN),
        .dev  = NULL,
        .func = can_rcv,
 };
@@ -854,6 +913,8 @@ static __exit void can_exit(void)
        }
        spin_unlock(&can_rcvlists_lock);
 
+       rcu_barrier(); /* Wait for completion of call_rcu()'s */
+
        kmem_cache_destroy(rcv_cache);
 }