]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/af_can.c
Added locking to CAN core proc_fs output.
[socketcan-devel.git] / kernel / 2.6 / net / can / af_can.c
1 /*
2  * af_can.c - Protocol family CAN core module
3  *            (used by different CAN protocol modules)
4  *
5  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, the following disclaimer and
13  *    the referenced file 'COPYING'.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of Volkswagen nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * Alternatively, provided that this notice is retained in full, this
22  * software may be distributed under the terms of the GNU General
23  * Public License ("GPL") version 2 as distributed in the 'COPYING'
24  * file from the main directory of the linux kernel source.
25  *
26  * The provided data structures and external interfaces from this code
27  * are not restricted to be used by modules with a GPL compatible license.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40  * DAMAGE.
41  *
42  * Send feedback to <socketcan-users@lists.berlios.de>
43  *
44  */
45
46 #include <linux/autoconf.h>
47 #include <linux/module.h>
48 #include <linux/version.h>
49 #include <linux/slab.h>
50 #include <linux/kmod.h>
51 #include <linux/init.h>
52 #include <linux/list.h>
53 #include <linux/spinlock.h>
54 #include <linux/rcupdate.h>
55 #include <linux/socket.h>
56 #include <linux/if_ether.h>
57 #include <linux/if_arp.h>
58 #include <linux/skbuff.h>
59 #include <linux/net.h>
60 #include <linux/netdevice.h>
61 #include <net/sock.h>
62 #include <asm/uaccess.h>
63
64 #include <linux/can.h>
65 #include <linux/can/core.h>
66 #include "af_can.h"
67
68 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
69 RCSID("$Id$");
70
71 #define IDENT "core"
72 static __initdata const char banner[] =
73         KERN_INFO "can: controller area network core # "
74         CAN_VERSION_STRING "\n";
75
76 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
79               "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
80
81 MODULE_ALIAS_NETPROTO(PF_CAN);
82
83 int stats_timer = 1; /* default: on */
84 module_param(stats_timer, int, S_IRUGO);
85 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
86
87 #ifdef CONFIG_CAN_DEBUG_CORE
88 static int debug = 0;
89 module_param(debug, int, S_IRUGO);
90 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
91 #endif
92
93 struct notifier {
94         struct list_head list;
95         struct net_device *dev;
96         void (*func)(unsigned long msg, void *data);
97         void *data;
98 };
99
100 static LIST_HEAD(notifier_list);
101 static DEFINE_RWLOCK(notifier_lock);
102
103 HLIST_HEAD(rx_dev_list);
104 static struct dev_rcv_lists rx_alldev_list;
105 static DEFINE_SPINLOCK(rcv_lists_lock);
106
107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
108 static struct kmem_cache *rcv_cache __read_mostly;
109 #else
110 static kmem_cache_t *rcv_cache;
111 #endif
112
113 /* table of registered CAN protocols */
114 static struct can_proto *proto_tab[CAN_NPROTO];
115
116 struct timer_list stattimer; /* timer for statistics update */
117 struct s_stats  stats;       /* packet statistics */
118 struct s_pstats pstats;      /* receive list statistics */
119 DEFINE_SPINLOCK(stats_lock);
120
121 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
122 static void *kzalloc(size_t size, unsigned int __nocast flags)
123 {
124         void *ret = kmalloc(size, flags);
125         if (ret)
126                 memset(ret, 0, size);
127         return ret;
128 }
129 #endif
130
131 /*
132  * af_can socket functions
133  */
134
135 static int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
136 {
137         struct sock *sk = sock->sk;
138
139         switch (cmd) {
140
141         case SIOCGSTAMP:
142                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
143
144         default:
145 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
146                 return -ENOIOCTLCMD;
147 #else
148                 return dev_ioctl(cmd, (void __user *)arg);
149 #endif
150         }
151 }
152
153 static void can_sock_destruct(struct sock *sk)
154 {
155         DBG("called for sock %p\n", sk);
156
157         skb_queue_purge(&sk->sk_receive_queue);
158         if (sk->sk_protinfo)
159                 kfree(sk->sk_protinfo);
160 }
161
162 static int can_create(struct socket *sock, int protocol)
163 {
164         struct sock *sk;
165         struct can_proto *cp;
166         char module_name[30];
167         int ret = 0;
168
169         DBG("socket %p, type %d, proto %d\n", sock, sock->type, protocol);
170
171         sock->state = SS_UNCONNECTED;
172
173         if (protocol < 0 || protocol >= CAN_NPROTO)
174                 return -EINVAL;
175
176         DBG("looking up proto %d in proto_tab[]\n", protocol);
177
178         /* try to load protocol module, when CONFIG_KMOD is defined */
179         if (!proto_tab[protocol]) {
180                 sprintf(module_name, "can-proto-%d", protocol);
181                 if (request_module(module_name) == -ENOSYS)
182                         printk(KERN_INFO "can: request_module(%s) not"
183                                " implemented.\n", module_name);
184         }
185
186         /* check for success and correct type */
187         cp = proto_tab[protocol];
188         if (!cp || cp->type != sock->type)
189                 return -EPROTONOSUPPORT;
190
191         if (cp->capability >= 0 && !capable(cp->capability))
192                 return -EPERM;
193
194         sock->ops = cp->ops;
195
196 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
197         sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
198         if (!sk)
199                 return -ENOMEM;
200 #else
201         sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
202         if (!sk)
203                 return -ENOMEM;
204
205         if (cp->obj_size) {
206                 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
207                 if (!sk->sk_protinfo) {
208                         sk_free(sk);
209                         return -ENOMEM;
210                 }
211         }
212         sk_set_owner(sk, proto_tab[protocol]->owner);
213 #endif
214
215         sock_init_data(sock, sk);
216         sk->sk_destruct = can_sock_destruct;
217
218         DBG("created sock: %p\n", sk);
219
220 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
221         if (sk->sk_prot->init)
222                 ret = sk->sk_prot->init(sk);
223 #else
224         if (cp->init)
225                 ret = cp->init(sk);
226 #endif
227
228         if (ret) {
229                 /* release sk on errors */
230                 sock_orphan(sk);
231                 sock_put(sk);
232         }
233
234         return ret;
235 }
236
237 /*
238  * af_can tx path
239  */
240
241 /**
242  * can_send - transmit a CAN frame (optional with local loopback)
243  * @skb: pointer to socket buffer with CAN frame in data section
244  * @loop: loopback for listeners on local CAN sockets (recommended default!)
245  *
246  * Return:
247  *  0 on success
248  *  -ENETDOWN when the selected interface is down
249  *  -ENOBUFS on full driver queue (see net_xmit_errno())
250  */
251 int can_send(struct sk_buff *skb, int loop)
252 {
253         struct sock **tx_sk = (struct sock **)skb->cb;
254         int err;
255
256         if (skb->dev->type != ARPHRD_CAN) {
257                 kfree_skb(skb);
258                 return -EPERM;
259         }
260
261         if (loop) {
262                 /* local loopback of sent CAN frames (default) */
263
264                 /* indication for the CAN driver: do loopback */
265                 *tx_sk = skb->sk;
266
267                 /*
268                  * The reference to the originating sock may be also required
269                  * by the receiving socket to indicate (and ignore) his own
270                  * sent data. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
271                  */
272
273                 /* interface not capabable to do the loopback itself? */
274                 if (!(skb->dev->flags & IFF_LOOPBACK)) {
275                         struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
276
277                         /* perform the local loopback here */
278                         newskb->protocol  = htons(ETH_P_CAN);
279                         newskb->ip_summed = CHECKSUM_UNNECESSARY;
280                         netif_rx(newskb);
281                 }
282         } else {
283                 /* indication for the CAN driver: no loopback required */
284                 *tx_sk = NULL;
285         }
286
287         if (!(skb->dev->flags & IFF_UP))
288                 return -ENETDOWN;
289
290         /* send to netdevice */
291         err = dev_queue_xmit(skb);
292         if (err > 0)
293                 err = net_xmit_errno(err);
294
295         /* update statistics */
296         spin_lock(&stats_lock);
297         stats.tx_frames++;
298         stats.tx_frames_delta++;
299         spin_unlock(&stats_lock);
300
301         return err;
302 }
303 EXPORT_SYMBOL(can_send);
304
305 /*
306  * af_can rx path
307  */
308
309 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
310 {
311         struct dev_rcv_lists *d;
312         struct hlist_node *n;
313
314         /*
315          * find receive list for this device
316          *
317          * The hlist_for_each_entry*() macros curse through the list
318          * using the pointer variable n and set d to the containing
319          * struct in each list iteration.  Therefore, after list
320          * iteration, d is unmodified when the list is empty, and it
321          * points to last list element, when the list is non-empty
322          * but no match in the loop body is found.  I.e. d is *not*
323          * NULL when no match is found.  We can, however, use the
324          * cursor variable n to decide if a match was found.
325          */
326
327         hlist_for_each_entry(d, n, &rx_dev_list, list) {
328                 if (d->dev == dev)
329                         break;
330         }
331
332         return n ? d : NULL;
333 }
334
335 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
336                                         struct dev_rcv_lists *d)
337 {
338         canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
339
340         /* filter error frames */
341         if (*mask & CAN_ERR_FLAG) {
342                 /* clear CAN_ERR_FLAG in list entry */
343                 *mask &= CAN_ERR_MASK;
344                 return &d->rx_err;
345         }
346
347         /* ensure valid values in can_mask */
348         if (*mask & CAN_EFF_FLAG)
349                 *mask &= (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG);
350         else
351                 *mask &= (CAN_SFF_MASK | CAN_RTR_FLAG);
352
353         /* reduce condition testing at receive time */
354         *can_id &= *mask;
355
356         /* inverse can_id/can_mask filter */
357         if (inv)
358                 return &d->rx_inv;
359
360         /* mask == 0 => no condition testing at receive time */
361         if (!(*mask))
362                 return &d->rx_all;
363
364         /* use extra filterset for the subscription of exactly *ONE* can_id */
365         if (*can_id & CAN_EFF_FLAG) {
366                 if (*mask == (CAN_EFF_MASK | CAN_EFF_FLAG)) {
367                         /* RFC: a use-case for hash-tables in the future? */
368                         return &d->rx_eff;
369                 }
370         } else {
371                 if (*mask == CAN_SFF_MASK)
372                         return &d->rx_sff[*can_id];
373         }
374
375         /* default: filter via can_id/can_mask */
376         return &d->rx_fil;
377 }
378
379 /**
380  * can_rx_register - subscribe CAN frames from a specific interface
381  * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
382  * @can_id: CAN identifier (see description)
383  * @mask: CAN mask (see description)
384  * @func: callback function on filter match
385  * @data: returned parameter for callback function
386  * @ident: string for calling module indentification
387  *
388  * Description:
389  *  Invokes the callback function with the received sk_buff and the given
390  *  parameter 'data' on a matching receive filter. A filter matches, when
391  *
392  *          <received_can_id> & mask == can_id & mask
393  *
394  *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
395  *  filter for error frames (CAN_ERR_FLAG bit set in mask).
396  *
397  * Return:
398  *  0 on success
399  *  -ENOMEM on missing cache mem to create subscription entry
400  *  -ENODEV unknown device
401  */
402 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
403                     void (*func)(struct sk_buff *, void *), void *data,
404                     char *ident)
405 {
406         struct receiver *r;
407         struct hlist_head *rl;
408         struct dev_rcv_lists *d;
409         int ret = 0;
410
411         /* insert new receiver  (dev,canid,mask) -> (func,data) */
412
413         DBG("dev %p, id %03X, mask %03X, callback %p, data %p, ident %s\n",
414             dev, can_id, mask, func, data, ident);
415
416         r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
417         if (!r)
418                 return -ENOMEM;
419
420         spin_lock_bh(&rcv_lists_lock);
421
422         d = find_dev_rcv_lists(dev);
423         if (d) {
424                 rl = find_rcv_list(&can_id, &mask, d);
425
426                 r->can_id  = can_id;
427                 r->mask    = mask;
428                 r->matches = 0;
429                 r->func    = func;
430                 r->data    = data;
431                 r->ident   = ident;
432
433                 hlist_add_head_rcu(&r->list, rl);
434                 d->entries++;
435
436                 pstats.rcv_entries++;
437                 if (pstats.rcv_entries_max < pstats.rcv_entries)
438                         pstats.rcv_entries_max = pstats.rcv_entries;
439         } else {
440                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
441                     DNAME(dev), can_id, mask);
442                 kmem_cache_free(rcv_cache, r);
443                 ret = -ENODEV;
444         }
445
446         spin_unlock_bh(&rcv_lists_lock);
447
448         return ret;
449 }
450 EXPORT_SYMBOL(can_rx_register);
451
452 static void can_rcv_lists_delete(struct rcu_head *rp)
453 {
454         struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
455
456         kfree(d);
457 }
458
459 static void can_rx_delete(struct rcu_head *rp)
460 {
461         struct receiver *r = container_of(rp, struct receiver, rcu);
462
463         kmem_cache_free(rcv_cache, r);
464 }
465
466 static void can_rx_delete_all(struct hlist_head *rl)
467 {
468         struct receiver *r;
469         struct hlist_node *n;
470
471         hlist_for_each_entry_rcu(r, n, rl, list) {
472                 hlist_del_rcu(&r->list);
473                 call_rcu(&r->rcu, can_rx_delete);
474         }
475 }
476
477 /**
478  * can_rx_unregister - unsubscribe CAN frames from a specific interface
479  * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
480  * @can_id: CAN identifier
481  * @mask: CAN mask
482  * @func: callback function on filter match
483  * @data: returned parameter for callback function
484  *
485  * Description:
486  *  Removes subscription entry depending on given (subscription) values.
487  *
488  * Return:
489  *  0 on success
490  *  -EINVAL on missing subscription entry
491  *  -ENODEV unknown device
492  */
493 int can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
494                       void (*func)(struct sk_buff *, void *), void *data)
495 {
496         struct receiver *r = NULL;
497         struct hlist_head *rl;
498         struct hlist_node *next;
499         struct dev_rcv_lists *d;
500         int ret = 0;
501
502         DBG("dev %p, id %03X, mask %03X, callback %p, data %p\n",
503             dev, can_id, mask, func, data);
504
505         spin_lock_bh(&rcv_lists_lock);
506
507         d = find_dev_rcv_lists(dev);
508         if (!d) {
509                 DBG("receive list not found for dev %s, id %03X, mask %03X\n",
510                     DNAME(dev), can_id, mask);
511                 ret = -ENODEV;
512                 goto out;
513         }
514
515         rl = find_rcv_list(&can_id, &mask, d);
516
517         /*
518          * Search the receiver list for the item to delete.  This should
519          * exist, since no receiver may be unregistered that hasn't
520          * been registered before.
521          */
522
523         hlist_for_each_entry(r, next, rl, list) {
524                 if (r->can_id == can_id && r->mask == mask
525                     && r->func == func && r->data == data)
526                         break;
527         }
528
529         /*
530          * Check for bug in CAN protocol implementations:
531          * If no matching list item was found, the list cursor variable next
532          * will be NULL, while r will point to the last item of the list.
533          */
534
535         if (!next) {
536                 DBG("receive list entry not found for "
537                     "dev %s, id %03X, mask %03X\n", DNAME(dev), can_id, mask);
538                 ret = -EINVAL;
539                 r = NULL;
540                 goto out;
541         }
542
543         hlist_del_rcu(&r->list);
544         d->entries--;
545
546         if (pstats.rcv_entries > 0)
547                 pstats.rcv_entries--;
548
549  out:
550         spin_unlock_bh(&rcv_lists_lock);
551
552         /* schedule the receiver item for deletion */
553         if (r)
554                 call_rcu(&r->rcu, can_rx_delete);
555
556         return ret;
557 }
558 EXPORT_SYMBOL(can_rx_unregister);
559
560 static inline void deliver(struct sk_buff *skb, struct receiver *r)
561 {
562         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
563
564         DBG("skbuff %p cloned to %p\n", skb, clone);
565         if (clone) {
566                 r->func(clone, r->data);
567                 r->matches++;
568         }
569 }
570
571 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
572 {
573         struct receiver *r;
574         struct hlist_node *n;
575         int matches = 0;
576         struct can_frame *cf = (struct can_frame*)skb->data;
577         canid_t can_id = cf->can_id;
578
579         if (d->entries == 0)
580                 return 0;
581
582         if (can_id & CAN_ERR_FLAG) {
583                 /* check for error frame entries only */
584                 hlist_for_each_entry_rcu(r, n, &d->rx_err, list) {
585                         if (can_id & r->mask) {
586                                 DBG("match on rx_err skbuff %p\n", skb);
587                                 deliver(skb, r);
588                                 matches++;
589                         }
590                 }
591                 return matches;
592         }
593
594         /* check for unfiltered entries */
595         hlist_for_each_entry_rcu(r, n, &d->rx_all, list) {
596                 DBG("match on rx_all skbuff %p\n", skb);
597                 deliver(skb, r);
598                 matches++;
599         }
600
601         /* check for can_id/mask entries */
602         hlist_for_each_entry_rcu(r, n, &d->rx_fil, list) {
603                 if ((can_id & r->mask) == r->can_id) {
604                         DBG("match on rx_fil skbuff %p\n", skb);
605                         deliver(skb, r);
606                         matches++;
607                 }
608         }
609
610         /* check for inverted can_id/mask entries */
611         hlist_for_each_entry_rcu(r, n, &d->rx_inv, list) {
612                 if ((can_id & r->mask) != r->can_id) {
613                         DBG("match on rx_inv skbuff %p\n", skb);
614                         deliver(skb, r);
615                         matches++;
616                 }
617         }
618
619         /* check CAN_ID specific entries */
620         if (can_id & CAN_EFF_FLAG) {
621                 hlist_for_each_entry_rcu(r, n, &d->rx_eff, list) {
622                         if (r->can_id == can_id) {
623                                 DBG("match on rx_eff skbuff %p\n", skb);
624                                 deliver(skb, r);
625                                 matches++;
626                         }
627                 }
628         } else {
629                 can_id &= CAN_SFF_MASK;
630                 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
631                         DBG("match on rx_sff skbuff %p\n", skb);
632                         deliver(skb, r);
633                         matches++;
634                 }
635         }
636
637         return matches;
638 }
639
640 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
641 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
642                    struct packet_type *pt, struct net_device *orig_dev)
643 #else
644 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
645                    struct packet_type *pt)
646 #endif
647 {
648         struct dev_rcv_lists *d;
649         int matches;
650
651         DBG("received skbuff on device %s, ptype %04x\n",
652             dev->name, ntohs(pt->type));
653         DBG_SKB(skb);
654         DBG_FRAME("af_can: can_rcv: received CAN frame",
655                   (struct can_frame *)skb->data);
656
657         if (dev->type != ARPHRD_CAN) {
658                 kfree_skb(skb);
659                 return 0;
660         }
661
662         /* update statistics */
663         spin_lock(&stats_lock);
664         stats.rx_frames++;
665         stats.rx_frames_delta++;
666         spin_unlock(&stats_lock);
667
668         rcu_read_lock();
669
670         /* deliver the packet to sockets listening on all devices */
671         matches = can_rcv_filter(&rx_alldev_list, skb);
672
673         /* find receive list for this device */
674         d = find_dev_rcv_lists(dev);
675         if (d)
676                 matches += can_rcv_filter(d, skb);
677
678         rcu_read_unlock();
679
680         /* free the skbuff allocated by the netdevice driver */
681         DBG("freeing skbuff %p\n", skb);
682         kfree_skb(skb);
683
684         if (matches > 0) {
685                 spin_lock(&stats_lock);
686                 stats.matches++;
687                 stats.matches_delta++;
688                 spin_unlock(&stats_lock);
689         }
690
691         return 0;
692 }
693
694 /*
695  * af_can debugging stuff
696  */
697
698 #ifdef CONFIG_CAN_DEBUG_CORE
699
700 #define DBG_BSIZE 1024
701
702 /**
703  * can_debug_cframe - print CAN frame
704  * @msg: pointer to message printed before the given CAN frame
705  * @cf: pointer to CAN frame
706  */
707 void can_debug_cframe(const char *msg, struct can_frame *cf, ...)
708 {
709         va_list ap;
710         int len;
711         int dlc, i;
712         char *buf;
713
714         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
715         if (!buf)
716                 return;
717
718         len = sprintf(buf, KERN_DEBUG);
719         va_start(ap, cf);
720         len += snprintf(buf + len, DBG_BSIZE - 64, msg, ap);
721         buf[len++] = ':';
722         buf[len++] = ' ';
723         va_end(ap);
724
725         dlc = cf->can_dlc;
726         if (dlc > 8)
727                 dlc = 8;
728
729         if (cf->can_id & CAN_EFF_FLAG)
730                 len += sprintf(buf + len, "<%08X> [%X] ",
731                                cf->can_id & CAN_EFF_MASK, dlc);
732         else
733                 len += sprintf(buf + len, "<%03X> [%X] ",
734                                cf->can_id & CAN_SFF_MASK, dlc);
735
736         for (i = 0; i < dlc; i++)
737                 len += sprintf(buf + len, "%02X ", cf->data[i]);
738
739         if (cf->can_id & CAN_RTR_FLAG)
740                 len += sprintf(buf + len, "(RTR)");
741
742         buf[len++] = '\n';
743         buf[len]   = '\0';
744         printk(buf);
745         kfree(buf);
746 }
747 EXPORT_SYMBOL(can_debug_cframe);
748
749 /**
750  * can_debug_skb - print socket buffer content to kernel log
751  * @skb: pointer to socket buffer
752  */
753 void can_debug_skb(struct sk_buff *skb)
754 {
755         int len, nbytes, i;
756         char *buf;
757
758         buf = kmalloc(DBG_BSIZE, GFP_ATOMIC);
759         if (!buf)
760                 return;
761
762         len = sprintf(buf,
763                       KERN_DEBUG "  skbuff at %p, dev: %d, proto: %04x\n"
764                       KERN_DEBUG "  users: %d, dataref: %d, nr_frags: %d, "
765                       "h,d,t,e,l: %p %+d %+d %+d, %d",
766                       skb, skb->dev ? skb->dev->ifindex : -1,
767                       ntohs(skb->protocol),
768                       atomic_read(&skb->users),
769                       atomic_read(&(skb_shinfo(skb)->dataref)),
770                       skb_shinfo(skb)->nr_frags,
771                       skb->head, skb->data - skb->head,
772                       skb->tail - skb->head, skb->end - skb->head, skb->len);
773         nbytes = skb->end - skb->head;
774         for (i = 0; i < nbytes; i++) {
775                 if (i % 16 == 0)
776                         len += sprintf(buf + len, "\n" KERN_DEBUG "  ");
777                 if (len < DBG_BSIZE - 16) {
778                         len += sprintf(buf + len, " %02x", skb->head[i]);
779                 } else {
780                         len += sprintf(buf + len, "...");
781                         break;
782                 }
783         }
784         buf[len++] = '\n';
785         buf[len]   = '\0';
786         printk(buf);
787         kfree(buf);
788 }
789 EXPORT_SYMBOL(can_debug_skb);
790
791 #endif
792
793 /*
794  * af_can protocol functions
795  */
796
797 /**
798  * can_proto_register - register CAN transport protocol
799  * @cp: pointer to CAN protocol structure
800  *
801  * Return:
802  *  0 on success
803  *  -EINVAL invalid (out of range) protocol number
804  *  -EBUSY  protocol already in use
805  *  -ENOBUF if proto_register() fails
806  */
807 int can_proto_register(struct can_proto *cp)
808 {
809         int proto = cp->protocol;
810         int err = 0;
811
812         if (proto < 0 || proto >= CAN_NPROTO) {
813                 printk(KERN_ERR "can: protocol number %d out "
814                        "of range\n", proto);
815                 return -EINVAL;
816         }
817         if (proto_tab[proto]) {
818                 printk(KERN_ERR "can: protocol %d already "
819                        "registered\n", proto);
820                 return -EBUSY;
821         }
822
823 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
824         err = proto_register(cp->prot, 0);
825         if (err < 0)
826                 return err;
827 #endif
828
829         proto_tab[proto] = cp;
830
831         /* use generic ioctl function if the module doesn't bring its own */
832         if (!cp->ops->ioctl)
833                 cp->ops->ioctl = can_ioctl;
834
835         return err;
836 }
837 EXPORT_SYMBOL(can_proto_register);
838
839 /**
840  * can_proto_unregister - unregister CAN transport protocol
841  * @cp: pointer to CAN protocol structure
842  *
843  * Return:
844  *  0 on success
845  *  -ESRCH protocol number was not registered
846  */
847 int can_proto_unregister(struct can_proto *cp)
848 {
849         int proto = cp->protocol;
850
851         if (!proto_tab[proto]) {
852                 printk(KERN_ERR "can: protocol %d is not registered\n", proto);
853                 return -ESRCH;
854         }
855 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
856         proto_unregister(cp->prot);
857 #endif
858         proto_tab[proto] = NULL;
859
860         return 0;
861 }
862 EXPORT_SYMBOL(can_proto_unregister);
863
864 /**
865  * can_dev_register - subscribe notifier for CAN device status changes
866  * @dev: pointer to netdevice
867  * @func: callback function on status change
868  * @data: returned parameter for callback function
869  *
870  * Description:
871  *  Invokes the callback function with the status 'msg' and the given
872  *  parameter 'data' on a status change of the given CAN network device.
873  *
874  * Return:
875  *  0 on success
876  *  -ENOMEM on missing mem to create subscription entry
877  *  -ENODEV unknown device
878  */
879 int can_dev_register(struct net_device *dev,
880                      void (*func)(unsigned long msg, void *), void *data)
881 {
882         struct notifier *n;
883
884         DBG("called for %s\n", dev->name);
885
886         if (!dev || dev->type != ARPHRD_CAN)
887                 return -ENODEV;
888
889         n = kmalloc(sizeof(*n), GFP_KERNEL);
890         if (!n)
891                 return -ENOMEM;
892
893         n->dev  = dev;
894         n->func = func;
895         n->data = data;
896
897         write_lock(&notifier_lock);
898         list_add(&n->list, &notifier_list);
899         write_unlock(&notifier_lock);
900
901         return 0;
902 }
903 EXPORT_SYMBOL(can_dev_register);
904
905 /**
906  * can_dev_unregister - unsubscribe notifier for CAN device status changes
907  * @dev: pointer to netdevice
908  * @func: callback function on filter match
909  * @data: returned parameter for callback function
910  *
911  * Description:
912  *  Removes subscription entry depending on given (subscription) values.
913  *
914  * Return:
915  *  0 on success
916  *  -EINVAL on missing subscription entry
917  */
918 int can_dev_unregister(struct net_device *dev,
919                        void (*func)(unsigned long msg, void *), void *data)
920 {
921         struct notifier *n, *next;
922         int ret = -EINVAL;
923
924         DBG("called for %s\n", dev->name);
925
926         write_lock(&notifier_lock);
927         list_for_each_entry_safe(n, next, &notifier_list, list) {
928                 if (n->dev == dev && n->func == func && n->data == data) {
929                         list_del(&n->list);
930                         kfree(n);
931                         ret = 0;
932                         break;
933                 }
934         }
935         write_unlock(&notifier_lock);
936
937         return ret;
938 }
939 EXPORT_SYMBOL(can_dev_unregister);
940
941 static int can_notifier(struct notifier_block *nb,
942                         unsigned long msg, void *data)
943 {
944         struct net_device *dev = (struct net_device *)data;
945         struct notifier *n;
946         struct dev_rcv_lists *d;
947         int i;
948
949         DBG("called for %s, msg = %lu\n", dev->name, msg);
950
951         if (dev->type != ARPHRD_CAN)
952                 return NOTIFY_DONE;
953
954         switch (msg) {
955
956         case NETDEV_REGISTER:
957
958                 /*
959                  * create new dev_rcv_lists for this device
960                  *
961                  * N.B. zeroing the struct is the correct initialization
962                  * for the embedded hlist_head structs.
963                  * Another list type, e.g. list_head, would require
964                  * explicit initialization.
965                  */
966
967                 DBG("creating new dev_rcv_lists for %s\n", dev->name);
968
969                 d = kzalloc(sizeof(*d),
970                             in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
971                 if (!d) {
972                         printk(KERN_ERR "can: allocation of receive "
973                                "list failed\n");
974                         return NOTIFY_DONE;
975                 }
976                 d->dev = dev;
977
978                 spin_lock_bh(&rcv_lists_lock);
979                 hlist_add_head_rcu(&d->list, &rx_dev_list);
980                 spin_unlock_bh(&rcv_lists_lock);
981
982                 break;
983
984         case NETDEV_UNREGISTER:
985                 spin_lock_bh(&rcv_lists_lock);
986
987                 d = find_dev_rcv_lists(dev);
988                 if (d) {
989                         hlist_del_rcu(&d->list);
990
991                         /* remove all receivers hooked at this netdevice */
992                         can_rx_delete_all(&d->rx_err);
993                         can_rx_delete_all(&d->rx_all);
994                         can_rx_delete_all(&d->rx_fil);
995                         can_rx_delete_all(&d->rx_inv);
996                         can_rx_delete_all(&d->rx_eff);
997                         for (i = 0; i < 2048; i++)
998                                 can_rx_delete_all(&d->rx_sff[i]);
999                 } else
1000                         printk(KERN_ERR "can: notifier: receive list not "
1001                                "found for dev %s\n", dev->name);
1002
1003                 spin_unlock_bh(&rcv_lists_lock);
1004
1005                 if (d)
1006                         call_rcu(&d->rcu, can_rcv_lists_delete);
1007
1008                 break;
1009         }
1010
1011         read_lock(&notifier_lock);
1012         list_for_each_entry(n, &notifier_list, list) {
1013                 if (n->dev == dev)
1014                         n->func(msg, n->data);
1015         }
1016         read_unlock(&notifier_lock);
1017
1018         return NOTIFY_DONE;
1019 }
1020
1021 /*
1022  * af_can module init/exit functions
1023  */
1024
1025 static struct packet_type can_packet = {
1026         .type = __constant_htons(ETH_P_CAN),
1027         .dev  = NULL,
1028         .func = can_rcv,
1029 };
1030
1031 static struct net_proto_family can_family_ops = {
1032         .family = PF_CAN,
1033         .create = can_create,
1034         .owner  = THIS_MODULE,
1035 };
1036
1037 /* notifier block for netdevice event */
1038 static struct notifier_block can_netdev_notifier = {
1039         .notifier_call = can_notifier,
1040 };
1041
1042 static __init int can_init(void)
1043 {
1044         printk(banner);
1045
1046         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1047                                       0, 0, NULL, NULL);
1048         if (!rcv_cache)
1049                 return -ENOMEM;
1050
1051         /*
1052          * Insert struct dev_rcv_lists for reception on all devices.
1053          * This struct is zero initialized which is correct for the 
1054          * embedded hlist heads, the dev pointer, and the entries counter.
1055          */
1056
1057         spin_lock_bh(&rcv_lists_lock);
1058         hlist_add_head_rcu(&rx_alldev_list.list, &rx_dev_list);
1059         spin_unlock_bh(&rcv_lists_lock);
1060
1061         if (stats_timer) {
1062                 /* the statistics are updated every second (timer triggered) */
1063                 init_timer(&stattimer);
1064                 stattimer.function = can_stat_update;
1065                 stattimer.data = 0;
1066                 /* update every second */
1067                 stattimer.expires = jiffies + HZ;
1068                 /* start statistics timer */
1069                 add_timer(&stattimer);
1070         } else
1071                 stattimer.function = NULL;
1072
1073         /* procfs init */
1074         can_init_proc();
1075
1076         /* protocol register */
1077         sock_register(&can_family_ops);
1078         register_netdevice_notifier(&can_netdev_notifier);
1079         dev_add_pack(&can_packet);
1080
1081         return 0;
1082 }
1083
1084 static __exit void can_exit(void)
1085 {
1086         struct dev_rcv_lists *d;
1087         struct hlist_node *n, *next;
1088
1089         if (stats_timer)
1090                 del_timer(&stattimer);
1091
1092         /* procfs remove */
1093         can_remove_proc();
1094
1095         /* protocol unregister */
1096         dev_remove_pack(&can_packet);
1097         unregister_netdevice_notifier(&can_netdev_notifier);
1098         sock_unregister(PF_CAN);
1099
1100         /* remove rx_dev_list */
1101         spin_lock_bh(&rcv_lists_lock);
1102         hlist_del(&rx_alldev_list.list);
1103         hlist_for_each_entry_safe(d, n, next, &rx_dev_list, list) {
1104                 hlist_del(&d->list);
1105                 kfree(d);
1106         }
1107         spin_unlock_bh(&rcv_lists_lock);
1108
1109         kmem_cache_destroy(rcv_cache);
1110 }
1111
1112 module_init(can_init);
1113 module_exit(can_exit);