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/spinlock.h>
51 #include <linux/socket.h>
52 #include <linux/skbuff.h>
53 #include <linux/net.h>
54 #include <linux/netdevice.h>
56 #include <asm/uaccess.h>
58 #include <linux/can/af_can.h>
59 #include <linux/can/version.h>
64 #define NAME "Volkswagen AG - Low Level CAN Framework (LLCF)"
65 #define IDENT "af_can"
66 static __initdata const char banner[] = BANNER(NAME);
68 MODULE_DESCRIPTION(NAME);
69 MODULE_LICENSE("Dual BSD/GPL");
70 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
71 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73 int stats_timer = 1; /* default: on */
74 module_param(stats_timer, int, S_IRUGO);
76 #ifdef CONFIG_CAN_DEBUG_CORE
78 module_param(debug, int, S_IRUGO);
79 #define DBG(args...) (debug & 1 ? \
80 (printk(KERN_DEBUG "CAN %s: ", __func__), \
82 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
83 #define DBG_SKB(skb) (debug & 4 ? can_debug_skb(skb) : 0)
86 #define DBG_FRAME(args...)
90 static __init int can_init(void);
91 static __exit void can_exit(void);
93 static int can_create(struct socket *sock, int protocol);
94 static int can_notifier(struct notifier_block *nb,
95 unsigned long msg, void *data);
96 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
97 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
98 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
99 struct packet_type *pt, struct net_device *orig_dev);
101 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
102 struct packet_type *pt);
104 static int can_rcv_filter(struct rcv_dev_list *q, struct sk_buff *skb);
105 static struct rcv_list **find_rcv_list(canid_t *can_id, canid_t *mask,
106 struct net_device *dev);
108 struct notifier_list {
109 struct notifier_list *next;
110 struct net_device *dev;
111 void (*func)(unsigned long msg, void *data);
115 static struct notifier_list *nlist;
117 struct rcv_dev_list *rx_dev_list;
118 struct rcv_dev_list rx_alldev_list;
119 rwlock_t rcv_lists_lock = RW_LOCK_UNLOCKED;
121 static struct packet_type can_packet = {
122 .type = __constant_htons(ETH_P_CAN),
127 static struct net_proto_family can_family_ops = {
129 .create = can_create,
130 .owner = THIS_MODULE,
133 static struct notifier_block can_netdev_notifier = {
134 .notifier_call = can_notifier,
137 static struct can_proto *proto_tab[CAN_MAX];
139 extern struct timer_list stattimer; /* timer for statistics update */
140 extern struct s_stats stats; /* statistics */
141 extern struct s_pstats pstats;
143 module_init(can_init);
144 module_exit(can_exit);
146 /**************************************************/
147 /* af_can module init/exit functions */
148 /**************************************************/
150 static __init int can_init(void)
155 /* statistics init */
156 init_timer(&stattimer);
162 /* protocol register */
163 sock_register(&can_family_ops);
164 register_netdevice_notifier(&can_netdev_notifier);
165 dev_add_pack(&can_packet);
170 static __exit void can_exit(void)
173 /* stop statistics timer */
174 del_timer(&stattimer);
180 /* protocol unregister */
181 dev_remove_pack(&can_packet);
182 unregister_netdevice_notifier(&can_netdev_notifier);
183 sock_unregister(PF_CAN);
186 /**************************************************/
187 /* af_can protocol functions */
188 /**************************************************/
190 void can_proto_register(int proto, struct can_proto *cp)
192 if (proto < 0 || proto >= CAN_MAX) {
193 printk(KERN_ERR "CAN: protocol number %d out of range\n", proto);
196 if (proto_tab[proto]) {
197 printk(KERN_ERR "CAN: protocol %d already registered\n", proto);
201 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
202 if (proto_register(cp->prot, 0) != 0) {
206 proto_tab[proto] = cp;
208 /* use our generic ioctl function if the module doesn't bring its own */
210 cp->ops->ioctl = can_ioctl;
213 void can_proto_unregister(int proto)
215 struct can_proto *cp;
217 if (!(cp = proto_tab[proto])) {
218 printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
221 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
222 proto_unregister(cp->prot);
224 proto_tab[proto] = NULL;
227 void can_dev_register(struct net_device *dev,
228 void (*func)(unsigned long msg, void *), void *data)
230 struct notifier_list *p = kmalloc(sizeof(*p), GFP_KERNEL);
232 DBG("called for %s\n", dev->name);
243 void can_dev_unregister(struct net_device *dev,
244 void (*func)(unsigned long msg, void *), void *data)
246 struct notifier_list *p, **q;
248 DBG("called for %s\n", dev->name);
250 for (q = &nlist; (p = *q); q = &p->next) {
251 if (p->dev == dev && p->func == func && p->data == data) {
259 /**************************************************/
260 /* af_can socket functions */
261 /**************************************************/
263 static void can_sock_destruct(struct sock *sk)
265 DBG("called for sock %p\n", sk);
267 skb_queue_purge(&sk->sk_receive_queue);
269 kfree(sk->sk_protinfo);
272 static int can_create(struct socket *sock, int protocol)
275 struct can_proto *cp;
277 DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
279 sock->state = SS_UNCONNECTED;
281 switch (sock->type) {
293 return -EPROTONOSUPPORT;
303 return -EPROTONOSUPPORT;
309 #ifndef CONFIG_CAN_RAW_USER
310 if (!capable(CAP_NET_RAW))
315 return -EPROTONOSUPPORT;
319 return -ESOCKTNOSUPPORT;
323 DBG("looking up proto %d in proto_tab[]\n", protocol);
325 /* try to load protocol module, when CONFIG_KMOD is defined */
326 if (!proto_tab[protocol]) {
327 char module_name[30];
328 sprintf(module_name, "can-proto-%d", protocol);
329 if (request_module(module_name) == -ENOSYS)
330 printk(KERN_INFO "af_can: request_module(%s) not implemented.\n",
334 /* check for success */
335 if (!(cp = proto_tab[protocol]))
336 return -EPROTONOSUPPORT;
340 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
341 sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
345 sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
349 !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
353 sk_set_owner(sk, proto_tab[protocol]->owner);
355 sock_init_data(sock, sk);
356 sk->sk_destruct = can_sock_destruct;
358 DBG("created sock: %p\n", sk);
366 static int can_notifier(struct notifier_block *nb,
367 unsigned long msg, void *data)
369 struct net_device *dev = (struct net_device *)data;
370 struct notifier_list *p;
372 DBG("called for %s, msg = %lu\n", dev->name, msg);
374 for (p = nlist; p; p = p->next) {
376 p->func(msg, p->data);
381 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
383 struct sock *sk = sock->sk;
387 return sock_get_timestamp(sk, (struct timeval __user *)arg);
389 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
392 return dev_ioctl(cmd, (void __user *)arg);
398 /**************************************************/
400 /**************************************************/
402 int can_send(struct sk_buff *skb)
404 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
407 if (!(skb->dev->flags & IFF_UP))
409 else if ((err = dev_queue_xmit(skb)) > 0) /* send to netdevice */
410 err = net_xmit_errno(err);
412 /* update statistics */
414 stats.tx_frames_delta++;
416 newskb->protocol = htons(ETH_P_CAN);
417 newskb->ip_summed = CHECKSUM_UNNECESSARY;
418 netif_rx(newskb); /* local loopback */
423 /**************************************************/
425 /**************************************************/
427 void can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
428 void (*func)(struct sk_buff *, void *), void *data,
431 struct rcv_list *p, **q;
432 struct rcv_dev_list *d;
434 DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
435 dev, can_id, mask, func, data, ident);
437 write_lock_bh(&rcv_lists_lock);
439 q = find_rcv_list(&can_id, &mask, dev);
442 printk(KERN_ERR "CAN: receive list not found for "
443 "dev %s, id %03X, mask %03X, ident %s\n",
444 dev->name, can_id, mask, ident);
448 /* insert (dev,canid,mask) -> (func,data) */
449 if (!(p = kmalloc(sizeof(struct rcv_list), GFP_KERNEL)))
464 for (d = rx_dev_list; d; d = d->next)
469 pstats.rcv_entries++;
470 if (pstats.rcv_entries_max < pstats.rcv_entries)
471 pstats.rcv_entries_max = pstats.rcv_entries;
474 write_unlock_bh(&rcv_lists_lock);
477 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
478 void (*func)(struct sk_buff *, void *), void *data)
480 struct rcv_list *p, **q;
481 struct rcv_dev_list *d;
483 DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
484 dev, can_id, mask, func, data);
486 write_lock_bh(&rcv_lists_lock);
488 q = find_rcv_list(&can_id, &mask, dev);
491 printk(KERN_ERR "CAN: receive list not found for "
492 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
496 for (; (p = *q); q = &p->next) {
497 if (p->can_id == can_id && p->mask == mask
498 && p->func == func && p->data == data)
503 printk(KERN_ERR "CAN: receive list entry not found for "
504 "dev %s, id %03X, mask %03X\n", dev->name, can_id, mask);
511 if (pstats.rcv_entries > 0)
512 pstats.rcv_entries--;
517 for (d = rx_dev_list; d; d = d->next)
523 d->dev = NULL; /* mark unused */
526 write_unlock_bh(&rcv_lists_lock);
529 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
530 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
531 struct packet_type *pt, struct net_device *orig_dev)
533 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
534 struct packet_type *pt)
537 struct rcv_dev_list *q;
540 DBG("received skbuff on device %s, ptype %04x\n",
541 dev->name, ntohs(pt->type));
543 DBG_FRAME("af_can: can_rcv: received CAN frame",
544 (struct can_frame *)skb->data);
546 /* update statistics */
548 stats.rx_frames_delta++;
550 read_lock(&rcv_lists_lock);
552 matches = can_rcv_filter(&rx_alldev_list, skb);
554 /* find receive list for this device */
555 for (q = rx_dev_list; q; q = q->next)
560 matches += can_rcv_filter(q, skb);
562 read_unlock(&rcv_lists_lock);
564 DBG("freeing skbuff %p\n", skb);
569 stats.matches_delta++;
576 static inline void deliver(struct sk_buff *skb, struct rcv_list *p)
578 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
579 DBG("skbuff %p cloned to %p\n", skb, clone);
581 p->func(clone, p->data);
582 p->matches++; /* update specific statistics */
586 static int can_rcv_filter(struct rcv_dev_list *q, struct sk_buff *skb)
590 struct can_frame *cf = (struct can_frame*)skb->data;
591 canid_t can_id = cf->can_id;
596 if (can_id & CAN_ERR_FLAG) {
597 /* check for error frame entries only */
598 for (p = q->rx_err; p; p = p->next) {
599 if (can_id & p->mask) {
600 DBG("match on rx_err skbuff %p\n", skb);
608 /* check for unfiltered entries */
609 for (p = q->rx_all; p; p = p->next) {
610 DBG("match on rx_all skbuff %p\n", skb);
615 /* check for can_id/mask entries */
616 for (p = q->rx_fil; p; p = p->next) {
617 if ((can_id & p->mask) == p->can_id) {
618 DBG("match on rx_fil skbuff %p\n", skb);
624 /* check for inverted can_id/mask entries */
625 for (p = q->rx_inv; p; p = p->next) {
626 if ((can_id & p->mask) != p->can_id) {
627 DBG("match on rx_inv skbuff %p\n", skb);
633 /* check CAN_ID specific entries */
634 if (can_id & CAN_EFF_FLAG) {
635 for (p = q->rx_eff; p; p = p->next) {
636 if (p->can_id == can_id) {
637 DBG("match on rx_eff skbuff %p\n", skb);
643 for (p = q->rx_sff[can_id & CAN_SFF_MASK]; p; p = p->next) {
644 DBG("match on rx_sff skbuff %p\n", skb);
654 static struct rcv_list **find_rcv_list(canid_t *can_id, canid_t *mask,
655 struct net_device *dev)
657 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking values */
658 canid_t eff = *can_id & *mask & CAN_EFF_FLAG; /* correct EFF check? */
659 canid_t rtr = *can_id & *mask & CAN_RTR_FLAG; /* correct RTR check? */
660 canid_t err = *mask & CAN_ERR_FLAG; /* mask for error frames only */
662 struct rcv_dev_list *p;
664 /* make some paranoic operations */
665 if (*can_id & CAN_EFF_FLAG)
666 *mask &= (CAN_EFF_MASK | eff | rtr);
668 *mask &= (CAN_SFF_MASK | rtr);
672 /* find receive list for this device */
676 /* find the list for dev or an unused list entry, otherwise */
677 struct rcv_dev_list *q;
679 for (q = rx_dev_list; q; q = q->next)
682 else if (q->dev == dev) {
688 DBG("reactivating rcv_dev_list for %s\n", dev->name);
694 /* create new rcv_dev_list for this device */
695 DBG("creating new rcv_dev_list for %s\n", dev->name);
696 if (!(p = kmalloc(sizeof(struct rcv_dev_list), GFP_KERNEL))) {
697 printk(KERN_ERR "CAN: allocation of receive list failed\n");
700 memset (p, 0, sizeof(struct rcv_dev_list));
702 p->next = rx_dev_list;
706 if (err) /* error frames */
709 if (inv) /* inverse can_id/can_mask filter and RTR */
712 if (*can_id & CAN_RTR_FLAG) /* positive filter RTR */
715 if (!(*mask)) /* mask == 0 => no filter */
718 if (*can_id & CAN_EFF_FLAG) {
719 if (*mask == CAN_EFF_MASK) /* filter exact EFF can_id */
722 if (*mask == CAN_SFF_MASK) /* filter exact SFF can_id */
723 return &p->rx_sff[*can_id];
726 return &p->rx_fil; /* filter via can_id/can_mask */
729 /**************************************************/
730 /* af_can utility stuff */
731 /**************************************************/
733 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
736 unsigned long sec = tv->tv_sec;
737 unsigned long usec = tv->tv_usec;
739 if (sec > ULONG_MAX / HZ) /* check for overflow */
742 if (round_up) /* any usec below one HZ? */
743 usec += 1000000 / HZ - 1; /* pump it up */
745 jif = usec / (1000000 / HZ);
747 if (sec * HZ > ULONG_MAX - jif) /* check for overflow */
750 return jif + sec * HZ;
754 /**************************************************/
755 /* af_can debugging stuff */
756 /**************************************************/
758 #ifdef CONFIG_CAN_DEBUG_CORE
760 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
767 len = sprintf(buf, KERN_DEBUG);
769 len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
774 if ((dlc = cf->can_dlc) > 8)
777 if (cf->can_id & CAN_EFF_FLAG)
778 len += sprintf(buf + len, "<%08X> [%X] ",
779 cf->can_id & CAN_EFF_MASK, dlc);
781 len += sprintf(buf + len, "<%03X> [%X] ",
782 cf->can_id & CAN_SFF_MASK, dlc);
784 for (i = 0; i < dlc; i++)
785 len += sprintf(buf + len, "%02X ", cf->data[i]);
787 if (cf->can_id & CAN_RTR_FLAG)
788 len += sprintf(buf + len, "(RTR)");
795 void can_debug_skb(struct sk_buff *skb)
801 KERN_DEBUG " skbuff at %p, dev: %d, proto: %04x\n"
802 KERN_DEBUG " users: %d, dataref: %d, nr_frags: %d, "
803 "h,d,t,e,l: %p %+d %+d %+d, %d",
804 skb, skb->dev ? skb->dev->ifindex : -1,
805 ntohs(skb->protocol),
806 atomic_read(&skb->users),
807 atomic_read(&(skb_shinfo(skb)->dataref)),
808 skb_shinfo(skb)->nr_frags,
809 skb->head, skb->data - skb->head,
810 skb->tail - skb->head, skb->end - skb->head, skb->len);
811 nbytes = skb->end - skb->head;
812 for (i = 0; i < nbytes; i++) {
814 len += sprintf(buf + len, "\n" KERN_DEBUG " ");
815 if (len < sizeof(buf) - 16) {
816 len += sprintf(buf + len, " %02x", skb->head[i]);
818 len += sprintf(buf + len, "...");
827 EXPORT_SYMBOL(can_debug_cframe);
828 EXPORT_SYMBOL(can_debug_skb);
832 /**************************************************/
833 /* Exported symbols */
834 /**************************************************/
835 EXPORT_SYMBOL(can_proto_register);
836 EXPORT_SYMBOL(can_proto_unregister);
837 EXPORT_SYMBOL(can_rx_register);
838 EXPORT_SYMBOL(can_rx_unregister);
839 EXPORT_SYMBOL(can_dev_register);
840 EXPORT_SYMBOL(can_dev_unregister);
841 EXPORT_SYMBOL(can_send);
842 EXPORT_SYMBOL(timeval2jiffies);