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