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>
47 #include <linux/module.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/socket.h>
54 #include <linux/skbuff.h>
55 #include <linux/net.h>
56 #include <linux/netdevice.h>
58 #include <asm/uaccess.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_PARM(stats_timer, "1i");
80 MODULE_PARM(debug, "1i");
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 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
100 struct packet_type *pt);
101 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb);
102 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev);
103 static struct receiver **find_rcv_list(canid_t *can_id, canid_t *mask,
104 struct dev_rcv_lists *d);
105 static void can_rx_delete_all(struct receiver **rl);
108 struct list_head list;
109 struct net_device *dev;
110 void (*func)(unsigned long msg, void *data);
114 static LIST_HEAD(notifier_list);
115 static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
117 static struct dev_rcv_lists rx_alldev_list;
118 struct dev_rcv_lists *rx_dev_list;
119 rwlock_t rcv_lists_lock = RW_LOCK_UNLOCKED;
121 static kmem_cache_t *rcv_cache;
123 static struct packet_type can_packet = {
124 .type = __constant_htons(ETH_P_CAN),
129 static struct net_proto_family can_family_ops = {
131 .create = can_create,
134 /* notifier block for netdevice event */
135 static struct notifier_block can_netdev_notifier = {
136 .notifier_call = can_notifier,
139 /* table of registered CAN protocols */
140 static struct can_proto *proto_tab[CAN_NPROTO];
142 extern struct timer_list stattimer; /* timer for statistics update */
143 extern struct s_stats stats; /* packet statistics */
144 extern struct s_pstats pstats; /* receive list statistics */
146 module_init(can_init);
147 module_exit(can_exit);
149 /**************************************************/
150 /* af_can module init/exit functions */
151 /**************************************************/
153 static __init int can_init(void)
155 struct net_device *dev;
159 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
164 /* Insert struct dev_rcv_lists for reception on all devices.
165 This struct is zero initialized which is correct for the
166 embedded receiver list head pointer, the dev pointer,
167 and the entries counter.
170 write_lock_bh(&rcv_lists_lock);
171 rx_alldev_list.pprev = &rx_dev_list;
172 rx_dev_list = &rx_alldev_list;
173 write_unlock_bh(&rcv_lists_lock);
176 /* statistics init */
177 init_timer(&stattimer);
183 /* protocol register */
184 sock_register(&can_family_ops);
186 /* netdevice notifier register & init currently existing devices */
187 read_lock_bh(&dev_base_lock);
188 register_netdevice_notifier(&can_netdev_notifier);
189 for (dev = dev_base; dev; dev = dev->next)
190 can_netdev_notifier.notifier_call(&can_netdev_notifier,
193 read_unlock_bh(&dev_base_lock);
195 dev_add_pack(&can_packet);
200 static __exit void can_exit(void)
202 struct dev_rcv_lists *d;
205 /* stop statistics timer */
206 del_timer(&stattimer);
212 /* protocol unregister */
213 dev_remove_pack(&can_packet);
214 unregister_netdevice_notifier(&can_netdev_notifier);
215 sock_unregister(PF_CAN);
217 /* remove rx_dev_list */
218 /* XXX: should we lock the receive list here? */
219 for (d = rx_dev_list; d; d = d->next)
220 if (d != &rx_alldev_list)
223 kmem_cache_destroy(rcv_cache);
226 /**************************************************/
227 /* af_can protocol functions */
228 /**************************************************/
230 void can_proto_register(struct can_proto *cp)
232 int proto = cp->protocol;
233 if (proto < 0 || proto >= CAN_NPROTO) {
234 printk(KERN_ERR "CAN: protocol number %d out of range\n", proto);
237 if (proto_tab[proto]) {
238 printk(KERN_ERR "CAN: protocol %d already registered\n", proto);
241 proto_tab[proto] = cp;
243 /* use our generic ioctl function if the module doesn't bring its own */
245 cp->ops->ioctl = can_ioctl;
248 void can_proto_unregister(struct can_proto *cp)
250 int proto = cp->protocol;
251 if (!proto_tab[proto]) {
252 printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
255 proto_tab[proto] = NULL;
258 void can_dev_register(struct net_device *dev,
259 void (*func)(unsigned long msg, void *), void *data)
263 DBG("called for %s\n", DNAME(dev));
265 if (!(n = kmalloc(sizeof(*n), GFP_KERNEL)))
272 write_lock(¬ifier_lock);
273 list_add(&n->list, ¬ifier_list);
274 write_unlock(¬ifier_lock);
277 void can_dev_unregister(struct net_device *dev,
278 void (*func)(unsigned long msg, void *), void *data)
280 struct notifier *n, *next;
282 DBG("called for %s\n", DNAME(dev));
284 write_lock(¬ifier_lock);
285 list_for_each_entry_safe(n, next, ¬ifier_list, list) {
286 if (n->dev == dev && n->func == func && n->data == data) {
292 write_unlock(¬ifier_lock);
295 /**************************************************/
296 /* af_can socket functions */
297 /**************************************************/
299 static void can_sock_destruct(struct sock *sk)
301 DBG("called for sock %p\n", sk);
303 skb_queue_purge(&sk->receive_queue);
306 static int can_create(struct socket *sock, int protocol)
309 struct can_proto *cp;
312 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
314 sock->state = SS_UNCONNECTED;
316 if (protocol < 0 || protocol >= CAN_NPROTO)
319 DBG("looking up proto %d in proto_tab[]\n", protocol);
321 /* try to load protocol module, when CONFIG_KMOD is defined */
322 if (!proto_tab[protocol]) {
323 char module_name[30];
324 sprintf(module_name, "can-proto-%d", protocol);
325 if (request_module(module_name) == -ENOSYS)
326 printk(KERN_INFO "af_can: request_module(%s) not implemented.\n",
330 /* check for success and correct type */
331 if (!(cp = proto_tab[protocol]) || cp->type != sock->type)
332 return -EPROTONOSUPPORT;
334 if (cp->capability >= 0 && !capable(cp->capability))
339 if (!(sk = sk_alloc(PF_CAN, GFP_KERNEL, 1)))
342 sock_init_data(sock, sk);
343 sk->destruct = can_sock_destruct;
345 DBG("created sock: %p\n", sk);
351 /* we must release sk */
363 static int can_notifier(struct notifier_block *nb,
364 unsigned long msg, void *data)
366 struct net_device *dev = (struct net_device *)data;
369 DBG("called for %s, msg = %lu\n", DNAME(dev), msg);
372 if (dev->type != ARPHRD_CAN)
377 struct dev_rcv_lists *d;
380 case NETDEV_REGISTER:
382 /* create new dev_rcv_lists for this device */
384 DBG("creating new dev_rcv_lists for %s\n", DNAME(dev));
385 if (!(d = kmalloc(sizeof(*d), GFP_KERNEL))) {
386 printk(KERN_ERR "CAN: allocation of receive list failed\n");
389 memset(d, 0, sizeof(*d));
392 /* insert d into the list */
393 write_lock_bh(&rcv_lists_lock);
394 d->next = rx_dev_list;
395 d->pprev = &rx_dev_list;
398 d->next->pprev = &d->next;
399 write_unlock_bh(&rcv_lists_lock);
403 case NETDEV_UNREGISTER:
404 write_lock_bh(&rcv_lists_lock);
406 if (!(d = find_dev_rcv_lists(dev))) {
407 printk(KERN_ERR "CAN: notifier: receive list not "
408 "found for dev %s\n", DNAME(dev));
412 /* remove d from the list */
414 d->next->pprev = d->pprev;
416 /* remove all receivers hooked at this netdevice */
417 can_rx_delete_all(&d->rx_err);
418 can_rx_delete_all(&d->rx_all);
419 can_rx_delete_all(&d->rx_fil);
420 can_rx_delete_all(&d->rx_inv);
421 can_rx_delete_all(&d->rx_eff);
422 for (i = 0; i < 2048; i++)
423 can_rx_delete_all(&d->rx_sff[i]);
427 write_unlock_bh(&rcv_lists_lock);
432 read_lock(¬ifier_lock);
433 list_for_each_entry(n, ¬ifier_list, list) {
435 n->func(msg, n->data);
437 read_unlock(¬ifier_lock);
442 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
445 struct sock *sk = sock->sk;
449 if (sk->stamp.tv_sec == 0)
451 if (err = copy_to_user((void *)arg, &sk->stamp, sizeof(sk->stamp)))
455 return dev_ioctl(cmd, (void *)arg);
460 /**************************************************/
462 /**************************************************/
464 int can_send(struct sk_buff *skb, int loop)
468 if (loop) { /* local loopback (default) */
469 *(struct sock **)skb->cb = skb->sk; /* tx sock reference */
471 /* interface not capabable to do the loopback itself? */
472 if (!(skb->dev->flags & IFF_LOOPBACK)) {
473 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
474 newskb->protocol = htons(ETH_P_CAN);
475 newskb->ip_summed = CHECKSUM_UNNECESSARY;
476 netif_rx(newskb); /* perform local loopback here */
479 *(struct sock **)skb->cb = NULL; /* no loopback required */
481 if (!(skb->dev->flags & IFF_UP))
483 else if ((err = dev_queue_xmit(skb)) > 0) /* send to netdevice */
484 err = net_xmit_errno(err);
486 /* update statistics */
488 stats.tx_frames_delta++;
493 /**************************************************/
495 /**************************************************/
497 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
498 void (*func)(struct sk_buff *, void *), void *data,
501 struct receiver *r, **rl;
502 struct dev_rcv_lists *d;
505 /* insert new receiver (dev,canid,mask) -> (func,data) */
507 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
508 dev, can_id, mask, func, data, ident);
510 if (!(r = kmem_cache_alloc(rcv_cache, GFP_KERNEL))) {
515 write_lock_bh(&rcv_lists_lock);
517 if (!(d = find_dev_rcv_lists(dev))) {
518 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
519 DNAME(dev), can_id, mask);
520 kmem_cache_free(rcv_cache, r);
525 rl = find_rcv_list(&can_id, &mask, d);
538 pstats.rcv_entries++;
539 if (pstats.rcv_entries_max < pstats.rcv_entries)
540 pstats.rcv_entries_max = pstats.rcv_entries;
543 write_unlock_bh(&rcv_lists_lock);
548 static void can_rx_delete_all(struct receiver **rl)
550 struct receiver *r, *n;
552 for (r = *rl; r; r = n) {
559 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
560 void (*func)(struct sk_buff *, void *), void *data)
562 struct receiver *r, **rl;
563 struct dev_rcv_lists *d;
566 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
567 dev, can_id, mask, func, data);
569 write_lock_bh(&rcv_lists_lock);
571 if (!(d = find_dev_rcv_lists(dev))) {
572 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
573 DNAME(dev), can_id, mask);
578 rl = find_rcv_list(&can_id, &mask, d);
580 /* Search the receiver list for the item to delete. This should
581 * exist, since no receiver may be unregistered that hasn't
582 * been registered before.
585 for (; r = *rl; rl = &r->next) {
586 if (r->can_id == can_id && r->mask == mask
587 && r->func == func && r->data == data)
591 /* Check for bug in CAN protocol implementations:
592 * If no matching list item was found, r is NULL.
596 DBG("receive list entry not found for "
597 "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
603 kmem_cache_free(rcv_cache, r);
606 if (pstats.rcv_entries > 0)
607 pstats.rcv_entries--;
610 write_unlock_bh(&rcv_lists_lock);
615 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
616 struct packet_type *pt)
618 struct dev_rcv_lists *d;
621 DBG("received skbuff on device %s, ptype %04x\n",
622 DNAME(dev), ntohs(pt->type));
624 DBG_FRAME("af_can: can_rcv: received CAN frame",
625 (struct can_frame *)skb->data);
627 /* update statistics */
629 stats.rx_frames_delta++;
631 read_lock(&rcv_lists_lock);
633 /* deliver the packet to sockets listening on all devices */
634 matches = can_rcv_filter(&rx_alldev_list, skb);
636 /* find receive list for this device */
637 if ((d = find_dev_rcv_lists(dev)))
638 matches += can_rcv_filter(d, skb);
640 read_unlock(&rcv_lists_lock);
642 DBG("freeing skbuff %p\n", skb);
647 stats.matches_delta++;
654 static inline void deliver(struct sk_buff *skb, struct receiver *r)
656 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
657 DBG("skbuff %p cloned to %p\n", skb, clone);
659 r->func(clone, r->data);
660 r->matches++; /* update specific statistics */
664 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
668 struct can_frame *cf = (struct can_frame*)skb->data;
669 canid_t can_id = cf->can_id;
674 if (can_id & CAN_ERR_FLAG) {
675 /* check for error frame entries only */
676 for (r = d->rx_err; r; r = r->next) {
677 if (can_id & r->mask) {
678 DBG("match on rx_err skbuff %p\n", skb);
686 /* check for unfiltered entries */
687 for (r = d->rx_all; r; r = r->next) {
688 DBG("match on rx_all skbuff %p\n", skb);
693 /* check for can_id/mask entries */
694 for (r = d->rx_fil; r; r = r->next) {
695 if ((can_id & r->mask) == r->can_id) {
696 DBG("match on rx_fil skbuff %p\n", skb);
702 /* check for inverted can_id/mask entries */
703 for (r = d->rx_inv; r; r = r->next) {
704 if ((can_id & r->mask) != r->can_id) {
705 DBG("match on rx_inv skbuff %p\n", skb);
711 /* check CAN_ID specific entries */
712 if (can_id & CAN_EFF_FLAG) {
713 for (r = d->rx_eff; r; r = r->next) {
714 if (r->can_id == can_id) {
715 DBG("match on rx_eff skbuff %p\n", skb);
721 for (r = d->rx_sff[can_id & CAN_SFF_MASK]; r; r = r->next) {
722 DBG("match on rx_sff skbuff %p\n", skb);
732 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
734 struct dev_rcv_lists *d;
736 /* find receive list for this device */
739 return &rx_alldev_list;
741 for (d = rx_dev_list; d; d = d->next)
748 static struct receiver **find_rcv_list(canid_t *can_id, canid_t *mask,
749 struct dev_rcv_lists *d)
751 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking values */
752 canid_t eff = *can_id & *mask & CAN_EFF_FLAG; /* correct EFF check? */
753 canid_t rtr = *can_id & *mask & CAN_RTR_FLAG; /* correct RTR check? */
754 canid_t err = *mask & CAN_ERR_FLAG; /* mask for error frames only */
756 /* make some paranoic operations */
757 if (*can_id & CAN_EFF_FLAG)
758 *mask &= (CAN_EFF_MASK | eff | rtr);
760 *mask &= (CAN_SFF_MASK | rtr);
764 if (err) /* error frames */
767 if (inv) /* inverse can_id/can_mask filter and RTR */
770 if (*can_id & CAN_RTR_FLAG) /* positive filter RTR */
773 if (!(*mask)) /* mask == 0 => no filter */
776 if (*can_id & CAN_EFF_FLAG) {
777 if (*mask == CAN_EFF_MASK) /* filter exact EFF can_id */
780 if (*mask == CAN_SFF_MASK) /* filter exact SFF can_id */
781 return &d->rx_sff[*can_id];
784 return &d->rx_fil; /* filter via can_id/can_mask */
787 /**************************************************/
788 /* af_can utility stuff */
789 /**************************************************/
791 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
794 unsigned long sec = tv->tv_sec;
795 unsigned long usec = tv->tv_usec;
797 if (sec > ULONG_MAX / HZ) /* check for overflow */
800 if (round_up) /* any usec below one HZ? */
801 usec += 1000000 / HZ - 1; /* pump it up */
803 jif = usec / (1000000 / HZ);
805 if (sec * HZ > ULONG_MAX - jif) /* check for overflow */
808 return jif + sec * HZ;
812 /**************************************************/
813 /* af_can debugging stuff */
814 /**************************************************/
818 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
825 len = sprintf(buf, KERN_DEBUG);
827 len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
832 if ((dlc = cf->can_dlc) > 8)
835 if (cf->can_id & CAN_EFF_FLAG)
836 len += sprintf(buf + len, "<%08X> [%X] ",
837 cf->can_id & CAN_EFF_MASK, dlc);
839 len += sprintf(buf + len, "<%03X> [%X] ",
840 cf->can_id & CAN_SFF_MASK, dlc);
842 for (i = 0; i < dlc; i++)
843 len += sprintf(buf + len, "%02X ", cf->data[i]);
845 if (cf->can_id & CAN_RTR_FLAG)
846 len += sprintf(buf + len, "(RTR)");
853 void can_debug_skb(struct sk_buff *skb)
859 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
860 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
861 "h,d,t,e,l: %p %+d %+d %+d, %d",
862 skb, skb->dev ? skb->dev->ifindex : -1,
863 ntohs(skb->protocol),
864 atomic_read(&skb->users),
865 atomic_read(&(skb_shinfo(skb)->dataref)),
866 skb_shinfo(skb)->nr_frags,
867 skb->head, skb->data - skb->head,
868 skb->tail - skb->head, skb->end - skb->head, skb->len);
869 nbytes = skb->end - skb->head;
870 for (i = 0; i < nbytes; i++) {
872 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
873 if (len < sizeof(buf) - 16) {
874 len += sprintf(buf + len, " %02x", skb->head[i]);
876 len += sprintf(buf + len, "...");
886 EXPORT_SYMBOL(can_debug_cframe);
887 EXPORT_SYMBOL(can_debug_skb);
892 /**************************************************/
893 /* Exported symbols */
894 /**************************************************/
896 EXPORT_SYMBOL(can_proto_register);
897 EXPORT_SYMBOL(can_proto_unregister);
898 EXPORT_SYMBOL(can_rx_register);
899 EXPORT_SYMBOL(can_rx_unregister);
900 EXPORT_SYMBOL(can_dev_register);
901 EXPORT_SYMBOL(can_dev_unregister);
902 EXPORT_SYMBOL(can_send);
903 EXPORT_SYMBOL(timeval2jiffies);