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, the following disclaimer and
13 * the referenced file 'COPYING'.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Volkswagen nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * Alternatively, provided that this notice is retained in full, this
22 * software may be distributed under the terms of the GNU General
23 * Public License ("GPL") version 2 as distributed in the 'COPYING'
24 * file from the main directory of the linux kernel source.
26 * The provided data structures and external interfaces from this code
27 * are not restricted to be used by modules with a GPL compatible license.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42 * Send feedback to <socketcan-users@lists.berlios.de>
46 #include <linux/module.h>
47 #include <linux/version.h>
48 #include <linux/slab.h>
49 #include <linux/kmod.h>
50 #include <linux/init.h>
51 #include <linux/list.h>
52 #include <linux/spinlock.h>
53 #include <linux/rcupdate.h>
54 #include <linux/socket.h>
55 #include <linux/if_ether.h>
56 #include <linux/if_arp.h>
57 #include <linux/skbuff.h>
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
60 #include <linux/can.h>
61 #include <linux/can/core.h>
63 #include <asm/uaccess.h>
67 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
71 static __initdata const char banner[] = KERN_INFO
72 "can: controller area network core (" CAN_VERSION_STRING ")\n";
74 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
75 MODULE_LICENSE("Dual BSD/GPL");
76 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
77 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
79 MODULE_ALIAS_NETPROTO(PF_CAN);
81 int stats_timer = 1; /* default: on */
82 module_param(stats_timer, int, S_IRUGO);
83 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
85 #ifdef CONFIG_CAN_DEBUG_CORE
87 module_param(debug, int, S_IRUGO);
88 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
91 HLIST_HEAD(rx_dev_list);
92 static struct dev_rcv_lists rx_alldev_list;
93 static DEFINE_SPINLOCK(rcv_lists_lock);
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
96 static struct kmem_cache *rcv_cache __read_mostly;
98 static kmem_cache_t *rcv_cache;
101 /* table of registered CAN protocols */
102 static struct can_proto *proto_tab[CAN_NPROTO];
104 struct timer_list stattimer; /* timer for statistics update */
105 struct s_stats stats; /* packet statistics */
106 struct s_pstats pstats; /* receive list statistics */
108 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
109 static void *kzalloc(size_t size, unsigned int __nocast flags)
111 void *ret = kmalloc(size, flags);
113 memset(ret, 0, size);
119 * af_can socket functions
122 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
124 struct sock *sk = sock->sk;
129 return sock_get_timestamp(sk, (struct timeval __user *)arg);
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
135 return dev_ioctl(cmd, (void __user *)arg);
140 static void can_sock_destruct(struct sock *sk)
142 DBG("called for sock %p\n", sk);
144 skb_queue_purge(&sk->sk_receive_queue);
146 kfree(sk->sk_protinfo);
149 static int can_create(struct socket *sock, int protocol)
152 struct can_proto *cp;
153 char module_name[sizeof("can-proto-000")];
156 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
158 sock->state = SS_UNCONNECTED;
160 if (protocol < 0 || protocol >= CAN_NPROTO)
163 DBG("looking up proto %d in proto_tab[]\n", protocol);
165 /* try to load protocol module, when CONFIG_KMOD is defined */
166 if (!proto_tab[protocol]) {
167 sprintf(module_name, "can-proto-%d", protocol);
168 ret = request_module(module_name);
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
176 printk(KERN_INFO "can: request_module(%s) not"
177 " implemented.\n", module_name);
179 printk(KERN_ERR "can: request_module(%s) failed\n",
183 /* check for success and correct type */
184 cp = proto_tab[protocol];
185 if (!cp || cp->type != sock->type)
186 return -EPROTONOSUPPORT;
188 if (cp->capability >= 0 && !capable(cp->capability))
193 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
194 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
198 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
203 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
204 if (!sk->sk_protinfo) {
209 sk_set_owner(sk, proto_tab[protocol]->owner);
212 sock_init_data(sock, sk);
213 sk->sk_destruct = can_sock_destruct;
215 DBG("created sock: %p\n", sk);
217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
218 if (sk->sk_prot->init)
219 ret = sk->sk_prot->init(sk);
226 /* release sk on errors */
239 * can_send - transmit a CAN frame (optional with local loopback)
240 * @skb: pointer to socket buffer with CAN frame in data section
241 * @loop: loopback for listeners on local CAN sockets (recommended default!)
245 * -ENETDOWN when the selected interface is down
246 * -ENOBUFS on full driver queue (see net_xmit_errno())
247 * -ENOMEM when local loopback failed at calling skb_clone()
249 int can_send(struct sk_buff *skb, int loop)
253 if (skb->dev->type != ARPHRD_CAN) {
258 if (!(skb->dev->flags & IFF_UP)) {
263 skb->protocol = htons(ETH_P_CAN);
266 /* local loopback of sent CAN frames */
268 /* indication for the CAN driver: do loopback */
269 skb->pkt_type = PACKET_LOOPBACK;
272 * The reference to the originating sock may be required
273 * by the receiving socket to check whether the frame is
274 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
275 * Therefore we have to ensure that skb->sk remains the
276 * reference to the originating sock by restoring skb->sk
277 * after each skb_clone() or skb_orphan() usage.
280 if (!(skb->dev->flags & IFF_LOOPBACK)) {
282 * If the interface is not capable to do loopback
283 * itself, we do it here.
285 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
292 newskb->sk = skb->sk;
293 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
296 newskb->input_dev = NULL;
298 newskb->ip_summed = CHECKSUM_UNNECESSARY;
299 newskb->pkt_type = PACKET_BROADCAST;
303 /* indication for the CAN driver: no loopback required */
304 skb->pkt_type = PACKET_HOST;
307 /* send to netdevice */
308 err = dev_queue_xmit(skb);
310 err = net_xmit_errno(err);
312 /* update statistics */
314 stats.tx_frames_delta++;
318 EXPORT_SYMBOL(can_send);
324 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
326 struct dev_rcv_lists *d;
327 struct hlist_node *n;
330 * find receive list for this device
332 * The hlist_for_each_entry*() macros curse through the list
333 * using the pointer variable n and set d to the containing
334 * struct in each list iteration. Therefore, after list
335 * iteration, d is unmodified when the list is empty, and it
336 * points to last list element, when the list is non-empty
337 * but no match in the loop body is found. I.e. d is *not*
338 * NULL when no match is found. We can, however, use the
339 * cursor variable n to decide if a match was found.
342 hlist_for_each_entry(d, n, &rx_dev_list, list) {
350 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
351 struct dev_rcv_lists *d)
353 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
355 /* filter error frames */
356 if (*mask & CAN_ERR_FLAG) {
357 /* clear CAN_ERR_FLAG in list entry */
358 *mask &= CAN_ERR_MASK;
359 return &d->rx[RX_ERR];
362 /* ensure valid values in can_mask */
363 if (*mask & CAN_EFF_FLAG)
364 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
366 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
368 /* reduce condition testing at receive time */
371 /* inverse can_id/can_mask filter */
373 return &d->rx[RX_INV];
375 /* mask == 0 => no condition testing at receive time */
377 return &d->rx[RX_ALL];
379 /* use extra filterset for the subscription of exactly *ONE* can_id */
380 if (*can_id & CAN_EFF_FLAG) {
381 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
382 /* RFC: a use-case for hash-tables in the future? */
383 return &d->rx[RX_EFF];
386 if (*mask == CAN_SFF_MASK)
387 return &d->rx_sff[*can_id];
390 /* default: filter via can_id/can_mask */
391 return &d->rx[RX_FIL];
395 * can_rx_register - subscribe CAN frames from a specific interface
396 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
397 * @can_id: CAN identifier (see description)
398 * @mask: CAN mask (see description)
399 * @func: callback function on filter match
400 * @data: returned parameter for callback function
401 * @ident: string for calling module indentification
404 * Invokes the callback function with the received sk_buff and the given
405 * parameter 'data' on a matching receive filter. A filter matches, when
407 * <received_can_id> & mask == can_id & mask
409 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
410 * filter for error frames (CAN_ERR_FLAG bit set in mask).
414 * -ENOMEM on missing cache mem to create subscription entry
415 * -ENODEV unknown device
417 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
418 void (*func)(struct sk_buff *, void *), void *data,
422 struct hlist_head *rl;
423 struct dev_rcv_lists *d;
426 /* insert new receiver (dev,canid,mask) -> (func,data) */
428 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
429 "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
431 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
435 spin_lock_bh(&rcv_lists_lock);
437 d = find_dev_rcv_lists(dev);
439 rl = find_rcv_list(&can_id, &mask, d);
448 hlist_add_head_rcu(&r->list, rl);
451 pstats.rcv_entries++;
452 if (pstats.rcv_entries_max < pstats.rcv_entries)
453 pstats.rcv_entries_max = pstats.rcv_entries;
455 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
456 DNAME(dev), can_id, mask);
457 kmem_cache_free(rcv_cache, r);
461 spin_unlock_bh(&rcv_lists_lock);
465 EXPORT_SYMBOL(can_rx_register);
468 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
470 static void can_rx_delete_device(struct rcu_head *rp)
472 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
474 DBG("removing dev_rcv_list at %p\n", d);
479 * can_rx_delete_receiver - rcu callback for single receiver entry removal
481 static void can_rx_delete_receiver(struct rcu_head *rp)
483 struct receiver *r = container_of(rp, struct receiver, rcu);
485 DBG("removing receiver at %p\n", r);
486 kmem_cache_free(rcv_cache, r);
490 * can_rx_unregister - unsubscribe CAN frames from a specific interface
491 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
492 * @can_id: CAN identifier
494 * @func: callback function on filter match
495 * @data: returned parameter for callback function
498 * Removes subscription entry depending on given (subscription) values.
502 * -EINVAL on missing subscription entry
503 * -ENODEV unknown device
505 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
506 void (*func)(struct sk_buff *, void *), void *data)
508 struct receiver *r = NULL;
509 struct hlist_head *rl;
510 struct hlist_node *next;
511 struct dev_rcv_lists *d;
514 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
515 dev, DNAME(dev), can_id, mask, func, data);
517 spin_lock_bh(&rcv_lists_lock);
519 d = find_dev_rcv_lists(dev);
521 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
522 DNAME(dev), can_id, mask);
527 rl = find_rcv_list(&can_id, &mask, d);
530 * Search the receiver list for the item to delete. This should
531 * exist, since no receiver may be unregistered that hasn't
532 * been registered before.
535 hlist_for_each_entry(r, next, rl, list) {
536 if (r->can_id == can_id && r->mask == mask
537 && r->func == func && r->data == data)
542 * Check for bug in CAN protocol implementations:
543 * If no matching list item was found, the list cursor variable next
544 * will be NULL, while r will point to the last item of the list.
548 DBG("receive list entry not found for "
549 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
556 hlist_del_rcu(&r->list);
559 if (pstats.rcv_entries > 0)
560 pstats.rcv_entries--;
562 /* remove device structure requested by NETDEV_UNREGISTER */
563 if (d->remove_on_zero_entries && !d->entries) {
564 DBG("removing dev_rcv_list for %s on zero entries\n",
566 hlist_del_rcu(&d->list);
571 spin_unlock_bh(&rcv_lists_lock);
573 /* schedule the receiver item for deletion */
575 call_rcu(&r->rcu, can_rx_delete_receiver);
577 /* schedule the device structure for deletion */
579 call_rcu(&d->rcu, can_rx_delete_device);
583 EXPORT_SYMBOL(can_rx_unregister);
585 static inline void deliver(struct sk_buff *skb, struct receiver *r)
587 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
589 DBG("skbuff %p cloned to %p\n", skb, clone);
592 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
593 clone->iif = skb->iif;
595 clone->input_dev = skb->input_dev;
597 r->func(clone, r->data);
602 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
605 struct hlist_node *n;
607 struct can_frame *cf = (struct can_frame*)skb->data;
608 canid_t can_id = cf->can_id;
613 if (can_id & CAN_ERR_FLAG) {
614 /* check for error frame entries only */
615 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
616 if (can_id & r->mask) {
617 DBG("match on rx_err skbuff %p\n", skb);
625 /* check for unfiltered entries */
626 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
627 DBG("match on rx_all skbuff %p\n", skb);
632 /* check for can_id/mask entries */
633 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
634 if ((can_id & r->mask) == r->can_id) {
635 DBG("match on rx_fil skbuff %p\n", skb);
641 /* check for inverted can_id/mask entries */
642 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
643 if ((can_id & r->mask) != r->can_id) {
644 DBG("match on rx_inv skbuff %p\n", skb);
650 /* check CAN_ID specific entries */
651 if (can_id & CAN_EFF_FLAG) {
652 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
653 if (r->can_id == can_id) {
654 DBG("match on rx_eff skbuff %p\n", skb);
660 can_id &= CAN_SFF_MASK;
661 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
662 DBG("match on rx_sff skbuff %p\n", skb);
671 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
672 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
673 struct packet_type *pt, struct net_device *orig_dev)
675 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
676 struct packet_type *pt)
679 struct dev_rcv_lists *d;
682 DBG("received skbuff on device %s, ptype %04x\n",
683 dev->name, ntohs(pt->type));
685 DBG_FRAME("af_can: can_rcv: received CAN frame",
686 (struct can_frame *)skb->data);
688 if (dev->type != ARPHRD_CAN) {
693 /* update statistics */
695 stats.rx_frames_delta++;
699 /* deliver the packet to sockets listening on all devices */
700 matches = can_rcv_filter(&rx_alldev_list, skb);
702 /* find receive list for this device */
703 d = find_dev_rcv_lists(dev);
705 matches += can_rcv_filter(d, skb);
709 /* free the skbuff allocated by the netdevice driver */
710 DBG("freeing skbuff %p\n", skb);
715 stats.matches_delta++;
722 * af_can protocol functions
726 * can_proto_register - register CAN transport protocol
727 * @cp: pointer to CAN protocol structure
731 * -EINVAL invalid (out of range) protocol number
732 * -EBUSY protocol already in use
733 * -ENOBUF if proto_register() fails
735 int can_proto_register(struct can_proto *cp)
737 int proto = cp->protocol;
740 if (proto < 0 || proto >= CAN_NPROTO) {
741 printk(KERN_ERR "can: protocol number %d out of range\n",
745 if (proto_tab[proto]) {
746 printk(KERN_ERR "can: protocol %d already registered\n",
751 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
752 err = proto_register(cp->prot, 0);
757 proto_tab[proto] = cp;
759 /* use generic ioctl function if the module doesn't bring its own */
761 cp->ops->ioctl = can_ioctl;
765 EXPORT_SYMBOL(can_proto_register);
768 * can_proto_unregister - unregister CAN transport protocol
769 * @cp: pointer to CAN protocol structure
773 * -ESRCH protocol number was not registered
775 int can_proto_unregister(struct can_proto *cp)
777 int proto = cp->protocol;
779 if (!proto_tab[proto]) {
780 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
783 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
784 proto_unregister(cp->prot);
786 proto_tab[proto] = NULL;
790 EXPORT_SYMBOL(can_proto_unregister);
793 * af_can notifier to create/remove CAN netdevice specific structs
795 static int can_notifier(struct notifier_block *nb, unsigned long msg,
798 struct net_device *dev = (struct net_device *)data;
799 struct dev_rcv_lists *d;
801 DBG("msg %ld for dev %p (%s idx %d)\n",
802 msg, dev, dev->name, dev->ifindex);
804 if (dev->type != ARPHRD_CAN)
809 case NETDEV_REGISTER:
812 * create new dev_rcv_lists for this device
814 * N.B. zeroing the struct is the correct initialization
815 * for the embedded hlist_head structs.
816 * Another list type, e.g. list_head, would require
817 * explicit initialization.
820 DBG("creating new dev_rcv_lists for %s\n", dev->name);
822 d = kzalloc(sizeof(*d),
823 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
826 "can: allocation of receive list failed\n");
831 spin_lock_bh(&rcv_lists_lock);
832 hlist_add_head_rcu(&d->list, &rx_dev_list);
833 spin_unlock_bh(&rcv_lists_lock);
837 case NETDEV_UNREGISTER:
838 spin_lock_bh(&rcv_lists_lock);
840 d = find_dev_rcv_lists(dev);
842 DBG("remove dev_rcv_list for %s (%d entries)\n",
843 dev->name, d->entries);
846 d->remove_on_zero_entries = 1;
849 hlist_del_rcu(&d->list);
851 printk(KERN_ERR "can: notifier: receive list not "
852 "found for dev %s\n", dev->name);
854 spin_unlock_bh(&rcv_lists_lock);
857 call_rcu(&d->rcu, can_rx_delete_device);
866 * af_can debugging stuff
869 #ifdef CONFIG_CAN_DEBUG_CORE
871 #define DBG_BSIZE 1024
874 * can_debug_cframe - print CAN frame
875 * @msg: pointer to message printed before the given CAN frame
876 * @cf: pointer to CAN frame
878 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
885 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
889 len = sprintf(buf, KERN_DEBUG);
891 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
900 if (cf->can_id & CAN_EFF_FLAG)
901 len += sprintf(buf + len, "<%08X> [%X] ",
902 cf->can_id & CAN_EFF_MASK, dlc);
904 len += sprintf(buf + len, "<%03X> [%X] ",
905 cf->can_id & CAN_SFF_MASK, dlc);
907 for (i = 0; i < dlc; i++)
908 len += sprintf(buf + len, "%02X ", cf->data[i]);
910 if (cf->can_id & CAN_RTR_FLAG)
911 len += sprintf(buf + len, "(RTR)");
918 EXPORT_SYMBOL(can_debug_cframe);
921 * can_debug_skb - print socket buffer content to kernel log
922 * @skb: pointer to socket buffer
924 void can_debug_skb(struct sk_buff *skb)
929 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
934 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
935 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
936 "h,d,t,e,l: %p %+d %+d %+d, %d",
937 skb, skb->dev ? skb->dev->ifindex : -1,
938 ntohs(skb->protocol),
939 atomic_read(&skb->users),
940 atomic_read(&(skb_shinfo(skb)->dataref)),
941 skb_shinfo(skb)->nr_frags,
942 skb->head, skb->data - skb->head,
943 skb->tail - skb->head, skb->end - skb->head, skb->len);
944 nbytes = skb->end - skb->head;
945 for (i = 0; i < nbytes; i++) {
947 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
948 if (len < DBG_BSIZE - 16) {
949 len += sprintf(buf + len, " %02x", skb->head[i]);
951 len += sprintf(buf + len, "...");
960 EXPORT_SYMBOL(can_debug_skb);
965 * af_can module init/exit functions
968 static struct packet_type can_packet = {
969 .type = __constant_htons(ETH_P_CAN),
974 static struct net_proto_family can_family_ops = {
976 .create = can_create,
977 .owner = THIS_MODULE,
980 /* notifier block for netdevice event */
981 static struct notifier_block can_netdev_notifier = {
982 .notifier_call = can_notifier,
985 static __init int can_init(void)
989 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
995 * Insert rx_alldev_list for reception on all devices.
996 * This struct is zero initialized which is correct for the
997 * embedded hlist heads, the dev pointer, and the entries counter.
1000 spin_lock_bh(&rcv_lists_lock);
1001 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1002 spin_unlock_bh(&rcv_lists_lock);
1005 /* the statistics are updated every second (timer triggered) */
1006 init_timer(&stattimer);
1007 stattimer.function = can_stat_update;
1009 /* update every second */
1010 stattimer.expires = jiffies + HZ;
1011 /* start statistics timer */
1012 add_timer(&stattimer);
1014 stattimer.function = NULL;
1019 /* protocol register */
1020 sock_register(&can_family_ops);
1021 register_netdevice_notifier(&can_netdev_notifier);
1022 dev_add_pack(&can_packet);
1027 static __exit void can_exit(void)
1029 struct dev_rcv_lists *d;
1030 struct hlist_node *n, *next;
1033 del_timer(&stattimer);
1038 /* protocol unregister */
1039 dev_remove_pack(&can_packet);
1040 unregister_netdevice_notifier(&can_netdev_notifier);
1041 sock_unregister(PF_CAN);
1043 /* remove rx_dev_list */
1044 spin_lock_bh(&rcv_lists_lock);
1045 hlist_del(&rx_alldev_list.list);
1046 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1047 hlist_del(&d->list);
1050 spin_unlock_bh(&rcv_lists_lock);
1052 kmem_cache_destroy(rcv_cache);
1055 module_init(can_init);
1056 module_exit(can_exit);