2 * af_can.c - Protocol family CAN core module
3 * (used by different CAN protocol modules)
5 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2, in which case the provisions of the
23 * GPL apply INSTEAD OF those given above.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/module.h>
46 #include <linux/version.h>
47 #include <linux/init.h>
48 #include <linux/kmod.h>
49 #include <linux/slab.h>
50 #include <linux/list.h>
51 #include <linux/spinlock.h>
52 #include <linux/rcupdate.h>
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
54 #include <linux/uaccess.h>
56 #include <asm/uaccess.h>
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
60 #include <linux/socket.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_arp.h>
63 #include <linux/skbuff.h>
64 #include <socketcan/can.h>
65 #include <socketcan/can/core.h>
66 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
67 #include <net/net_namespace.h>
72 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
76 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
79 static __initdata const char banner[] = KERN_INFO
80 "can: controller area network core (" CAN_VERSION_STRING ")\n";
82 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
85 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
87 MODULE_ALIAS_NETPROTO(PF_CAN);
89 static int stats_timer __read_mostly = 1;
90 module_param(stats_timer, int, S_IRUGO);
91 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
93 HLIST_HEAD(can_rx_dev_list);
94 static struct dev_rcv_lists can_rx_alldev_list;
95 static DEFINE_SPINLOCK(can_rcvlists_lock);
97 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
98 static struct kmem_cache *rcv_cache __read_mostly;
100 static kmem_cache_t *rcv_cache;
103 /* table of registered CAN protocols */
104 static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
105 static DEFINE_SPINLOCK(proto_tab_lock);
107 struct timer_list can_stattimer; /* timer for statistics update */
108 struct s_stats can_stats; /* packet statistics */
109 struct s_pstats can_pstats; /* receive list statistics */
112 * af_can socket functions
115 int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
117 struct sock *sk = sock->sk;
122 return sock_get_timestamp(sk, (struct timeval __user *)arg);
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
128 return dev_ioctl(cmd, (void __user *)arg);
132 EXPORT_SYMBOL(can_ioctl);
134 static void can_sock_destruct(struct sock *sk)
136 skb_queue_purge(&sk->sk_receive_queue);
137 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
139 kfree(sk->sk_protinfo);
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
144 static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
145 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
146 static int can_create(struct net *net, struct socket *sock, int protocol)
148 static int can_create(struct socket *sock, int protocol)
152 struct can_proto *cp;
155 sock->state = SS_UNCONNECTED;
157 if (protocol < 0 || protocol >= CAN_NPROTO)
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
161 if (net != &init_net)
162 return -EAFNOSUPPORT;
165 #ifdef CONFIG_MODULES
166 /* try to load protocol module kernel is modular */
167 if (!proto_tab[protocol]) {
168 err = request_module("can-proto-%d", protocol);
171 * In case of error we only print a message but don't
172 * return the error code immediately. Below we will
173 * return -EPROTONOSUPPORT
175 if (err && printk_ratelimit())
176 printk(KERN_ERR "can: request_module "
177 "(can-proto-%d) failed.\n", protocol);
181 spin_lock(&proto_tab_lock);
182 cp = proto_tab[protocol];
183 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
184 if (cp && !try_module_get(cp->prot->owner))
187 if (cp && !try_module_get(cp->owner))
190 spin_unlock(&proto_tab_lock);
192 /* check for available protocol and correct usage */
195 return -EPROTONOSUPPORT;
197 if (cp->type != sock->type) {
198 err = -EPROTONOSUPPORT;
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
203 if (cp->capability >= 0 && !capable(cp->capability)) {
210 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
211 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
212 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
213 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
215 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
222 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
224 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
225 if (!sk->sk_protinfo) {
231 sk_set_owner(sk, proto_tab[protocol]->owner);
234 sock_init_data(sock, sk);
235 sk->sk_destruct = can_sock_destruct;
237 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
238 if (sk->sk_prot->init)
239 err = sk->sk_prot->init(sk);
246 /* release sk on errors */
252 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
253 module_put(cp->prot->owner);
255 module_put(cp->owner);
265 * can_send - transmit a CAN frame (optional with local loopback)
266 * @skb: pointer to socket buffer with CAN frame in data section
267 * @loop: loopback for listeners on local CAN sockets (recommended default!)
269 * Due to the loopback this routine must not be called from hardirq context.
273 * -ENETDOWN when the selected interface is down
274 * -ENOBUFS on full driver queue (see net_xmit_errno())
275 * -ENOMEM when local loopback failed at calling skb_clone()
276 * -EPERM when trying to send on a non-CAN interface
277 * -EINVAL when the skb->data does not contain a valid CAN frame
279 int can_send(struct sk_buff *skb, int loop)
281 struct sk_buff *newskb = NULL;
282 struct can_frame *cf = (struct can_frame *)skb->data;
285 if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
290 if (skb->dev->type != ARPHRD_CAN) {
295 if (!(skb->dev->flags & IFF_UP)) {
300 skb->protocol = htons(ETH_P_CAN);
301 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
302 skb_reset_network_header(skb);
303 skb_reset_transport_header(skb);
305 skb->nh.raw = skb->data;
306 skb->h.raw = skb->data;
310 /* local loopback of sent CAN frames */
312 /* indication for the CAN driver: do loopback */
313 skb->pkt_type = PACKET_LOOPBACK;
316 * The reference to the originating sock may be required
317 * by the receiving socket to check whether the frame is
318 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
319 * Therefore we have to ensure that skb->sk remains the
320 * reference to the originating sock by restoring skb->sk
321 * after each skb_clone() or skb_orphan() usage.
324 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
325 #define IFF_ECHO IFF_LOOPBACK
327 if (!(skb->dev->flags & IFF_ECHO)) {
329 * If the interface is not capable to do loopback
330 * itself, we do it here.
332 newskb = skb_clone(skb, GFP_ATOMIC);
338 newskb->sk = skb->sk;
339 newskb->ip_summed = CHECKSUM_UNNECESSARY;
340 newskb->pkt_type = PACKET_BROADCAST;
343 /* indication for the CAN driver: no loopback required */
344 skb->pkt_type = PACKET_HOST;
347 /* send to netdevice */
348 err = dev_queue_xmit(skb);
350 err = net_xmit_errno(err);
353 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
354 /* kfree_skb() does not check for !NULL on older kernels */
366 /* update statistics */
367 can_stats.tx_frames++;
368 can_stats.tx_frames_delta++;
372 EXPORT_SYMBOL(can_send);
378 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
379 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
382 * find receive list for this device
384 * Since 2.6.26 a new "midlevel private" ml_priv pointer has been
385 * introduced in struct net_device. We use this pointer to omit the
386 * linear walk through the can_rx_dev_list. A similar speedup has been
387 * queued for 2.6.34 mainline but using the new netdev_rcu lists.
388 * Therefore the can_rx_dev_list is still needed (e.g. in proc.c)
391 /* dev == NULL is the indicator for the 'all' filterlist */
393 return &can_rx_alldev_list;
395 return (struct dev_rcv_lists *)dev->ml_priv;
398 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
400 struct dev_rcv_lists *d = NULL;
401 struct hlist_node *n;
404 * find receive list for this device
406 * The hlist_for_each_entry*() macros curse through the list
407 * using the pointer variable n and set d to the containing
408 * struct in each list iteration. Therefore, after list
409 * iteration, d is unmodified when the list is empty, and it
410 * points to last list element, when the list is non-empty
411 * but no match in the loop body is found. I.e. d is *not*
412 * NULL when no match is found. We can, however, use the
413 * cursor variable n to decide if a match was found.
416 hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
426 * find_rcv_list - determine optimal filterlist inside device filter struct
427 * @can_id: pointer to CAN identifier of a given can_filter
428 * @mask: pointer to CAN mask of a given can_filter
429 * @d: pointer to the device filter struct
432 * Returns the optimal filterlist to reduce the filter handling in the
433 * receive path. This function is called by service functions that need
434 * to register or unregister a can_filter in the filter lists.
436 * A filter matches in general, when
438 * <received_can_id> & mask == can_id & mask
440 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
441 * relevant bits for the filter.
443 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
444 * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
445 * there is a special filterlist and a special rx path filter handling.
448 * Pointer to optimal filterlist for the given can_id/mask pair.
449 * Constistency checked mask.
450 * Reduced can_id to have a preprocessed filter compare value.
452 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
453 struct dev_rcv_lists *d)
455 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
457 /* filter for error frames in extra filterlist */
458 if (*mask & CAN_ERR_FLAG) {
459 /* clear CAN_ERR_FLAG in filter entry */
460 *mask &= CAN_ERR_MASK;
461 return &d->rx[RX_ERR];
464 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
466 #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
468 /* ensure valid values in can_mask for 'SFF only' frame filtering */
469 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
470 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
472 /* reduce condition testing at receive time */
475 /* inverse can_id/can_mask filter */
477 return &d->rx[RX_INV];
479 /* mask == 0 => no condition testing at receive time */
481 return &d->rx[RX_ALL];
483 /* extra filterlists for the subscription of a single non-RTR can_id */
484 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
485 && !(*can_id & CAN_RTR_FLAG)) {
487 if (*can_id & CAN_EFF_FLAG) {
488 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
489 /* RFC: a future use-case for hash-tables? */
490 return &d->rx[RX_EFF];
493 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
494 return &d->rx_sff[*can_id];
498 /* default: filter via can_id/can_mask */
499 return &d->rx[RX_FIL];
503 * can_rx_register - subscribe CAN frames from a specific interface
504 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
505 * @can_id: CAN identifier (see description)
506 * @mask: CAN mask (see description)
507 * @func: callback function on filter match
508 * @data: returned parameter for callback function
509 * @ident: string for calling module indentification
512 * Invokes the callback function with the received sk_buff and the given
513 * parameter 'data' on a matching receive filter. A filter matches, when
515 * <received_can_id> & mask == can_id & mask
517 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
518 * filter for error frames (CAN_ERR_FLAG bit set in mask).
520 * The provided pointer to the sk_buff is guaranteed to be valid as long as
521 * the callback function is running. The callback function must *not* free
522 * the given sk_buff while processing it's task. When the given sk_buff is
523 * needed after the end of the callback function it must be cloned inside
524 * the callback function with skb_clone().
528 * -ENOMEM on missing cache mem to create subscription entry
529 * -ENODEV unknown device
531 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
532 void (*func)(struct sk_buff *, void *), void *data,
536 struct hlist_head *rl;
537 struct dev_rcv_lists *d;
540 /* insert new receiver (dev,canid,mask) -> (func,data) */
542 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
543 if (dev && dev->type != ARPHRD_CAN)
547 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
551 spin_lock(&can_rcvlists_lock);
553 d = find_dev_rcv_lists(dev);
555 rl = find_rcv_list(&can_id, &mask, d);
564 hlist_add_head_rcu(&r->list, rl);
567 can_pstats.rcv_entries++;
568 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
569 can_pstats.rcv_entries_max = can_pstats.rcv_entries;
571 kmem_cache_free(rcv_cache, r);
575 spin_unlock(&can_rcvlists_lock);
579 EXPORT_SYMBOL(can_rx_register);
582 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
584 static void can_rx_delete_device(struct rcu_head *rp)
586 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
592 * can_rx_delete_receiver - rcu callback for single receiver entry removal
594 static void can_rx_delete_receiver(struct rcu_head *rp)
596 struct receiver *r = container_of(rp, struct receiver, rcu);
598 kmem_cache_free(rcv_cache, r);
602 * can_rx_unregister - unsubscribe CAN frames from a specific interface
603 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
604 * @can_id: CAN identifier
606 * @func: callback function on filter match
607 * @data: returned parameter for callback function
610 * Removes subscription entry depending on given (subscription) values.
612 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
613 void (*func)(struct sk_buff *, void *), void *data)
615 struct receiver *r = NULL;
616 struct hlist_head *rl;
617 struct hlist_node *next;
618 struct dev_rcv_lists *d;
620 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
621 if (dev && dev->type != ARPHRD_CAN)
625 spin_lock(&can_rcvlists_lock);
627 d = find_dev_rcv_lists(dev);
629 printk(KERN_ERR "BUG: receive list not found for "
630 "dev %s, id %03X, mask %03X\n",
631 DNAME(dev), can_id, mask);
635 rl = find_rcv_list(&can_id, &mask, d);
638 * Search the receiver list for the item to delete. This should
639 * exist, since no receiver may be unregistered that hasn't
640 * been registered before.
643 hlist_for_each_entry_rcu(r, next, rl, list) {
644 if (r->can_id == can_id && r->mask == mask
645 && r->func == func && r->data == data)
650 * Check for bugs in CAN protocol implementations:
651 * If no matching list item was found, the list cursor variable next
652 * will be NULL, while r will point to the last item of the list.
656 printk(KERN_ERR "BUG: receive list entry not found for "
657 "dev %s, id %03X, mask %03X\n",
658 DNAME(dev), can_id, mask);
664 hlist_del_rcu(&r->list);
667 if (can_pstats.rcv_entries > 0)
668 can_pstats.rcv_entries--;
670 /* remove device structure requested by NETDEV_UNREGISTER */
671 if (d->remove_on_zero_entries && !d->entries) {
672 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
675 hlist_del_rcu(&d->list);
680 spin_unlock(&can_rcvlists_lock);
682 /* schedule the receiver item for deletion */
684 call_rcu(&r->rcu, can_rx_delete_receiver);
686 /* schedule the device structure for deletion */
688 call_rcu(&d->rcu, can_rx_delete_device);
690 EXPORT_SYMBOL(can_rx_unregister);
692 static inline void deliver(struct sk_buff *skb, struct receiver *r)
694 r->func(skb, r->data);
698 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
701 struct hlist_node *n;
703 struct can_frame *cf = (struct can_frame *)skb->data;
704 canid_t can_id = cf->can_id;
709 if (can_id & CAN_ERR_FLAG) {
710 /* check for error frame entries only */
711 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
712 if (can_id & r->mask) {
720 /* check for unfiltered entries */
721 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
726 /* check for can_id/mask entries */
727 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
728 if ((can_id & r->mask) == r->can_id) {
734 /* check for inverted can_id/mask entries */
735 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
736 if ((can_id & r->mask) != r->can_id) {
742 /* check filterlists for single non-RTR can_ids */
743 if (can_id & CAN_RTR_FLAG)
746 if (can_id & CAN_EFF_FLAG) {
747 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
748 if (r->can_id == can_id) {
754 can_id &= CAN_SFF_MASK;
755 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
764 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
765 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
766 struct packet_type *pt, struct net_device *orig_dev)
768 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
769 struct packet_type *pt)
772 struct dev_rcv_lists *d;
773 struct can_frame *cf = (struct can_frame *)skb->data;
776 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
777 if (!net_eq(dev_net(dev), &init_net))
779 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
780 if (dev->nd_net != &init_net)
784 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
785 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
786 skb->len != sizeof(struct can_frame) ||
788 "PF_CAN: dropped non conform skbuf: "
789 "dev type %d, len %d, can_dlc %d\n",
790 dev->type, skb->len, cf->can_dlc))
793 BUG_ON(dev->type != ARPHRD_CAN ||
794 skb->len != sizeof(struct can_frame) ||
798 /* update statistics */
799 can_stats.rx_frames++;
800 can_stats.rx_frames_delta++;
804 /* deliver the packet to sockets listening on all devices */
805 matches = can_rcv_filter(&can_rx_alldev_list, skb);
807 /* find receive list for this device */
808 d = find_dev_rcv_lists(dev);
810 matches += can_rcv_filter(d, skb);
814 /* consume the skbuff allocated by the netdevice driver */
815 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
823 can_stats.matches_delta++;
826 return NET_RX_SUCCESS;
828 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
836 * af_can protocol functions
840 * can_proto_register - register CAN transport protocol
841 * @cp: pointer to CAN protocol structure
845 * -EINVAL invalid (out of range) protocol number
846 * -EBUSY protocol already in use
847 * -ENOBUF if proto_register() fails
849 int can_proto_register(struct can_proto *cp)
851 int proto = cp->protocol;
854 if (proto < 0 || proto >= CAN_NPROTO) {
855 printk(KERN_ERR "can: protocol number %d out of range\n",
860 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
861 err = proto_register(cp->prot, 0);
866 spin_lock(&proto_tab_lock);
867 if (proto_tab[proto]) {
868 printk(KERN_ERR "can: protocol %d already registered\n",
872 proto_tab[proto] = cp;
874 spin_unlock(&proto_tab_lock);
876 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
878 proto_unregister(cp->prot);
883 EXPORT_SYMBOL(can_proto_register);
886 * can_proto_unregister - unregister CAN transport protocol
887 * @cp: pointer to CAN protocol structure
889 void can_proto_unregister(struct can_proto *cp)
891 int proto = cp->protocol;
893 spin_lock(&proto_tab_lock);
894 if (!proto_tab[proto]) {
895 printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
898 proto_tab[proto] = NULL;
899 spin_unlock(&proto_tab_lock);
901 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
902 proto_unregister(cp->prot);
905 EXPORT_SYMBOL(can_proto_unregister);
908 * af_can notifier to create/remove CAN netdevice specific structs
910 static int can_notifier(struct notifier_block *nb, unsigned long msg,
913 struct net_device *dev = (struct net_device *)data;
914 struct dev_rcv_lists *d;
916 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
917 if (!net_eq(dev_net(dev), &init_net))
919 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
920 if (dev->nd_net != &init_net)
924 if (dev->type != ARPHRD_CAN)
929 case NETDEV_REGISTER:
932 * create new dev_rcv_lists for this device
934 * N.B. zeroing the struct is the correct initialization
935 * for the embedded hlist_head structs.
936 * Another list type, e.g. list_head, would require
937 * explicit initialization.
940 d = kzalloc(sizeof(*d), GFP_KERNEL);
943 "can: allocation of receive list failed\n");
948 spin_lock(&can_rcvlists_lock);
949 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
950 BUG_ON(dev->ml_priv);
953 hlist_add_head_rcu(&d->list, &can_rx_dev_list);
954 spin_unlock(&can_rcvlists_lock);
958 case NETDEV_UNREGISTER:
959 spin_lock(&can_rcvlists_lock);
961 d = find_dev_rcv_lists(dev);
964 d->remove_on_zero_entries = 1;
967 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
970 hlist_del_rcu(&d->list);
973 printk(KERN_ERR "can: notifier: receive list not "
974 "found for dev %s\n", dev->name);
976 spin_unlock(&can_rcvlists_lock);
979 call_rcu(&d->rcu, can_rx_delete_device);
988 * af_can module init/exit functions
991 static struct packet_type can_packet __read_mostly = {
992 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
993 .type = cpu_to_be16(ETH_P_CAN),
995 .type = __constant_htons(ETH_P_CAN),
1001 static struct net_proto_family can_family_ops __read_mostly = {
1003 .create = can_create,
1004 .owner = THIS_MODULE,
1007 /* notifier block for netdevice event */
1008 static struct notifier_block can_netdev_notifier __read_mostly = {
1009 .notifier_call = can_notifier,
1012 static __init int can_init(void)
1016 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1017 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1020 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1027 * Insert can_rx_alldev_list for reception on all devices.
1028 * This struct is zero initialized which is correct for the
1029 * embedded hlist heads, the dev pointer, and the entries counter.
1032 spin_lock(&can_rcvlists_lock);
1033 hlist_add_head_rcu(&can_rx_alldev_list.list, &can_rx_dev_list);
1034 spin_unlock(&can_rcvlists_lock);
1037 /* the statistics are updated every second (timer triggered) */
1038 setup_timer(&can_stattimer, can_stat_update, 0);
1039 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
1041 can_stattimer.function = NULL;
1045 /* protocol register */
1046 sock_register(&can_family_ops);
1047 register_netdevice_notifier(&can_netdev_notifier);
1048 dev_add_pack(&can_packet);
1053 static __exit void can_exit(void)
1055 struct dev_rcv_lists *d;
1056 struct hlist_node *n, *next;
1059 del_timer(&can_stattimer);
1063 /* protocol unregister */
1064 dev_remove_pack(&can_packet);
1065 unregister_netdevice_notifier(&can_netdev_notifier);
1066 sock_unregister(PF_CAN);
1068 /* remove can_rx_dev_list */
1069 spin_lock(&can_rcvlists_lock);
1070 hlist_del(&can_rx_alldev_list.list);
1071 hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
1072 hlist_del(&d->list);
1074 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1075 d->dev->ml_priv = NULL;
1079 spin_unlock(&can_rcvlists_lock);
1081 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1082 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1085 kmem_cache_destroy(rcv_cache);
1088 module_init(can_init);
1089 module_exit(can_exit);