blob: 76cf83b2bd409cae94c728573de8f248a6f29fc0 [file] [log] [blame]
Oliver Hartkoppfba76a52019-07-23 15:17:55 +02001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
Oliver Hartkopp0d665482007-11-16 15:52:17 -08002/*
3 * af_can.c - Protocol family CAN core module
4 * (used by different CAN protocol modules)
5 *
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +02006 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
Oliver Hartkopp0d665482007-11-16 15:52:17 -08007 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Volkswagen nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * Alternatively, provided that this notice is retained in full, this
22 * software may be distributed under the terms of the GNU General
23 * Public License ("GPL") version 2, in which case the provisions of the
24 * GPL apply INSTEAD OF those given above.
25 *
26 * The provided data structures and external interfaces from this code
27 * are not restricted to be used by modules with a GPL compatible license.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * DAMAGE.
41 *
Oliver Hartkopp0d665482007-11-16 15:52:17 -080042 */
43
44#include <linux/module.h>
Oliver Hartkopp7c941632012-06-13 20:04:33 +020045#include <linux/stddef.h>
Oliver Hartkopp0d665482007-11-16 15:52:17 -080046#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>
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +010061#include <linux/can/skb.h>
Manuel Zerpiesd751e622011-06-16 02:08:01 +000062#include <linux/ratelimit.h>
Oliver Hartkopp0d665482007-11-16 15:52:17 -080063#include <net/net_namespace.h>
64#include <net/sock.h>
65
66#include "af_can.h"
67
Oliver Hartkopp0d665482007-11-16 15:52:17 -080068MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
69MODULE_LICENSE("Dual BSD/GPL");
70MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
71 "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
72
73MODULE_ALIAS_NETPROTO(PF_CAN);
74
75static int stats_timer __read_mostly = 1;
Joe Perchesd6444062018-03-23 15:54:38 -070076module_param(stats_timer, int, 0444);
Oliver Hartkopp0d665482007-11-16 15:52:17 -080077MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
78
Oliver Hartkopp0d665482007-11-16 15:52:17 -080079static struct kmem_cache *rcv_cache __read_mostly;
80
81/* table of registered CAN protocols */
Marc Kleine-Buddecae1d5b2017-10-17 07:18:35 +020082static const struct can_proto __rcu *proto_tab[CAN_NPROTO] __read_mostly;
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +000083static DEFINE_MUTEX(proto_tab_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -080084
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +020085static atomic_t skbcounter = ATOMIC_INIT(0);
86
Oliver Hartkopp0d665482007-11-16 15:52:17 -080087/*
88 * af_can socket functions
89 */
90
Oliver Hartkopp0d665482007-11-16 15:52:17 -080091static void can_sock_destruct(struct sock *sk)
92{
93 skb_queue_purge(&sk->sk_receive_queue);
Willem de Bruijnfd704bd2019-06-07 16:46:07 -040094 skb_queue_purge(&sk->sk_error_queue);
Oliver Hartkopp0d665482007-11-16 15:52:17 -080095}
96
Kurt Van Dijckc8d55a92011-05-03 18:42:04 +000097static const struct can_proto *can_get_proto(int protocol)
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +000098{
Kurt Van Dijck16506292011-05-03 18:40:57 +000099 const struct can_proto *cp;
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000100
101 rcu_read_lock();
102 cp = rcu_dereference(proto_tab[protocol]);
103 if (cp && !try_module_get(cp->prot->owner))
104 cp = NULL;
105 rcu_read_unlock();
106
107 return cp;
108}
109
Kurt Van Dijckc8d55a92011-05-03 18:42:04 +0000110static inline void can_put_proto(const struct can_proto *cp)
111{
112 module_put(cp->prot->owner);
113}
114
Eric Paris3f378b62009-11-05 22:18:14 -0800115static int can_create(struct net *net, struct socket *sock, int protocol,
116 int kern)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800117{
118 struct sock *sk;
Kurt Van Dijck16506292011-05-03 18:40:57 +0000119 const struct can_proto *cp;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800120 int err = 0;
121
122 sock->state = SS_UNCONNECTED;
123
124 if (protocol < 0 || protocol >= CAN_NPROTO)
125 return -EINVAL;
126
Kurt Van Dijckc8d55a92011-05-03 18:42:04 +0000127 cp = can_get_proto(protocol);
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000128
Johannes Berg95a5afc2008-10-16 15:24:51 -0700129#ifdef CONFIG_MODULES
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000130 if (!cp) {
131 /* try to load protocol module if kernel is modular */
132
Urs Thuermann5423dd62008-02-07 18:04:21 -0800133 err = request_module("can-proto-%d", protocol);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800134
135 /*
136 * In case of error we only print a message but don't
137 * return the error code immediately. Below we will
138 * return -EPROTONOSUPPORT
139 */
Manuel Zerpiesd751e622011-06-16 02:08:01 +0000140 if (err)
141 printk_ratelimited(KERN_ERR "can: request_module "
Urs Thuermann5423dd62008-02-07 18:04:21 -0800142 "(can-proto-%d) failed.\n", protocol);
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000143
Kurt Van Dijckc8d55a92011-05-03 18:42:04 +0000144 cp = can_get_proto(protocol);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800145 }
Urs Thuermann5423dd62008-02-07 18:04:21 -0800146#endif
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800147
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800148 /* check for available protocol and correct usage */
149
150 if (!cp)
151 return -EPROTONOSUPPORT;
152
153 if (cp->type != sock->type) {
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000154 err = -EPROTOTYPE;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800155 goto errout;
156 }
157
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800158 sock->ops = cp->ops;
159
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500160 sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, kern);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800161 if (!sk) {
162 err = -ENOMEM;
163 goto errout;
164 }
165
166 sock_init_data(sock, sk);
167 sk->sk_destruct = can_sock_destruct;
168
169 if (sk->sk_prot->init)
170 err = sk->sk_prot->init(sk);
171
172 if (err) {
173 /* release sk on errors */
174 sock_orphan(sk);
175 sock_put(sk);
176 }
177
178 errout:
Kurt Van Dijckc8d55a92011-05-03 18:42:04 +0000179 can_put_proto(cp);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800180 return err;
181}
182
183/*
184 * af_can tx path
185 */
186
187/**
188 * can_send - transmit a CAN frame (optional with local loopback)
189 * @skb: pointer to socket buffer with CAN frame in data section
190 * @loop: loopback for listeners on local CAN sockets (recommended default!)
191 *
Oliver Hartkopp481a8192009-09-15 01:31:34 -0700192 * Due to the loopback this routine must not be called from hardirq context.
193 *
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800194 * Return:
195 * 0 on success
196 * -ENETDOWN when the selected interface is down
197 * -ENOBUFS on full driver queue (see net_xmit_errno())
198 * -ENOMEM when local loopback failed at calling skb_clone()
199 * -EPERM when trying to send on a non-CAN interface
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200200 * -EMSGSIZE CAN frame size is bigger than CAN interface MTU
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700201 * -EINVAL when the skb->data does not contain a valid CAN frame
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800202 */
203int can_send(struct sk_buff *skb, int loop)
204{
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700205 struct sk_buff *newskb = NULL;
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200206 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200207 struct s_stats *can_stats = dev_net(skb->dev)->can.can_stats;
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200208 int err = -EINVAL;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800209
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200210 if (skb->len == CAN_MTU) {
211 skb->protocol = htons(ETH_P_CAN);
212 if (unlikely(cfd->len > CAN_MAX_DLEN))
213 goto inval_skb;
214 } else if (skb->len == CANFD_MTU) {
215 skb->protocol = htons(ETH_P_CANFD);
216 if (unlikely(cfd->len > CANFD_MAX_DLEN))
217 goto inval_skb;
218 } else
219 goto inval_skb;
220
221 /*
222 * Make sure the CAN frame can pass the selected CAN netdevice.
223 * As structs can_frame and canfd_frame are similar, we can provide
224 * CAN FD frames to legacy CAN drivers as long as the length is <= 8
225 */
226 if (unlikely(skb->len > skb->dev->mtu && cfd->len > CAN_MAX_DLEN)) {
227 err = -EMSGSIZE;
228 goto inval_skb;
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700229 }
230
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200231 if (unlikely(skb->dev->type != ARPHRD_CAN)) {
232 err = -EPERM;
233 goto inval_skb;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800234 }
235
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200236 if (unlikely(!(skb->dev->flags & IFF_UP))) {
237 err = -ENETDOWN;
238 goto inval_skb;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800239 }
240
Oliver Hartkopp9694390162015-02-23 20:37:54 +0100241 skb->ip_summed = CHECKSUM_UNNECESSARY;
242
243 skb_reset_mac_header(skb);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800244 skb_reset_network_header(skb);
245 skb_reset_transport_header(skb);
246
247 if (loop) {
248 /* local loopback of sent CAN frames */
249
250 /* indication for the CAN driver: do loopback */
251 skb->pkt_type = PACKET_LOOPBACK;
252
253 /*
254 * The reference to the originating sock may be required
255 * by the receiving socket to check whether the frame is
256 * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
257 * Therefore we have to ensure that skb->sk remains the
258 * reference to the originating sock by restoring skb->sk
259 * after each skb_clone() or skb_orphan() usage.
260 */
261
262 if (!(skb->dev->flags & IFF_ECHO)) {
263 /*
264 * If the interface is not capable to do loopback
265 * itself, we do it here.
266 */
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700267 newskb = skb_clone(skb, GFP_ATOMIC);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800268 if (!newskb) {
269 kfree_skb(skb);
270 return -ENOMEM;
271 }
272
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100273 can_skb_set_owner(newskb, skb->sk);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800274 newskb->ip_summed = CHECKSUM_UNNECESSARY;
275 newskb->pkt_type = PACKET_BROADCAST;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800276 }
277 } else {
278 /* indication for the CAN driver: no loopback required */
279 skb->pkt_type = PACKET_HOST;
280 }
281
282 /* send to netdevice */
283 err = dev_queue_xmit(skb);
284 if (err > 0)
285 err = net_xmit_errno(err);
286
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700287 if (err) {
Wei Yongjunce030ed2009-02-25 00:35:44 +0000288 kfree_skb(newskb);
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700289 return err;
290 }
291
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200292 if (newskb)
Oliver Hartkopp481a8192009-09-15 01:31:34 -0700293 netif_rx_ni(newskb);
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700294
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800295 /* update statistics */
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200296 can_stats->tx_frames++;
297 can_stats->tx_frames_delta++;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800298
Oliver Hartkoppc2ab7ac2008-05-08 02:49:55 -0700299 return 0;
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200300
301inval_skb:
302 kfree_skb(skb);
303 return err;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800304}
305EXPORT_SYMBOL(can_send);
306
307/*
308 * af_can rx path
309 */
310
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200311static struct can_dev_rcv_lists *find_dev_rcv_lists(struct net *net,
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100312 struct net_device *dev)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800313{
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000314 if (!dev)
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100315 return net->can.can_rx_alldev_list;
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000316 else
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200317 return (struct can_dev_rcv_lists *)dev->ml_priv;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800318}
319
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800320/**
Oliver Hartkopp45c70022014-04-02 20:25:26 +0200321 * effhash - hash function for 29 bit CAN identifier reduction
322 * @can_id: 29 bit CAN identifier
323 *
324 * Description:
325 * To reduce the linear traversal in one linked list of _single_ EFF CAN
326 * frame subscriptions the 29 bit identifier is mapped to 10 bits.
327 * (see CAN_EFF_RCV_HASH_BITS definition)
328 *
329 * Return:
330 * Hash value from 0x000 - 0x3FF ( enforced by CAN_EFF_RCV_HASH_BITS mask )
331 */
332static unsigned int effhash(canid_t can_id)
333{
334 unsigned int hash;
335
336 hash = can_id;
337 hash ^= can_id >> CAN_EFF_RCV_HASH_BITS;
338 hash ^= can_id >> (2 * CAN_EFF_RCV_HASH_BITS);
339
340 return hash & ((1 << CAN_EFF_RCV_HASH_BITS) - 1);
341}
342
343/**
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800344 * find_rcv_list - determine optimal filterlist inside device filter struct
345 * @can_id: pointer to CAN identifier of a given can_filter
346 * @mask: pointer to CAN mask of a given can_filter
347 * @d: pointer to the device filter struct
348 *
349 * Description:
350 * Returns the optimal filterlist to reduce the filter handling in the
351 * receive path. This function is called by service functions that need
352 * to register or unregister a can_filter in the filter lists.
353 *
354 * A filter matches in general, when
355 *
356 * <received_can_id> & mask == can_id & mask
357 *
358 * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
359 * relevant bits for the filter.
360 *
361 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
Oliver Hartkoppd6e640f2012-05-08 22:20:33 +0200362 * filter for error messages (CAN_ERR_FLAG bit set in mask). For error msg
363 * frames there is a special filterlist and a special rx path filter handling.
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800364 *
365 * Return:
366 * Pointer to optimal filterlist for the given can_id/mask pair.
367 * Constistency checked mask.
368 * Reduced can_id to have a preprocessed filter compare value.
369 */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800370static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200371 struct can_dev_rcv_lists *d)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800372{
373 canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
374
Oliver Hartkoppd6e640f2012-05-08 22:20:33 +0200375 /* filter for error message frames in extra filterlist */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800376 if (*mask & CAN_ERR_FLAG) {
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800377 /* clear CAN_ERR_FLAG in filter entry */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800378 *mask &= CAN_ERR_MASK;
379 return &d->rx[RX_ERR];
380 }
381
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800382 /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
383
384#define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
385
386 /* ensure valid values in can_mask for 'SFF only' frame filtering */
387 if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
388 *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800389
390 /* reduce condition testing at receive time */
391 *can_id &= *mask;
392
393 /* inverse can_id/can_mask filter */
394 if (inv)
395 return &d->rx[RX_INV];
396
397 /* mask == 0 => no condition testing at receive time */
398 if (!(*mask))
399 return &d->rx[RX_ALL];
400
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800401 /* extra filterlists for the subscription of a single non-RTR can_id */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800402 if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
403 !(*can_id & CAN_RTR_FLAG)) {
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800404
405 if (*can_id & CAN_EFF_FLAG) {
Oliver Hartkopp45c70022014-04-02 20:25:26 +0200406 if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS))
407 return &d->rx_eff[effhash(*can_id)];
Oliver Hartkoppd253eee2008-12-03 15:52:35 -0800408 } else {
409 if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
410 return &d->rx_sff[*can_id];
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800411 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800412 }
413
414 /* default: filter via can_id/can_mask */
415 return &d->rx[RX_FIL];
416}
417
418/**
419 * can_rx_register - subscribe CAN frames from a specific interface
420 * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list)
421 * @can_id: CAN identifier (see description)
422 * @mask: CAN mask (see description)
423 * @func: callback function on filter match
424 * @data: returned parameter for callback function
Maxime Jayat3f794102013-10-12 01:29:46 +0200425 * @ident: string for calling module identification
Eric Dumazetf1712c72017-01-27 08:11:44 -0800426 * @sk: socket pointer (might be NULL)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800427 *
428 * Description:
429 * Invokes the callback function with the received sk_buff and the given
430 * parameter 'data' on a matching receive filter. A filter matches, when
431 *
432 * <received_can_id> & mask == can_id & mask
433 *
434 * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
Oliver Hartkoppd6e640f2012-05-08 22:20:33 +0200435 * filter for error message frames (CAN_ERR_FLAG bit set in mask).
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800436 *
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800437 * The provided pointer to the sk_buff is guaranteed to be valid as long as
438 * the callback function is running. The callback function must *not* free
439 * the given sk_buff while processing it's task. When the given sk_buff is
440 * needed after the end of the callback function it must be cloned inside
441 * the callback function with skb_clone().
442 *
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800443 * Return:
444 * 0 on success
445 * -ENOMEM on missing cache mem to create subscription entry
446 * -ENODEV unknown device
447 */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100448int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,
449 canid_t mask, void (*func)(struct sk_buff *, void *),
450 void *data, char *ident, struct sock *sk)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800451{
452 struct receiver *r;
453 struct hlist_head *rl;
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200454 struct can_dev_rcv_lists *d;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200455 struct s_pstats *can_pstats = net->can.can_pstats;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800456 int err = 0;
457
458 /* insert new receiver (dev,canid,mask) -> (func,data) */
459
Oliver Hartkopp8b640562010-02-02 07:21:34 -0800460 if (dev && dev->type != ARPHRD_CAN)
461 return -ENODEV;
462
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100463 if (dev && !net_eq(net, dev_net(dev)))
464 return -ENODEV;
465
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800466 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
467 if (!r)
468 return -ENOMEM;
469
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100470 spin_lock(&net->can.can_rcvlists_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800471
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100472 d = find_dev_rcv_lists(net, dev);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800473 if (d) {
474 rl = find_rcv_list(&can_id, &mask, d);
475
476 r->can_id = can_id;
477 r->mask = mask;
478 r->matches = 0;
479 r->func = func;
480 r->data = data;
481 r->ident = ident;
Eric Dumazetf1712c72017-01-27 08:11:44 -0800482 r->sk = sk;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800483
484 hlist_add_head_rcu(&r->list, rl);
485 d->entries++;
486
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200487 can_pstats->rcv_entries++;
488 if (can_pstats->rcv_entries_max < can_pstats->rcv_entries)
489 can_pstats->rcv_entries_max = can_pstats->rcv_entries;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800490 } else {
491 kmem_cache_free(rcv_cache, r);
492 err = -ENODEV;
493 }
494
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100495 spin_unlock(&net->can.can_rcvlists_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800496
497 return err;
498}
499EXPORT_SYMBOL(can_rx_register);
500
501/*
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800502 * can_rx_delete_receiver - rcu callback for single receiver entry removal
503 */
504static void can_rx_delete_receiver(struct rcu_head *rp)
505{
506 struct receiver *r = container_of(rp, struct receiver, rcu);
Eric Dumazetf1712c72017-01-27 08:11:44 -0800507 struct sock *sk = r->sk;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800508
509 kmem_cache_free(rcv_cache, r);
Eric Dumazetf1712c72017-01-27 08:11:44 -0800510 if (sk)
511 sock_put(sk);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800512}
513
514/**
515 * can_rx_unregister - unsubscribe CAN frames from a specific interface
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800516 * @dev: pointer to netdevice (NULL => unsubscribe from 'all' CAN devices list)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800517 * @can_id: CAN identifier
518 * @mask: CAN mask
519 * @func: callback function on filter match
520 * @data: returned parameter for callback function
521 *
522 * Description:
523 * Removes subscription entry depending on given (subscription) values.
524 */
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100525void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
526 canid_t mask, void (*func)(struct sk_buff *, void *),
527 void *data)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800528{
529 struct receiver *r = NULL;
530 struct hlist_head *rl;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200531 struct s_pstats *can_pstats = net->can.can_pstats;
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200532 struct can_dev_rcv_lists *d;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800533
Oliver Hartkopp8b640562010-02-02 07:21:34 -0800534 if (dev && dev->type != ARPHRD_CAN)
535 return;
536
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100537 if (dev && !net_eq(net, dev_net(dev)))
538 return;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800539
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100540 spin_lock(&net->can.can_rcvlists_lock);
541
542 d = find_dev_rcv_lists(net, dev);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800543 if (!d) {
Valentin Ilief4f3efd2013-03-10 11:15:13 +0000544 pr_err("BUG: receive list not found for "
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800545 "dev %s, id %03X, mask %03X\n",
546 DNAME(dev), can_id, mask);
547 goto out;
548 }
549
550 rl = find_rcv_list(&can_id, &mask, d);
551
552 /*
553 * Search the receiver list for the item to delete. This should
554 * exist, since no receiver may be unregistered that hasn't
555 * been registered before.
556 */
557
Sasha Levinb67bfe02013-02-27 17:06:00 -0800558 hlist_for_each_entry_rcu(r, rl, list) {
Joe Perchesf64f9e72009-11-29 16:55:45 -0800559 if (r->can_id == can_id && r->mask == mask &&
560 r->func == func && r->data == data)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800561 break;
562 }
563
564 /*
Oliver Hartkoppc9bbb752013-03-18 07:52:06 +0000565 * Check for bugs in CAN protocol implementations using af_can.c:
566 * 'r' will be NULL if no matching list item was found for removal.
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800567 */
568
Sasha Levinb67bfe02013-02-27 17:06:00 -0800569 if (!r) {
Oliver Hartkoppc9bbb752013-03-18 07:52:06 +0000570 WARN(1, "BUG: receive list entry not found for dev %s, "
571 "id %03X, mask %03X\n", DNAME(dev), can_id, mask);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800572 goto out;
573 }
574
575 hlist_del_rcu(&r->list);
576 d->entries--;
577
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200578 if (can_pstats->rcv_entries > 0)
579 can_pstats->rcv_entries--;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800580
581 /* remove device structure requested by NETDEV_UNREGISTER */
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000582 if (d->remove_on_zero_entries && !d->entries) {
583 kfree(d);
584 dev->ml_priv = NULL;
585 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800586
587 out:
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100588 spin_unlock(&net->can.can_rcvlists_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800589
590 /* schedule the receiver item for deletion */
Eric Dumazetf1712c72017-01-27 08:11:44 -0800591 if (r) {
592 if (r->sk)
593 sock_hold(r->sk);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800594 call_rcu(&r->rcu, can_rx_delete_receiver);
Eric Dumazetf1712c72017-01-27 08:11:44 -0800595 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800596}
597EXPORT_SYMBOL(can_rx_unregister);
598
599static inline void deliver(struct sk_buff *skb, struct receiver *r)
600{
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800601 r->func(skb, r->data);
602 r->matches++;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800603}
604
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200605static int can_rcv_filter(struct can_dev_rcv_lists *d, struct sk_buff *skb)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800606{
607 struct receiver *r;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800608 int matches = 0;
609 struct can_frame *cf = (struct can_frame *)skb->data;
610 canid_t can_id = cf->can_id;
611
612 if (d->entries == 0)
613 return 0;
614
615 if (can_id & CAN_ERR_FLAG) {
Oliver Hartkoppd6e640f2012-05-08 22:20:33 +0200616 /* check for error message frame entries only */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800617 hlist_for_each_entry_rcu(r, &d->rx[RX_ERR], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800618 if (can_id & r->mask) {
619 deliver(skb, r);
620 matches++;
621 }
622 }
623 return matches;
624 }
625
626 /* check for unfiltered entries */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800627 hlist_for_each_entry_rcu(r, &d->rx[RX_ALL], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800628 deliver(skb, r);
629 matches++;
630 }
631
632 /* check for can_id/mask entries */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800633 hlist_for_each_entry_rcu(r, &d->rx[RX_FIL], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800634 if ((can_id & r->mask) == r->can_id) {
635 deliver(skb, r);
636 matches++;
637 }
638 }
639
640 /* check for inverted can_id/mask entries */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800641 hlist_for_each_entry_rcu(r, &d->rx[RX_INV], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800642 if ((can_id & r->mask) != r->can_id) {
643 deliver(skb, r);
644 matches++;
645 }
646 }
647
Oliver Hartkoppf706644d2008-12-04 15:01:08 -0800648 /* check filterlists for single non-RTR can_ids */
649 if (can_id & CAN_RTR_FLAG)
650 return matches;
651
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800652 if (can_id & CAN_EFF_FLAG) {
Oliver Hartkopp45c70022014-04-02 20:25:26 +0200653 hlist_for_each_entry_rcu(r, &d->rx_eff[effhash(can_id)], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800654 if (r->can_id == can_id) {
655 deliver(skb, r);
656 matches++;
657 }
658 }
659 } else {
660 can_id &= CAN_SFF_MASK;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800661 hlist_for_each_entry_rcu(r, &d->rx_sff[can_id], list) {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800662 deliver(skb, r);
663 matches++;
664 }
665 }
666
667 return matches;
668}
669
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200670static void can_receive(struct sk_buff *skb, struct net_device *dev)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800671{
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200672 struct can_dev_rcv_lists *d;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200673 struct net *net = dev_net(dev);
674 struct s_stats *can_stats = net->can.can_stats;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800675 int matches;
676
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800677 /* update statistics */
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200678 can_stats->rx_frames++;
679 can_stats->rx_frames_delta++;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800680
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200681 /* create non-zero unique skb identifier together with *skb */
682 while (!(can_skb_prv(skb)->skbcnt))
683 can_skb_prv(skb)->skbcnt = atomic_inc_return(&skbcounter);
684
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800685 rcu_read_lock();
686
687 /* deliver the packet to sockets listening on all devices */
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200688 matches = can_rcv_filter(net->can.can_rx_alldev_list, skb);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800689
690 /* find receive list for this device */
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200691 d = find_dev_rcv_lists(net, dev);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800692 if (d)
693 matches += can_rcv_filter(d, skb);
694
695 rcu_read_unlock();
696
Oliver Hartkopp62bcaa12009-04-17 01:38:46 -0700697 /* consume the skbuff allocated by the netdevice driver */
698 consume_skb(skb);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800699
700 if (matches > 0) {
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200701 can_stats->matches++;
702 can_stats->matches_delta++;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800703 }
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200704}
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800705
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200706static int can_rcv(struct sk_buff *skb, struct net_device *dev,
707 struct packet_type *pt, struct net_device *orig_dev)
708{
709 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
710
Marc Kleine-Budde8cb68752018-01-16 19:30:14 +0100711 if (unlikely(dev->type != ARPHRD_CAN || skb->len != CAN_MTU ||
712 cfd->len > CAN_MAX_DLEN)) {
713 pr_warn_once("PF_CAN: dropped non conform CAN skbuf: dev type %d, len %d, datalen %d\n",
714 dev->type, skb->len, cfd->len);
715 kfree_skb(skb);
716 return NET_RX_DROP;
717 }
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200718
719 can_receive(skb, dev);
720 return NET_RX_SUCCESS;
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200721}
722
723static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
724 struct packet_type *pt, struct net_device *orig_dev)
725{
726 struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
727
Marc Kleine-Budded4689842018-01-16 19:30:14 +0100728 if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU ||
729 cfd->len > CANFD_MAX_DLEN)) {
730 pr_warn_once("PF_CAN: dropped non conform CAN FD skbuf: dev type %d, len %d, datalen %d\n",
731 dev->type, skb->len, cfd->len);
732 kfree_skb(skb);
733 return NET_RX_DROP;
734 }
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200735
736 can_receive(skb, dev);
Oliver Hartkopp6ca8b992009-08-29 06:45:09 +0000737 return NET_RX_SUCCESS;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800738}
739
740/*
741 * af_can protocol functions
742 */
743
744/**
745 * can_proto_register - register CAN transport protocol
746 * @cp: pointer to CAN protocol structure
747 *
748 * Return:
749 * 0 on success
750 * -EINVAL invalid (out of range) protocol number
751 * -EBUSY protocol already in use
752 * -ENOBUF if proto_register() fails
753 */
Kurt Van Dijck16506292011-05-03 18:40:57 +0000754int can_proto_register(const struct can_proto *cp)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800755{
756 int proto = cp->protocol;
757 int err = 0;
758
759 if (proto < 0 || proto >= CAN_NPROTO) {
Valentin Ilief4f3efd2013-03-10 11:15:13 +0000760 pr_err("can: protocol number %d out of range\n", proto);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800761 return -EINVAL;
762 }
763
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800764 err = proto_register(cp->prot, 0);
765 if (err < 0)
766 return err;
767
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000768 mutex_lock(&proto_tab_lock);
769
Marc Kleine-Buddecae1d5b2017-10-17 07:18:35 +0200770 if (rcu_access_pointer(proto_tab[proto])) {
Valentin Ilief4f3efd2013-03-10 11:15:13 +0000771 pr_err("can: protocol %d already registered\n", proto);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800772 err = -EBUSY;
Oliver Hartkopp53914b62011-03-22 08:27:25 +0000773 } else
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000774 RCU_INIT_POINTER(proto_tab[proto], cp);
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800775
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000776 mutex_unlock(&proto_tab_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800777
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800778 if (err < 0)
779 proto_unregister(cp->prot);
780
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800781 return err;
782}
783EXPORT_SYMBOL(can_proto_register);
784
785/**
786 * can_proto_unregister - unregister CAN transport protocol
787 * @cp: pointer to CAN protocol structure
788 */
Kurt Van Dijck16506292011-05-03 18:40:57 +0000789void can_proto_unregister(const struct can_proto *cp)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800790{
791 int proto = cp->protocol;
792
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000793 mutex_lock(&proto_tab_lock);
Marc Kleine-Buddecae1d5b2017-10-17 07:18:35 +0200794 BUG_ON(rcu_access_pointer(proto_tab[proto]) != cp);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000795 RCU_INIT_POINTER(proto_tab[proto], NULL);
Oliver Hartkopp1ca050d2011-04-05 08:01:16 +0000796 mutex_unlock(&proto_tab_lock);
797
798 synchronize_rcu();
Urs Thuermanna2fea5f2008-02-07 18:04:45 -0800799
800 proto_unregister(cp->prot);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800801}
802EXPORT_SYMBOL(can_proto_unregister);
803
804/*
805 * af_can notifier to create/remove CAN netdevice specific structs
806 */
807static int can_notifier(struct notifier_block *nb, unsigned long msg,
Jiri Pirko351638e2013-05-28 01:30:21 +0000808 void *ptr)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800809{
Jiri Pirko351638e2013-05-28 01:30:21 +0000810 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200811 struct can_dev_rcv_lists *d;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800812
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800813 if (dev->type != ARPHRD_CAN)
814 return NOTIFY_DONE;
815
816 switch (msg) {
817
818 case NETDEV_REGISTER:
819
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000820 /* create new dev_rcv_lists for this device */
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800821 d = kzalloc(sizeof(*d), GFP_KERNEL);
Valentin Ilief4f3efd2013-03-10 11:15:13 +0000822 if (!d)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800823 return NOTIFY_DONE;
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000824 BUG_ON(dev->ml_priv);
825 dev->ml_priv = d;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800826
827 break;
828
829 case NETDEV_UNREGISTER:
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100830 spin_lock(&dev_net(dev)->can.can_rcvlists_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800831
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000832 d = dev->ml_priv;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800833 if (d) {
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000834 if (d->entries)
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800835 d->remove_on_zero_entries = 1;
Oliver Hartkopp20dd3852009-12-25 06:47:47 +0000836 else {
837 kfree(d);
838 dev->ml_priv = NULL;
839 }
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800840 } else
Valentin Ilief4f3efd2013-03-10 11:15:13 +0000841 pr_err("can: notifier: receive list not found for dev "
842 "%s\n", dev->name);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800843
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100844 spin_unlock(&dev_net(dev)->can.can_rcvlists_lock);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800845
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800846 break;
847 }
848
849 return NOTIFY_DONE;
850}
851
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100852static int can_pernet_init(struct net *net)
853{
Marc Kleine-Budde74b7b4902017-06-06 13:53:16 +0200854 spin_lock_init(&net->can.can_rcvlists_lock);
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100855 net->can.can_rx_alldev_list =
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200856 kzalloc(sizeof(struct can_dev_rcv_lists), GFP_KERNEL);
Marc Kleine-Budde5a606222017-07-29 11:51:01 +0200857 if (!net->can.can_rx_alldev_list)
858 goto out;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200859 net->can.can_stats = kzalloc(sizeof(struct s_stats), GFP_KERNEL);
Marc Kleine-Budde5a606222017-07-29 11:51:01 +0200860 if (!net->can.can_stats)
861 goto out_free_alldev_list;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200862 net->can.can_pstats = kzalloc(sizeof(struct s_pstats), GFP_KERNEL);
Marc Kleine-Budde5a606222017-07-29 11:51:01 +0200863 if (!net->can.can_pstats)
864 goto out_free_can_stats;
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200865
866 if (IS_ENABLED(CONFIG_PROC_FS)) {
867 /* the statistics are updated every second (timer triggered) */
868 if (stats_timer) {
Kees Cook1fccb562017-10-16 17:29:06 -0700869 timer_setup(&net->can.can_stattimer, can_stat_update,
870 0);
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200871 mod_timer(&net->can.can_stattimer,
872 round_jiffies(jiffies + HZ));
873 }
874 net->can.can_stats->jiffies_init = jiffies;
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100875 can_init_proc(net);
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200876 }
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100877
878 return 0;
Marc Kleine-Budde5a606222017-07-29 11:51:01 +0200879
880 out_free_can_stats:
881 kfree(net->can.can_stats);
882 out_free_alldev_list:
883 kfree(net->can.can_rx_alldev_list);
884 out:
885 return -ENOMEM;
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100886}
887
888static void can_pernet_exit(struct net *net)
889{
890 struct net_device *dev;
891
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200892 if (IS_ENABLED(CONFIG_PROC_FS)) {
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100893 can_remove_proc(net);
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200894 if (stats_timer)
895 del_timer_sync(&net->can.can_stattimer);
896 }
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100897
898 /* remove created dev_rcv_lists from still registered CAN devices */
899 rcu_read_lock();
900 for_each_netdev_rcu(net, dev) {
901 if (dev->type == ARPHRD_CAN && dev->ml_priv) {
Marc Kleine-Buddeff847ee2017-06-03 20:10:03 +0200902 struct can_dev_rcv_lists *d = dev->ml_priv;
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100903
904 BUG_ON(d->entries);
905 kfree(d);
906 dev->ml_priv = NULL;
907 }
908 }
909 rcu_read_unlock();
Oliver Hartkoppa7bbd282017-04-25 08:19:38 +0200910
911 kfree(net->can.can_rx_alldev_list);
Oliver Hartkoppcb5635a2017-04-25 08:19:41 +0200912 kfree(net->can.can_stats);
913 kfree(net->can.can_pstats);
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100914}
915
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800916/*
917 * af_can module init/exit functions
918 */
919
920static struct packet_type can_packet __read_mostly = {
Harvey Harrison09640e632009-02-01 00:45:17 -0800921 .type = cpu_to_be16(ETH_P_CAN),
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800922 .func = can_rcv,
923};
924
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200925static struct packet_type canfd_packet __read_mostly = {
926 .type = cpu_to_be16(ETH_P_CANFD),
927 .func = canfd_rcv,
928};
929
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000930static const struct net_proto_family can_family_ops = {
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800931 .family = PF_CAN,
932 .create = can_create,
933 .owner = THIS_MODULE,
934};
935
936/* notifier block for netdevice event */
937static struct notifier_block can_netdev_notifier __read_mostly = {
938 .notifier_call = can_notifier,
939};
940
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100941static struct pernet_operations can_pernet_ops __read_mostly = {
942 .init = can_pernet_init,
943 .exit = can_pernet_exit,
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100944};
945
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800946static __init int can_init(void)
947{
YueHaibingc5a3aed2019-05-16 22:36:26 +0800948 int err;
949
Oliver Hartkopp7c941632012-06-13 20:04:33 +0200950 /* check for correct padding to be able to use the structs similarly */
951 BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
952 offsetof(struct canfd_frame, len) ||
953 offsetof(struct can_frame, data) !=
954 offsetof(struct canfd_frame, data));
955
Jeremiah Mahlerb111b782014-11-21 23:42:35 -0800956 pr_info("can: controller area network core (" CAN_VERSION_STRING ")\n");
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800957
958 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
959 0, 0, NULL);
960 if (!rcv_cache)
961 return -ENOMEM;
962
YueHaibingc5a3aed2019-05-16 22:36:26 +0800963 err = register_pernet_subsys(&can_pernet_ops);
964 if (err)
965 goto out_pernet;
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100966
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800967 /* protocol register */
YueHaibingc5a3aed2019-05-16 22:36:26 +0800968 err = sock_register(&can_family_ops);
969 if (err)
970 goto out_sock;
971 err = register_netdevice_notifier(&can_netdev_notifier);
972 if (err)
973 goto out_notifier;
974
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800975 dev_add_pack(&can_packet);
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200976 dev_add_pack(&canfd_packet);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800977
978 return 0;
YueHaibingc5a3aed2019-05-16 22:36:26 +0800979
980out_notifier:
981 sock_unregister(PF_CAN);
982out_sock:
983 unregister_pernet_subsys(&can_pernet_ops);
984out_pernet:
985 kmem_cache_destroy(rcv_cache);
986
987 return err;
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800988}
989
990static __exit void can_exit(void)
991{
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800992 /* protocol unregister */
Oliver Hartkopp8b019392012-06-13 20:33:02 +0200993 dev_remove_pack(&canfd_packet);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800994 dev_remove_pack(&can_packet);
995 unregister_netdevice_notifier(&can_netdev_notifier);
996 sock_unregister(PF_CAN);
997
Mario Kicherer8e8cda62017-02-21 12:19:47 +0100998 unregister_pernet_subsys(&can_pernet_ops);
Oliver Hartkopp0d665482007-11-16 15:52:17 -0800999
Jesper Dangaard Brouer382bfee2009-06-08 03:11:38 +00001000 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1001
Oliver Hartkopp0d665482007-11-16 15:52:17 -08001002 kmem_cache_destroy(rcv_cache);
1003}
1004
1005module_init(can_init);
1006module_exit(can_exit);