4 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
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 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
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/config.h>
46 #include <linux/module.h>
47 #include <linux/version.h>
48 #include <linux/kmod.h>
49 #include <linux/init.h>
50 #include <linux/list.h>
51 #include <linux/spinlock.h>
52 #include <linux/rcupdate.h>
53 #include <linux/socket.h>
54 #include <linux/skbuff.h>
55 #include <linux/net.h>
56 #include <linux/netdevice.h>
58 #include <asm/uaccess.h>
60 #include <linux/can/af_can.h>
61 #include <linux/can/version.h>
66 #define NAME "Volkswagen AG - Low Level CAN Framework (LLCF)"
67 #define IDENT "af_can"
68 static __initdata const char banner[] = BANNER(NAME);
70 MODULE_DESCRIPTION(NAME);
71 MODULE_LICENSE("Dual BSD/GPL");
72 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
73 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
75 int stats_timer = 1; /* default: on */
76 module_param(stats_timer, int, S_IRUGO);
78 #ifdef CONFIG_CAN_DEBUG_CORE
80 module_param(debug, int, S_IRUGO);
81 #define DBG(args...) (debug & 1 ? \
82 (printk(KERN_DEBUG "CAN %s: ", __func__), \
84 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
85 #define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
88 #define DBG_FRAME(args...)
92 static __init int can_init(void);
93 static __exit void can_exit(void);
95 static int can_create(struct socket *sock, int protocol);
96 static int can_notifier(struct notifier_block *nb,
97 unsigned long msg, void *data);
98 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
100 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
101 struct packet_type *pt, struct net_device *orig_dev);
103 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
104 struct packet_type *pt);
106 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb);
107 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev,
109 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
110 struct dev_rcv_lists *d);
113 struct list_head list;
114 struct net_device *dev;
115 void (*func)(unsigned long msg, void *data);
119 static LIST_HEAD(notifier_list);
120 static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
122 HLIST_HEAD(rx_dev_list);
123 struct dev_rcv_lists rx_alldev_list;
124 static spinlock_t rcv_lists_lock = SPIN_LOCK_UNLOCKED;
126 static struct packet_type can_packet = {
127 .type = __constant_htons(ETH_P_CAN),
132 static struct net_proto_family can_family_ops = {
134 .create = can_create,
135 .owner = THIS_MODULE,
138 /* notifier block for netdevice event */
139 static struct notifier_block can_netdev_notifier = {
140 .notifier_call = can_notifier,
143 /* table of registered CAN protocols */
144 static struct can_proto *proto_tab[CAN_MAX];
146 extern struct timer_list stattimer; /* timer for statistics update */
147 extern struct s_stats stats; /* packet statistics */
148 extern struct s_pstats pstats; /* receive list statistics */
150 module_init(can_init);
151 module_exit(can_exit);
153 /**************************************************/
154 /* af_can module init/exit functions */
155 /**************************************************/
157 static __init int can_init(void)
162 /* statistics init */
163 init_timer(&stattimer);
169 /* protocol register */
170 sock_register(&can_family_ops);
171 register_netdevice_notifier(&can_netdev_notifier);
172 dev_add_pack(&can_packet);
177 static __exit void can_exit(void)
180 /* stop statistics timer */
181 del_timer(&stattimer);
187 /* protocol unregister */
188 dev_remove_pack(&can_packet);
189 unregister_netdevice_notifier(&can_netdev_notifier);
190 sock_unregister(PF_CAN);
193 /**************************************************/
194 /* af_can protocol functions */
195 /**************************************************/
197 void can_proto_register(int proto, struct can_proto *cp)
199 if (proto < 0 || proto >= CAN_MAX) {
200 printk(KERN_ERR "CAN: protocol number %d out of range\n", proto);
203 if (proto_tab[proto]) {
204 printk(KERN_ERR "CAN: protocol %d already registered\n", proto);
208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
209 if (proto_register(cp->prot, 0) != 0) {
213 proto_tab[proto] = cp;
215 /* use our generic ioctl function if the module doesn't bring its own */
217 cp->ops->ioctl = can_ioctl;
220 void can_proto_unregister(int proto)
222 struct can_proto *cp;
224 if (!(cp = proto_tab[proto])) {
225 printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
228 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
229 proto_unregister(cp->prot);
231 proto_tab[proto] = NULL;
234 void can_dev_register(struct net_device *dev,
235 void (*func)(unsigned long msg, void *), void *data)
239 DBG("called for %s\n", dev->name);
241 if (!(n = kmalloc(sizeof(*n), GFP_KERNEL)))
248 write_lock(¬ifier_lock);
249 list_add(&n->list, ¬ifier_list);
250 write_unlock(¬ifier_lock);
253 void can_dev_unregister(struct net_device *dev,
254 void (*func)(unsigned long msg, void *), void *data)
256 struct notifier *n, *next;
258 DBG("called for %s\n", dev->name);
260 write_lock(¬ifier_lock);
261 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
262 if (n->dev == dev && n->func == func && n->data == data) {
268 write_unlock(¬ifier_lock);
271 /**************************************************/
272 /* af_can socket functions */
273 /**************************************************/
275 static void can_sock_destruct(struct sock *sk)
277 DBG("called for sock %p\n", sk);
279 skb_queue_purge(&sk->sk_receive_queue);
281 kfree(sk->sk_protinfo);
284 static int can_create(struct socket *sock, int protocol)
287 struct can_proto *cp;
290 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
292 sock->state = SS_UNCONNECTED;
294 switch (sock->type) {
306 return -EPROTONOSUPPORT;
312 #ifndef CONFIG_CAN_BCM_USER
313 if (!capable(CAP_NET_RAW))
320 return -EPROTONOSUPPORT;
326 #ifndef CONFIG_CAN_RAW_USER
327 if (!capable(CAP_NET_RAW))
332 return -EPROTONOSUPPORT;
336 return -ESOCKTNOSUPPORT;
340 DBG("looking up proto %d in proto_tab[]\n", protocol);
342 /* try to load protocol module, when CONFIG_KMOD is defined */
343 if (!proto_tab[protocol]) {
344 char module_name[30];
345 sprintf(module_name, "can-proto-%d", protocol);
346 if (request_module(module_name) == -ENOSYS)
347 printk(KERN_INFO "af_can: request_module(%s) not implemented.\n",
351 /* check for success */
352 if (!(cp = proto_tab[protocol]))
353 return -EPROTONOSUPPORT;
357 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
358 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
362 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
366 !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
370 sk_set_owner(sk, proto_tab[protocol]->owner);
372 sock_init_data(sock, sk);
373 sk->sk_destruct = can_sock_destruct;
375 DBG("created sock: %p\n", sk);
378 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
379 if (sk->sk_prot->init)
380 ret = sk->sk_prot->init(sk);
386 /* we must release sk */
398 static int can_notifier(struct notifier_block *nb,
399 unsigned long msg, void *data)
401 struct net_device *dev = (struct net_device *)data;
404 DBG("called for %s, msg = %lu\n", dev->name, msg);
406 read_lock(¬ifier_lock);
407 list_for_each_entry (n, ¬ifier_list, list) {
409 n->func(msg, n->data);
411 read_unlock(¬ifier_lock);
416 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
418 struct sock *sk = sock->sk;
422 return sock_get_timestamp(sk, (struct timeval __user *)arg);
424 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
427 return dev_ioctl(cmd, (void __user *)arg);
433 /**************************************************/
435 /**************************************************/
437 int can_send(struct sk_buff *skb, int loop)
441 if (loop) { /* local loopback (default) */
442 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
443 newskb->protocol = htons(ETH_P_CAN);
444 newskb->ip_summed = CHECKSUM_UNNECESSARY;
445 *(struct sock **)newskb->cb = skb->sk; /* tx sock reference */
446 netif_rx(newskb); /* => local loopback */
449 if (!(skb->dev->flags & IFF_UP))
451 else if ((err = dev_queue_xmit(skb)) > 0) /* send to netdevice */
452 err = net_xmit_errno(err);
454 /* update statistics */
456 stats.tx_frames_delta++;
461 /**************************************************/
463 /**************************************************/
465 void can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
466 void (*func)(struct sk_buff *, void *), void *data,
470 struct hlist_head *rl;
471 struct dev_rcv_lists *d;
473 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
474 dev, can_id, mask, func, data, ident);
476 spin_lock(&rcv_lists_lock);
478 d = find_dev_rcv_lists(dev, 1);
479 rl = find_rcv_list(&can_id, &mask, d);
482 printk(KERN_ERR "CAN: receive list not found for "
483 "dev %s, id %03X, mask %03X, ident %s\n",
484 dev->name, can_id, mask, ident);
488 /* insert (dev,canid,mask) -> (func,data) */
489 if (!(r = kmalloc(sizeof(struct receiver), GFP_KERNEL)))
499 hlist_add_head_rcu(&r->list, rl);
502 pstats.rcv_entries++;
503 if (pstats.rcv_entries_max < pstats.rcv_entries)
504 pstats.rcv_entries_max = pstats.rcv_entries;
507 spin_unlock(&rcv_lists_lock);
510 void can_rx_delete(struct rcu_head *rp)
512 struct receiver *r = container_of(rp, struct receiver, rcu);
516 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
517 void (*func)(struct sk_buff *, void *), void *data)
520 struct hlist_head *rl;
521 struct hlist_node *next;
522 struct dev_rcv_lists *d;
524 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
525 dev, can_id, mask, func, data);
529 spin_lock(&rcv_lists_lock);
531 if (!(d = find_dev_rcv_lists(dev, 0))) {
532 printk(KERN_ERR "CAN: receive list not found for "
533 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
537 rl = find_rcv_list(&can_id, &mask, d);
540 printk(KERN_ERR "CAN: receive list not found for "
541 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
545 /* Search the receiver list for the item to delete. This must
546 * exist, since no receiver may be unregistered that hasn't
547 * been registered before.
550 hlist_for_each_entry(r, next, rl, list) {
551 if (r->can_id == can_id && r->mask == mask
552 && r->func == func && r->data == data)
556 /* Check for bug in CAN protocol implementations:
557 * If no matching list item was found, the list cursor variable next
558 * will be NULL, while r will point to the item of the list.
562 printk(KERN_ERR "CAN: receive list entry not found for "
563 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
568 hlist_del_rcu(&r->list);
571 d->dev = NULL; /* mark unused */
573 if (pstats.rcv_entries > 0)
574 pstats.rcv_entries--;
577 spin_unlock(&rcv_lists_lock);
579 /* schedule the receiver item for deletion */
581 call_rcu(&r->rcu, can_rx_delete);
584 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
585 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
586 struct packet_type *pt, struct net_device *orig_dev)
588 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
589 struct packet_type *pt)
592 struct dev_rcv_lists *d;
593 struct hlist_node *n;
596 DBG("received skbuff on device %s, ptype %04x\n",
597 dev->name, ntohs(pt->type));
599 DBG_FRAME("af_can: can_rcv: received CAN frame",
600 (struct can_frame *)skb->data);
602 /* update statistics */
604 stats.rx_frames_delta++;
608 /* deliver the packet to sockets listening on all devices */
609 matches = can_rcv_filter(&rx_alldev_list, skb);
611 /* find receive list for this device
613 * The hlist_for_each_entry*() macros curse through the list
614 * using the pointer variable n and set d to the containing
615 * struct in each list iteration. Therefore, after list
616 * iteration, d is unmodified when the list is empty, and it
617 * points to last list element, when the list is non-empty
618 * but no match in the loop body is found. I.e. d is *not*
619 * NULL when no match is found. We can, however, use the
620 * cursor variable n to decide if a match was found.
623 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list)
628 matches += can_rcv_filter(d, skb);
632 /* free the skbuff allocated by the netdevice driver */
633 DBG("freeing skbuff %p\n", skb);
638 stats.matches_delta++;
645 static inline void deliver(struct sk_buff *skb, struct receiver *r)
647 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
648 DBG("skbuff %p cloned to %p\n", skb, clone);
650 r->func(clone, r->data);
651 r->matches++; /* update specific statistics */
655 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
658 struct hlist_node *n;
660 struct can_frame *cf = (struct can_frame*)skb->data;
661 canid_t can_id = cf->can_id;
666 if (can_id & CAN_ERR_FLAG) {
667 /* check for error frame entries only */
668 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
669 if (can_id & r->mask) {
670 DBG("match on rx_err skbuff %p\n", skb);
678 /* check for unfiltered entries */
679 hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
680 DBG("match on rx_all skbuff %p\n", skb);
685 /* check for can_id/mask entries */
686 hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
687 if ((can_id & r->mask) == r->can_id) {
688 DBG("match on rx_fil skbuff %p\n", skb);
694 /* check for inverted can_id/mask entries */
695 hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
696 if ((can_id & r->mask) != r->can_id) {
697 DBG("match on rx_inv skbuff %p\n", skb);
703 /* check CAN_ID specific entries */
704 if (can_id & CAN_EFF_FLAG) {
705 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
706 if (r->can_id == can_id) {
707 DBG("match on rx_eff skbuff %p\n", skb);
713 can_id &= CAN_SFF_MASK;
714 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
715 DBG("match on rx_sff skbuff %p\n", skb);
725 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev,
728 struct dev_rcv_lists *d, *q;
729 struct hlist_node *n;
731 /* find receive list for this device */
733 return &rx_alldev_list;
735 /* find the list for dev or an unused list entry, otherwise */
738 hlist_for_each_entry(q, n, &rx_dev_list, list)
739 if (!q->dev && create)
741 else if (q->dev == dev) {
747 DBG("reactivating dev_rcv_lists for %s\n", dev->name);
752 /* create new dev_rcv_lists for this device */
753 DBG("creating new dev_rcv_lists for %s\n", dev->name);
754 if (!(d = kmalloc(sizeof(struct dev_rcv_lists), GFP_KERNEL))) {
755 printk(KERN_ERR "CAN: allocation of receive list failed\n");
758 /* N.B. zeroing the struct is the correct initialization
759 for the embedded hlist_head structs.
760 Another list type, e.g. list_head, would require
761 explicit initialization. */
762 memset(d, 0, sizeof(struct dev_rcv_lists));
765 hlist_add_head(&d->list, &rx_dev_list);
771 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
772 struct dev_rcv_lists *d)
774 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking values */
775 canid_t eff = *can_id & *mask & CAN_EFF_FLAG; /* correct EFF check? */
776 canid_t rtr = *can_id & *mask & CAN_RTR_FLAG; /* correct RTR check? */
777 canid_t err = *mask & CAN_ERR_FLAG; /* mask for error frames only */
779 /* make some paranoic operations */
780 if (*can_id & CAN_EFF_FLAG)
781 *mask &= (CAN_EFF_MASK | eff | rtr);
783 *mask &= (CAN_SFF_MASK | rtr);
787 if (err) /* error frames */
790 if (inv) /* inverse can_id/can_mask filter and RTR */
793 if (*can_id & CAN_RTR_FLAG) /* positive filter RTR */
796 if (!(*mask)) /* mask == 0 => no filter */
799 if (*can_id & CAN_EFF_FLAG) {
800 if (*mask == CAN_EFF_MASK) /* filter exact EFF can_id */
803 if (*mask == CAN_SFF_MASK) /* filter exact SFF can_id */
804 return &d->rx_sff[*can_id];
807 return &d->rx_fil; /* filter via can_id/can_mask */
810 /**************************************************/
811 /* af_can utility stuff */
812 /**************************************************/
814 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
817 unsigned long sec = tv->tv_sec;
818 unsigned long usec = tv->tv_usec;
820 if (sec > ULONG_MAX / HZ) /* check for overflow */
823 if (round_up) /* any usec below one HZ? */
824 usec += 1000000 / HZ - 1; /* pump it up */
826 jif = usec / (1000000 / HZ);
828 if (sec * HZ > ULONG_MAX - jif) /* check for overflow */
831 return jif + sec * HZ;
835 /**************************************************/
836 /* af_can debugging stuff */
837 /**************************************************/
839 #ifdef CONFIG_CAN_DEBUG_CORE
841 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
848 len = sprintf(buf, KERN_DEBUG);
850 len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
855 if ((dlc = cf->can_dlc) > 8)
858 if (cf->can_id & CAN_EFF_FLAG)
859 len += sprintf(buf + len, "<%08X> [%X] ",
860 cf->can_id & CAN_EFF_MASK, dlc);
862 len += sprintf(buf + len, "<%03X> [%X] ",
863 cf->can_id & CAN_SFF_MASK, dlc);
865 for (i = 0; i < dlc; i++)
866 len += sprintf(buf + len, "%02X ", cf->data[i]);
868 if (cf->can_id & CAN_RTR_FLAG)
869 len += sprintf(buf + len, "(RTR)");
876 void can_debug_skb(struct sk_buff *skb)
882 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
883 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
884 "h,d,t,e,l: %p %+d %+d %+d, %d",
885 skb, skb->dev ? skb->dev->ifindex : -1,
886 ntohs(skb->protocol),
887 atomic_read(&skb->users),
888 atomic_read(&(skb_shinfo(skb)->dataref)),
889 skb_shinfo(skb)->nr_frags,
890 skb->head, skb->data - skb->head,
891 skb->tail - skb->head, skb->end - skb->head, skb->len);
892 nbytes = skb->end - skb->head;
893 for (i = 0; i < nbytes; i++) {
895 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
896 if (len < sizeof(buf) - 16) {
897 len += sprintf(buf + len, " %02x", skb->head[i]);
899 len += sprintf(buf + len, "...");
908 EXPORT_SYMBOL(can_debug_cframe);
909 EXPORT_SYMBOL(can_debug_skb);
913 /**************************************************/
914 /* Exported symbols */
915 /**************************************************/
916 EXPORT_SYMBOL(can_proto_register);
917 EXPORT_SYMBOL(can_proto_unregister);
918 EXPORT_SYMBOL(can_rx_register);
919 EXPORT_SYMBOL(can_rx_unregister);
920 EXPORT_SYMBOL(can_dev_register);
921 EXPORT_SYMBOL(can_dev_unregister);
922 EXPORT_SYMBOL(can_send);
923 EXPORT_SYMBOL(timeval2jiffies);