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 const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
105 static DEFINE_MUTEX(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 static const struct can_proto *can_get_proto(int protocol)
145 const struct can_proto *cp;
148 cp = rcu_dereference(proto_tab[protocol]);
149 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
150 if (cp && !try_module_get(cp->prot->owner))
153 if (cp && !try_module_get(cp->owner))
161 static inline void can_put_proto(const struct can_proto *cp)
163 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
164 module_put(cp->prot->owner);
166 module_put(cp->owner);
170 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
171 static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
172 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
173 static int can_create(struct net *net, struct socket *sock, int protocol)
175 static int can_create(struct socket *sock, int protocol)
179 const struct can_proto *cp;
182 sock->state = SS_UNCONNECTED;
184 if (protocol < 0 || protocol >= CAN_NPROTO)
187 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
188 if (net != &init_net)
189 return -EAFNOSUPPORT;
192 cp = can_get_proto(protocol);
194 #ifdef CONFIG_MODULES
196 /* try to load protocol module if kernel is modular */
198 err = request_module("can-proto-%d", protocol);
201 * In case of error we only print a message but don't
202 * return the error code immediately. Below we will
203 * return -EPROTONOSUPPORT
205 if (err && printk_ratelimit())
206 printk(KERN_ERR "can: request_module "
207 "(can-proto-%d) failed.\n", protocol);
209 cp = can_get_proto(protocol);
213 /* check for available protocol and correct usage */
216 return -EPROTONOSUPPORT;
218 if (cp->type != sock->type) {
223 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
224 if (cp->capability >= 0 && !capable(cp->capability)) {
231 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
232 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
233 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
234 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
236 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
245 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
246 if (!sk->sk_protinfo) {
252 sk_set_owner(sk, proto_tab[protocol]->owner);
255 sock_init_data(sock, sk);
256 sk->sk_destruct = can_sock_destruct;
258 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
259 if (sk->sk_prot->init)
260 err = sk->sk_prot->init(sk);
267 /* release sk on errors */
282 * can_send - transmit a CAN frame (optional with local loopback)
283 * @skb: pointer to socket buffer with CAN frame in data section
284 * @loop: loopback for listeners on local CAN sockets (recommended default!)
286 * Due to the loopback this routine must not be called from hardirq context.
290 * -ENETDOWN when the selected interface is down
291 * -ENOBUFS on full driver queue (see net_xmit_errno())
292 * -ENOMEM when local loopback failed at calling skb_clone()
293 * -EPERM when trying to send on a non-CAN interface
294 * -EINVAL when the skb->data does not contain a valid CAN frame
296 int can_send(struct sk_buff *skb, int loop)
298 struct sk_buff *newskb = NULL;
299 struct can_frame *cf = (struct can_frame *)skb->data;
302 if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
307 if (skb->dev->type != ARPHRD_CAN) {
312 if (!(skb->dev->flags & IFF_UP)) {
317 skb->protocol = htons(ETH_P_CAN);
318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
319 skb_reset_network_header(skb);
320 skb_reset_transport_header(skb);
322 skb->nh.raw = skb->data;
323 skb->h.raw = skb->data;
327 /* local loopback of sent CAN frames */
329 /* indication for the CAN driver: do loopback */
330 skb->pkt_type = PACKET_LOOPBACK;
333 * The reference to the originating sock may be required
334 * by the receiving socket to check whether the frame is
335 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
336 * Therefore we have to ensure that skb->sk remains the
337 * reference to the originating sock by restoring skb->sk
338 * after each skb_clone() or skb_orphan() usage.
341 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
342 #define IFF_ECHO IFF_LOOPBACK
344 if (!(skb->dev->flags & IFF_ECHO)) {
346 * If the interface is not capable to do loopback
347 * itself, we do it here.
349 newskb = skb_clone(skb, GFP_ATOMIC);
355 newskb->sk = skb->sk;
356 newskb->ip_summed = CHECKSUM_UNNECESSARY;
357 newskb->pkt_type = PACKET_BROADCAST;
360 /* indication for the CAN driver: no loopback required */
361 skb->pkt_type = PACKET_HOST;
364 /* send to netdevice */
365 err = dev_queue_xmit(skb);
367 err = net_xmit_errno(err);
370 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
371 /* kfree_skb() does not check for !NULL on older kernels */
383 /* update statistics */
384 can_stats.tx_frames++;
385 can_stats.tx_frames_delta++;
389 EXPORT_SYMBOL(can_send);
395 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
396 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
399 * find receive list for this device
401 * Since 2.6.26 a new "midlevel private" ml_priv pointer has been
402 * introduced in struct net_device. We use this pointer to omit the
403 * linear walk through the can_rx_dev_list. A similar speedup has been
404 * queued for 2.6.34 mainline but using the new netdev_rcu lists.
405 * Therefore the can_rx_dev_list is still needed (e.g. in proc.c)
408 /* dev == NULL is the indicator for the 'all' filterlist */
410 return &can_rx_alldev_list;
412 return (struct dev_rcv_lists *)dev->ml_priv;
415 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
417 struct dev_rcv_lists *d = NULL;
418 struct hlist_node *n;
421 * find receive list for this device
423 * The hlist_for_each_entry*() macros curse through the list
424 * using the pointer variable n and set d to the containing
425 * struct in each list iteration. Therefore, after list
426 * iteration, d is unmodified when the list is empty, and it
427 * points to last list element, when the list is non-empty
428 * but no match in the loop body is found. I.e. d is *not*
429 * NULL when no match is found. We can, however, use the
430 * cursor variable n to decide if a match was found.
433 hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
443 * find_rcv_list - determine optimal filterlist inside device filter struct
444 * @can_id: pointer to CAN identifier of a given can_filter
445 * @mask: pointer to CAN mask of a given can_filter
446 * @d: pointer to the device filter struct
449 * Returns the optimal filterlist to reduce the filter handling in the
450 * receive path. This function is called by service functions that need
451 * to register or unregister a can_filter in the filter lists.
453 * A filter matches in general, when
455 * <received_can_id> & mask == can_id & mask
457 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
458 * relevant bits for the filter.
460 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
461 * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
462 * there is a special filterlist and a special rx path filter handling.
465 * Pointer to optimal filterlist for the given can_id/mask pair.
466 * Constistency checked mask.
467 * Reduced can_id to have a preprocessed filter compare value.
469 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
470 struct dev_rcv_lists *d)
472 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
474 /* filter for error frames in extra filterlist */
475 if (*mask & CAN_ERR_FLAG) {
476 /* clear CAN_ERR_FLAG in filter entry */
477 *mask &= CAN_ERR_MASK;
478 return &d->rx[RX_ERR];
481 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
483 #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
485 /* ensure valid values in can_mask for 'SFF only' frame filtering */
486 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
487 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
489 /* reduce condition testing at receive time */
492 /* inverse can_id/can_mask filter */
494 return &d->rx[RX_INV];
496 /* mask == 0 => no condition testing at receive time */
498 return &d->rx[RX_ALL];
500 /* extra filterlists for the subscription of a single non-RTR can_id */
501 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
502 && !(*can_id & CAN_RTR_FLAG)) {
504 if (*can_id & CAN_EFF_FLAG) {
505 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
506 /* RFC: a future use-case for hash-tables? */
507 return &d->rx[RX_EFF];
510 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
511 return &d->rx_sff[*can_id];
515 /* default: filter via can_id/can_mask */
516 return &d->rx[RX_FIL];
520 * can_rx_register - subscribe CAN frames from a specific interface
521 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
522 * @can_id: CAN identifier (see description)
523 * @mask: CAN mask (see description)
524 * @func: callback function on filter match
525 * @data: returned parameter for callback function
526 * @ident: string for calling module indentification
529 * Invokes the callback function with the received sk_buff and the given
530 * parameter 'data' on a matching receive filter. A filter matches, when
532 * <received_can_id> & mask == can_id & mask
534 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
535 * filter for error frames (CAN_ERR_FLAG bit set in mask).
537 * The provided pointer to the sk_buff is guaranteed to be valid as long as
538 * the callback function is running. The callback function must *not* free
539 * the given sk_buff while processing it's task. When the given sk_buff is
540 * needed after the end of the callback function it must be cloned inside
541 * the callback function with skb_clone().
545 * -ENOMEM on missing cache mem to create subscription entry
546 * -ENODEV unknown device
548 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
549 void (*func)(struct sk_buff *, void *), void *data,
553 struct hlist_head *rl;
554 struct dev_rcv_lists *d;
557 /* insert new receiver (dev,canid,mask) -> (func,data) */
559 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
560 if (dev && dev->type != ARPHRD_CAN)
564 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
568 spin_lock(&can_rcvlists_lock);
570 d = find_dev_rcv_lists(dev);
572 rl = find_rcv_list(&can_id, &mask, d);
581 hlist_add_head_rcu(&r->list, rl);
584 can_pstats.rcv_entries++;
585 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
586 can_pstats.rcv_entries_max = can_pstats.rcv_entries;
588 kmem_cache_free(rcv_cache, r);
592 spin_unlock(&can_rcvlists_lock);
596 EXPORT_SYMBOL(can_rx_register);
599 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
601 static void can_rx_delete_device(struct rcu_head *rp)
603 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
609 * can_rx_delete_receiver - rcu callback for single receiver entry removal
611 static void can_rx_delete_receiver(struct rcu_head *rp)
613 struct receiver *r = container_of(rp, struct receiver, rcu);
615 kmem_cache_free(rcv_cache, r);
619 * can_rx_unregister - unsubscribe CAN frames from a specific interface
620 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
621 * @can_id: CAN identifier
623 * @func: callback function on filter match
624 * @data: returned parameter for callback function
627 * Removes subscription entry depending on given (subscription) values.
629 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
630 void (*func)(struct sk_buff *, void *), void *data)
632 struct receiver *r = NULL;
633 struct hlist_head *rl;
634 struct hlist_node *next;
635 struct dev_rcv_lists *d;
637 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
638 if (dev && dev->type != ARPHRD_CAN)
642 spin_lock(&can_rcvlists_lock);
644 d = find_dev_rcv_lists(dev);
646 printk(KERN_ERR "BUG: receive list not found for "
647 "dev %s, id %03X, mask %03X\n",
648 DNAME(dev), can_id, mask);
652 rl = find_rcv_list(&can_id, &mask, d);
655 * Search the receiver list for the item to delete. This should
656 * exist, since no receiver may be unregistered that hasn't
657 * been registered before.
660 hlist_for_each_entry_rcu(r, next, rl, list) {
661 if (r->can_id == can_id && r->mask == mask
662 && r->func == func && r->data == data)
667 * Check for bugs in CAN protocol implementations:
668 * If no matching list item was found, the list cursor variable next
669 * will be NULL, while r will point to the last item of the list.
673 printk(KERN_ERR "BUG: receive list entry not found for "
674 "dev %s, id %03X, mask %03X\n",
675 DNAME(dev), can_id, mask);
681 hlist_del_rcu(&r->list);
684 if (can_pstats.rcv_entries > 0)
685 can_pstats.rcv_entries--;
687 /* remove device structure requested by NETDEV_UNREGISTER */
688 if (d->remove_on_zero_entries && !d->entries) {
689 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
692 hlist_del_rcu(&d->list);
697 spin_unlock(&can_rcvlists_lock);
699 /* schedule the receiver item for deletion */
701 call_rcu(&r->rcu, can_rx_delete_receiver);
703 /* schedule the device structure for deletion */
705 call_rcu(&d->rcu, can_rx_delete_device);
707 EXPORT_SYMBOL(can_rx_unregister);
709 static inline void deliver(struct sk_buff *skb, struct receiver *r)
711 r->func(skb, r->data);
715 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
718 struct hlist_node *n;
720 struct can_frame *cf = (struct can_frame *)skb->data;
721 canid_t can_id = cf->can_id;
726 if (can_id & CAN_ERR_FLAG) {
727 /* check for error frame entries only */
728 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
729 if (can_id & r->mask) {
737 /* check for unfiltered entries */
738 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
743 /* check for can_id/mask entries */
744 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
745 if ((can_id & r->mask) == r->can_id) {
751 /* check for inverted can_id/mask entries */
752 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
753 if ((can_id & r->mask) != r->can_id) {
759 /* check filterlists for single non-RTR can_ids */
760 if (can_id & CAN_RTR_FLAG)
763 if (can_id & CAN_EFF_FLAG) {
764 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
765 if (r->can_id == can_id) {
771 can_id &= CAN_SFF_MASK;
772 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
781 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
782 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
783 struct packet_type *pt, struct net_device *orig_dev)
785 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
786 struct packet_type *pt)
789 struct dev_rcv_lists *d;
790 struct can_frame *cf = (struct can_frame *)skb->data;
793 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
794 if (!net_eq(dev_net(dev), &init_net))
796 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
797 if (dev->nd_net != &init_net)
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
802 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
803 skb->len != sizeof(struct can_frame) ||
805 "PF_CAN: dropped non conform skbuf: "
806 "dev type %d, len %d, can_dlc %d\n",
807 dev->type, skb->len, cf->can_dlc))
810 BUG_ON(dev->type != ARPHRD_CAN ||
811 skb->len != sizeof(struct can_frame) ||
815 /* update statistics */
816 can_stats.rx_frames++;
817 can_stats.rx_frames_delta++;
821 /* deliver the packet to sockets listening on all devices */
822 matches = can_rcv_filter(&can_rx_alldev_list, skb);
824 /* find receive list for this device */
825 d = find_dev_rcv_lists(dev);
827 matches += can_rcv_filter(d, skb);
831 /* consume the skbuff allocated by the netdevice driver */
832 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
840 can_stats.matches_delta++;
843 return NET_RX_SUCCESS;
845 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
853 * af_can protocol functions
857 * can_proto_register - register CAN transport protocol
858 * @cp: pointer to CAN protocol structure
862 * -EINVAL invalid (out of range) protocol number
863 * -EBUSY protocol already in use
864 * -ENOBUF if proto_register() fails
866 int can_proto_register(const struct can_proto *cp)
868 int proto = cp->protocol;
871 if (proto < 0 || proto >= CAN_NPROTO) {
872 printk(KERN_ERR "can: protocol number %d out of range\n",
877 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
878 err = proto_register(cp->prot, 0);
883 mutex_lock(&proto_tab_lock);
885 if (proto_tab[proto]) {
886 printk(KERN_ERR "can: protocol %d already registered\n",
890 rcu_assign_pointer(proto_tab[proto], cp);
892 mutex_unlock(&proto_tab_lock);
894 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
896 proto_unregister(cp->prot);
901 EXPORT_SYMBOL(can_proto_register);
904 * can_proto_unregister - unregister CAN transport protocol
905 * @cp: pointer to CAN protocol structure
907 void can_proto_unregister(const struct can_proto *cp)
909 int proto = cp->protocol;
911 mutex_lock(&proto_tab_lock);
912 BUG_ON(proto_tab[proto] != cp);
913 rcu_assign_pointer(proto_tab[proto], NULL);
914 mutex_unlock(&proto_tab_lock);
918 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
919 proto_unregister(cp->prot);
922 EXPORT_SYMBOL(can_proto_unregister);
925 * af_can notifier to create/remove CAN netdevice specific structs
927 static int can_notifier(struct notifier_block *nb, unsigned long msg,
930 struct net_device *dev = (struct net_device *)data;
931 struct dev_rcv_lists *d;
933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
934 if (!net_eq(dev_net(dev), &init_net))
936 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
937 if (dev->nd_net != &init_net)
941 if (dev->type != ARPHRD_CAN)
946 case NETDEV_REGISTER:
949 * create new dev_rcv_lists for this device
951 * N.B. zeroing the struct is the correct initialization
952 * for the embedded hlist_head structs.
953 * Another list type, e.g. list_head, would require
954 * explicit initialization.
957 d = kzalloc(sizeof(*d), GFP_KERNEL);
960 "can: allocation of receive list failed\n");
965 spin_lock(&can_rcvlists_lock);
966 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
967 BUG_ON(dev->ml_priv);
970 hlist_add_head_rcu(&d->list, &can_rx_dev_list);
971 spin_unlock(&can_rcvlists_lock);
975 case NETDEV_UNREGISTER:
976 spin_lock(&can_rcvlists_lock);
978 d = find_dev_rcv_lists(dev);
981 d->remove_on_zero_entries = 1;
984 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
987 hlist_del_rcu(&d->list);
990 printk(KERN_ERR "can: notifier: receive list not "
991 "found for dev %s\n", dev->name);
993 spin_unlock(&can_rcvlists_lock);
996 call_rcu(&d->rcu, can_rx_delete_device);
1005 * af_can module init/exit functions
1008 static struct packet_type can_packet __read_mostly = {
1009 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
1010 .type = cpu_to_be16(ETH_P_CAN),
1012 .type = __constant_htons(ETH_P_CAN),
1018 static struct net_proto_family can_family_ops __read_mostly = {
1020 .create = can_create,
1021 .owner = THIS_MODULE,
1024 /* notifier block for netdevice event */
1025 static struct notifier_block can_netdev_notifier __read_mostly = {
1026 .notifier_call = can_notifier,
1029 static __init int can_init(void)
1033 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1034 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1037 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1044 * Insert can_rx_alldev_list for reception on all devices.
1045 * This struct is zero initialized which is correct for the
1046 * embedded hlist heads, the dev pointer, and the entries counter.
1049 spin_lock(&can_rcvlists_lock);
1050 hlist_add_head_rcu(&can_rx_alldev_list.list, &can_rx_dev_list);
1051 spin_unlock(&can_rcvlists_lock);
1054 /* the statistics are updated every second (timer triggered) */
1055 setup_timer(&can_stattimer, can_stat_update, 0);
1056 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
1058 can_stattimer.function = NULL;
1062 /* protocol register */
1063 sock_register(&can_family_ops);
1064 register_netdevice_notifier(&can_netdev_notifier);
1065 dev_add_pack(&can_packet);
1070 static __exit void can_exit(void)
1072 struct dev_rcv_lists *d;
1073 struct hlist_node *n, *next;
1076 del_timer(&can_stattimer);
1080 /* protocol unregister */
1081 dev_remove_pack(&can_packet);
1082 unregister_netdevice_notifier(&can_netdev_notifier);
1083 sock_unregister(PF_CAN);
1085 /* remove can_rx_dev_list */
1086 spin_lock(&can_rcvlists_lock);
1087 hlist_del(&can_rx_alldev_list.list);
1088 hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
1089 hlist_del(&d->list);
1091 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1092 d->dev->ml_priv = NULL;
1096 spin_unlock(&can_rcvlists_lock);
1098 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1099 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1102 kmem_cache_destroy(rcv_cache);
1105 module_init(can_init);
1106 module_exit(can_exit);