blob: a8dcaa49675ae2fa848a732452673096d17d2803 [file] [log] [blame]
Oliver Hartkopp0d665482007-11-16 15:52:17 -08001/*
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/init.h>
47#include <linux/kmod.h>
48#include <linux/slab.h>
49#include <linux/list.h>
50#include <linux/spinlock.h>
51#include <linux/rcupdate.h>
52#include <linux/uaccess.h>
53#include <linux/net.h>
54#include <linux/netdevice.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/can.h>
60#include <linux/can/core.h>
61#include <net/net_namespace.h>
62#include <net/sock.h>
63
64#include "af_can.h"
65
66static __initdata const char banner[] = KERN_INFO
67 "can: controller area network core (" CAN_VERSION_STRING ")\n";
68
69MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
70MODULE_LICENSE("Dual BSD/GPL");
71MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
72 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
73
74MODULE_ALIAS_NETPROTO(PF_CAN);
75
76static int stats_timer __read_mostly = 1;
77module_param(stats_timer, int, S_IRUGO);
78MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
79
Oliver Hartkopp20dd3852009-12-25 06:47:47 +000080/* receive filters subscribed for 'all' CAN devices */
81struct dev_rcv_lists can_rx_alldev_list;
Oliver Hartkopp0d665482007-11-16 15:52:17 -080082static DEFINE_SPINLOCK(can_rcvlists_lock);
83
84static struct kmem_cache *rcv_cache __read_mostly;
85
86/* table of registered CAN protocols */
87static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +000088static DEFINE_MUTEX(proto_tab_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -080089
90struct timer_list can_stattimer; /* timer for statistics update */
91struct s_stats can_stats; /* packet statistics */
92struct s_pstats can_pstats; /* receive list statistics */
93
94/*
95 * af_can socket functions
96 */
97
Oliver Hartkopp53914b62011-03-22 08:27:25 +000098int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Oliver Hartkopp0d665482007-11-16 15:52:17 -080099{
100 struct sock *sk = sock->sk;
101
102 switch (cmd) {
103
104 case SIOCGSTAMP:
105 return sock_get_timestamp(sk, (struct timeval __user *)arg);
106
107 default:
108 return -ENOIOCTLCMD;
109 }
110}
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000111EXPORT_SYMBOL(can_ioctl);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800112
113static void can_sock_destruct(struct sock *sk)
114{
115 skb_queue_purge(&sk->sk_receive_queue);
116}
117
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000118static struct can_proto *can_try_module_get(int protocol)
119{
120 struct can_proto *cp;
121
122 rcu_read_lock();
123 cp = rcu_dereference(proto_tab[protocol]);
124 if (cp && !try_module_get(cp->prot->owner))
125 cp = NULL;
126 rcu_read_unlock();
127
128 return cp;
129}
130
Eric Paris3f378b62009-11-05 22:18:14 -0800131static int can_create(struct net *net, struct socket *sock, int protocol,
132 int kern)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800133{
134 struct sock *sk;
135 struct can_proto *cp;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800136 int err = 0;
137
138 sock->state = SS_UNCONNECTED;
139
140 if (protocol < 0 || protocol >= CAN_NPROTO)
141 return -EINVAL;
142
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800143 if (!net_eq(net, &init_net))
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800144 return -EAFNOSUPPORT;
145
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000146 cp = can_try_module_get(protocol);
147
Johannes Berg95a5afc2008-10-16 15:24:51 -0700148#ifdef CONFIG_MODULES
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000149 if (!cp) {
150 /* try to load protocol module if kernel is modular */
151
Urs Thuermann5423dd62008-02-07 18:04:21 -0800152 err = request_module("can-proto-%d", protocol);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800153
154 /*
155 * In case of error we only print a message but don't
156 * return the error code immediately. Below we will
157 * return -EPROTONOSUPPORT
158 */
Urs Thuermann5423dd62008-02-07 18:04:21 -0800159 if (err && printk_ratelimit())
160 printk(KERN_ERR "can: request_module "
161 "(can-proto-%d) failed.\n", protocol);
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000162
163 cp = can_try_module_get(protocol);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800164 }
Urs Thuermann5423dd62008-02-07 18:04:21 -0800165#endif
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800166
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800167 /* check for available protocol and correct usage */
168
169 if (!cp)
170 return -EPROTONOSUPPORT;
171
172 if (cp->type != sock->type) {
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000173 err = -EPROTOTYPE;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800174 goto errout;
175 }
176
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800177 sock->ops = cp->ops;
178
179 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
180 if (!sk) {
181 err = -ENOMEM;
182 goto errout;
183 }
184
185 sock_init_data(sock, sk);
186 sk->sk_destruct = can_sock_destruct;
187
188 if (sk->sk_prot->init)
189 err = sk->sk_prot->init(sk);
190
191 if (err) {
192 /* release sk on errors */
193 sock_orphan(sk);
194 sock_put(sk);
195 }
196
197 errout:
198 module_put(cp->prot->owner);
199 return err;
200}
201
202/*
203 * af_can tx path
204 */
205
206/**
207 * can_send - transmit a CAN frame (optional with local loopback)
208 * @skb: pointer to socket buffer with CAN frame in data section
209 * @loop: loopback for listeners on local CAN sockets (recommended default!)
210 *
Oliver Hartkopp481a8192009-09-15 01:31:34 -0700211 * Due to the loopback this routine must not be called from hardirq context.
212 *
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800213 * Return:
214 * 0 on success
215 * -ENETDOWN when the selected interface is down
216 * -ENOBUFS on full driver queue (see net_xmit_errno())
217 * -ENOMEM when local loopback failed at calling skb_clone()
218 * -EPERM when trying to send on a non-CAN interface
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700219 * -EINVAL when the skb->data does not contain a valid CAN frame
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800220 */
221int can_send(struct sk_buff *skb, int loop)
222{
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700223 struct sk_buff *newskb = NULL;
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700224 struct can_frame *cf = (struct can_frame *)skb->data;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800225 int err;
226
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700227 if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) {
228 kfree_skb(skb);
229 return -EINVAL;
230 }
231
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800232 if (skb->dev->type != ARPHRD_CAN) {
233 kfree_skb(skb);
234 return -EPERM;
235 }
236
237 if (!(skb->dev->flags & IFF_UP)) {
238 kfree_skb(skb);
239 return -ENETDOWN;
240 }
241
242 skb->protocol = htons(ETH_P_CAN);
243 skb_reset_network_header(skb);
244 skb_reset_transport_header(skb);
245
246 if (loop) {
247 /* local loopback of sent CAN frames */
248
249 /* indication for the CAN driver: do loopback */
250 skb->pkt_type = PACKET_LOOPBACK;
251
252 /*
253 * The reference to the originating sock may be required
254 * by the receiving socket to check whether the frame is
255 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
256 * Therefore we have to ensure that skb->sk remains the
257 * reference to the originating sock by restoring skb->sk
258 * after each skb_clone() or skb_orphan() usage.
259 */
260
261 if (!(skb->dev->flags & IFF_ECHO)) {
262 /*
263 * If the interface is not capable to do loopback
264 * itself, we do it here.
265 */
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700266 newskb = skb_clone(skb, GFP_ATOMIC);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800267 if (!newskb) {
268 kfree_skb(skb);
269 return -ENOMEM;
270 }
271
272 newskb->sk = skb->sk;
273 newskb->ip_summed = CHECKSUM_UNNECESSARY;
274 newskb->pkt_type = PACKET_BROADCAST;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800275 }
276 } else {
277 /* indication for the CAN driver: no loopback required */
278 skb->pkt_type = PACKET_HOST;
279 }
280
281 /* send to netdevice */
282 err = dev_queue_xmit(skb);
283 if (err > 0)
284 err = net_xmit_errno(err);
285
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700286 if (err) {
Wei Yongjunce030ed2009-02-25 00:35:44 +0000287 kfree_skb(newskb);
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700288 return err;
289 }
290
291 if (newskb)
Oliver Hartkopp481a8192009-09-15 01:31:34 -0700292 netif_rx_ni(newskb);
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700293
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800294 /* update statistics */
295 can_stats.tx_frames++;
296 can_stats.tx_frames_delta++;
297
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700298 return 0;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800299}
300EXPORT_SYMBOL(can_send);
301
302/*
303 * af_can rx path
304 */
305
306static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
307{
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000308 if (!dev)
309 return &can_rx_alldev_list;
310 else
311 return (struct dev_rcv_lists *)dev->ml_priv;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800312}
313
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800314/**
315 * find_rcv_list - determine optimal filterlist inside device filter struct
316 * @can_id: pointer to CAN identifier of a given can_filter
317 * @mask: pointer to CAN mask of a given can_filter
318 * @d: pointer to the device filter struct
319 *
320 * Description:
321 * Returns the optimal filterlist to reduce the filter handling in the
322 * receive path. This function is called by service functions that need
323 * to register or unregister a can_filter in the filter lists.
324 *
325 * A filter matches in general, when
326 *
327 * <received_can_id> & mask == can_id & mask
328 *
329 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
330 * relevant bits for the filter.
331 *
332 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
333 * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames
334 * there is a special filterlist and a special rx path filter handling.
335 *
336 * Return:
337 * Pointer to optimal filterlist for the given can_id/mask pair.
338 * Constistency checked mask.
339 * Reduced can_id to have a preprocessed filter compare value.
340 */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800341static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
342 struct dev_rcv_lists *d)
343{
344 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
345
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800346 /* filter for error frames in extra filterlist */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800347 if (*mask & CAN_ERR_FLAG) {
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800348 /* clear CAN_ERR_FLAG in filter entry */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800349 *mask &= CAN_ERR_MASK;
350 return &d->rx[RX_ERR];
351 }
352
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800353 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
354
355#define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
356
357 /* ensure valid values in can_mask for 'SFF only' frame filtering */
358 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
359 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800360
361 /* reduce condition testing at receive time */
362 *can_id &= *mask;
363
364 /* inverse can_id/can_mask filter */
365 if (inv)
366 return &d->rx[RX_INV];
367
368 /* mask == 0 => no condition testing at receive time */
369 if (!(*mask))
370 return &d->rx[RX_ALL];
371
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800372 /* extra filterlists for the subscription of a single non-RTR can_id */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800373 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
374 !(*can_id & CAN_RTR_FLAG)) {
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800375
376 if (*can_id & CAN_EFF_FLAG) {
377 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
378 /* RFC: a future use-case for hash-tables? */
379 return &d->rx[RX_EFF];
380 }
381 } else {
382 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
383 return &d->rx_sff[*can_id];
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800384 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800385 }
386
387 /* default: filter via can_id/can_mask */
388 return &d->rx[RX_FIL];
389}
390
391/**
392 * can_rx_register - subscribe CAN frames from a specific interface
393 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
394 * @can_id: CAN identifier (see description)
395 * @mask: CAN mask (see description)
396 * @func: callback function on filter match
397 * @data: returned parameter for callback function
398 * @ident: string for calling module indentification
399 *
400 * Description:
401 * Invokes the callback function with the received sk_buff and the given
402 * parameter 'data' on a matching receive filter. A filter matches, when
403 *
404 * <received_can_id> & mask == can_id & mask
405 *
406 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
407 * filter for error frames (CAN_ERR_FLAG bit set in mask).
408 *
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800409 * The provided pointer to the sk_buff is guaranteed to be valid as long as
410 * the callback function is running. The callback function must *not* free
411 * the given sk_buff while processing it's task. When the given sk_buff is
412 * needed after the end of the callback function it must be cloned inside
413 * the callback function with skb_clone().
414 *
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800415 * Return:
416 * 0 on success
417 * -ENOMEM on missing cache mem to create subscription entry
418 * -ENODEV unknown device
419 */
420int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask,
421 void (*func)(struct sk_buff *, void *), void *data,
422 char *ident)
423{
424 struct receiver *r;
425 struct hlist_head *rl;
426 struct dev_rcv_lists *d;
427 int err = 0;
428
429 /* insert new receiver (dev,canid,mask) -> (func,data) */
430
Oliver Hartkopp8b640562010-02-02 07:21:34 -0800431 if (dev && dev->type != ARPHRD_CAN)
432 return -ENODEV;
433
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800434 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
435 if (!r)
436 return -ENOMEM;
437
438 spin_lock(&can_rcvlists_lock);
439
440 d = find_dev_rcv_lists(dev);
441 if (d) {
442 rl = find_rcv_list(&can_id, &mask, d);
443
444 r->can_id = can_id;
445 r->mask = mask;
446 r->matches = 0;
447 r->func = func;
448 r->data = data;
449 r->ident = ident;
450
451 hlist_add_head_rcu(&r->list, rl);
452 d->entries++;
453
454 can_pstats.rcv_entries++;
455 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
456 can_pstats.rcv_entries_max = can_pstats.rcv_entries;
457 } else {
458 kmem_cache_free(rcv_cache, r);
459 err = -ENODEV;
460 }
461
462 spin_unlock(&can_rcvlists_lock);
463
464 return err;
465}
466EXPORT_SYMBOL(can_rx_register);
467
468/*
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800469 * can_rx_delete_receiver - rcu callback for single receiver entry removal
470 */
471static void can_rx_delete_receiver(struct rcu_head *rp)
472{
473 struct receiver *r = container_of(rp, struct receiver, rcu);
474
475 kmem_cache_free(rcv_cache, r);
476}
477
478/**
479 * can_rx_unregister - unsubscribe CAN frames from a specific interface
480 * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list)
481 * @can_id: CAN identifier
482 * @mask: CAN mask
483 * @func: callback function on filter match
484 * @data: returned parameter for callback function
485 *
486 * Description:
487 * Removes subscription entry depending on given (subscription) values.
488 */
489void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask,
490 void (*func)(struct sk_buff *, void *), void *data)
491{
492 struct receiver *r = NULL;
493 struct hlist_head *rl;
494 struct hlist_node *next;
495 struct dev_rcv_lists *d;
496
Oliver Hartkopp8b640562010-02-02 07:21:34 -0800497 if (dev && dev->type != ARPHRD_CAN)
498 return;
499
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800500 spin_lock(&can_rcvlists_lock);
501
502 d = find_dev_rcv_lists(dev);
503 if (!d) {
504 printk(KERN_ERR "BUG: receive list not found for "
505 "dev %s, id %03X, mask %03X\n",
506 DNAME(dev), can_id, mask);
507 goto out;
508 }
509
510 rl = find_rcv_list(&can_id, &mask, d);
511
512 /*
513 * Search the receiver list for the item to delete. This should
514 * exist, since no receiver may be unregistered that hasn't
515 * been registered before.
516 */
517
518 hlist_for_each_entry_rcu(r, next, rl, list) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800519 if (r->can_id == can_id && r->mask == mask &&
520 r->func == func && r->data == data)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800521 break;
522 }
523
524 /*
525 * Check for bugs in CAN protocol implementations:
526 * If no matching list item was found, the list cursor variable next
527 * will be NULL, while r will point to the last item of the list.
528 */
529
530 if (!next) {
531 printk(KERN_ERR "BUG: receive list entry not found for "
532 "dev %s, id %03X, mask %03X\n",
533 DNAME(dev), can_id, mask);
534 r = NULL;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800535 goto out;
536 }
537
538 hlist_del_rcu(&r->list);
539 d->entries--;
540
541 if (can_pstats.rcv_entries > 0)
542 can_pstats.rcv_entries--;
543
544 /* remove device structure requested by NETDEV_UNREGISTER */
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000545 if (d->remove_on_zero_entries && !d->entries) {
546 kfree(d);
547 dev->ml_priv = NULL;
548 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800549
550 out:
551 spin_unlock(&can_rcvlists_lock);
552
553 /* schedule the receiver item for deletion */
554 if (r)
555 call_rcu(&r->rcu, can_rx_delete_receiver);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800556}
557EXPORT_SYMBOL(can_rx_unregister);
558
559static inline void deliver(struct sk_buff *skb, struct receiver *r)
560{
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800561 r->func(skb, r->data);
562 r->matches++;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800563}
564
565static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
566{
567 struct receiver *r;
568 struct hlist_node *n;
569 int matches = 0;
570 struct can_frame *cf = (struct can_frame *)skb->data;
571 canid_t can_id = cf->can_id;
572
573 if (d->entries == 0)
574 return 0;
575
576 if (can_id & CAN_ERR_FLAG) {
577 /* check for error frame entries only */
578 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) {
579 if (can_id & r->mask) {
580 deliver(skb, r);
581 matches++;
582 }
583 }
584 return matches;
585 }
586
587 /* check for unfiltered entries */
588 hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) {
589 deliver(skb, r);
590 matches++;
591 }
592
593 /* check for can_id/mask entries */
594 hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) {
595 if ((can_id & r->mask) == r->can_id) {
596 deliver(skb, r);
597 matches++;
598 }
599 }
600
601 /* check for inverted can_id/mask entries */
602 hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) {
603 if ((can_id & r->mask) != r->can_id) {
604 deliver(skb, r);
605 matches++;
606 }
607 }
608
Oliver Hartkoppf706644d2008-12-04 15:01:08 -0800609 /* check filterlists for single non-RTR can_ids */
610 if (can_id & CAN_RTR_FLAG)
611 return matches;
612
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800613 if (can_id & CAN_EFF_FLAG) {
614 hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) {
615 if (r->can_id == can_id) {
616 deliver(skb, r);
617 matches++;
618 }
619 }
620 } else {
621 can_id &= CAN_SFF_MASK;
622 hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) {
623 deliver(skb, r);
624 matches++;
625 }
626 }
627
628 return matches;
629}
630
631static int can_rcv(struct sk_buff *skb, struct net_device *dev,
632 struct packet_type *pt, struct net_device *orig_dev)
633{
634 struct dev_rcv_lists *d;
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700635 struct can_frame *cf = (struct can_frame *)skb->data;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800636 int matches;
637
Oliver Hartkopp1758c092009-08-13 22:54:25 +0000638 if (!net_eq(dev_net(dev), &init_net))
639 goto drop;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800640
Oliver Hartkopp1758c092009-08-13 22:54:25 +0000641 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
642 skb->len != sizeof(struct can_frame) ||
643 cf->can_dlc > 8,
644 "PF_CAN: dropped non conform skbuf: "
645 "dev type %d, len %d, can_dlc %d\n",
646 dev->type, skb->len, cf->can_dlc))
647 goto drop;
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700648
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800649 /* update statistics */
650 can_stats.rx_frames++;
651 can_stats.rx_frames_delta++;
652
653 rcu_read_lock();
654
655 /* deliver the packet to sockets listening on all devices */
656 matches = can_rcv_filter(&can_rx_alldev_list, skb);
657
658 /* find receive list for this device */
659 d = find_dev_rcv_lists(dev);
660 if (d)
661 matches += can_rcv_filter(d, skb);
662
663 rcu_read_unlock();
664
Oliver Hartkopp62bcaa12009-04-17 01:38:46 -0700665 /* consume the skbuff allocated by the netdevice driver */
666 consume_skb(skb);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800667
668 if (matches > 0) {
669 can_stats.matches++;
670 can_stats.matches_delta++;
671 }
672
Oliver Hartkopp6ca8b992009-08-29 06:45:09 +0000673 return NET_RX_SUCCESS;
Oliver Hartkopp1758c092009-08-13 22:54:25 +0000674
675drop:
676 kfree_skb(skb);
Oliver Hartkopp6ca8b992009-08-29 06:45:09 +0000677 return NET_RX_DROP;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800678}
679
680/*
681 * af_can protocol functions
682 */
683
684/**
685 * can_proto_register - register CAN transport protocol
686 * @cp: pointer to CAN protocol structure
687 *
688 * Return:
689 * 0 on success
690 * -EINVAL invalid (out of range) protocol number
691 * -EBUSY protocol already in use
692 * -ENOBUF if proto_register() fails
693 */
694int can_proto_register(struct can_proto *cp)
695{
696 int proto = cp->protocol;
697 int err = 0;
698
699 if (proto < 0 || proto >= CAN_NPROTO) {
700 printk(KERN_ERR "can: protocol number %d out of range\n",
701 proto);
702 return -EINVAL;
703 }
704
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800705 err = proto_register(cp->prot, 0);
706 if (err < 0)
707 return err;
708
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000709 mutex_lock(&proto_tab_lock);
710
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800711 if (proto_tab[proto]) {
712 printk(KERN_ERR "can: protocol %d already registered\n",
713 proto);
714 err = -EBUSY;
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000715 } else
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000716 rcu_assign_pointer(proto_tab[proto], cp);
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800717
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000718 mutex_unlock(&proto_tab_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800719
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800720 if (err < 0)
721 proto_unregister(cp->prot);
722
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800723 return err;
724}
725EXPORT_SYMBOL(can_proto_register);
726
727/**
728 * can_proto_unregister - unregister CAN transport protocol
729 * @cp: pointer to CAN protocol structure
730 */
731void can_proto_unregister(struct can_proto *cp)
732{
733 int proto = cp->protocol;
734
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000735 mutex_lock(&proto_tab_lock);
736 BUG_ON(proto_tab[proto] != cp);
737 rcu_assign_pointer(proto_tab[proto], NULL);
738 mutex_unlock(&proto_tab_lock);
739
740 synchronize_rcu();
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800741
742 proto_unregister(cp->prot);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800743}
744EXPORT_SYMBOL(can_proto_unregister);
745
746/*
747 * af_can notifier to create/remove CAN netdevice specific structs
748 */
749static int can_notifier(struct notifier_block *nb, unsigned long msg,
750 void *data)
751{
752 struct net_device *dev = (struct net_device *)data;
753 struct dev_rcv_lists *d;
754
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700755 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800756 return NOTIFY_DONE;
757
758 if (dev->type != ARPHRD_CAN)
759 return NOTIFY_DONE;
760
761 switch (msg) {
762
763 case NETDEV_REGISTER:
764
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000765 /* create new dev_rcv_lists for this device */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800766 d = kzalloc(sizeof(*d), GFP_KERNEL);
767 if (!d) {
768 printk(KERN_ERR
769 "can: allocation of receive list failed\n");
770 return NOTIFY_DONE;
771 }
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000772 BUG_ON(dev->ml_priv);
773 dev->ml_priv = d;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800774
775 break;
776
777 case NETDEV_UNREGISTER:
778 spin_lock(&can_rcvlists_lock);
779
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000780 d = dev->ml_priv;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800781 if (d) {
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000782 if (d->entries)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800783 d->remove_on_zero_entries = 1;
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000784 else {
785 kfree(d);
786 dev->ml_priv = NULL;
787 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800788 } else
789 printk(KERN_ERR "can: notifier: receive list not "
790 "found for dev %s\n", dev->name);
791
792 spin_unlock(&can_rcvlists_lock);
793
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800794 break;
795 }
796
797 return NOTIFY_DONE;
798}
799
800/*
801 * af_can module init/exit functions
802 */
803
804static struct packet_type can_packet __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800805 .type = cpu_to_be16(ETH_P_CAN),
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800806 .dev = NULL,
807 .func = can_rcv,
808};
809
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000810static const struct net_proto_family can_family_ops = {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800811 .family = PF_CAN,
812 .create = can_create,
813 .owner = THIS_MODULE,
814};
815
816/* notifier block for netdevice event */
817static struct notifier_block can_netdev_notifier __read_mostly = {
818 .notifier_call = can_notifier,
819};
820
821static __init int can_init(void)
822{
823 printk(banner);
824
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000825 memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list));
826
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800827 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
828 0, 0, NULL);
829 if (!rcv_cache)
830 return -ENOMEM;
831
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800832 if (stats_timer) {
833 /* the statistics are updated every second (timer triggered) */
834 setup_timer(&can_stattimer, can_stat_update, 0);
835 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
836 } else
837 can_stattimer.function = NULL;
838
839 can_init_proc();
840
841 /* protocol register */
842 sock_register(&can_family_ops);
843 register_netdevice_notifier(&can_netdev_notifier);
844 dev_add_pack(&can_packet);
845
846 return 0;
847}
848
849static __exit void can_exit(void)
850{
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000851 struct net_device *dev;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800852
853 if (stats_timer)
854 del_timer(&can_stattimer);
855
856 can_remove_proc();
857
858 /* protocol unregister */
859 dev_remove_pack(&can_packet);
860 unregister_netdevice_notifier(&can_netdev_notifier);
861 sock_unregister(PF_CAN);
862
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000863 /* remove created dev_rcv_lists from still registered CAN devices */
864 rcu_read_lock();
865 for_each_netdev_rcu(&init_net, dev) {
866 if (dev->type == ARPHRD_CAN && dev->ml_priv){
867
868 struct dev_rcv_lists *d = dev->ml_priv;
869
870 BUG_ON(d->entries);
871 kfree(d);
872 dev->ml_priv = NULL;
873 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800874 }
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000875 rcu_read_unlock();
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800876
Jesper Dangaard Brouer382bfee2009-06-08 03:11:38 +0000877 rcu_barrier(); /* Wait for completion of call_rcu()'s */
878
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800879 kmem_cache_destroy(rcv_cache);
880}
881
882module_init(can_init);
883module_exit(can_exit);