^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* af_can.c - Protocol family CAN core module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (used by different CAN protocol modules)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * 3. Neither the name of Volkswagen nor the names of its contributors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * may be used to endorse or promote products derived from this software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Alternatively, provided that this notice is retained in full, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * software may be distributed under the terms of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Public License ("GPL") version 2, in which case the provisions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * GPL apply INSTEAD OF those given above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * The provided data structures and external interfaces from this code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * are not restricted to be used by modules with a GPL compatible license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/can.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/can/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/can/skb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/can/can-ml.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include "af_can.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) MODULE_DESCRIPTION("Controller Area Network PF_CAN core");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) MODULE_ALIAS_NETPROTO(PF_CAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int stats_timer __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) module_param(stats_timer, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static struct kmem_cache *rcv_cache __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* table of registered CAN protocols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static const struct can_proto __rcu *proto_tab[CAN_NPROTO] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static DEFINE_MUTEX(proto_tab_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static atomic_t skbcounter = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* af_can socket functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void can_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) skb_queue_purge(&sk->sk_error_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL(can_sock_destruct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static const struct can_proto *can_get_proto(int protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) const struct can_proto *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) cp = rcu_dereference(proto_tab[protocol]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (cp && !try_module_get(cp->prot->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static inline void can_put_proto(const struct can_proto *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) module_put(cp->prot->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int can_create(struct net *net, struct socket *sock, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const struct can_proto *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (protocol < 0 || protocol >= CAN_NPROTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) cp = can_get_proto(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #ifdef CONFIG_MODULES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!cp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* try to load protocol module if kernel is modular */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) err = request_module("can-proto-%d", protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* In case of error we only print a message but don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * return the error code immediately. Below we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * return -EPROTONOSUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pr_err_ratelimited("can: request_module (can-proto-%d) failed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) cp = can_get_proto(protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* check for available protocol and correct usage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (cp->type != sock->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err = -EPROTOTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sock->ops = cp->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sk->sk_destruct = can_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (sk->sk_prot->init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) err = sk->sk_prot->init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* release sk on errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) can_put_proto(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* af_can tx path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * can_send - transmit a CAN frame (optional with local loopback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @skb: pointer to socket buffer with CAN frame in data section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @loop: loopback for listeners on local CAN sockets (recommended default!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Due to the loopback this routine must not be called from hardirq context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * -ENETDOWN when the selected interface is down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * -ENOBUFS on full driver queue (see net_xmit_errno())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * -ENOMEM when local loopback failed at calling skb_clone()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * -EPERM when trying to send on a non-CAN interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * -EMSGSIZE CAN frame size is bigger than CAN interface MTU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * -EINVAL when the skb->data does not contain a valid CAN frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int can_send(struct sk_buff *skb, int loop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct sk_buff *newskb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct can_pkg_stats *pkg_stats = dev_net(skb->dev)->can.pkg_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (skb->len == CAN_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) skb->protocol = htons(ETH_P_CAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (unlikely(cfd->len > CAN_MAX_DLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) } else if (skb->len == CANFD_MTU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) skb->protocol = htons(ETH_P_CANFD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (unlikely(cfd->len > CANFD_MAX_DLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* Make sure the CAN frame can pass the selected CAN netdevice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * As structs can_frame and canfd_frame are similar, we can provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * CAN FD frames to legacy CAN drivers as long as the length is <= 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (unlikely(skb->len > skb->dev->mtu && cfd->len > CAN_MAX_DLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (unlikely(skb->dev->type != ARPHRD_CAN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (unlikely(!(skb->dev->flags & IFF_UP))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) err = -ENETDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) goto inval_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) skb->ip_summed = CHECKSUM_UNNECESSARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (loop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* local loopback of sent CAN frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* indication for the CAN driver: do loopback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) skb->pkt_type = PACKET_LOOPBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* The reference to the originating sock may be required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * by the receiving socket to check whether the frame is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Therefore we have to ensure that skb->sk remains the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * reference to the originating sock by restoring skb->sk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * after each skb_clone() or skb_orphan() usage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!(skb->dev->flags & IFF_ECHO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* If the interface is not capable to do loopback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * itself, we do it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) newskb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (!newskb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) can_skb_set_owner(newskb, skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) newskb->ip_summed = CHECKSUM_UNNECESSARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) newskb->pkt_type = PACKET_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* indication for the CAN driver: no loopback required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) skb->pkt_type = PACKET_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* send to netdevice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) err = dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) err = net_xmit_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) kfree_skb(newskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (newskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) netif_rx_ni(newskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* update statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pkg_stats->tx_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) pkg_stats->tx_frames_delta++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) inval_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) EXPORT_SYMBOL(can_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* af_can rx path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static struct can_dev_rcv_lists *can_dev_rcv_lists_find(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct can_ml_priv *can_ml = can_get_ml_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return &can_ml->dev_rcv_lists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return net->can.rx_alldev_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * effhash - hash function for 29 bit CAN identifier reduction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * @can_id: 29 bit CAN identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * To reduce the linear traversal in one linked list of _single_ EFF CAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * frame subscriptions the 29 bit identifier is mapped to 10 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * (see CAN_EFF_RCV_HASH_BITS definition)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Hash value from 0x000 - 0x3FF ( enforced by CAN_EFF_RCV_HASH_BITS mask )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static unsigned int effhash(canid_t can_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) unsigned int hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) hash = can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) hash ^= can_id >> CAN_EFF_RCV_HASH_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) hash ^= can_id >> (2 * CAN_EFF_RCV_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return hash & ((1 << CAN_EFF_RCV_HASH_BITS) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * can_rcv_list_find - determine optimal filterlist inside device filter struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @can_id: pointer to CAN identifier of a given can_filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * @mask: pointer to CAN mask of a given can_filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * @dev_rcv_lists: pointer to the device filter struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * Returns the optimal filterlist to reduce the filter handling in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * receive path. This function is called by service functions that need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * to register or unregister a can_filter in the filter lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * A filter matches in general, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * <received_can_id> & mask == can_id & mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * relevant bits for the filter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * filter for error messages (CAN_ERR_FLAG bit set in mask). For error msg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * frames there is a special filterlist and a special rx path filter handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Pointer to optimal filterlist for the given can_id/mask pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * Consistency checked mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * Reduced can_id to have a preprocessed filter compare value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static struct hlist_head *can_rcv_list_find(canid_t *can_id, canid_t *mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct can_dev_rcv_lists *dev_rcv_lists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* filter for error message frames in extra filterlist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (*mask & CAN_ERR_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* clear CAN_ERR_FLAG in filter entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) *mask &= CAN_ERR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return &dev_rcv_lists->rx[RX_ERR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* ensure valid values in can_mask for 'SFF only' frame filtering */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* reduce condition testing at receive time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) *can_id &= *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* inverse can_id/can_mask filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (inv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return &dev_rcv_lists->rx[RX_INV];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /* mask == 0 => no condition testing at receive time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (!(*mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return &dev_rcv_lists->rx[RX_ALL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* extra filterlists for the subscription of a single non-RTR can_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) !(*can_id & CAN_RTR_FLAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (*can_id & CAN_EFF_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return &dev_rcv_lists->rx_eff[effhash(*can_id)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return &dev_rcv_lists->rx_sff[*can_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* default: filter via can_id/can_mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return &dev_rcv_lists->rx[RX_FIL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * can_rx_register - subscribe CAN frames from a specific interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * @net: the applicable net namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * @dev: pointer to netdevice (NULL => subscribe from 'all' CAN devices list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * @can_id: CAN identifier (see description)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * @mask: CAN mask (see description)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * @func: callback function on filter match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * @data: returned parameter for callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * @ident: string for calling module identification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @sk: socket pointer (might be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * Invokes the callback function with the received sk_buff and the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * parameter 'data' on a matching receive filter. A filter matches, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * <received_can_id> & mask == can_id & mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * filter for error message frames (CAN_ERR_FLAG bit set in mask).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * The provided pointer to the sk_buff is guaranteed to be valid as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * the callback function is running. The callback function must *not* free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * the given sk_buff while processing it's task. When the given sk_buff is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * needed after the end of the callback function it must be cloned inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * the callback function with skb_clone().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * -ENOMEM on missing cache mem to create subscription entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * -ENODEV unknown device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) canid_t mask, void (*func)(struct sk_buff *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) void *data, char *ident, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct receiver *rcv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct hlist_head *rcv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct can_dev_rcv_lists *dev_rcv_lists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* insert new receiver (dev,canid,mask) -> (func,data) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (dev && dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (dev && !net_eq(net, dev_net(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) rcv = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (!rcv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) spin_lock_bh(&net->can.rcvlists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) dev_rcv_lists = can_dev_rcv_lists_find(net, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) rcv_list = can_rcv_list_find(&can_id, &mask, dev_rcv_lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) rcv->can_id = can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) rcv->mask = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) rcv->matches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) rcv->func = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) rcv->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) rcv->ident = ident;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) rcv->sk = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) hlist_add_head_rcu(&rcv->list, rcv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) dev_rcv_lists->entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) rcv_lists_stats->rcv_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) rcv_lists_stats->rcv_entries_max = max(rcv_lists_stats->rcv_entries_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) rcv_lists_stats->rcv_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) spin_unlock_bh(&net->can.rcvlists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) EXPORT_SYMBOL(can_rx_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* can_rx_delete_receiver - rcu callback for single receiver entry removal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static void can_rx_delete_receiver(struct rcu_head *rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct receiver *rcv = container_of(rp, struct receiver, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct sock *sk = rcv->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) kmem_cache_free(rcv_cache, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * can_rx_unregister - unsubscribe CAN frames from a specific interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * @net: the applicable net namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * @dev: pointer to netdevice (NULL => unsubscribe from 'all' CAN devices list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * @can_id: CAN identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * @mask: CAN mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * @func: callback function on filter match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * @data: returned parameter for callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * Removes subscription entry depending on given (subscription) values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) canid_t mask, void (*func)(struct sk_buff *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct receiver *rcv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct hlist_head *rcv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct can_dev_rcv_lists *dev_rcv_lists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (dev && dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (dev && !net_eq(net, dev_net(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) spin_lock_bh(&net->can.rcvlists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) dev_rcv_lists = can_dev_rcv_lists_find(net, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) rcv_list = can_rcv_list_find(&can_id, &mask, dev_rcv_lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /* Search the receiver list for the item to delete. This should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * exist, since no receiver may be unregistered that hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * been registered before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) hlist_for_each_entry_rcu(rcv, rcv_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (rcv->can_id == can_id && rcv->mask == mask &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) rcv->func == func && rcv->data == data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* Check for bugs in CAN protocol implementations using af_can.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * 'rcv' will be NULL if no matching list item was found for removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * As this case may potentially happen when closing a socket while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * the notifier for removing the CAN netdev is running we just print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * a warning here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (!rcv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) pr_warn("can: receive list entry not found for dev %s, id %03X, mask %03X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) DNAME(dev), can_id, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) hlist_del_rcu(&rcv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) dev_rcv_lists->entries--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (rcv_lists_stats->rcv_entries > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) rcv_lists_stats->rcv_entries--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) spin_unlock_bh(&net->can.rcvlists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /* schedule the receiver item for deletion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (rcv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (rcv->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) sock_hold(rcv->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) call_rcu(&rcv->rcu, can_rx_delete_receiver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) EXPORT_SYMBOL(can_rx_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static inline void deliver(struct sk_buff *skb, struct receiver *rcv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) rcv->func(skb, rcv->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) rcv->matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static int can_rcv_filter(struct can_dev_rcv_lists *dev_rcv_lists, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct receiver *rcv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int matches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct can_frame *cf = (struct can_frame *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) canid_t can_id = cf->can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (dev_rcv_lists->entries == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (can_id & CAN_ERR_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /* check for error message frame entries only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx[RX_ERR], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (can_id & rcv->mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /* check for unfiltered entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx[RX_ALL], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) /* check for can_id/mask entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx[RX_FIL], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if ((can_id & rcv->mask) == rcv->can_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* check for inverted can_id/mask entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx[RX_INV], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if ((can_id & rcv->mask) != rcv->can_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /* check filterlists for single non-RTR can_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (can_id & CAN_RTR_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (can_id & CAN_EFF_FLAG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx_eff[effhash(can_id)], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (rcv->can_id == can_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) can_id &= CAN_SFF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) hlist_for_each_entry_rcu(rcv, &dev_rcv_lists->rx_sff[can_id], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) deliver(skb, rcv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static void can_receive(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct can_dev_rcv_lists *dev_rcv_lists;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) int matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /* update statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) pkg_stats->rx_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) pkg_stats->rx_frames_delta++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) /* create non-zero unique skb identifier together with *skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) while (!(can_skb_prv(skb)->skbcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) can_skb_prv(skb)->skbcnt = atomic_inc_return(&skbcounter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /* deliver the packet to sockets listening on all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) matches = can_rcv_filter(net->can.rx_alldev_list, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) /* find receive list for this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) dev_rcv_lists = can_dev_rcv_lists_find(net, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) matches += can_rcv_filter(dev_rcv_lists, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) /* consume the skbuff allocated by the netdevice driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (matches > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) pkg_stats->matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) pkg_stats->matches_delta++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static int can_rcv(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct packet_type *pt, struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (unlikely(dev->type != ARPHRD_CAN || skb->len != CAN_MTU)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) pr_warn_once("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) dev->type, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* This check is made separately since cfd->len would be uninitialized if skb->len = 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (unlikely(cfd->len > CAN_MAX_DLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) pr_warn_once("PF_CAN: dropped non conform CAN skbuff: dev type %d, len %d, datalen %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) dev->type, skb->len, cfd->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) can_receive(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) struct packet_type *pt, struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (unlikely(dev->type != ARPHRD_CAN || skb->len != CANFD_MTU)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) dev->type, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /* This check is made separately since cfd->len would be uninitialized if skb->len = 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (unlikely(cfd->len > CANFD_MAX_DLEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) pr_warn_once("PF_CAN: dropped non conform CAN FD skbuff: dev type %d, len %d, datalen %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) dev->type, skb->len, cfd->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) can_receive(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) /* af_can protocol functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * can_proto_register - register CAN transport protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * @cp: pointer to CAN protocol structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * -EINVAL invalid (out of range) protocol number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * -EBUSY protocol already in use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * -ENOBUF if proto_register() fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) int can_proto_register(const struct can_proto *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) int proto = cp->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (proto < 0 || proto >= CAN_NPROTO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) pr_err("can: protocol number %d out of range\n", proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) err = proto_register(cp->prot, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) mutex_lock(&proto_tab_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (rcu_access_pointer(proto_tab[proto])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) pr_err("can: protocol %d already registered\n", proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) RCU_INIT_POINTER(proto_tab[proto], cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) mutex_unlock(&proto_tab_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) proto_unregister(cp->prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) EXPORT_SYMBOL(can_proto_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * can_proto_unregister - unregister CAN transport protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * @cp: pointer to CAN protocol structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) void can_proto_unregister(const struct can_proto *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int proto = cp->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) mutex_lock(&proto_tab_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) BUG_ON(rcu_access_pointer(proto_tab[proto]) != cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) RCU_INIT_POINTER(proto_tab[proto], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) mutex_unlock(&proto_tab_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) proto_unregister(cp->prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) EXPORT_SYMBOL(can_proto_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static int can_pernet_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) spin_lock_init(&net->can.rcvlists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) net->can.rx_alldev_list =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) kzalloc(sizeof(*net->can.rx_alldev_list), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (!net->can.rx_alldev_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) net->can.pkg_stats = kzalloc(sizeof(*net->can.pkg_stats), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (!net->can.pkg_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) goto out_free_rx_alldev_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) net->can.rcv_lists_stats = kzalloc(sizeof(*net->can.rcv_lists_stats), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (!net->can.rcv_lists_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) goto out_free_pkg_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (IS_ENABLED(CONFIG_PROC_FS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) /* the statistics are updated every second (timer triggered) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (stats_timer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) timer_setup(&net->can.stattimer, can_stat_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) mod_timer(&net->can.stattimer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) round_jiffies(jiffies + HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) net->can.pkg_stats->jiffies_init = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) can_init_proc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) out_free_pkg_stats:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) kfree(net->can.pkg_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) out_free_rx_alldev_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) kfree(net->can.rx_alldev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) static void can_pernet_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (IS_ENABLED(CONFIG_PROC_FS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) can_remove_proc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) if (stats_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) del_timer_sync(&net->can.stattimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) kfree(net->can.rx_alldev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) kfree(net->can.pkg_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) kfree(net->can.rcv_lists_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /* af_can module init/exit functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) static struct packet_type can_packet __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) .type = cpu_to_be16(ETH_P_CAN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) .func = can_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) static struct packet_type canfd_packet __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) .type = cpu_to_be16(ETH_P_CANFD),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) .func = canfd_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) static const struct net_proto_family can_family_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) .family = PF_CAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) .create = can_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static struct pernet_operations can_pernet_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .init = can_pernet_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) .exit = can_pernet_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) static __init int can_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) /* check for correct padding to be able to use the structs similarly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) BUILD_BUG_ON(offsetof(struct can_frame, can_dlc) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) offsetof(struct canfd_frame, len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) offsetof(struct can_frame, data) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) offsetof(struct canfd_frame, data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) pr_info("can: controller area network core\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (!rcv_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) err = register_pernet_subsys(&can_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) goto out_pernet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) /* protocol register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) err = sock_register(&can_family_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) goto out_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) dev_add_pack(&can_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) dev_add_pack(&canfd_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) out_sock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) unregister_pernet_subsys(&can_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) out_pernet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) kmem_cache_destroy(rcv_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) static __exit void can_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) /* protocol unregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) dev_remove_pack(&canfd_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) dev_remove_pack(&can_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) sock_unregister(PF_CAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) unregister_pernet_subsys(&can_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) rcu_barrier(); /* Wait for completion of call_rcu()'s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) kmem_cache_destroy(rcv_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) module_init(can_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) module_exit(can_exit);