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