]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/can/af_can.c
can: remove obsolete pernet_operations definitions
[mirror_ubuntu-bionic-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 *
0d66548a
OH
41 */
42
43#include <linux/module.h>
7c941636 44#include <linux/stddef.h>
0d66548a
OH
45#include <linux/init.h>
46#include <linux/kmod.h>
47#include <linux/slab.h>
48#include <linux/list.h>
49#include <linux/spinlock.h>
50#include <linux/rcupdate.h>
51#include <linux/uaccess.h>
52#include <linux/net.h>
53#include <linux/netdevice.h>
54#include <linux/socket.h>
55#include <linux/if_ether.h>
56#include <linux/if_arp.h>
57#include <linux/skbuff.h>
58#include <linux/can.h>
59#include <linux/can/core.h>
0ae89beb 60#include <linux/can/skb.h>
d751e623 61#include <linux/ratelimit.h>
0d66548a
OH
62#include <net/net_namespace.h>
63#include <net/sock.h>
64
65#include "af_can.h"
66
0d66548a
OH
67MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
68MODULE_LICENSE("Dual BSD/GPL");
69MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
70 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
71
72MODULE_ALIAS_NETPROTO(PF_CAN);
73
74static int stats_timer __read_mostly = 1;
75module_param(stats_timer, int, S_IRUGO);
76MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
77
0d66548a
OH
78static struct kmem_cache *rcv_cache __read_mostly;
79
80/* table of registered CAN protocols */
1650629d 81static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
1ca050d9 82static DEFINE_MUTEX(proto_tab_lock);
0d66548a
OH
83
84struct timer_list can_stattimer; /* timer for statistics update */
85struct s_stats can_stats; /* packet statistics */
86struct s_pstats can_pstats; /* receive list statistics */
87
d3b58c47
OH
88static atomic_t skbcounter = ATOMIC_INIT(0);
89
0d66548a
OH
90/*
91 * af_can socket functions
92 */
93
53914b67 94int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
0d66548a
OH
95{
96 struct sock *sk = sock->sk;
97
98 switch (cmd) {
99
100 case SIOCGSTAMP:
101 return sock_get_timestamp(sk, (struct timeval __user *)arg);
102
103 default:
104 return -ENOIOCTLCMD;
105 }
106}
53914b67 107EXPORT_SYMBOL(can_ioctl);
0d66548a
OH
108
109static void can_sock_destruct(struct sock *sk)
110{
111 skb_queue_purge(&sk->sk_receive_queue);
112}
113
c8d55a9d 114static const struct can_proto *can_get_proto(int protocol)
1ca050d9 115{
1650629d 116 const struct can_proto *cp;
1ca050d9
OH
117
118 rcu_read_lock();
119 cp = rcu_dereference(proto_tab[protocol]);
120 if (cp && !try_module_get(cp->prot->owner))
121 cp = NULL;
122 rcu_read_unlock();
123
124 return cp;
125}
126
c8d55a9d
KVD
127static inline void can_put_proto(const struct can_proto *cp)
128{
129 module_put(cp->prot->owner);
130}
131
3f378b68
EP
132static int can_create(struct net *net, struct socket *sock, int protocol,
133 int kern)
0d66548a
OH
134{
135 struct sock *sk;
1650629d 136 const struct can_proto *cp;
0d66548a
OH
137 int err = 0;
138
139 sock->state = SS_UNCONNECTED;
140
141 if (protocol < 0 || protocol >= CAN_NPROTO)
142 return -EINVAL;
143
c8d55a9d 144 cp = can_get_proto(protocol);
1ca050d9 145
95a5afca 146#ifdef CONFIG_MODULES
1ca050d9
OH
147 if (!cp) {
148 /* try to load protocol module if kernel is modular */
149
5423dd67 150 err = request_module("can-proto-%d", protocol);
0d66548a
OH
151
152 /*
153 * In case of error we only print a message but don't
154 * return the error code immediately. Below we will
155 * return -EPROTONOSUPPORT
156 */
d751e623
MZ
157 if (err)
158 printk_ratelimited(KERN_ERR "can: request_module "
5423dd67 159 "(can-proto-%d) failed.\n", protocol);
1ca050d9 160
c8d55a9d 161 cp = can_get_proto(protocol);
0d66548a 162 }
5423dd67 163#endif
0d66548a 164
0d66548a
OH
165 /* check for available protocol and correct usage */
166
167 if (!cp)
168 return -EPROTONOSUPPORT;
169
170 if (cp->type != sock->type) {
1ca050d9 171 err = -EPROTOTYPE;
0d66548a
OH
172 goto errout;
173 }
174
0d66548a
OH
175 sock->ops = cp->ops;
176
11aa9c28 177 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, kern);
0d66548a
OH
178 if (!sk) {
179 err = -ENOMEM;
180 goto errout;
181 }
182
183 sock_init_data(sock, sk);
184 sk->sk_destruct = can_sock_destruct;
185
186 if (sk->sk_prot->init)
187 err = sk->sk_prot->init(sk);
188
189 if (err) {
190 /* release sk on errors */
191 sock_orphan(sk);
192 sock_put(sk);
193 }
194
195 errout:
c8d55a9d 196 can_put_proto(cp);
0d66548a
OH
197 return err;
198}
199
200/*
201 * af_can tx path
202 */
203
204/**
205 * can_send - transmit a CAN frame (optional with local loopback)
206 * @skb: pointer to socket buffer with CAN frame in data section
207 * @loop: loopback for listeners on local CAN sockets (recommended default!)
208 *
481a8199
OH
209 * Due to the loopback this routine must not be called from hardirq context.
210 *
0d66548a
OH
211 * Return:
212 * 0 on success
213 * -ENETDOWN when the selected interface is down
214 * -ENOBUFS on full driver queue (see net_xmit_errno())
215 * -ENOMEM when local loopback failed at calling skb_clone()
216 * -EPERM when trying to send on a non-CAN interface
8b01939f 217 * -EMSGSIZE CAN frame size is bigger than CAN interface MTU
7f2d38eb 218 * -EINVAL when the skb->data does not contain a valid CAN frame
0d66548a
OH
219 */
220int can_send(struct sk_buff *skb, int loop)
221{
c2ab7ac2 222 struct sk_buff *newskb = NULL;
8b01939f
OH
223 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
224 int err = -EINVAL;
225
226 if (skb->len == CAN_MTU) {
227 skb->protocol = htons(ETH_P_CAN);
228 if (unlikely(cfd->len > CAN_MAX_DLEN))
229 goto inval_skb;
230 } else if (skb->len == CANFD_MTU) {
231 skb->protocol = htons(ETH_P_CANFD);
232 if (unlikely(cfd->len > CANFD_MAX_DLEN))
233 goto inval_skb;
234 } else
235 goto inval_skb;
0d66548a 236
8b01939f
OH
237 /*
238 * Make sure the CAN frame can pass the selected CAN netdevice.
239 * As structs can_frame and canfd_frame are similar, we can provide
240 * CAN FD frames to legacy CAN drivers as long as the length is <= 8
241 */
242 if (unlikely(skb->len > skb->dev->mtu && cfd->len > CAN_MAX_DLEN)) {
243 err = -EMSGSIZE;
244 goto inval_skb;
7f2d38eb
OH
245 }
246
8b01939f
OH
247 if (unlikely(skb->dev->type != ARPHRD_CAN)) {
248 err = -EPERM;
249 goto inval_skb;
0d66548a
OH
250 }
251
8b01939f
OH
252 if (unlikely(!(skb->dev->flags & IFF_UP))) {
253 err = -ENETDOWN;
254 goto inval_skb;
0d66548a
OH
255 }
256
96943901
OH
257 skb->ip_summed = CHECKSUM_UNNECESSARY;
258
259 skb_reset_mac_header(skb);
0d66548a
OH
260 skb_reset_network_header(skb);
261 skb_reset_transport_header(skb);
262
263 if (loop) {
264 /* local loopback of sent CAN frames */
265
266 /* indication for the CAN driver: do loopback */
267 skb->pkt_type = PACKET_LOOPBACK;
268
269 /*
270 * The reference to the originating sock may be required
271 * by the receiving socket to check whether the frame is
272 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
273 * Therefore we have to ensure that skb->sk remains the
274 * reference to the originating sock by restoring skb->sk
275 * after each skb_clone() or skb_orphan() usage.
276 */
277
278 if (!(skb->dev->flags & IFF_ECHO)) {
279 /*
280 * If the interface is not capable to do loopback
281 * itself, we do it here.
282 */
c2ab7ac2 283 newskb = skb_clone(skb, GFP_ATOMIC);
0d66548a
OH
284 if (!newskb) {
285 kfree_skb(skb);
286 return -ENOMEM;
287 }
288
0ae89beb 289 can_skb_set_owner(newskb, skb->sk);
0d66548a
OH
290 newskb->ip_summed = CHECKSUM_UNNECESSARY;
291 newskb->pkt_type = PACKET_BROADCAST;
0d66548a
OH
292 }
293 } else {
294 /* indication for the CAN driver: no loopback required */
295 skb->pkt_type = PACKET_HOST;
296 }
297
298 /* send to netdevice */
299 err = dev_queue_xmit(skb);
300 if (err > 0)
301 err = net_xmit_errno(err);
302
c2ab7ac2 303 if (err) {
ce030edf 304 kfree_skb(newskb);
c2ab7ac2
OH
305 return err;
306 }
307
d3b58c47 308 if (newskb)
481a8199 309 netif_rx_ni(newskb);
c2ab7ac2 310
0d66548a
OH
311 /* update statistics */
312 can_stats.tx_frames++;
313 can_stats.tx_frames_delta++;
314
c2ab7ac2 315 return 0;
8b01939f
OH
316
317inval_skb:
318 kfree_skb(skb);
319 return err;
0d66548a
OH
320}
321EXPORT_SYMBOL(can_send);
322
323/*
324 * af_can rx path
325 */
326
8e8cda6d
MK
327static struct dev_rcv_lists *find_dev_rcv_lists(struct net *net,
328 struct net_device *dev)
0d66548a 329{
20dd3850 330 if (!dev)
8e8cda6d 331 return net->can.can_rx_alldev_list;
20dd3850
OH
332 else
333 return (struct dev_rcv_lists *)dev->ml_priv;
0d66548a
OH
334}
335
45c70029
OH
336/**
337 * effhash - hash function for 29 bit CAN identifier reduction
338 * @can_id: 29 bit CAN identifier
339 *
340 * Description:
341 * To reduce the linear traversal in one linked list of _single_ EFF CAN
342 * frame subscriptions the 29 bit identifier is mapped to 10 bits.
343 * (see CAN_EFF_RCV_HASH_BITS definition)
344 *
345 * Return:
346 * Hash value from 0x000 - 0x3FF ( enforced by CAN_EFF_RCV_HASH_BITS mask )
347 */
348static unsigned int effhash(canid_t can_id)
349{
350 unsigned int hash;
351
352 hash = can_id;
353 hash ^= can_id >> CAN_EFF_RCV_HASH_BITS;
354 hash ^= can_id >> (2 * CAN_EFF_RCV_HASH_BITS);
355
356 return hash & ((1 << CAN_EFF_RCV_HASH_BITS) - 1);
357}
358
d253eee2
OH
359/**
360 * find_rcv_list - determine optimal filterlist inside device filter struct
361 * @can_id: pointer to CAN identifier of a given can_filter
362 * @mask: pointer to CAN mask of a given can_filter
363 * @d: pointer to the device filter struct
364 *
365 * Description:
366 * Returns the optimal filterlist to reduce the filter handling in the
367 * receive path. This function is called by service functions that need
368 * to register or unregister a can_filter in the filter lists.
369 *
370 * A filter matches in general, when
371 *
372 * <received_can_id> & mask == can_id & mask
373 *
374 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
375 * relevant bits for the filter.
376 *
377 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
d6e640f9
OH
378 * filter for error messages (CAN_ERR_FLAG bit set in mask). For error msg
379 * frames there is a special filterlist and a special rx path filter handling.
d253eee2
OH
380 *
381 * Return:
382 * Pointer to optimal filterlist for the given can_id/mask pair.
383 * Constistency checked mask.
384 * Reduced can_id to have a preprocessed filter compare value.
385 */
0d66548a
OH
386static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
387 struct dev_rcv_lists *d)
388{
389 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
390
d6e640f9 391 /* filter for error message frames in extra filterlist */
0d66548a 392 if (*mask & CAN_ERR_FLAG) {
d253eee2 393 /* clear CAN_ERR_FLAG in filter entry */
0d66548a
OH
394 *mask &= CAN_ERR_MASK;
395 return &d->rx[RX_ERR];
396 }
397
d253eee2
OH
398 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
399
400#define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
401
402 /* ensure valid values in can_mask for 'SFF only' frame filtering */
403 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
404 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
0d66548a
OH
405
406 /* reduce condition testing at receive time */
407 *can_id &= *mask;
408
409 /* inverse can_id/can_mask filter */
410 if (inv)
411 return &d->rx[RX_INV];
412
413 /* mask == 0 => no condition testing at receive time */
414 if (!(*mask))
415 return &d->rx[RX_ALL];
416
d253eee2 417 /* extra filterlists for the subscription of a single non-RTR can_id */
f64f9e71
JP
418 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
419 !(*can_id & CAN_RTR_FLAG)) {
d253eee2
OH
420
421 if (*can_id & CAN_EFF_FLAG) {
45c70029
OH
422 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS))
423 return &d->rx_eff[effhash(*can_id)];
d253eee2
OH
424 } else {
425 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
426 return &d->rx_sff[*can_id];
0d66548a 427 }
0d66548a
OH
428 }
429
430 /* default: filter via can_id/can_mask */
431 return &d->rx[RX_FIL];
432}
433
434/**
435 * can_rx_register - subscribe CAN frames from a specific interface
436 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
437 * @can_id: CAN identifier (see description)
438 * @mask: CAN mask (see description)
439 * @func: callback function on filter match
440 * @data: returned parameter for callback function
3f79410c 441 * @ident: string for calling module identification
f1712c73 442 * @sk: socket pointer (might be NULL)
0d66548a
OH
443 *
444 * Description:
445 * Invokes the callback function with the received sk_buff and the given
446 * parameter 'data' on a matching receive filter. A filter matches, when
447 *
448 * <received_can_id> & mask == can_id & mask
449 *
450 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
d6e640f9 451 * filter for error message frames (CAN_ERR_FLAG bit set in mask).
0d66548a 452 *
1fa17d4b
OH
453 * The provided pointer to the sk_buff is guaranteed to be valid as long as
454 * the callback function is running. The callback function must *not* free
455 * the given sk_buff while processing it's task. When the given sk_buff is
456 * needed after the end of the callback function it must be cloned inside
457 * the callback function with skb_clone().
458 *
0d66548a
OH
459 * Return:
460 * 0 on success
461 * -ENOMEM on missing cache mem to create subscription entry
462 * -ENODEV unknown device
463 */
8e8cda6d
MK
464int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,
465 canid_t mask, void (*func)(struct sk_buff *, void *),
466 void *data, char *ident, struct sock *sk)
0d66548a
OH
467{
468 struct receiver *r;
469 struct hlist_head *rl;
470 struct dev_rcv_lists *d;
471 int err = 0;
472
473 /* insert new receiver (dev,canid,mask) -> (func,data) */
474
8b64056d
OH
475 if (dev && dev->type != ARPHRD_CAN)
476 return -ENODEV;
477
8e8cda6d
MK
478 if (dev && !net_eq(net, dev_net(dev)))
479 return -ENODEV;
480
0d66548a
OH
481 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
482 if (!r)
483 return -ENOMEM;
484
8e8cda6d 485 spin_lock(&net->can.can_rcvlists_lock);
0d66548a 486
8e8cda6d 487 d = find_dev_rcv_lists(net, dev);
0d66548a
OH
488 if (d) {
489 rl = find_rcv_list(&can_id, &mask, d);
490
491 r->can_id = can_id;
492 r->mask = mask;
493 r->matches = 0;
494 r->func = func;
495 r->data = data;
496 r->ident = ident;
f1712c73 497 r->sk = sk;
0d66548a
OH
498
499 hlist_add_head_rcu(&r->list, rl);
500 d->entries++;
501
502 can_pstats.rcv_entries++;
503 if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
504 can_pstats.rcv_entries_max = can_pstats.rcv_entries;
505 } else {
506 kmem_cache_free(rcv_cache, r);
507 err = -ENODEV;
508 }
509
8e8cda6d 510 spin_unlock(&net->can.can_rcvlists_lock);
0d66548a
OH
511
512 return err;
513}
514EXPORT_SYMBOL(can_rx_register);
515
0d66548a
OH
516/*
517 * can_rx_delete_receiver - rcu callback for single receiver entry removal
518 */
519static void can_rx_delete_receiver(struct rcu_head *rp)
520{
521 struct receiver *r = container_of(rp, struct receiver, rcu);
f1712c73 522 struct sock *sk = r->sk;
0d66548a
OH
523
524 kmem_cache_free(rcv_cache, r);
f1712c73
ED
525 if (sk)
526 sock_put(sk);
0d66548a
OH
527}
528
529/**
530 * can_rx_unregister - unsubscribe CAN frames from a specific interface
069f8457 531 * @dev: pointer to netdevice (NULL => unsubscribe from 'all' CAN devices list)
0d66548a
OH
532 * @can_id: CAN identifier
533 * @mask: CAN mask
534 * @func: callback function on filter match
535 * @data: returned parameter for callback function
536 *
537 * Description:
538 * Removes subscription entry depending on given (subscription) values.
539 */
8e8cda6d
MK
540void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
541 canid_t mask, void (*func)(struct sk_buff *, void *),
542 void *data)
0d66548a
OH
543{
544 struct receiver *r = NULL;
545 struct hlist_head *rl;
0d66548a
OH
546 struct dev_rcv_lists *d;
547
8b64056d
OH
548 if (dev && dev->type != ARPHRD_CAN)
549 return;
550
8e8cda6d
MK
551 if (dev && !net_eq(net, dev_net(dev)))
552 return;
0d66548a 553
8e8cda6d
MK
554 spin_lock(&net->can.can_rcvlists_lock);
555
556 d = find_dev_rcv_lists(net, dev);
0d66548a 557 if (!d) {
f4f3efda 558 pr_err("BUG: receive list not found for "
0d66548a
OH
559 "dev %s, id %03X, mask %03X\n",
560 DNAME(dev), can_id, mask);
561 goto out;
562 }
563
564 rl = find_rcv_list(&can_id, &mask, d);
565
566 /*
567 * Search the receiver list for the item to delete. This should
568 * exist, since no receiver may be unregistered that hasn't
569 * been registered before.
570 */
571
b67bfe0d 572 hlist_for_each_entry_rcu(r, rl, list) {
f64f9e71
JP
573 if (r->can_id == can_id && r->mask == mask &&
574 r->func == func && r->data == data)
0d66548a
OH
575 break;
576 }
577
578 /*
c9bbb75f
OH
579 * Check for bugs in CAN protocol implementations using af_can.c:
580 * 'r' will be NULL if no matching list item was found for removal.
0d66548a
OH
581 */
582
b67bfe0d 583 if (!r) {
c9bbb75f
OH
584 WARN(1, "BUG: receive list entry not found for dev %s, "
585 "id %03X, mask %03X\n", DNAME(dev), can_id, mask);
0d66548a
OH
586 goto out;
587 }
588
589 hlist_del_rcu(&r->list);
590 d->entries--;
591
592 if (can_pstats.rcv_entries > 0)
593 can_pstats.rcv_entries--;
594
595 /* remove device structure requested by NETDEV_UNREGISTER */
20dd3850
OH
596 if (d->remove_on_zero_entries && !d->entries) {
597 kfree(d);
598 dev->ml_priv = NULL;
599 }
0d66548a
OH
600
601 out:
8e8cda6d 602 spin_unlock(&net->can.can_rcvlists_lock);
0d66548a
OH
603
604 /* schedule the receiver item for deletion */
f1712c73
ED
605 if (r) {
606 if (r->sk)
607 sock_hold(r->sk);
0d66548a 608 call_rcu(&r->rcu, can_rx_delete_receiver);
f1712c73 609 }
0d66548a
OH
610}
611EXPORT_SYMBOL(can_rx_unregister);
612
613static inline void deliver(struct sk_buff *skb, struct receiver *r)
614{
1fa17d4b
OH
615 r->func(skb, r->data);
616 r->matches++;
0d66548a
OH
617}
618
619static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
620{
621 struct receiver *r;
0d66548a
OH
622 int matches = 0;
623 struct can_frame *cf = (struct can_frame *)skb->data;
624 canid_t can_id = cf->can_id;
625
626 if (d->entries == 0)
627 return 0;
628
629 if (can_id & CAN_ERR_FLAG) {
d6e640f9 630 /* check for error message frame entries only */
b67bfe0d 631 hlist_for_each_entry_rcu(r, &d->rx[RX_ERR], list) {
0d66548a
OH
632 if (can_id & r->mask) {
633 deliver(skb, r);
634 matches++;
635 }
636 }
637 return matches;
638 }
639
640 /* check for unfiltered entries */
b67bfe0d 641 hlist_for_each_entry_rcu(r, &d->rx[RX_ALL], list) {
0d66548a
OH
642 deliver(skb, r);
643 matches++;
644 }
645
646 /* check for can_id/mask entries */
b67bfe0d 647 hlist_for_each_entry_rcu(r, &d->rx[RX_FIL], list) {
0d66548a
OH
648 if ((can_id & r->mask) == r->can_id) {
649 deliver(skb, r);
650 matches++;
651 }
652 }
653
654 /* check for inverted can_id/mask entries */
b67bfe0d 655 hlist_for_each_entry_rcu(r, &d->rx[RX_INV], list) {
0d66548a
OH
656 if ((can_id & r->mask) != r->can_id) {
657 deliver(skb, r);
658 matches++;
659 }
660 }
661
f706644d
OH
662 /* check filterlists for single non-RTR can_ids */
663 if (can_id & CAN_RTR_FLAG)
664 return matches;
665
0d66548a 666 if (can_id & CAN_EFF_FLAG) {
45c70029 667 hlist_for_each_entry_rcu(r, &d->rx_eff[effhash(can_id)], list) {
0d66548a
OH
668 if (r->can_id == can_id) {
669 deliver(skb, r);
670 matches++;
671 }
672 }
673 } else {
674 can_id &= CAN_SFF_MASK;
b67bfe0d 675 hlist_for_each_entry_rcu(r, &d->rx_sff[can_id], list) {
0d66548a
OH
676 deliver(skb, r);
677 matches++;
678 }
679 }
680
681 return matches;
682}
683
8b01939f 684static void can_receive(struct sk_buff *skb, struct net_device *dev)
0d66548a
OH
685{
686 struct dev_rcv_lists *d;
687 int matches;
688
0d66548a
OH
689 /* update statistics */
690 can_stats.rx_frames++;
691 can_stats.rx_frames_delta++;
692
d3b58c47
OH
693 /* create non-zero unique skb identifier together with *skb */
694 while (!(can_skb_prv(skb)->skbcnt))
695 can_skb_prv(skb)->skbcnt = atomic_inc_return(&skbcounter);
696
0d66548a
OH
697 rcu_read_lock();
698
699 /* deliver the packet to sockets listening on all devices */
8e8cda6d 700 matches = can_rcv_filter(dev_net(dev)->can.can_rx_alldev_list, skb);
0d66548a
OH
701
702 /* find receive list for this device */
8e8cda6d 703 d = find_dev_rcv_lists(dev_net(dev), dev);
0d66548a
OH
704 if (d)
705 matches += can_rcv_filter(d, skb);
706
707 rcu_read_unlock();
708
62bcaa13
OH
709 /* consume the skbuff allocated by the netdevice driver */
710 consume_skb(skb);
0d66548a
OH
711
712 if (matches > 0) {
713 can_stats.matches++;
714 can_stats.matches_delta++;
715 }
8b01939f
OH
716}
717
718static int can_rcv(struct sk_buff *skb, struct net_device *dev,
719 struct packet_type *pt, struct net_device *orig_dev)
720{
721 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
0d66548a 722
8b01939f
OH
723 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
724 skb->len != CAN_MTU ||
725 cfd->len > CAN_MAX_DLEN,
726 "PF_CAN: dropped non conform CAN skbuf: "
727 "dev type %d, len %d, datalen %d\n",
728 dev->type, skb->len, cfd->len))
729 goto drop;
730
731 can_receive(skb, dev);
732 return NET_RX_SUCCESS;
733
734drop:
735 kfree_skb(skb);
736 return NET_RX_DROP;
737}
738
739static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
740 struct packet_type *pt, struct net_device *orig_dev)
741{
742 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
743
8b01939f
OH
744 if (WARN_ONCE(dev->type != ARPHRD_CAN ||
745 skb->len != CANFD_MTU ||
746 cfd->len > CANFD_MAX_DLEN,
747 "PF_CAN: dropped non conform CAN FD skbuf: "
748 "dev type %d, len %d, datalen %d\n",
749 dev->type, skb->len, cfd->len))
750 goto drop;
751
752 can_receive(skb, dev);
6ca8b990 753 return NET_RX_SUCCESS;
1758c094
OH
754
755drop:
756 kfree_skb(skb);
6ca8b990 757 return NET_RX_DROP;
0d66548a
OH
758}
759
760/*
761 * af_can protocol functions
762 */
763
764/**
765 * can_proto_register - register CAN transport protocol
766 * @cp: pointer to CAN protocol structure
767 *
768 * Return:
769 * 0 on success
770 * -EINVAL invalid (out of range) protocol number
771 * -EBUSY protocol already in use
772 * -ENOBUF if proto_register() fails
773 */
1650629d 774int can_proto_register(const struct can_proto *cp)
0d66548a
OH
775{
776 int proto = cp->protocol;
777 int err = 0;
778
779 if (proto < 0 || proto >= CAN_NPROTO) {
f4f3efda 780 pr_err("can: protocol number %d out of range\n", proto);
0d66548a
OH
781 return -EINVAL;
782 }
783
a2fea5f1
UT
784 err = proto_register(cp->prot, 0);
785 if (err < 0)
786 return err;
787
1ca050d9
OH
788 mutex_lock(&proto_tab_lock);
789
0d66548a 790 if (proto_tab[proto]) {
f4f3efda 791 pr_err("can: protocol %d already registered\n", proto);
0d66548a 792 err = -EBUSY;
53914b67 793 } else
a9b3cd7f 794 RCU_INIT_POINTER(proto_tab[proto], cp);
a2fea5f1 795
1ca050d9 796 mutex_unlock(&proto_tab_lock);
0d66548a 797
0d66548a 798 if (err < 0)
a2fea5f1 799 proto_unregister(cp->prot);
0d66548a
OH
800
801 return err;
802}
803EXPORT_SYMBOL(can_proto_register);
804
805/**
806 * can_proto_unregister - unregister CAN transport protocol
807 * @cp: pointer to CAN protocol structure
808 */
1650629d 809void can_proto_unregister(const struct can_proto *cp)
0d66548a
OH
810{
811 int proto = cp->protocol;
812
1ca050d9
OH
813 mutex_lock(&proto_tab_lock);
814 BUG_ON(proto_tab[proto] != cp);
a9b3cd7f 815 RCU_INIT_POINTER(proto_tab[proto], NULL);
1ca050d9
OH
816 mutex_unlock(&proto_tab_lock);
817
818 synchronize_rcu();
a2fea5f1
UT
819
820 proto_unregister(cp->prot);
0d66548a
OH
821}
822EXPORT_SYMBOL(can_proto_unregister);
823
824/*
825 * af_can notifier to create/remove CAN netdevice specific structs
826 */
827static int can_notifier(struct notifier_block *nb, unsigned long msg,
351638e7 828 void *ptr)
0d66548a 829{
351638e7 830 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0d66548a
OH
831 struct dev_rcv_lists *d;
832
0d66548a
OH
833 if (dev->type != ARPHRD_CAN)
834 return NOTIFY_DONE;
835
836 switch (msg) {
837
838 case NETDEV_REGISTER:
839
20dd3850 840 /* create new dev_rcv_lists for this device */
0d66548a 841 d = kzalloc(sizeof(*d), GFP_KERNEL);
f4f3efda 842 if (!d)
0d66548a 843 return NOTIFY_DONE;
20dd3850
OH
844 BUG_ON(dev->ml_priv);
845 dev->ml_priv = d;
0d66548a
OH
846
847 break;
848
849 case NETDEV_UNREGISTER:
8e8cda6d 850 spin_lock(&dev_net(dev)->can.can_rcvlists_lock);
0d66548a 851
20dd3850 852 d = dev->ml_priv;
0d66548a 853 if (d) {
20dd3850 854 if (d->entries)
0d66548a 855 d->remove_on_zero_entries = 1;
20dd3850
OH
856 else {
857 kfree(d);
858 dev->ml_priv = NULL;
859 }
0d66548a 860 } else
f4f3efda
VI
861 pr_err("can: notifier: receive list not found for dev "
862 "%s\n", dev->name);
0d66548a 863
8e8cda6d 864 spin_unlock(&dev_net(dev)->can.can_rcvlists_lock);
0d66548a 865
0d66548a
OH
866 break;
867 }
868
869 return NOTIFY_DONE;
870}
871
8e8cda6d
MK
872static int can_pernet_init(struct net *net)
873{
874 net->can.can_rcvlists_lock =
875 __SPIN_LOCK_UNLOCKED(net->can.can_rcvlists_lock);
876 net->can.can_rx_alldev_list =
877 kzalloc(sizeof(struct dev_rcv_lists), GFP_KERNEL);
878
879 if (IS_ENABLED(CONFIG_PROC_FS))
880 can_init_proc(net);
881
882 return 0;
883}
884
885static void can_pernet_exit(struct net *net)
886{
887 struct net_device *dev;
888
889 if (IS_ENABLED(CONFIG_PROC_FS))
890 can_remove_proc(net);
891
892 /* remove created dev_rcv_lists from still registered CAN devices */
893 rcu_read_lock();
894 for_each_netdev_rcu(net, dev) {
895 if (dev->type == ARPHRD_CAN && dev->ml_priv) {
896 struct dev_rcv_lists *d = dev->ml_priv;
897
898 BUG_ON(d->entries);
899 kfree(d);
900 dev->ml_priv = NULL;
901 }
902 }
903 rcu_read_unlock();
a7bbd28f
OH
904
905 kfree(net->can.can_rx_alldev_list);
8e8cda6d
MK
906}
907
0d66548a
OH
908/*
909 * af_can module init/exit functions
910 */
911
912static struct packet_type can_packet __read_mostly = {
09640e63 913 .type = cpu_to_be16(ETH_P_CAN),
0d66548a
OH
914 .func = can_rcv,
915};
916
8b01939f
OH
917static struct packet_type canfd_packet __read_mostly = {
918 .type = cpu_to_be16(ETH_P_CANFD),
919 .func = canfd_rcv,
920};
921
ec1b4cf7 922static const struct net_proto_family can_family_ops = {
0d66548a
OH
923 .family = PF_CAN,
924 .create = can_create,
925 .owner = THIS_MODULE,
926};
927
928/* notifier block for netdevice event */
929static struct notifier_block can_netdev_notifier __read_mostly = {
930 .notifier_call = can_notifier,
931};
932
8e8cda6d
MK
933static struct pernet_operations can_pernet_ops __read_mostly = {
934 .init = can_pernet_init,
935 .exit = can_pernet_exit,
8e8cda6d
MK
936};
937
0d66548a
OH
938static __init int can_init(void)
939{
7c941636
OH
940 /* check for correct padding to be able to use the structs similarly */
941 BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
942 offsetof(struct canfd_frame, len) ||
943 offsetof(struct can_frame, data) !=
944 offsetof(struct canfd_frame, data));
945
b111b78c 946 pr_info("can: controller area network core (" CAN_VERSION_STRING ")\n");
0d66548a
OH
947
948 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
949 0, 0, NULL);
950 if (!rcv_cache)
951 return -ENOMEM;
952
2781ff5c
AB
953 if (IS_ENABLED(CONFIG_PROC_FS)) {
954 if (stats_timer) {
0d66548a 955 /* the statistics are updated every second (timer triggered) */
2781ff5c
AB
956 setup_timer(&can_stattimer, can_stat_update, 0);
957 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
958 }
2781ff5c 959 }
0d66548a 960
8e8cda6d
MK
961 register_pernet_subsys(&can_pernet_ops);
962
0d66548a
OH
963 /* protocol register */
964 sock_register(&can_family_ops);
965 register_netdevice_notifier(&can_netdev_notifier);
966 dev_add_pack(&can_packet);
8b01939f 967 dev_add_pack(&canfd_packet);
0d66548a
OH
968
969 return 0;
970}
971
972static __exit void can_exit(void)
973{
2781ff5c
AB
974 if (IS_ENABLED(CONFIG_PROC_FS)) {
975 if (stats_timer)
976 del_timer_sync(&can_stattimer);
2781ff5c 977 }
0d66548a
OH
978
979 /* protocol unregister */
8b01939f 980 dev_remove_pack(&canfd_packet);
0d66548a
OH
981 dev_remove_pack(&can_packet);
982 unregister_netdevice_notifier(&can_netdev_notifier);
983 sock_unregister(PF_CAN);
984
8e8cda6d 985 unregister_pernet_subsys(&can_pernet_ops);
0d66548a 986
382bfeec
JDB
987 rcu_barrier(); /* Wait for completion of call_rcu()'s */
988
0d66548a
OH
989 kmem_cache_destroy(rcv_cache);
990}
991
992module_init(can_init);
993module_exit(can_exit);