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