]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/af_can.c
Add modifiers for sampling-point and sjw to can_if start/stop script.
[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 and the following disclaimer.
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, in which case the provisions of the
23  * GPL apply INSTEAD OF those given above.
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/module.h>
46 #include <linux/version.h>
47 #include <linux/init.h>
48 #include <linux/kmod.h>
49 #include <linux/slab.h>
50 #include <linux/list.h>
51 #include <linux/spinlock.h>
52 #include <linux/rcupdate.h>
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
54 #include <linux/uaccess.h>
55 #else
56 #include <asm/uaccess.h>
57 #endif
58 #include <linux/net.h>
59 #include <linux/netdevice.h>
60 #include <linux/socket.h>
61 #include <linux/if_ether.h>
62 #include <linux/if_arp.h>
63 #include <linux/skbuff.h>
64 #include <socketcan/can.h>
65 #include <socketcan/can/core.h>
66 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
67 #include <net/net_namespace.h>
68 #endif
69 #include <net/sock.h>
70
71 #include "af_can.h"
72 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
73 #include "compat.h"
74 #endif
75
76 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
77 RCSID("$Id$");
78
79 static __initdata const char banner[] = KERN_INFO
80         "can: controller area network core (" CAN_VERSION_STRING ")\n";
81
82 MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
85               "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
86
87 MODULE_ALIAS_NETPROTO(PF_CAN);
88
89 static int stats_timer __read_mostly = 1;
90 module_param(stats_timer, int, S_IRUGO);
91 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
92
93 HLIST_HEAD(can_rx_dev_list);
94 static struct dev_rcv_lists can_rx_alldev_list;
95 static DEFINE_SPINLOCK(can_rcvlists_lock);
96
97 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
98 static struct kmem_cache *rcv_cache __read_mostly;
99 #else
100 static kmem_cache_t *rcv_cache;
101 #endif
102
103 /* table of registered CAN protocols */
104 static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
105 static DEFINE_MUTEX(proto_tab_lock);
106
107 struct timer_list can_stattimer;   /* timer for statistics update */
108 struct s_stats    can_stats;       /* packet statistics */
109 struct s_pstats   can_pstats;      /* receive list statistics */
110
111 /*
112  * af_can socket functions
113  */
114
115 int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
116 {
117         struct sock *sk = sock->sk;
118
119         switch (cmd) {
120
121         case SIOCGSTAMP:
122                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
123
124         default:
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
126                 return -ENOIOCTLCMD;
127 #else
128                 return dev_ioctl(cmd, (void __user *)arg);
129 #endif
130         }
131 }
132 EXPORT_SYMBOL(can_ioctl);
133
134 static void can_sock_destruct(struct sock *sk)
135 {
136         skb_queue_purge(&sk->sk_receive_queue);
137 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
138         if (sk->sk_protinfo)
139                 kfree(sk->sk_protinfo);
140 #endif
141 }
142
143 static const struct can_proto *can_get_proto(int protocol)
144 {
145         const struct can_proto *cp;
146
147         rcu_read_lock();
148         cp = rcu_dereference(proto_tab[protocol]);
149 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
150         if (cp && !try_module_get(cp->prot->owner))
151                 cp = NULL;
152 #else
153         if (cp && !try_module_get(cp->owner))
154                 cp = NULL;
155 #endif
156         rcu_read_unlock();
157
158         return cp;
159 }
160
161 static inline void can_put_proto(const struct can_proto *cp)
162 {
163 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
164         module_put(cp->prot->owner);
165 #else
166         module_put(cp->owner);
167 #endif
168 }
169
170 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
171 static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
172 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
173 static int can_create(struct net *net, struct socket *sock, int protocol)
174 #else
175 static int can_create(struct socket *sock, int protocol)
176 #endif
177 {
178         struct sock *sk;
179         const struct can_proto *cp;
180         int err = 0;
181
182         sock->state = SS_UNCONNECTED;
183
184         if (protocol < 0 || protocol >= CAN_NPROTO)
185                 return -EINVAL;
186
187 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
188         if (net != &init_net)
189                 return -EAFNOSUPPORT;
190 #endif
191
192         cp = can_get_proto(protocol);
193
194 #ifdef CONFIG_MODULES
195         if (!cp) {
196                 /* try to load protocol module if kernel is modular */
197
198                 err = request_module("can-proto-%d", protocol);
199
200                 /*
201                  * In case of error we only print a message but don't
202                  * return the error code immediately.  Below we will
203                  * return -EPROTONOSUPPORT
204                  */
205                 if (err && printk_ratelimit())
206                         printk(KERN_ERR "can: request_module "
207                                "(can-proto-%d) failed.\n", protocol);
208
209                 cp = can_get_proto(protocol);
210         }
211 #endif
212
213         /* check for available protocol and correct usage */
214
215         if (!cp)
216                 return -EPROTONOSUPPORT;
217
218         if (cp->type != sock->type) {
219                 err = -EPROTOTYPE;
220                 goto errout;
221         }
222
223 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
224         if (cp->capability >= 0 && !capable(cp->capability)) {
225                 err = -EPERM;
226                 goto errout;
227         }
228 #endif
229         sock->ops = cp->ops;
230
231 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
232         sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
233 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
234         sk = sk_alloc(PF_CAN, GFP_KERNEL, cp->prot, 1);
235 #else
236         sk = sk_alloc(PF_CAN, GFP_KERNEL, 1, 0);
237 #endif
238         if (!sk) {
239                 err = -ENOMEM;
240                 goto errout;
241         }
242
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
244         if (cp->obj_size) {
245                 sk->sk_protinfo = kmalloc(cp->obj_size, GFP_KERNEL);
246                 if (!sk->sk_protinfo) {
247                         sk_free(sk);
248                         err = -ENOMEM;
249                         goto errout;
250                 }
251         }
252         sk_set_owner(sk, proto_tab[protocol]->owner);
253 #endif
254
255         sock_init_data(sock, sk);
256         sk->sk_destruct = can_sock_destruct;
257
258 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
259         if (sk->sk_prot->init)
260                 err = sk->sk_prot->init(sk);
261 #else
262         if (cp->init)
263                 err = cp->init(sk);
264 #endif
265
266         if (err) {
267                 /* release sk on errors */
268                 sock_orphan(sk);
269                 sock_put(sk);
270         }
271
272  errout:
273         can_put_proto(cp);
274         return err;
275 }
276
277 /*
278  * af_can tx path
279  */
280
281 /**
282  * can_send - transmit a CAN frame (optional with local loopback)
283  * @skb: pointer to socket buffer with CAN frame in data section
284  * @loop: loopback for listeners on local CAN sockets (recommended default!)
285  *
286  * Due to the loopback this routine must not be called from hardirq context.
287  *
288  * Return:
289  *  0 on success
290  *  -ENETDOWN when the selected interface is down
291  *  -ENOBUFS on full driver queue (see net_xmit_errno())
292  *  -ENOMEM when local loopback failed at calling skb_clone()
293  *  -EPERM when trying to send on a non-CAN interface
294  *  -EINVAL when the skb->data does not contain a valid CAN frame
295  */
296 int can_send(struct sk_buff *skb, int loop)
297 {
298         struct sk_buff *newskb = NULL;
299         struct can_frame *cf = (struct can_frame *)skb->data;
300         int err;
301
302         if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
303                 kfree_skb(skb);
304                 return -EINVAL;
305         }
306
307         if (skb->dev->type != ARPHRD_CAN) {
308                 kfree_skb(skb);
309                 return -EPERM;
310         }
311
312         if (!(skb->dev->flags & IFF_UP)) {
313                 kfree_skb(skb);
314                 return -ENETDOWN;
315         }
316
317         skb->protocol = htons(ETH_P_CAN);
318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
319         skb_reset_network_header(skb);
320         skb_reset_transport_header(skb);
321 #else
322         skb->nh.raw = skb->data;
323         skb->h.raw  = skb->data;
324 #endif
325
326         if (loop) {
327                 /* local loopback of sent CAN frames */
328
329                 /* indication for the CAN driver: do loopback */
330                 skb->pkt_type = PACKET_LOOPBACK;
331
332                 /*
333                  * The reference to the originating sock may be required
334                  * by the receiving socket to check whether the frame is
335                  * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
336                  * Therefore we have to ensure that skb->sk remains the
337                  * reference to the originating sock by restoring skb->sk
338                  * after each skb_clone() or skb_orphan() usage.
339                  */
340
341 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
342 #define IFF_ECHO IFF_LOOPBACK
343 #endif
344                 if (!(skb->dev->flags & IFF_ECHO)) {
345                         /*
346                          * If the interface is not capable to do loopback
347                          * itself, we do it here.
348                          */
349                         newskb = skb_clone(skb, GFP_ATOMIC);
350                         if (!newskb) {
351                                 kfree_skb(skb);
352                                 return -ENOMEM;
353                         }
354
355                         newskb->sk = skb->sk;
356                         newskb->ip_summed = CHECKSUM_UNNECESSARY;
357                         newskb->pkt_type = PACKET_BROADCAST;
358                 }
359         } else {
360                 /* indication for the CAN driver: no loopback required */
361                 skb->pkt_type = PACKET_HOST;
362         }
363
364         /* send to netdevice */
365         err = dev_queue_xmit(skb);
366         if (err > 0)
367                 err = net_xmit_errno(err);
368
369         if (err) {
370 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
371                 /* kfree_skb() does not check for !NULL on older kernels */
372                 if (newskb)
373                         kfree_skb(newskb);
374 #else
375                 kfree_skb(newskb);
376 #endif
377                 return err;
378         }
379
380         if (newskb)
381                 netif_rx_ni(newskb);
382
383         /* update statistics */
384         can_stats.tx_frames++;
385         can_stats.tx_frames_delta++;
386
387         return 0;
388 }
389 EXPORT_SYMBOL(can_send);
390
391 /*
392  * af_can rx path
393  */
394
395 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
396 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
397 {
398         /*
399          * find receive list for this device
400          *
401          * Since 2.6.26 a new "midlevel private" ml_priv pointer has been
402          * introduced in struct net_device. We use this pointer to omit the
403          * linear walk through the can_rx_dev_list. A similar speedup has been
404          * queued for 2.6.34 mainline but using the new netdev_rcu lists.
405          * Therefore the can_rx_dev_list is still needed (e.g. in proc.c)
406          */
407
408         /* dev == NULL is the indicator for the 'all' filterlist */
409         if (!dev)
410                 return &can_rx_alldev_list;
411         else
412                 return (struct dev_rcv_lists *)dev->ml_priv;
413 }
414 #else
415 static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
416 {
417         struct dev_rcv_lists *d = NULL;
418         struct hlist_node *n;
419
420         /*
421          * find receive list for this device
422          *
423          * The hlist_for_each_entry*() macros curse through the list
424          * using the pointer variable n and set d to the containing
425          * struct in each list iteration.  Therefore, after list
426          * iteration, d is unmodified when the list is empty, and it
427          * points to last list element, when the list is non-empty
428          * but no match in the loop body is found.  I.e. d is *not*
429          * NULL when no match is found.  We can, however, use the
430          * cursor variable n to decide if a match was found.
431          */
432
433         hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
434                 if (d->dev == dev)
435                         break;
436         }
437
438         return n ? d : NULL;
439 }
440 #endif
441
442 /**
443  * find_rcv_list - determine optimal filterlist inside device filter struct
444  * @can_id: pointer to CAN identifier of a given can_filter
445  * @mask: pointer to CAN mask of a given can_filter
446  * @d: pointer to the device filter struct
447  *
448  * Description:
449  *  Returns the optimal filterlist to reduce the filter handling in the
450  *  receive path. This function is called by service functions that need
451  *  to register or unregister a can_filter in the filter lists.
452  *
453  *  A filter matches in general, when
454  *
455  *          <received_can_id> & mask == can_id & mask
456  *
457  *  so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
458  *  relevant bits for the filter.
459  *
460  *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
461  *  filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
462  *  there is a special filterlist and a special rx path filter handling.
463  *
464  * Return:
465  *  Pointer to optimal filterlist for the given can_id/mask pair.
466  *  Constistency checked mask.
467  *  Reduced can_id to have a preprocessed filter compare value.
468  */
469 static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
470                                         struct dev_rcv_lists *d)
471 {
472         canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
473
474         /* filter for error frames in extra filterlist */
475         if (*mask & CAN_ERR_FLAG) {
476                 /* clear CAN_ERR_FLAG in filter entry */
477                 *mask &= CAN_ERR_MASK;
478                 return &d->rx[RX_ERR];
479         }
480
481         /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
482
483 #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
484
485         /* ensure valid values in can_mask for 'SFF only' frame filtering */
486         if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
487                 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
488
489         /* reduce condition testing at receive time */
490         *can_id &= *mask;
491
492         /* inverse can_id/can_mask filter */
493         if (inv)
494                 return &d->rx[RX_INV];
495
496         /* mask == 0 => no condition testing at receive time */
497         if (!(*mask))
498                 return &d->rx[RX_ALL];
499
500         /* extra filterlists for the subscription of a single non-RTR can_id */
501         if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
502             && !(*can_id & CAN_RTR_FLAG)) {
503
504                 if (*can_id & CAN_EFF_FLAG) {
505                         if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
506                                 /* RFC: a future use-case for hash-tables? */
507                                 return &d->rx[RX_EFF];
508                         }
509                 } else {
510                         if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
511                                 return &d->rx_sff[*can_id];
512                 }
513         }
514
515         /* default: filter via can_id/can_mask */
516         return &d->rx[RX_FIL];
517 }
518
519 /**
520  * can_rx_register - subscribe CAN frames from a specific interface
521  * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
522  * @can_id: CAN identifier (see description)
523  * @mask: CAN mask (see description)
524  * @func: callback function on filter match
525  * @data: returned parameter for callback function
526  * @ident: string for calling module indentification
527  *
528  * Description:
529  *  Invokes the callback function with the received sk_buff and the given
530  *  parameter 'data' on a matching receive filter. A filter matches, when
531  *
532  *          <received_can_id> & mask == can_id & mask
533  *
534  *  The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
535  *  filter for error frames (CAN_ERR_FLAG bit set in mask).
536  *
537  *  The provided pointer to the sk_buff is guaranteed to be valid as long as
538  *  the callback function is running. The callback function must *not* free
539  *  the given sk_buff while processing it's task. When the given sk_buff is
540  *  needed after the end of the callback function it must be cloned inside
541  *  the callback function with skb_clone().
542  *
543  * Return:
544  *  0 on success
545  *  -ENOMEM on missing cache mem to create subscription entry
546  *  -ENODEV unknown device
547  */
548 int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
549                     void (*func)(struct sk_buff *, void *), void *data,
550                     char *ident)
551 {
552         struct receiver *r;
553         struct hlist_head *rl;
554         struct dev_rcv_lists *d;
555         int err = 0;
556
557         /* insert new receiver  (dev,canid,mask) -> (func,data) */
558
559 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
560         if (dev && dev->type != ARPHRD_CAN)
561                 return -ENODEV;
562 #endif
563
564         r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
565         if (!r)
566                 return -ENOMEM;
567
568         spin_lock(&can_rcvlists_lock);
569
570         d = find_dev_rcv_lists(dev);
571         if (d) {
572                 rl = find_rcv_list(&can_id, &mask, d);
573
574                 r->can_id  = can_id;
575                 r->mask    = mask;
576                 r->matches = 0;
577                 r->func    = func;
578                 r->data    = data;
579                 r->ident   = ident;
580
581                 hlist_add_head_rcu(&r->list, rl);
582                 d->entries++;
583
584                 can_pstats.rcv_entries++;
585                 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
586                         can_pstats.rcv_entries_max = can_pstats.rcv_entries;
587         } else {
588                 kmem_cache_free(rcv_cache, r);
589                 err = -ENODEV;
590         }
591
592         spin_unlock(&can_rcvlists_lock);
593
594         return err;
595 }
596 EXPORT_SYMBOL(can_rx_register);
597
598 /*
599  * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
600  */
601 static void can_rx_delete_device(struct rcu_head *rp)
602 {
603         struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
604
605         kfree(d);
606 }
607
608 /*
609  * can_rx_delete_receiver - rcu callback for single receiver entry removal
610  */
611 static void can_rx_delete_receiver(struct rcu_head *rp)
612 {
613         struct receiver *r = container_of(rp, struct receiver, rcu);
614
615         kmem_cache_free(rcv_cache, r);
616 }
617
618 /**
619  * can_rx_unregister - unsubscribe CAN frames from a specific interface
620  * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
621  * @can_id: CAN identifier
622  * @mask: CAN mask
623  * @func: callback function on filter match
624  * @data: returned parameter for callback function
625  *
626  * Description:
627  *  Removes subscription entry depending on given (subscription) values.
628  */
629 void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
630                        void (*func)(struct sk_buff *, void *), void *data)
631 {
632         struct receiver *r = NULL;
633         struct hlist_head *rl;
634         struct hlist_node *next;
635         struct dev_rcv_lists *d;
636
637 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
638         if (dev && dev->type != ARPHRD_CAN)
639                 return;
640 #endif
641
642         spin_lock(&can_rcvlists_lock);
643
644         d = find_dev_rcv_lists(dev);
645         if (!d) {
646                 printk(KERN_ERR "BUG: receive list not found for "
647                        "dev %s, id %03X, mask %03X\n",
648                        DNAME(dev), can_id, mask);
649                 goto out;
650         }
651
652         rl = find_rcv_list(&can_id, &mask, d);
653
654         /*
655          * Search the receiver list for the item to delete.  This should
656          * exist, since no receiver may be unregistered that hasn't
657          * been registered before.
658          */
659
660         hlist_for_each_entry_rcu(r, next, rl, list) {
661                 if (r->can_id == can_id && r->mask == mask
662                     && r->func == func && r->data == data)
663                         break;
664         }
665
666         /*
667          * Check for bugs in CAN protocol implementations:
668          * If no matching list item was found, the list cursor variable next
669          * will be NULL, while r will point to the last item of the list.
670          */
671
672         if (!next) {
673                 printk(KERN_ERR "BUG: receive list entry not found for "
674                        "dev %s, id %03X, mask %03X\n",
675                        DNAME(dev), can_id, mask);
676                 r = NULL;
677                 d = NULL;
678                 goto out;
679         }
680
681         hlist_del_rcu(&r->list);
682         d->entries--;
683
684         if (can_pstats.rcv_entries > 0)
685                 can_pstats.rcv_entries--;
686
687         /* remove device structure requested by NETDEV_UNREGISTER */
688         if (d->remove_on_zero_entries && !d->entries) {
689 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
690                 dev->ml_priv = NULL;
691 #endif
692                 hlist_del_rcu(&d->list);
693         } else
694                 d = NULL;
695
696  out:
697         spin_unlock(&can_rcvlists_lock);
698
699         /* schedule the receiver item for deletion */
700         if (r)
701                 call_rcu(&r->rcu, can_rx_delete_receiver);
702
703         /* schedule the device structure for deletion */
704         if (d)
705                 call_rcu(&d->rcu, can_rx_delete_device);
706 }
707 EXPORT_SYMBOL(can_rx_unregister);
708
709 static inline void deliver(struct sk_buff *skb, struct receiver *r)
710 {
711         r->func(skb, r->data);
712         r->matches++;
713 }
714
715 static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
716 {
717         struct receiver *r;
718         struct hlist_node *n;
719         int matches = 0;
720         struct can_frame *cf = (struct can_frame *)skb->data;
721         canid_t can_id = cf->can_id;
722
723         if (d->entries == 0)
724                 return 0;
725
726         if (can_id & CAN_ERR_FLAG) {
727                 /* check for error frame entries only */
728                 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
729                         if (can_id & r->mask) {
730                                 deliver(skb, r);
731                                 matches++;
732                         }
733                 }
734                 return matches;
735         }
736
737         /* check for unfiltered entries */
738         hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
739                 deliver(skb, r);
740                 matches++;
741         }
742
743         /* check for can_id/mask entries */
744         hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
745                 if ((can_id & r->mask) == r->can_id) {
746                         deliver(skb, r);
747                         matches++;
748                 }
749         }
750
751         /* check for inverted can_id/mask entries */
752         hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
753                 if ((can_id & r->mask) != r->can_id) {
754                         deliver(skb, r);
755                         matches++;
756                 }
757         }
758
759         /* check filterlists for single non-RTR can_ids */
760         if (can_id & CAN_RTR_FLAG)
761                 return matches;
762
763         if (can_id & CAN_EFF_FLAG) {
764                 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
765                         if (r->can_id == can_id) {
766                                 deliver(skb, r);
767                                 matches++;
768                         }
769                 }
770         } else {
771                 can_id &= CAN_SFF_MASK;
772                 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
773                         deliver(skb, r);
774                         matches++;
775                 }
776         }
777
778         return matches;
779 }
780
781 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
782 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
783                    struct packet_type *pt, struct net_device *orig_dev)
784 #else
785 static int can_rcv(struct sk_buff *skb, struct net_device *dev,
786                    struct packet_type *pt)
787 #endif
788 {
789         struct dev_rcv_lists *d;
790         struct can_frame *cf = (struct can_frame *)skb->data;
791         int matches;
792
793 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
794         if (!net_eq(dev_net(dev), &init_net))
795                 goto drop;
796 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
797         if (dev->nd_net != &init_net)
798                 goto drop;
799 #endif
800
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
802         if (WARN_ONCE(dev->type != ARPHRD_CAN ||
803                       skb->len != sizeof(struct can_frame) ||
804                       cf->can_dlc > 8,
805                       "PF_CAN: dropped non conform skbuf: "
806                       "dev type %d, len %d, can_dlc %d\n",
807                       dev->type, skb->len, cf->can_dlc))
808                 goto drop;
809 #else
810         BUG_ON(dev->type != ARPHRD_CAN ||
811                skb->len != sizeof(struct can_frame) ||
812                cf->can_dlc > 8);
813 #endif
814
815         /* update statistics */
816         can_stats.rx_frames++;
817         can_stats.rx_frames_delta++;
818
819         rcu_read_lock();
820
821         /* deliver the packet to sockets listening on all devices */
822         matches = can_rcv_filter(&can_rx_alldev_list, skb);
823
824         /* find receive list for this device */
825         d = find_dev_rcv_lists(dev);
826         if (d)
827                 matches += can_rcv_filter(d, skb);
828
829         rcu_read_unlock();
830
831         /* consume the skbuff allocated by the netdevice driver */
832 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
833         consume_skb(skb);
834 #else
835         kfree_skb(skb);
836 #endif
837
838         if (matches > 0) {
839                 can_stats.matches++;
840                 can_stats.matches_delta++;
841         }
842
843         return NET_RX_SUCCESS;
844
845 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
846 drop:
847         kfree_skb(skb);
848         return NET_RX_DROP;
849 #endif
850 }
851
852 /*
853  * af_can protocol functions
854  */
855
856 /**
857  * can_proto_register - register CAN transport protocol
858  * @cp: pointer to CAN protocol structure
859  *
860  * Return:
861  *  0 on success
862  *  -EINVAL invalid (out of range) protocol number
863  *  -EBUSY  protocol already in use
864  *  -ENOBUF if proto_register() fails
865  */
866 int can_proto_register(const struct can_proto *cp)
867 {
868         int proto = cp->protocol;
869         int err = 0;
870
871         if (proto < 0 || proto >= CAN_NPROTO) {
872                 printk(KERN_ERR "can: protocol number %d out of range\n",
873                        proto);
874                 return -EINVAL;
875         }
876
877 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
878         err = proto_register(cp->prot, 0);
879         if (err < 0)
880                 return err;
881 #endif
882
883         mutex_lock(&proto_tab_lock);
884
885         if (proto_tab[proto]) {
886                 printk(KERN_ERR "can: protocol %d already registered\n",
887                        proto);
888                 err = -EBUSY;
889         } else
890                 rcu_assign_pointer(proto_tab[proto], cp);
891
892         mutex_unlock(&proto_tab_lock);
893
894 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
895         if (err < 0)
896                 proto_unregister(cp->prot);
897 #endif
898
899         return err;
900 }
901 EXPORT_SYMBOL(can_proto_register);
902
903 /**
904  * can_proto_unregister - unregister CAN transport protocol
905  * @cp: pointer to CAN protocol structure
906  */
907 void can_proto_unregister(const struct can_proto *cp)
908 {
909         int proto = cp->protocol;
910
911         mutex_lock(&proto_tab_lock);
912         BUG_ON(proto_tab[proto] != cp);
913         rcu_assign_pointer(proto_tab[proto], NULL);
914         mutex_unlock(&proto_tab_lock);
915
916         synchronize_rcu();
917
918 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
919         proto_unregister(cp->prot);
920 #endif
921 }
922 EXPORT_SYMBOL(can_proto_unregister);
923
924 /*
925  * af_can notifier to create/remove CAN netdevice specific structs
926  */
927 static int can_notifier(struct notifier_block *nb, unsigned long msg,
928                         void *data)
929 {
930         struct net_device *dev = (struct net_device *)data;
931         struct dev_rcv_lists *d;
932
933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
934         if (!net_eq(dev_net(dev), &init_net))
935                 return NOTIFY_DONE;
936 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
937         if (dev->nd_net != &init_net)
938                 return NOTIFY_DONE;
939 #endif
940
941         if (dev->type != ARPHRD_CAN)
942                 return NOTIFY_DONE;
943
944         switch (msg) {
945
946         case NETDEV_REGISTER:
947
948                 /*
949                  * create new dev_rcv_lists for this device
950                  *
951                  * N.B. zeroing the struct is the correct initialization
952                  * for the embedded hlist_head structs.
953                  * Another list type, e.g. list_head, would require
954                  * explicit initialization.
955                  */
956
957                 d = kzalloc(sizeof(*d), GFP_KERNEL);
958                 if (!d) {
959                         printk(KERN_ERR
960                                "can: allocation of receive list failed\n");
961                         return NOTIFY_DONE;
962                 }
963                 d->dev = dev;
964
965                 spin_lock(&can_rcvlists_lock);
966 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
967                 BUG_ON(dev->ml_priv);
968                 dev->ml_priv = d;
969 #endif
970                 hlist_add_head_rcu(&d->list, &can_rx_dev_list);
971                 spin_unlock(&can_rcvlists_lock);
972
973                 break;
974
975         case NETDEV_UNREGISTER:
976                 spin_lock(&can_rcvlists_lock);
977
978                 d = find_dev_rcv_lists(dev);
979                 if (d) {
980                         if (d->entries) {
981                                 d->remove_on_zero_entries = 1;
982                                 d = NULL;
983                         } else {
984 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
985                                 dev->ml_priv = NULL;
986 #endif
987                                 hlist_del_rcu(&d->list);
988                         }
989                 } else
990                         printk(KERN_ERR "can: notifier: receive list not "
991                                "found for dev %s\n", dev->name);
992
993                 spin_unlock(&can_rcvlists_lock);
994
995                 if (d)
996                         call_rcu(&d->rcu, can_rx_delete_device);
997
998                 break;
999         }
1000
1001         return NOTIFY_DONE;
1002 }
1003
1004 /*
1005  * af_can module init/exit functions
1006  */
1007
1008 static struct packet_type can_packet __read_mostly = {
1009 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
1010         .type = cpu_to_be16(ETH_P_CAN),
1011 #else
1012         .type = __constant_htons(ETH_P_CAN),
1013 #endif
1014         .dev  = NULL,
1015         .func = can_rcv,
1016 };
1017
1018 static struct net_proto_family can_family_ops __read_mostly = {
1019         .family = PF_CAN,
1020         .create = can_create,
1021         .owner  = THIS_MODULE,
1022 };
1023
1024 /* notifier block for netdevice event */
1025 static struct notifier_block can_netdev_notifier __read_mostly = {
1026         .notifier_call = can_notifier,
1027 };
1028
1029 static __init int can_init(void)
1030 {
1031         printk(banner);
1032
1033 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1034         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1035                                       0, 0, NULL);
1036 #else
1037         rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
1038                                       0, 0, NULL, NULL);
1039 #endif
1040         if (!rcv_cache)
1041                 return -ENOMEM;
1042
1043         /*
1044          * Insert can_rx_alldev_list for reception on all devices.
1045          * This struct is zero initialized which is correct for the
1046          * embedded hlist heads, the dev pointer, and the entries counter.
1047          */
1048
1049         spin_lock(&can_rcvlists_lock);
1050         hlist_add_head_rcu(&can_rx_alldev_list.list, &can_rx_dev_list);
1051         spin_unlock(&can_rcvlists_lock);
1052
1053         if (stats_timer) {
1054                 /* the statistics are updated every second (timer triggered) */
1055                 setup_timer(&can_stattimer, can_stat_update, 0);
1056                 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
1057         } else
1058                 can_stattimer.function = NULL;
1059
1060         can_init_proc();
1061
1062         /* protocol register */
1063         sock_register(&can_family_ops);
1064         register_netdevice_notifier(&can_netdev_notifier);
1065         dev_add_pack(&can_packet);
1066
1067         return 0;
1068 }
1069
1070 static __exit void can_exit(void)
1071 {
1072         struct dev_rcv_lists *d;
1073         struct hlist_node *n, *next;
1074
1075         if (stats_timer)
1076                 del_timer(&can_stattimer);
1077
1078         can_remove_proc();
1079
1080         /* protocol unregister */
1081         dev_remove_pack(&can_packet);
1082         unregister_netdevice_notifier(&can_netdev_notifier);
1083         sock_unregister(PF_CAN);
1084
1085         /* remove can_rx_dev_list */
1086         spin_lock(&can_rcvlists_lock);
1087         hlist_del(&can_rx_alldev_list.list);
1088         hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
1089                 hlist_del(&d->list);
1090                 BUG_ON(d->entries);
1091 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1092                 d->dev->ml_priv = NULL;
1093 #endif
1094                 kfree(d);
1095         }
1096         spin_unlock(&can_rcvlists_lock);
1097
1098 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1099         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1100 #endif
1101
1102         kmem_cache_destroy(rcv_cache);
1103 }
1104
1105 module_init(can_init);
1106 module_exit(can_exit);