]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/af_can.c
Fixed wrong error frame filter handling.
[socketcan-devel.git] / kernel / 2.6 / net / can / af_can.c
1 /*
2  * af_can.c
3  *
4  * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
19  *
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.
24  *
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.
27  *
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
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
45 #include <linux/autoconf.h>
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/skbuff.h>
56 #include <linux/net.h>
57 #include <linux/netdevice.h>
58 #include <net/sock.h>
59 #include <asm/uaccess.h>
60
61 #include <linux/can.h>
62 #include <linux/can/version.h>
63
64 #include "af_can.h"
65
66
67 RCSID("$Id$");
68
69 #define NAME "Volkswagen AG - Low Level CAN Framework (LLCF)"
70 #define IDENT "af_can"
71 static __initdata const char banner[] = BANNER(NAME);
72
73 MODULE_DESCRIPTION(NAME);
74 MODULE_LICENSE("Dual BSD/GPL");
75 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
76               "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
77
78 int stats_timer = 1; /* default: on */
79 module_param(stats_timer, int, S_IRUGO);
80
81 #ifdef CONFIG_CAN_DEBUG_CORE
82 static int debug = 0;
83 module_param(debug, int, S_IRUGO);
84 #define DBG(args...)       (debug & 1 ? \
85                                (printk(KERN_DEBUG "CAN %s: ", __func__), \
86                                 printk(args)) : 0)
87 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
88 #define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
89 #else
90 #define DBG(args...)
91 #define DBG_FRAME(args...)
92 #define DBG_SKB(skb)
93 #endif
94
95 static __init int  can_init(void);
96 static __exit void can_exit(void);
97
98 static int can_create(struct socket *sock, int protocol);
99 static int can_notifier(struct notifier_block *nb,
100                         unsigned long msg, void *data);
101 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
103 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
104                    struct packet_type *pt, struct net_device *orig_dev);
105 #else
106 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
107                    struct packet_type *pt);
108 #endif
109 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb);
110 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev);
111 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
112                                         struct dev_rcv_lists *d);
113 static void can_rcv_lists_delete(struct rcu_head *rp);
114 static void can_rx_delete(struct rcu_head *rp);
115 static void can_rx_delete_all(struct hlist_head *rl);
116
117
118 struct notifier {
119         struct list_head list;
120         struct net_device *dev;
121         void (*func)(unsigned long msg, void *data);
122         void *data;
123 };
124
125 static LIST_HEAD(notifier_list);
126 static rwlock_t notifier_lock = RW_LOCK_UNLOCKED;
127
128 HLIST_HEAD(rx_dev_list);
129 static struct dev_rcv_lists rx_alldev_list;
130 static spinlock_t rcv_lists_lock = SPIN_LOCK_UNLOCKED;
131
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
133 static struct kmem_cache *rcv_cache __read_mostly;
134 #else
135 static kmem_cache_t *rcv_cache;
136 #endif
137
138 static struct packet_type can_packet = {
139         .type = __constant_htons(ETH_P_CAN),
140         .dev  = NULL,
141         .func = can_rcv,
142 };
143
144 static struct net_proto_family can_family_ops = {
145         .family = PF_CAN,
146         .create = can_create,
147         .owner  = THIS_MODULE,
148 };
149
150 /* notifier block for netdevice event */
151 static struct notifier_block can_netdev_notifier = {
152         .notifier_call = can_notifier,
153 };
154
155 /* table of registered CAN protocols */
156 static struct can_proto *proto_tab[CAN_NPROTO];
157
158 extern struct timer_list stattimer; /* timer for statistics update */
159 extern struct s_stats  stats;       /* packet statistics */
160 extern struct s_pstats pstats;      /* receive list statistics */
161
162 module_init(can_init);
163 module_exit(can_exit);
164
165 /**************************************************/
166 /* af_can module init/exit functions              */
167 /**************************************************/
168
169 static __init int can_init(void)
170 {
171         printk(banner);
172
173         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
174                                       0, 0, NULL, NULL);
175         if (!rcv_cache)
176                 return -ENOMEM;
177
178         /* Insert struct dev_rcv_lists for reception on all devices.
179            This struct is zero initialized which is correct for the 
180            embedded hlist heads, the dev pointer, and the entries counter.
181         */
182
183         spin_lock(&rcv_lists_lock);
184         hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
185         spin_unlock(&rcv_lists_lock);
186
187         if (stats_timer) {
188                 /* statistics init */
189                 init_timer(&stattimer);
190         }
191
192         /* procfs init */
193         can_init_proc();
194
195         /* protocol register */
196         sock_register(&can_family_ops);
197         register_netdevice_notifier(&can_netdev_notifier);
198         dev_add_pack(&can_packet);
199
200         return 0;
201 }
202
203 static __exit void can_exit(void)
204 {
205         struct dev_rcv_lists *d;
206         struct hlist_node *n, *next;
207
208         if (stats_timer) {
209                 /* stop statistics timer */
210                 del_timer(&stattimer);
211         }
212
213         /* procfs remove */
214         can_remove_proc();
215
216         /* protocol unregister */
217         dev_remove_pack(&can_packet);
218         unregister_netdevice_notifier(&can_netdev_notifier);
219         sock_unregister(PF_CAN);
220
221         /* remove rx_dev_list */
222         /* XXX: should we lock the receive list here? */
223         hlist_del(&rx_alldev_list.list);
224         hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list)
225                 kfree(d);
226
227         kmem_cache_destroy(rcv_cache);
228 }
229
230 /**************************************************/
231 /* af_can protocol functions                      */
232 /**************************************************/
233
234 void can_proto_register(struct can_proto *cp)
235 {
236         int proto = cp->protocol;
237         if (proto < 0 || proto >= CAN_NPROTO) {
238                 printk(KERN_ERR "CAN: protocol number %d out of range\n", proto);
239                 return;
240         }
241         if (proto_tab[proto]) {
242                 printk(KERN_ERR "CAN: protocol %d already registered\n", proto);
243                 return;
244         }
245
246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
247         if (proto_register(cp->prot, 0) != 0) {
248                 return;
249         }
250 #endif
251         proto_tab[proto] = cp;
252
253         /* use our generic ioctl function if the module doesn't bring its own */
254         if (!cp->ops->ioctl)
255                 cp->ops->ioctl = can_ioctl;
256 }
257
258 void can_proto_unregister(struct can_proto *cp)
259 {
260         int proto = cp->protocol;
261         if (!proto_tab[proto]) {
262                 printk(KERN_ERR "CAN: protocol %d is not registered\n", proto);
263                 return;
264         }
265 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
266         proto_unregister(cp->prot);
267 #endif
268         proto_tab[proto] = NULL;
269 }
270
271 void can_dev_register(struct net_device *dev,
272                       void (*func)(unsigned long msg, void *), void *data)
273 {
274         struct notifier *n;
275
276         DBG("called for %s\n", dev->name);
277
278         if (!(n = kmalloc(sizeof(*n), GFP_KERNEL)))
279                 return;
280
281         n->dev  = dev;
282         n->func = func;
283         n->data = data;
284
285         write_lock(&notifier_lock);
286         list_add(&n->list, &notifier_list);
287         write_unlock(&notifier_lock);
288 }
289
290 void can_dev_unregister(struct net_device *dev,
291                         void (*func)(unsigned long msg, void *), void *data)
292 {
293         struct notifier *n, *next;
294
295         DBG("called for %s\n", dev->name);
296
297         write_lock(&notifier_lock);
298         list_for_each_entry_safe(n, next, &notifier_list, list) {
299                 if (n->dev == dev && n->func == func && n->data == data) {
300                         list_del(&n->list);
301                         kfree(n);
302                         break;
303                 }
304         }
305         write_unlock(&notifier_lock);
306 }
307
308 /**************************************************/
309 /* af_can socket functions                        */
310 /**************************************************/
311
312 static void can_sock_destruct(struct sock *sk)
313 {
314         DBG("called for sock %p\n", sk);
315
316         skb_queue_purge(&sk->sk_receive_queue);
317         if (sk->sk_protinfo)
318                 kfree(sk->sk_protinfo);
319 }
320
321 static int can_create(struct socket *sock, int protocol)
322 {
323         struct sock *sk;
324         struct can_proto *cp;
325         int ret;
326
327         DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
328
329         sock->state = SS_UNCONNECTED;
330
331         if (protocol < 0 || protocol >= CAN_NPROTO)
332                 return -EINVAL;
333
334         DBG("looking up proto %d in proto_tab[]\n", protocol);
335
336         /* try to load protocol module, when CONFIG_KMOD is defined */
337         if (!proto_tab[protocol]) {
338                 char module_name[30];
339                 sprintf(module_name, "can-proto-%d", protocol);
340                 if (request_module(module_name) == -ENOSYS)
341                         printk(KERN_INFO "CAN: request_module(%s) not implemented.\n",
342                                module_name);
343         }
344
345         /* check for success and correct type */
346         if (!(cp = proto_tab[protocol]) || cp->type != sock->type)
347                 return -EPROTONOSUPPORT;
348
349         if (cp->capability >= 0 && !capable(cp->capability))
350                 return -EPERM;
351
352         sock->ops = cp->ops;
353
354 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
355         sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
356         if (!sk)
357                 goto oom;
358 #else
359         sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
360         if (!sk)
361                 goto oom;
362         if (cp->obj_size &&
363             !(sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL))) {
364                 sk_free(sk);
365                 goto oom;
366         }
367         sk_set_owner(sk, proto_tab[protocol]->owner);
368 #endif
369         sock_init_data(sock, sk);
370         sk->sk_destruct = can_sock_destruct;
371
372         DBG("created sock: %p\n", sk);
373
374         ret = 0;
375 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
376         if (sk->sk_prot->init)
377                 ret = sk->sk_prot->init(sk);
378 #else
379         if (cp->init)
380                 ret = cp->init(sk);
381 #endif
382         if (ret) {
383                 /* we must release sk */
384                 sock_orphan(sk);
385                 sock_put(sk);
386                 return ret;
387         }
388
389         return 0;
390
391  oom:
392         return -ENOMEM;
393 }
394
395 static int can_notifier(struct notifier_block *nb,
396                         unsigned long msg, void *data)
397 {
398         struct net_device *dev = (struct net_device *)data;
399         struct notifier *n;
400
401         DBG("called for %s, msg = %lu\n", dev->name, msg);
402
403 #if 0
404         if (dev->type != ARPHRD_CAN)
405                 return NOTIFY_DONE;
406 #endif
407
408         switch (msg) {
409                 struct dev_rcv_lists *d;
410                 int i;
411
412         case NETDEV_REGISTER:
413
414                 /* create new dev_rcv_lists for this device */
415
416                 DBG("creating new dev_rcv_lists for %s\n", dev->name);
417                 if (!(d = kmalloc(sizeof(*d), GFP_KERNEL))) {
418                         printk(KERN_ERR "CAN: allocation of receive list failed\n");
419                         return NOTIFY_DONE;
420                 }
421                 /* N.B. zeroing the struct is the correct initialization
422                         for the embedded hlist_head structs.
423                         Another list type, e.g. list_head, would require
424                         explicit initialization. */
425                 memset(d, 0, sizeof(*d));
426                 d->dev = dev;
427
428                 spin_lock(&rcv_lists_lock);
429                 hlist_add_head_rcu(&d->list, &rx_dev_list);
430                 spin_unlock(&rcv_lists_lock);
431
432                 break;
433
434         case NETDEV_UNREGISTER:
435                 spin_lock(&rcv_lists_lock);
436
437                 if (!(d = find_dev_rcv_lists(dev))) {
438                         printk(KERN_ERR "CAN: notifier: receive list not "
439                                "found for dev %s\n", dev->name);
440                         goto unreg_out;
441                 }
442
443                 hlist_del_rcu(&d->list);
444
445                 /* remove all receivers hooked at this netdevice */
446                 can_rx_delete_all(&d->rx_err);
447                 can_rx_delete_all(&d->rx_all);
448                 can_rx_delete_all(&d->rx_fil);
449                 can_rx_delete_all(&d->rx_inv);
450                 can_rx_delete_all(&d->rx_eff);
451                 for (i = 0; i < 2048; i++)
452                         can_rx_delete_all(&d->rx_sff[i]);
453
454         unreg_out:
455                 spin_unlock(&rcv_lists_lock);
456
457                 if (d)
458                         call_rcu(&d->rcu, can_rcv_lists_delete);
459
460                 break;
461         }
462
463         read_lock(&notifier_lock);
464         list_for_each_entry(n, &notifier_list, list) {
465                 if (n->dev == dev)
466                         n->func(msg, n->data);
467         }
468         read_unlock(&notifier_lock);
469
470         return NOTIFY_DONE;
471 }
472
473 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
474 {
475         struct sock *sk = sock->sk;
476
477         switch (cmd) {
478         case SIOCGSTAMP:
479                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
480         default:
481 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
482                 return -ENOIOCTLCMD;
483 #else
484                 return dev_ioctl(cmd, (void __user *)arg);
485 #endif
486         }
487         return 0;
488 }
489
490 /**************************************************/
491 /* af_can tx path                                 */
492 /**************************************************/
493
494 int can_send(struct sk_buff *skb, int loop)
495 {
496         int err;
497
498         if (loop) { /* local loopback (default) */
499                 *(struct sock **)skb->cb = skb->sk; /* tx sock reference */
500
501                 /* interface not capabable to do the loopback itself? */
502                 if (!(skb->dev->flags & IFF_LOOPBACK)) {
503                         struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
504                         newskb->protocol  = htons(ETH_P_CAN);
505                         newskb->ip_summed = CHECKSUM_UNNECESSARY;
506                         netif_rx(newskb); /* perform local loopback here */
507                 }
508         } else
509                 *(struct sock **)skb->cb = NULL; /* no loopback required */
510
511         if (!(skb->dev->flags & IFF_UP))
512                 err = -ENETDOWN;
513         else if ((err = dev_queue_xmit(skb)) > 0)  /* send to netdevice */
514                 err = net_xmit_errno(err);
515
516         /* update statistics */
517         stats.tx_frames++;
518         stats.tx_frames_delta++;
519
520         return err;
521 }
522
523 /**************************************************/
524 /* af_can rx path                                 */
525 /**************************************************/
526
527 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
528                     void (*func)(struct sk_buff *, void *), void *data,
529                     char *ident)
530 {
531         struct receiver *r;
532         struct hlist_head *rl;
533         struct dev_rcv_lists *d;
534         int ret = 0;
535
536         /* insert new receiver  (dev,canid,mask) -> (func,data) */
537
538         DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
539             dev, can_id, mask, func, data, ident);
540
541         if (!(r = kmem_cache_alloc(rcv_cache, GFP_KERNEL))) {
542                 ret = -ENOMEM;
543                 goto out;
544         }
545
546         spin_lock(&rcv_lists_lock);
547
548         if (!(d = find_dev_rcv_lists(dev))) {
549                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
550                     DNAME(dev), can_id, mask);
551                 kmem_cache_free(rcv_cache, r);
552                 ret = -ENODEV;
553                 goto out_unlock;
554         }
555
556         rl = find_rcv_list(&can_id, &mask, d);
557
558         r->can_id  = can_id;
559         r->mask    = mask;
560         r->matches = 0;
561         r->func    = func;
562         r->data    = data;
563         r->ident   = ident;
564
565         hlist_add_head_rcu(&r->list, rl);
566         d->entries++;
567
568         pstats.rcv_entries++;
569         if (pstats.rcv_entries_max < pstats.rcv_entries)
570                 pstats.rcv_entries_max = pstats.rcv_entries;
571
572  out_unlock:
573         spin_unlock(&rcv_lists_lock);
574  out:
575         return ret;
576 }
577
578 static void can_rcv_lists_delete(struct rcu_head *rp)
579 {
580         struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
581         kfree(d);
582 }
583
584 static void can_rx_delete(struct rcu_head *rp)
585 {
586         struct receiver *r = container_of(rp, struct receiver, rcu);
587         kmem_cache_free(rcv_cache, r);
588 }
589
590 static void can_rx_delete_all(struct hlist_head *rl)
591 {
592         struct receiver *r;
593         struct hlist_node *n;
594
595         hlist_for_each_entry_rcu(r, n, rl, list) {
596                 hlist_del_rcu(&r->list);
597                 call_rcu(&r->rcu, can_rx_delete);
598         }
599 }
600
601 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
602                       void (*func)(struct sk_buff *, void *), void *data)
603 {
604         struct receiver *r;
605         struct hlist_head *rl;
606         struct hlist_node *next;
607         struct dev_rcv_lists *d;
608         int ret = 0;
609
610         DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
611             dev, can_id, mask, func, data);
612
613         r = NULL;
614
615         spin_lock(&rcv_lists_lock);
616
617         if (!(d = find_dev_rcv_lists(dev))) {
618                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
619                     DNAME(dev), can_id, mask);
620                 ret = -ENODEV;
621                 goto out;
622         }
623
624         rl = find_rcv_list(&can_id, &mask, d);
625
626         /*  Search the receiver list for the item to delete.  This should
627          *  exist, since no receiver may be unregistered that hasn't
628          *  been registered before.
629          */
630
631         hlist_for_each_entry(r, next, rl, list) {
632                 if (r->can_id == can_id && r->mask == mask
633                     && r->func == func && r->data == data)
634                         break;
635         }
636
637         /*  Check for bug in CAN protocol implementations:
638          *  If no matching list item was found, the list cursor variable next
639          *  will be NULL, while r will point to the last item of the list.
640          */
641
642         if (!next) {
643                 DBG("receive list entry not found for "
644                     "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
645                 ret = -EINVAL;
646                 r = NULL;
647                 goto out;
648         }
649
650         hlist_del_rcu(&r->list);
651         d->entries--;
652
653         if (pstats.rcv_entries > 0)
654                 pstats.rcv_entries--;
655
656  out:
657         spin_unlock(&rcv_lists_lock);
658
659         /* schedule the receiver item for deletion */
660         if (r)
661                 call_rcu(&r->rcu, can_rx_delete);
662
663         return ret;
664 }
665
666 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
667 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
668                    struct packet_type *pt, struct net_device *orig_dev)
669 #else
670 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
671                    struct packet_type *pt)
672 #endif
673 {
674         struct dev_rcv_lists *d;
675         int matches;
676
677         DBG("received skbuff on device %s, ptype %04x\n",
678             dev->name, ntohs(pt->type));
679         DBG_SKB(skb);
680         DBG_FRAME("af_can: can_rcv: received CAN frame",
681                   (struct can_frame *)skb->data);
682
683         /* update statistics */
684         stats.rx_frames++;
685         stats.rx_frames_delta++;
686
687         rcu_read_lock();
688
689         /* deliver the packet to sockets listening on all devices */
690         matches = can_rcv_filter(&rx_alldev_list, skb);
691
692         /* find receive list for this device */
693         if ((d = find_dev_rcv_lists(dev)))
694                 matches += can_rcv_filter(d, skb);
695
696         rcu_read_unlock();
697
698         /* free the skbuff allocated by the netdevice driver */
699         DBG("freeing skbuff %p\n", skb);
700         kfree_skb(skb);
701
702         if (matches > 0) {
703                 stats.matches++;
704                 stats.matches_delta++;
705         }
706
707         return 0;
708 }
709
710
711 static inline void deliver(struct sk_buff *skb, struct receiver *r)
712 {
713         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
714         DBG("skbuff %p cloned to %p\n", skb, clone);
715         if (clone) {
716                 r->func(clone, r->data);
717                 r->matches++;    /* update specific statistics */
718         }
719 }
720
721 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
722 {
723         struct receiver *r;
724         struct hlist_node *n;
725         int matches = 0;
726         struct can_frame *cf = (struct can_frame*)skb->data;
727         canid_t can_id = cf->can_id;
728
729         if (d->entries == 0)
730                 return 0;
731
732         if (can_id & CAN_ERR_FLAG) {
733                 /* check for error frame entries only */
734                 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
735                         if (can_id & r->mask) {
736                                 DBG("match on rx_err skbuff %p\n", skb);
737                                 deliver(skb, r);
738                                 matches++;
739                         }
740                 }
741                 goto out;
742         }
743
744         /* check for unfiltered entries */
745         hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
746                 DBG("match on rx_all skbuff %p\n", skb);
747                 deliver(skb, r);
748                 matches++;
749         }
750
751         /* check for can_id/mask entries */
752         hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
753                 if ((can_id & r->mask) == r->can_id) {
754                         DBG("match on rx_fil skbuff %p\n", skb);
755                         deliver(skb, r);
756                         matches++;
757                 }
758         }
759
760         /* check for inverted can_id/mask entries */
761         hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
762                 if ((can_id & r->mask) != r->can_id) {
763                         DBG("match on rx_inv skbuff %p\n", skb);
764                         deliver(skb, r);
765                         matches++;
766                 }
767         }
768
769         /* check CAN_ID specific entries */
770         if (can_id & CAN_EFF_FLAG) {
771                 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
772                         if (r->can_id == can_id) {
773                                 DBG("match on rx_eff skbuff %p\n", skb);
774                                 deliver(skb, r);
775                                 matches++;
776                         }
777                 }
778         } else {
779                 can_id &= CAN_SFF_MASK;
780                 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
781                         DBG("match on rx_sff skbuff %p\n", skb);
782                         deliver(skb, r);
783                         matches++;
784                 }
785         }
786
787  out:
788         return matches;
789 }
790
791 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
792 {
793         struct dev_rcv_lists *d;
794         struct hlist_node *n;
795
796         /* find receive list for this device */
797
798         /*  The hlist_for_each_entry*() macros curse through the list
799          *  using the pointer variable n and set d to the containing
800          *  struct in each list iteration.  Therefore, after list
801          *  iteration, d is unmodified when the list is empty, and it
802          *  points to last list element, when the list is non-empty
803          *  but no match in the loop body is found.  I.e. d is *not*
804          *  NULL when no match is found.  We can, however, use the
805          *  cursor variable n to decide if a match was found.
806          */
807
808         hlist_for_each_entry(d, n, &rx_dev_list, list)
809                 if (d->dev == dev)
810                         break;
811
812         return n ? d : NULL;
813 }
814
815 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
816                                         struct dev_rcv_lists *d)
817 {
818         canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking values */
819         canid_t eff = *can_id & *mask & CAN_EFF_FLAG; /* correct EFF check? */
820         canid_t rtr = *can_id & *mask & CAN_RTR_FLAG; /* correct RTR check? */
821
822         /* do not modify the error mask due to the can_id which is 0 */
823         if (*mask & CAN_ERR_FLAG) { /* filter error frames */
824                 *mask &= CAN_ERR_MASK; /* clear CAN_ERR_FLAG in list entry */
825                 return &d->rx_err;
826         }
827
828         /* make some paranoic operations */
829         if (*can_id & CAN_EFF_FLAG)
830                 *mask &= (CAN_EFF_MASK | eff | rtr);
831         else
832                 *mask &= (CAN_SFF_MASK | rtr);
833
834         *can_id &= *mask;
835
836         if (inv) /* inverse can_id/can_mask filter and RTR */
837                 return &d->rx_inv;
838
839         if (*can_id & CAN_RTR_FLAG) /* positive filter RTR */
840                 return &d->rx_fil;
841
842         if (!(*mask)) /* mask == 0 => no filter */
843                 return &d->rx_all;
844
845         if (*can_id & CAN_EFF_FLAG) {
846                 if (*mask == CAN_EFF_MASK) /* filter exact EFF can_id */
847                         return &d->rx_eff;
848         } else {
849                 if (*mask == CAN_SFF_MASK) /* filter exact SFF can_id */
850                         return &d->rx_sff[*can_id];
851         }
852
853         return &d->rx_fil;  /* filter via can_id/can_mask */
854 }
855
856 /**************************************************/
857 /* af_can utility stuff                           */
858 /**************************************************/
859
860 unsigned long timeval2jiffies(struct timeval *tv, int round_up)
861 {
862         unsigned long jif;
863         unsigned long sec  = tv->tv_sec;
864         unsigned long usec = tv->tv_usec;
865
866         if (sec > ULONG_MAX / HZ)          /* check for overflow */
867                 return ULONG_MAX;
868
869         if (round_up)                      /* any usec below one HZ? */
870                 usec += 1000000 / HZ - 1;  /* pump it up */
871
872         jif = usec / (1000000 / HZ);
873
874         if (sec * HZ > ULONG_MAX - jif)    /* check for overflow */
875                 return ULONG_MAX;
876         else
877                 return jif + sec * HZ;
878 }
879
880
881 /**************************************************/
882 /* af_can debugging stuff                         */
883 /**************************************************/
884
885 #ifdef CONFIG_CAN_DEBUG_CORE
886
887 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
888 {
889         va_list ap;
890         int len;
891         int dlc, i;
892         char buf[1024];
893
894         len = sprintf(buf, KERN_DEBUG);
895         va_start(ap, cf);
896         len += snprintf(buf + len, sizeof(buf) - 64, msg, ap);
897         buf[len++] = ':';
898         buf[len++] = ' ';
899         va_end(ap);
900
901         if ((dlc = cf->can_dlc) > 8)
902                 dlc = 8;
903
904         if (cf->can_id & CAN_EFF_FLAG)
905                 len += sprintf(buf + len, "<%08X> [%X] ",
906                                cf->can_id & CAN_EFF_MASK, dlc);
907         else
908                 len += sprintf(buf + len, "<%03X> [%X] ",
909                                cf->can_id & CAN_SFF_MASK, dlc);
910
911         for (i = 0; i < dlc; i++)
912                 len += sprintf(buf + len, "%02X ", cf->data[i]);
913
914         if (cf->can_id & CAN_RTR_FLAG)
915                 len += sprintf(buf + len, "(RTR)");
916
917         buf[len++] = '\n';
918         buf[len]   = '\0';
919         printk(buf);
920 }
921
922 void can_debug_skb(struct sk_buff *skb)
923 {
924         int len, nbytes, i;
925         char buf[1024];
926
927         len = sprintf(buf,
928                       KERN_DEBUG "  skbuff at %p, dev: %d, proto: %04x\n"
929                       KERN_DEBUG "  users: %d, dataref: %d, nr_frags: %d, "
930                       "h,d,t,e,l: %p %+d %+d %+d, %d",
931                       skb, skb->dev ? skb->dev->ifindex : -1,
932                       ntohs(skb->protocol),
933                       atomic_read(&skb->users),
934                       atomic_read(&(skb_shinfo(skb)->dataref)),
935                       skb_shinfo(skb)->nr_frags,
936                       skb->head, skb->data - skb->head,
937                       skb->tail - skb->head, skb->end - skb->head, skb->len);
938         nbytes = skb->end - skb->head;
939         for (i = 0; i < nbytes; i++) {
940                 if (i % 16 == 0)
941                         len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
942                 if (len < sizeof(buf) - 16) {
943                         len += sprintf(buf + len, " %02x", skb->head[i]);
944                 } else {
945                         len += sprintf(buf + len, "...");
946                         break;
947                 }
948         }
949         buf[len++] = '\n';
950         buf[len]   = '\0';
951         printk(buf);
952 }
953
954 EXPORT_SYMBOL(can_debug_cframe);
955 EXPORT_SYMBOL(can_debug_skb);
956
957 #endif
958
959 /**************************************************/
960 /* Exported symbols                               */
961 /**************************************************/
962 EXPORT_SYMBOL(can_proto_register);
963 EXPORT_SYMBOL(can_proto_unregister);
964 EXPORT_SYMBOL(can_rx_register);
965 EXPORT_SYMBOL(can_rx_unregister);
966 EXPORT_SYMBOL(can_dev_register);
967 EXPORT_SYMBOL(can_dev_unregister);
968 EXPORT_SYMBOL(can_send);
969 EXPORT_SYMBOL(timeval2jiffies);