]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/net/can/af_can.c
can: make struct can_proto const
[socketcan-devel.git] / kernel / 2.6 / net / can / af_can.c
index 00de9efa1313c1ae611c89d4a9897bba6a23080d..91f872764c093749320f5fcf775a8f12cf49fbf6 100644 (file)
@@ -101,8 +101,8 @@ static kmem_cache_t *rcv_cache;
 #endif
 
 /* table of registered CAN protocols */
-static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
-static DEFINE_SPINLOCK(proto_tab_lock);
+static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
+static DEFINE_MUTEX(proto_tab_lock);
 
 struct timer_list can_stattimer;   /* timer for statistics update */
 struct s_stats    can_stats;       /* packet statistics */
@@ -112,7 +112,7 @@ struct s_pstats   can_pstats;      /* receive list statistics */
  * af_can socket functions
  */
 
-static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
+int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
        struct sock *sk = sock->sk;
 
@@ -129,6 +129,7 @@ static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 #endif
        }
 }
+EXPORT_SYMBOL(can_ioctl);
 
 static void can_sock_destruct(struct sock *sk)
 {
@@ -139,14 +140,34 @@ static void can_sock_destruct(struct sock *sk)
 #endif
 }
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
+static const struct can_proto *can_try_module_get(int protocol)
+{
+       const struct can_proto *cp;
+
+       rcu_read_lock();
+       cp = rcu_dereference(proto_tab[protocol]);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+       if (cp && !try_module_get(cp->prot->owner))
+               cp = NULL;
+#else
+       if (cp && !try_module_get(cp->owner))
+               cp = NULL;
+#endif
+       rcu_read_unlock();
+
+       return cp;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
+static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
 static int can_create(struct net *net, struct socket *sock, int protocol)
 #else
 static int can_create(struct socket *sock, int protocol)
 #endif
 {
        struct sock *sk;
-       struct can_proto *cp;
+       const struct can_proto *cp;
        int err = 0;
 
        sock->state = SS_UNCONNECTED;
@@ -159,9 +180,12 @@ static int can_create(struct socket *sock, int protocol)
                return -EAFNOSUPPORT;
 #endif
 
+       cp = can_try_module_get(protocol);
+
 #ifdef CONFIG_MODULES
-       /* try to load protocol module kernel is modular */
-       if (!proto_tab[protocol]) {
+       if (!cp) {
+               /* try to load protocol module if kernel is modular */
+
                err = request_module("can-proto-%d", protocol);
 
                /*
@@ -172,19 +196,10 @@ static int can_create(struct socket *sock, int protocol)
                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];
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
-       if (cp && !try_module_get(cp->prot->owner))
-               cp = NULL;
-#else
-       if (cp && !try_module_get(cp->owner))
-               cp = NULL;
+               cp = can_try_module_get(protocol);
+       }
 #endif
-       spin_unlock(&proto_tab_lock);
 
        /* check for available protocol and correct usage */
 
@@ -192,15 +207,16 @@ static int can_create(struct socket *sock, int protocol)
                return -EPROTONOSUPPORT;
 
        if (cp->type != sock->type) {
-               err = -EPROTONOSUPPORT;
+               err = -EPROTOTYPE;
                goto errout;
        }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
        if (cp->capability >= 0 && !capable(cp->capability)) {
                err = -EPERM;
                goto errout;
        }
-
+#endif
        sock->ops = cp->ops;
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
@@ -262,6 +278,8 @@ static int can_create(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
@@ -355,7 +373,7 @@ int can_send(struct sk_buff *skb, int loop)
        }
 
        if (newskb)
-               netif_rx(newskb);
+               netif_rx_ni(newskb);
 
        /* update statistics */
        can_stats.tx_frames++;
@@ -369,6 +387,26 @@ EXPORT_SYMBOL(can_send);
  * af_can rx path
  */
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
+{
+       /*
+        * find receive list for this device
+        *
+        * Since 2.6.26 a new "midlevel private" ml_priv pointer has been
+        * introduced in struct net_device. We use this pointer to omit the
+        * linear walk through the can_rx_dev_list. A similar speedup has been
+        * queued for 2.6.34 mainline but using the new netdev_rcu lists.
+        * Therefore the can_rx_dev_list is still needed (e.g. in proc.c)
+        */
+
+       /* dev == NULL is the indicator for the 'all' filterlist */
+       if (!dev)
+               return &can_rx_alldev_list;
+       else
+               return (struct dev_rcv_lists *)dev->ml_priv;
+}
+#else
 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
 {
        struct dev_rcv_lists *d = NULL;
@@ -394,6 +432,7 @@ static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
 
        return n ? d : NULL;
 }
+#endif
 
 /**
  * find_rcv_list - determine optimal filterlist inside device filter struct
@@ -512,6 +551,11 @@ int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
 
        /* insert new receiver  (dev,canid,mask) -> (func,data) */
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+       if (dev && dev->type != ARPHRD_CAN)
+               return -ENODEV;
+#endif
+
        r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
        if (!r)
                return -ENOMEM;
@@ -585,6 +629,11 @@ void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
        struct hlist_node *next;
        struct dev_rcv_lists *d;
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+       if (dev && dev->type != ARPHRD_CAN)
+               return;
+#endif
+
        spin_lock(&can_rcvlists_lock);
 
        d = find_dev_rcv_lists(dev);
@@ -631,9 +680,12 @@ void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
                can_pstats.rcv_entries--;
 
        /* remove device structure requested by NETDEV_UNREGISTER */
-       if (d->remove_on_zero_entries && !d->entries)
+       if (d->remove_on_zero_entries && !d->entries) {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+               dev->ml_priv = NULL;
+#endif
                hlist_del_rcu(&d->list);
-       else
+       else
                d = NULL;
 
  out:
@@ -734,17 +786,26 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
        int matches;
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
-       if (dev->type != ARPHRD_CAN || !net_eq(dev_net(dev), &init_net)) {
+       if (!net_eq(dev_net(dev), &init_net))
+               goto drop;
 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
-       if (dev->type != ARPHRD_CAN || dev->nd_net != &init_net) {
-#else
-       if (dev->type != ARPHRD_CAN) {
+       if (dev->nd_net != &init_net)
+               goto drop;
 #endif
-               kfree_skb(skb);
-               return 0;
-       }
 
-       BUG_ON(skb->len != sizeof(struct can_frame) || cf->can_dlc > 8);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
+       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;
+#else
+       BUG_ON(dev->type != ARPHRD_CAN ||
+              skb->len != sizeof(struct can_frame) ||
+              cf->can_dlc > 8);
+#endif
 
        /* update statistics */
        can_stats.rx_frames++;
@@ -774,7 +835,13 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
                can_stats.matches_delta++;
        }
 
-       return 0;
+       return NET_RX_SUCCESS;
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
+drop:
+       kfree_skb(skb);
+       return NET_RX_DROP;
+#endif
 }
 
 /*
@@ -791,7 +858,7 @@ static int can_rcv(struct sk_buff *skb, struct net_device *dev,
  *  -EBUSY  protocol already in use
  *  -ENOBUF if proto_register() fails
  */
-int can_proto_register(struct can_proto *cp)
+int can_proto_register(const struct can_proto *cp)
 {
        int proto = cp->protocol;
        int err = 0;
@@ -808,19 +875,16 @@ int can_proto_register(struct can_proto *cp)
                return err;
 #endif
 
-       spin_lock(&proto_tab_lock);
+       mutex_lock(&proto_tab_lock);
+
        if (proto_tab[proto]) {
                printk(KERN_ERR "can: protocol %d already registered\n",
                       proto);
                err = -EBUSY;
-       } else {
-               proto_tab[proto] = cp;
+       } else
+               rcu_assign_pointer(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);
+       mutex_unlock(&proto_tab_lock);
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
        if (err < 0)
@@ -835,17 +899,16 @@ EXPORT_SYMBOL(can_proto_register);
  * can_proto_unregister - unregister CAN transport protocol
  * @cp: pointer to CAN protocol structure
  */
-void can_proto_unregister(struct can_proto *cp)
+void can_proto_unregister(const struct can_proto *cp)
 {
        int proto = cp->protocol;
 
-       spin_lock(&proto_tab_lock);
-       if (!proto_tab[proto]) {
-               printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
-                      proto);
-       }
-       proto_tab[proto] = NULL;
-       spin_unlock(&proto_tab_lock);
+       mutex_lock(&proto_tab_lock);
+       BUG_ON(proto_tab[proto] != cp);
+       rcu_assign_pointer(proto_tab[proto], NULL);
+       mutex_unlock(&proto_tab_lock);
+
+       synchronize_rcu();
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
        proto_unregister(cp->prot);
@@ -895,6 +958,10 @@ static int can_notifier(struct notifier_block *nb, unsigned long msg,
                d->dev = dev;
 
                spin_lock(&can_rcvlists_lock);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+               BUG_ON(dev->ml_priv);
+               dev->ml_priv = d;
+#endif
                hlist_add_head_rcu(&d->list, &can_rx_dev_list);
                spin_unlock(&can_rcvlists_lock);
 
@@ -908,8 +975,12 @@ static int can_notifier(struct notifier_block *nb, unsigned long msg,
                        if (d->entries) {
                                d->remove_on_zero_entries = 1;
                                d = NULL;
-                       } else
+                       } else {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+                               dev->ml_priv = NULL;
+#endif
                                hlist_del_rcu(&d->list);
+                       }
                } else
                        printk(KERN_ERR "can: notifier: receive list not "
                               "found for dev %s\n", dev->name);
@@ -1011,6 +1082,10 @@ static __exit void can_exit(void)
        hlist_del(&can_rx_alldev_list.list);
        hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
                hlist_del(&d->list);
+               BUG_ON(d->entries);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
+               d->dev->ml_priv = NULL;
+#endif
                kfree(d);
        }
        spin_unlock(&can_rcvlists_lock);