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/init.h>
49 #include <linux/kmod.h>
50 #include <linux/slab.h>
51 #include <linux/list.h>
52 #include <linux/spinlock.h>
53 #include <linux/rcupdate.h>
54 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
55 #include <linux/uaccess.h>
57 #include <asm/uaccess.h>
59 #include <linux/net.h>
60 #include <linux/netdevice.h>
61 #include <linux/socket.h>
62 #include <linux/if_ether.h>
63 #include <linux/if_arp.h>
64 #include <linux/skbuff.h>
65 #include <linux/can.h>
66 #include <linux/can/core.h>
67 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
68 #include <net/net_namespace.h>
74 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
78 static __initdata const char banner[] = KERN_INFO
79 "can: controller area network core (" CAN_VERSION_STRING ")\n";
81 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
82 MODULE_LICENSE("Dual BSD/GPL");
83 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
84 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
86 MODULE_ALIAS_NETPROTO(PF_CAN);
88 static int stats_timer __read_mostly = 1;
89 module_param(stats_timer, int, S_IRUGO);
90 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
92 #ifdef CONFIG_CAN_DEBUG_CORE
93 static int debug __read_mostly;
94 module_param(debug, int, S_IRUGO);
95 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
98 HLIST_HEAD(rx_dev_list);
99 static struct dev_rcv_lists rx_alldev_list;
100 static DEFINE_SPINLOCK(rcv_lists_lock);
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
103 static struct kmem_cache *rcv_cache __read_mostly;
105 static kmem_cache_t *rcv_cache;
108 /* table of registered CAN protocols */
109 static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
111 struct timer_list stattimer; /* timer for statistics update */
112 struct s_stats stats; /* packet statistics */
113 struct s_pstats pstats; /* receive list statistics */
115 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
116 static void *kzalloc(size_t size, unsigned int __nocast flags)
118 void *ret = kmalloc(size, flags);
120 memset(ret, 0, size);
126 * af_can socket functions
129 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
131 struct sock *sk = sock->sk;
136 return sock_get_timestamp(sk, (struct timeval __user *)arg);
139 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
142 return dev_ioctl(cmd, (void __user *)arg);
147 static void can_sock_destruct(struct sock *sk)
149 DBG("called for sock %p\n", sk);
151 skb_queue_purge(&sk->sk_receive_queue);
153 kfree(sk->sk_protinfo);
156 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
157 static int can_create(struct net *net, struct socket *sock, int protocol)
159 static int can_create(struct socket *sock, int protocol)
163 struct can_proto *cp;
164 char module_name[sizeof("can-proto-000")];
167 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
169 sock->state = SS_UNCONNECTED;
171 if (protocol < 0 || protocol >= CAN_NPROTO)
174 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
175 if (net != &init_net)
176 return -EAFNOSUPPORT;
179 DBG("looking up proto %d in proto_tab[]\n", protocol);
181 /* try to load protocol module, when CONFIG_KMOD is defined */
182 if (!proto_tab[protocol]) {
183 sprintf(module_name, "can-proto-%d", protocol);
184 ret = request_module(module_name);
187 * In case of error we only print a message but don't
188 * return the error code immediately. Below we will
189 * return -EPROTONOSUPPORT
192 printk(KERN_INFO "can: request_module(%s) not"
193 " implemented.\n", module_name);
195 printk(KERN_ERR "can: request_module(%s) failed\n",
199 /* check for success and correct type */
200 cp = proto_tab[protocol];
201 if (!cp || cp->type != sock->type)
202 return -EPROTONOSUPPORT;
204 if (cp->capability >= 0 && !capable(cp->capability))
209 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
210 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, 1);
211 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
212 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
214 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
219 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
221 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
222 if (!sk->sk_protinfo) {
227 sk_set_owner(sk, proto_tab[protocol]->owner);
230 sock_init_data(sock, sk);
231 sk->sk_destruct = can_sock_destruct;
233 DBG("created sock: %p\n", sk);
235 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
236 if (sk->sk_prot->init)
237 ret = sk->sk_prot->init(sk);
244 /* release sk on errors */
257 * can_send - transmit a CAN frame (optional with local loopback)
258 * @skb: pointer to socket buffer with CAN frame in data section
259 * @loop: loopback for listeners on local CAN sockets (recommended default!)
263 * -ENETDOWN when the selected interface is down
264 * -ENOBUFS on full driver queue (see net_xmit_errno())
265 * -ENOMEM when local loopback failed at calling skb_clone()
267 int can_send(struct sk_buff *skb, int loop)
271 if (skb->dev->type != ARPHRD_CAN) {
276 if (!(skb->dev->flags & IFF_UP)) {
281 skb->protocol = htons(ETH_P_CAN);
284 /* local loopback of sent CAN frames */
286 /* indication for the CAN driver: do loopback */
287 skb->pkt_type = PACKET_LOOPBACK;
290 * The reference to the originating sock may be required
291 * by the receiving socket to check whether the frame is
292 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
293 * Therefore we have to ensure that skb->sk remains the
294 * reference to the originating sock by restoring skb->sk
295 * after each skb_clone() or skb_orphan() usage.
298 if (!(skb->dev->flags & IFF_LOOPBACK)) {
300 * If the interface is not capable to do loopback
301 * itself, we do it here.
303 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
310 newskb->sk = skb->sk;
311 newskb->ip_summed = CHECKSUM_UNNECESSARY;
312 newskb->pkt_type = PACKET_BROADCAST;
316 /* indication for the CAN driver: no loopback required */
317 skb->pkt_type = PACKET_HOST;
320 /* send to netdevice */
321 err = dev_queue_xmit(skb);
323 err = net_xmit_errno(err);
325 /* update statistics */
327 stats.tx_frames_delta++;
331 EXPORT_SYMBOL(can_send);
337 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
339 struct dev_rcv_lists *d;
340 struct hlist_node *n;
343 * find receive list for this device
345 * The hlist_for_each_entry*() macros curse through the list
346 * using the pointer variable n and set d to the containing
347 * struct in each list iteration. Therefore, after list
348 * iteration, d is unmodified when the list is empty, and it
349 * points to last list element, when the list is non-empty
350 * but no match in the loop body is found. I.e. d is *not*
351 * NULL when no match is found. We can, however, use the
352 * cursor variable n to decide if a match was found.
355 hlist_for_each_entry(d, n, &rx_dev_list, list) {
363 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
364 struct dev_rcv_lists *d)
366 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
368 /* filter error frames */
369 if (*mask & CAN_ERR_FLAG) {
370 /* clear CAN_ERR_FLAG in list entry */
371 *mask &= CAN_ERR_MASK;
372 return &d->rx[RX_ERR];
375 /* ensure valid values in can_mask */
376 if (*mask & CAN_EFF_FLAG)
377 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
379 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
381 /* reduce condition testing at receive time */
384 /* inverse can_id/can_mask filter */
386 return &d->rx[RX_INV];
388 /* mask == 0 => no condition testing at receive time */
390 return &d->rx[RX_ALL];
392 /* use extra filterset for the subscription of exactly *ONE* can_id */
393 if (*can_id & CAN_EFF_FLAG) {
394 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
395 /* RFC: a use-case for hash-tables in the future? */
396 return &d->rx[RX_EFF];
399 if (*mask == CAN_SFF_MASK)
400 return &d->rx_sff[*can_id];
403 /* default: filter via can_id/can_mask */
404 return &d->rx[RX_FIL];
408 * can_rx_register - subscribe CAN frames from a specific interface
409 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
410 * @can_id: CAN identifier (see description)
411 * @mask: CAN mask (see description)
412 * @func: callback function on filter match
413 * @data: returned parameter for callback function
414 * @ident: string for calling module indentification
417 * Invokes the callback function with the received sk_buff and the given
418 * parameter 'data' on a matching receive filter. A filter matches, when
420 * <received_can_id> & mask == can_id & mask
422 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
423 * filter for error frames (CAN_ERR_FLAG bit set in mask).
427 * -ENOMEM on missing cache mem to create subscription entry
428 * -ENODEV unknown device
430 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
431 void (*func)(struct sk_buff *, void *), void *data,
435 struct hlist_head *rl;
436 struct dev_rcv_lists *d;
439 /* insert new receiver (dev,canid,mask) -> (func,data) */
441 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p, "
442 "ident %s\n", dev, DNAME(dev), can_id, mask, func, data, ident);
444 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
448 spin_lock_bh(&rcv_lists_lock);
450 d = find_dev_rcv_lists(dev);
452 rl = find_rcv_list(&can_id, &mask, d);
461 hlist_add_head_rcu(&r->list, rl);
464 pstats.rcv_entries++;
465 if (pstats.rcv_entries_max < pstats.rcv_entries)
466 pstats.rcv_entries_max = pstats.rcv_entries;
468 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
469 DNAME(dev), can_id, mask);
470 kmem_cache_free(rcv_cache, r);
474 spin_unlock_bh(&rcv_lists_lock);
478 EXPORT_SYMBOL(can_rx_register);
481 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
483 static void can_rx_delete_device(struct rcu_head *rp)
485 struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
487 DBG("removing dev_rcv_list at %p\n", d);
492 * can_rx_delete_receiver - rcu callback for single receiver entry removal
494 static void can_rx_delete_receiver(struct rcu_head *rp)
496 struct receiver *r = container_of(rp, struct receiver, rcu);
498 DBG("removing receiver at %p\n", r);
499 kmem_cache_free(rcv_cache, r);
503 * can_rx_unregister - unsubscribe CAN frames from a specific interface
504 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
505 * @can_id: CAN identifier
507 * @func: callback function on filter match
508 * @data: returned parameter for callback function
511 * Removes subscription entry depending on given (subscription) values.
515 * -EINVAL on missing subscription entry
516 * -ENODEV unknown device
518 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
519 void (*func)(struct sk_buff *, void *), void *data)
521 struct receiver *r = NULL;
522 struct hlist_head *rl;
523 struct hlist_node *next;
524 struct dev_rcv_lists *d;
527 DBG("dev %p (%s), id %03X, mask %03X, callback %p, data %p\n",
528 dev, DNAME(dev), can_id, mask, func, data);
530 spin_lock_bh(&rcv_lists_lock);
532 d = find_dev_rcv_lists(dev);
534 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
535 DNAME(dev), can_id, mask);
540 rl = find_rcv_list(&can_id, &mask, d);
543 * Search the receiver list for the item to delete. This should
544 * exist, since no receiver may be unregistered that hasn't
545 * been registered before.
548 hlist_for_each_entry(r, next, rl, list) {
549 if (r->can_id == can_id && r->mask == mask
550 && r->func == func && r->data == data)
555 * Check for bug in CAN protocol implementations:
556 * If no matching list item was found, the list cursor variable next
557 * will be NULL, while r will point to the last item of the list.
561 DBG("receive list entry not found for "
562 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
569 hlist_del_rcu(&r->list);
572 if (pstats.rcv_entries > 0)
573 pstats.rcv_entries--;
575 /* remove device structure requested by NETDEV_UNREGISTER */
576 if (d->remove_on_zero_entries && !d->entries) {
577 DBG("removing dev_rcv_list for %s on zero entries\n",
579 hlist_del_rcu(&d->list);
584 spin_unlock_bh(&rcv_lists_lock);
586 /* schedule the receiver item for deletion */
588 call_rcu(&r->rcu, can_rx_delete_receiver);
590 /* schedule the device structure for deletion */
592 call_rcu(&d->rcu, can_rx_delete_device);
596 EXPORT_SYMBOL(can_rx_unregister);
598 static inline void deliver(struct sk_buff *skb, struct receiver *r)
600 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
602 DBG("skbuff %p cloned to %p\n", skb, clone);
605 r->func(clone, r->data);
610 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
613 struct hlist_node *n;
615 struct can_frame *cf = (struct can_frame *)skb->data;
616 canid_t can_id = cf->can_id;
621 if (can_id & CAN_ERR_FLAG) {
622 /* check for error frame entries only */
623 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
624 if (can_id & r->mask) {
625 DBG("match on rx_err skbuff %p\n", skb);
633 /* check for unfiltered entries */
634 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
635 DBG("match on rx_all skbuff %p\n", skb);
640 /* check for can_id/mask entries */
641 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
642 if ((can_id & r->mask) == r->can_id) {
643 DBG("match on rx_fil skbuff %p\n", skb);
649 /* check for inverted can_id/mask entries */
650 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
651 if ((can_id & r->mask) != r->can_id) {
652 DBG("match on rx_inv skbuff %p\n", skb);
658 /* check CAN_ID specific entries */
659 if (can_id & CAN_EFF_FLAG) {
660 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
661 if (r->can_id == can_id) {
662 DBG("match on rx_eff skbuff %p\n", skb);
668 can_id &= CAN_SFF_MASK;
669 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
670 DBG("match on rx_sff skbuff %p\n", skb);
679 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
680 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
681 struct packet_type *pt, struct net_device *orig_dev)
683 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
684 struct packet_type *pt)
687 struct dev_rcv_lists *d;
690 DBG("received skbuff on device %s, ptype %04x\n",
691 dev->name, ntohs(pt->type));
693 DBG_FRAME("af_can: can_rcv: received CAN frame",
694 (struct can_frame *)skb->data);
696 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
697 if (dev->type != ARPHRD_CAN || dev->nd_net != &init_net) {
699 if (dev->type != ARPHRD_CAN) {
705 /* update statistics */
707 stats.rx_frames_delta++;
711 /* deliver the packet to sockets listening on all devices */
712 matches = can_rcv_filter(&rx_alldev_list, skb);
714 /* find receive list for this device */
715 d = find_dev_rcv_lists(dev);
717 matches += can_rcv_filter(d, skb);
721 /* free the skbuff allocated by the netdevice driver */
722 DBG("freeing skbuff %p\n", skb);
727 stats.matches_delta++;
734 * af_can protocol functions
738 * can_proto_register - register CAN transport protocol
739 * @cp: pointer to CAN protocol structure
743 * -EINVAL invalid (out of range) protocol number
744 * -EBUSY protocol already in use
745 * -ENOBUF if proto_register() fails
747 int can_proto_register(struct can_proto *cp)
749 int proto = cp->protocol;
752 if (proto < 0 || proto >= CAN_NPROTO) {
753 printk(KERN_ERR "can: protocol number %d out of range\n",
757 if (proto_tab[proto]) {
758 printk(KERN_ERR "can: protocol %d already registered\n",
763 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
764 err = proto_register(cp->prot, 0);
769 proto_tab[proto] = cp;
771 /* use generic ioctl function if the module doesn't bring its own */
773 cp->ops->ioctl = can_ioctl;
777 EXPORT_SYMBOL(can_proto_register);
780 * can_proto_unregister - unregister CAN transport protocol
781 * @cp: pointer to CAN protocol structure
785 * -ESRCH protocol number was not registered
787 int can_proto_unregister(struct can_proto *cp)
789 int proto = cp->protocol;
791 if (!proto_tab[proto]) {
792 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
795 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
796 proto_unregister(cp->prot);
798 proto_tab[proto] = NULL;
802 EXPORT_SYMBOL(can_proto_unregister);
805 * af_can notifier to create/remove CAN netdevice specific structs
807 static int can_notifier(struct notifier_block *nb, unsigned long msg,
810 struct net_device *dev = (struct net_device *)data;
811 struct dev_rcv_lists *d;
813 DBG("msg %ld for dev %p (%s idx %d)\n",
814 msg, dev, dev->name, dev->ifindex);
816 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
817 if (dev->nd_net != &init_net)
821 if (dev->type != ARPHRD_CAN)
826 case NETDEV_REGISTER:
829 * create new dev_rcv_lists for this device
831 * N.B. zeroing the struct is the correct initialization
832 * for the embedded hlist_head structs.
833 * Another list type, e.g. list_head, would require
834 * explicit initialization.
837 DBG("creating new dev_rcv_lists for %s\n", dev->name);
839 BUG_ON(in_interrupt());
840 d = kzalloc(sizeof(*d), GFP_KERNEL);
843 "can: allocation of receive list failed\n");
848 spin_lock_bh(&rcv_lists_lock);
849 hlist_add_head_rcu(&d->list, &rx_dev_list);
850 spin_unlock_bh(&rcv_lists_lock);
854 case NETDEV_UNREGISTER:
855 spin_lock_bh(&rcv_lists_lock);
857 d = find_dev_rcv_lists(dev);
859 DBG("remove dev_rcv_list for %s (%d entries)\n",
860 dev->name, d->entries);
863 d->remove_on_zero_entries = 1;
866 hlist_del_rcu(&d->list);
868 printk(KERN_ERR "can: notifier: receive list not "
869 "found for dev %s\n", dev->name);
871 spin_unlock_bh(&rcv_lists_lock);
874 call_rcu(&d->rcu, can_rx_delete_device);
883 * af_can debugging stuff
886 #ifdef CONFIG_CAN_DEBUG_CORE
888 #define DBG_BSIZE 1024
891 * can_debug_cframe - print CAN frame
892 * @msg: pointer to message printed before the given CAN frame
893 * @cf: pointer to CAN frame
895 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
902 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
906 len = sprintf(buf, KERN_DEBUG);
908 len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
917 if (cf->can_id & CAN_EFF_FLAG)
918 len += sprintf(buf + len, "<%08X> [%X] ",
919 cf->can_id & CAN_EFF_MASK, dlc);
921 len += sprintf(buf + len, "<%03X> [%X] ",
922 cf->can_id & CAN_SFF_MASK, dlc);
924 for (i = 0; i < dlc; i++)
925 len += sprintf(buf + len, "%02X ", cf->data[i]);
927 if (cf->can_id & CAN_RTR_FLAG)
928 len += sprintf(buf + len, "(RTR)");
935 EXPORT_SYMBOL(can_debug_cframe);
938 * can_debug_skb - print socket buffer content to kernel log
939 * @skb: pointer to socket buffer
941 void can_debug_skb(struct sk_buff *skb)
946 buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
951 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
952 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
953 "h,d,t,e,l: %p %+d %+d %+d, %d",
954 skb, skb->dev ? skb->dev->ifindex : -1,
955 ntohs(skb->protocol),
956 atomic_read(&skb->users),
957 atomic_read(&(skb_shinfo(skb)->dataref)),
958 skb_shinfo(skb)->nr_frags,
959 skb->head, skb->data - skb->head,
960 skb->tail - skb->head, skb->end - skb->head, skb->len);
961 nbytes = skb->end - skb->head;
962 for (i = 0; i < nbytes; i++) {
964 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
965 if (len < DBG_BSIZE - 16) {
966 len += sprintf(buf + len, " %02x", skb->head[i]);
968 len += sprintf(buf + len, "...");
977 EXPORT_SYMBOL(can_debug_skb);
982 * af_can module init/exit functions
985 static struct packet_type can_packet __read_mostly = {
986 .type = __constant_htons(ETH_P_CAN),
991 static struct net_proto_family can_family_ops __read_mostly = {
993 .create = can_create,
994 .owner = THIS_MODULE,
997 /* notifier block for netdevice event */
998 static struct notifier_block can_netdev_notifier __read_mostly = {
999 .notifier_call = can_notifier,
1002 static __init int can_init(void)
1006 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1007 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1010 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1017 * Insert rx_alldev_list for reception on all devices.
1018 * This struct is zero initialized which is correct for the
1019 * embedded hlist heads, the dev pointer, and the entries counter.
1022 spin_lock_bh(&rcv_lists_lock);
1023 hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1024 spin_unlock_bh(&rcv_lists_lock);
1027 /* the statistics are updated every second (timer triggered) */
1028 init_timer(&stattimer);
1029 stattimer.function = can_stat_update;
1031 /* update every second */
1032 stattimer.expires = round_jiffies(jiffies + HZ);
1033 /* start statistics timer */
1034 add_timer(&stattimer);
1036 stattimer.function = NULL;
1041 /* protocol register */
1042 sock_register(&can_family_ops);
1043 register_netdevice_notifier(&can_netdev_notifier);
1044 dev_add_pack(&can_packet);
1049 static __exit void can_exit(void)
1051 struct dev_rcv_lists *d;
1052 struct hlist_node *n, *next;
1055 del_timer(&stattimer);
1060 /* protocol unregister */
1061 dev_remove_pack(&can_packet);
1062 unregister_netdevice_notifier(&can_netdev_notifier);
1063 sock_unregister(PF_CAN);
1065 /* remove rx_dev_list */
1066 spin_lock_bh(&rcv_lists_lock);
1067 hlist_del(&rx_alldev_list.list);
1068 hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1069 hlist_del(&d->list);
1072 spin_unlock_bh(&rcv_lists_lock);
1074 kmem_cache_destroy(rcv_cache);
1077 module_init(can_init);
1078 module_exit(can_exit);