^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) /* gw.c - CAN frame Gateway/Router/Bridge with netlink interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2019 Volkswagen Group Electronic Research
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 3. Neither the name of Volkswagen nor the names of its contributors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * may be used to endorse or promote products derived from this software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Alternatively, provided that this notice is retained in full, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * software may be distributed under the terms of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Public License ("GPL") version 2, in which case the provisions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * GPL apply INSTEAD OF those given above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * The provided data structures and external interfaces from this code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * are not restricted to be used by modules with a GPL compatible license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^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) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/can.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/can/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/can/skb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/can/gw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define CAN_GW_NAME "can-gw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) MODULE_DESCRIPTION("PF_CAN netlink gateway");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) MODULE_ALIAS(CAN_GW_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define CGW_MIN_HOPS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define CGW_MAX_HOPS 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define CGW_DEFAULT_HOPS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) module_param(max_hops, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) MODULE_PARM_DESC(max_hops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) "maximum " CAN_GW_NAME " routing hops for CAN frames "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "(valid values: " __stringify(CGW_MIN_HOPS) "-"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) __stringify(CGW_MAX_HOPS) " hops, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "default: " __stringify(CGW_DEFAULT_HOPS) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct notifier_block notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct kmem_cache *cgw_cache __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* structure that contains the (on-the-fly) CAN frame modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct cf_mod {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct canfd_frame and;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct canfd_frame or;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct canfd_frame xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct canfd_frame set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } modframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) u8 and;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) u8 or;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) u8 xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u8 set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void (*modfunc[MAX_MODFUNCTIONS])(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct cf_mod *mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* CAN frame checksum calculation after CAN frame modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct cgw_csum_xor xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct cgw_csum_crc8 crc8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void (*xor)(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct cgw_csum_xor *xor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void (*crc8)(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct cgw_csum_crc8 *crc8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } csumfunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) u32 uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* So far we just support CAN -> CAN routing and frame modifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * The internal can_can_gw structure contains data and attributes for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * a CAN -> CAN gateway job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct can_can_gw {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct can_filter filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int src_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int dst_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* list entry for CAN gateways jobs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct cgw_job {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct hlist_node list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u32 handled_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u32 dropped_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u32 deleted_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct cf_mod mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* CAN frame data source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* CAN frame data destination */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) } dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct can_can_gw ccgw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* tbc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u8 gwtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u8 limit_hops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u16 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* modification functions that are invoked in the hot path in can_can_gw_rcv */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define MODFUNC(func, op) static void func(struct canfd_frame *cf, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct cf_mod *mod) { op ; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) MODFUNC(mod_and_len, cf->len &= mod->modframe.and.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODFUNC(mod_and_flags, cf->flags &= mod->modframe.and.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) MODFUNC(mod_or_len, cf->len |= mod->modframe.or.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) MODFUNC(mod_or_flags, cf->flags |= mod->modframe.or.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) MODFUNC(mod_xor_len, cf->len ^= mod->modframe.xor.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODFUNC(mod_xor_flags, cf->flags ^= mod->modframe.xor.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODFUNC(mod_set_len, cf->len = mod->modframe.set.len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODFUNC(mod_set_flags, cf->flags = mod->modframe.set.flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void mod_and_fddata(struct canfd_frame *cf, struct cf_mod *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) for (i = 0; i < CANFD_MAX_DLEN; i += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *(u64 *)(cf->data + i) &= *(u64 *)(mod->modframe.and.data + i);
^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) static void mod_or_fddata(struct canfd_frame *cf, struct cf_mod *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) for (i = 0; i < CANFD_MAX_DLEN; i += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *(u64 *)(cf->data + i) |= *(u64 *)(mod->modframe.or.data + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void mod_xor_fddata(struct canfd_frame *cf, struct cf_mod *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) for (i = 0; i < CANFD_MAX_DLEN; i += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *(u64 *)(cf->data + i) ^= *(u64 *)(mod->modframe.xor.data + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void mod_set_fddata(struct canfd_frame *cf, struct cf_mod *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) memcpy(cf->data, mod->modframe.set.data, CANFD_MAX_DLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static void canframecpy(struct canfd_frame *dst, struct can_frame *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /* Copy the struct members separately to ensure that no uninitialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * data are copied in the 3 bytes hole of the struct. This is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * to make easy compares of the data in the struct cf_mod.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dst->can_id = src->can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dst->len = src->can_dlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *(u64 *)dst->data = *(u64 *)src->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void canfdframecpy(struct canfd_frame *dst, struct canfd_frame *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* Copy the struct members separately to ensure that no uninitialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * data are copied in the 2 bytes hole of the struct. This is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * to make easy compares of the data in the struct cf_mod.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dst->can_id = src->can_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) dst->flags = src->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) dst->len = src->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) memcpy(dst->data, src->data, CANFD_MAX_DLEN);
^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) static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re, struct rtcanmsg *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) s8 dlen = CAN_MAX_DLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (r->flags & CGW_FLAGS_CAN_FD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dlen = CANFD_MAX_DLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * relative to received dlc -1 .. -8 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * e.g. for received dlc = 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * -1 => index = 7 (data[7])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * -3 => index = 5 (data[5])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * -8 => index = 0 (data[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (fr >= -dlen && fr < dlen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) to >= -dlen && to < dlen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) re >= -dlen && re < dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static inline int calc_idx(int idx, int rx_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return rx_len + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void cgw_csum_xor_rel(struct canfd_frame *cf, struct cgw_csum_xor *xor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int from = calc_idx(xor->from_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int to = calc_idx(xor->to_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int res = calc_idx(xor->result_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) u8 val = xor->init_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (from < 0 || to < 0 || res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (from <= to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for (i = from; i <= to; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) val ^= cf->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) for (i = from; i >= to; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) val ^= cf->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cf->data[res] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static void cgw_csum_xor_pos(struct canfd_frame *cf, struct cgw_csum_xor *xor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) u8 val = xor->init_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) for (i = xor->from_idx; i <= xor->to_idx; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) val ^= cf->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) cf->data[xor->result_idx] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void cgw_csum_xor_neg(struct canfd_frame *cf, struct cgw_csum_xor *xor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) u8 val = xor->init_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) for (i = xor->from_idx; i >= xor->to_idx; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) val ^= cf->data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) cf->data[xor->result_idx] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void cgw_csum_crc8_rel(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct cgw_csum_crc8 *crc8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int from = calc_idx(crc8->from_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int to = calc_idx(crc8->to_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int res = calc_idx(crc8->result_idx, cf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) u8 crc = crc8->init_crc_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (from < 0 || to < 0 || res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (from <= to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) for (i = crc8->from_idx; i <= crc8->to_idx; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) crc = crc8->crctab[crc ^ cf->data[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) for (i = crc8->from_idx; i >= crc8->to_idx; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) crc = crc8->crctab[crc ^ cf->data[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) switch (crc8->profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) case CGW_CRC8PRF_1U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) crc = crc8->crctab[crc ^ crc8->profile_data[0]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) case CGW_CRC8PRF_16U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case CGW_CRC8PRF_SFFID_XOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) (cf->can_id >> 8 & 0xFF)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) break;
^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) cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static void cgw_csum_crc8_pos(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct cgw_csum_crc8 *crc8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) u8 crc = crc8->init_crc_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) for (i = crc8->from_idx; i <= crc8->to_idx; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) crc = crc8->crctab[crc ^ cf->data[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) switch (crc8->profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) case CGW_CRC8PRF_1U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) crc = crc8->crctab[crc ^ crc8->profile_data[0]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) case CGW_CRC8PRF_16U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) case CGW_CRC8PRF_SFFID_XOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) (cf->can_id >> 8 & 0xFF)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static void cgw_csum_crc8_neg(struct canfd_frame *cf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct cgw_csum_crc8 *crc8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) u8 crc = crc8->init_crc_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) for (i = crc8->from_idx; i >= crc8->to_idx; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) crc = crc8->crctab[crc ^ cf->data[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) switch (crc8->profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) case CGW_CRC8PRF_1U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) crc = crc8->crctab[crc ^ crc8->profile_data[0]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) case CGW_CRC8PRF_16U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) case CGW_CRC8PRF_SFFID_XOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) (cf->can_id >> 8 & 0xFF)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* the receive & process & send function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static void can_can_gw_rcv(struct sk_buff *skb, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct cgw_job *gwj = (struct cgw_job *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct canfd_frame *cf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct sk_buff *nskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int modidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* process strictly Classic CAN or CAN FD frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (gwj->flags & CGW_FLAGS_CAN_FD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (skb->len != CANFD_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (skb->len != CAN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return;
^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) /* Do not handle CAN frames routed more than 'max_hops' times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * In general we should never catch this delimiter which is intended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * to cover a misconfiguration protection (e.g. circular CAN routes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * The Controller Area Network controllers only accept CAN frames with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * correct CRCs - which are not visible in the controller registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * According to skbuff.h documentation the csum_start element for IP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * Only CAN skbs can be processed here which already have this property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) #define cgw_hops(skb) ((skb)->csum_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (cgw_hops(skb) >= max_hops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* indicate deleted frames due to misconfiguration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) gwj->deleted_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!(gwj->dst.dev->flags & IFF_UP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) gwj->dropped_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* is sending the skb back to the incoming interface not allowed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* clone the given skb, which has not been done in can_rcv()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * When there is at least one modification function activated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * we need to copy the skb as we want to modify skb->data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (gwj->mod.modfunc[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) nskb = skb_copy(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) nskb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (!nskb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) gwj->dropped_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* put the incremented hop counter in the cloned skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) cgw_hops(nskb) = cgw_hops(skb) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* first processing of this CAN frame -> adjust to private hop limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (gwj->limit_hops && cgw_hops(nskb) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) nskb->dev = gwj->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* pointer to modifiable CAN frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) cf = (struct canfd_frame *)nskb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* perform preprocessed modification functions if there are any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* Has the CAN frame been modified? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (modidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* get available space for the processed CAN frame type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int max_len = nskb->len - offsetof(struct canfd_frame, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* dlc may have changed, make sure it fits to the CAN frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (cf->len > max_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* delete frame due to misconfiguration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) gwj->deleted_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) kfree_skb(nskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /* check for checksum updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (gwj->mod.csumfunc.crc8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (gwj->mod.csumfunc.xor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /* clear the skb timestamp if not configured the other way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) nskb->tstamp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* send to netdevice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) gwj->dropped_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) gwj->handled_frames++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) gwj->ccgw.filter.can_mask, can_can_gw_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) gwj, "gw", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static int cgw_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) unsigned long msg, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (msg == NETDEV_UNREGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct cgw_job *gwj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct hlist_node *nx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (gwj->src.dev == dev || gwj->dst.dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) hlist_del(&gwj->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) cgw_unregister_filter(net, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) kmem_cache_free(cgw_cache, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) u32 pid, u32 seq, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct rtcanmsg *rtcan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) rtcan = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) rtcan->can_family = AF_CAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) rtcan->gwtype = gwj->gwtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) rtcan->flags = gwj->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* add statistics if available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (gwj->handled_frames) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (gwj->dropped_frames) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (gwj->deleted_frames) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto cancel;
^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) /* check non default settings of attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (gwj->limit_hops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (gwj->flags & CGW_FLAGS_CAN_FD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct cgw_fdframe_mod mb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (gwj->mod.modtype.and) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) mb.modtype = gwj->mod.modtype.and;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (nla_put(skb, CGW_FDMOD_AND, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (gwj->mod.modtype.or) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) mb.modtype = gwj->mod.modtype.or;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (nla_put(skb, CGW_FDMOD_OR, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (gwj->mod.modtype.xor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) mb.modtype = gwj->mod.modtype.xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (nla_put(skb, CGW_FDMOD_XOR, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (gwj->mod.modtype.set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) mb.modtype = gwj->mod.modtype.set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (nla_put(skb, CGW_FDMOD_SET, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct cgw_frame_mod mb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (gwj->mod.modtype.and) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) mb.modtype = gwj->mod.modtype.and;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (gwj->mod.modtype.or) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) mb.modtype = gwj->mod.modtype.or;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) goto cancel;
^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) if (gwj->mod.modtype.xor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) mb.modtype = gwj->mod.modtype.xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) goto cancel;
^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) if (gwj->mod.modtype.set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) mb.modtype = gwj->mod.modtype.set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) if (gwj->mod.uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (gwj->mod.csumfunc.crc8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) &gwj->mod.csum.crc8) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (gwj->mod.csumfunc.xor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) &gwj->mod.csum.xor) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) &gwj->ccgw.filter) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) goto cancel;
^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) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) struct cgw_job *gwj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) int idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) int s_idx = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (idx < s_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) goto cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (cgw_put_job(skb, gwj, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) cb->args[0] = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static const struct nla_policy cgw_policy[CGW_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) [CGW_SRC_IF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) [CGW_DST_IF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) [CGW_FILTER] = { .len = sizeof(struct can_filter) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) [CGW_LIM_HOPS] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) [CGW_MOD_UID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) [CGW_FDMOD_AND] = { .len = sizeof(struct cgw_fdframe_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) [CGW_FDMOD_OR] = { .len = sizeof(struct cgw_fdframe_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) [CGW_FDMOD_XOR] = { .len = sizeof(struct cgw_fdframe_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) [CGW_FDMOD_SET] = { .len = sizeof(struct cgw_fdframe_mod) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /* check for common and gwtype specific attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) u8 gwtype, void *gwtypeattr, u8 *limhops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct nlattr *tb[CGW_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct rtcanmsg *r = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) int modidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* initialize modification & checksum data space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) memset(mod, 0, sizeof(*mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) err = nlmsg_parse_deprecated(nlh, sizeof(struct rtcanmsg), tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) CGW_MAX, cgw_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (tb[CGW_LIM_HOPS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) *limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (*limhops < 1 || *limhops > max_hops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* check for AND/OR/XOR/SET modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (r->flags & CGW_FLAGS_CAN_FD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) struct cgw_fdframe_mod mb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (tb[CGW_FDMOD_AND]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) nla_memcpy(&mb, tb[CGW_FDMOD_AND], CGW_FDMODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) canfdframecpy(&mod->modframe.and, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) mod->modtype.and = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) mod->modfunc[modidx++] = mod_and_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) mod->modfunc[modidx++] = mod_and_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (mb.modtype & CGW_MOD_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) mod->modfunc[modidx++] = mod_and_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) mod->modfunc[modidx++] = mod_and_fddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (tb[CGW_FDMOD_OR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) nla_memcpy(&mb, tb[CGW_FDMOD_OR], CGW_FDMODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) canfdframecpy(&mod->modframe.or, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) mod->modtype.or = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) mod->modfunc[modidx++] = mod_or_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) mod->modfunc[modidx++] = mod_or_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (mb.modtype & CGW_MOD_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) mod->modfunc[modidx++] = mod_or_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) mod->modfunc[modidx++] = mod_or_fddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (tb[CGW_FDMOD_XOR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) nla_memcpy(&mb, tb[CGW_FDMOD_XOR], CGW_FDMODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) canfdframecpy(&mod->modframe.xor, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) mod->modtype.xor = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) mod->modfunc[modidx++] = mod_xor_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) mod->modfunc[modidx++] = mod_xor_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (mb.modtype & CGW_MOD_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) mod->modfunc[modidx++] = mod_xor_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) mod->modfunc[modidx++] = mod_xor_fddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (tb[CGW_FDMOD_SET]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) nla_memcpy(&mb, tb[CGW_FDMOD_SET], CGW_FDMODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) canfdframecpy(&mod->modframe.set, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) mod->modtype.set = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) mod->modfunc[modidx++] = mod_set_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) mod->modfunc[modidx++] = mod_set_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (mb.modtype & CGW_MOD_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) mod->modfunc[modidx++] = mod_set_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) mod->modfunc[modidx++] = mod_set_fddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) struct cgw_frame_mod mb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (tb[CGW_MOD_AND]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) canframecpy(&mod->modframe.and, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) mod->modtype.and = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) mod->modfunc[modidx++] = mod_and_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) mod->modfunc[modidx++] = mod_and_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) mod->modfunc[modidx++] = mod_and_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (tb[CGW_MOD_OR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) canframecpy(&mod->modframe.or, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) mod->modtype.or = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) mod->modfunc[modidx++] = mod_or_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) mod->modfunc[modidx++] = mod_or_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) mod->modfunc[modidx++] = mod_or_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (tb[CGW_MOD_XOR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) canframecpy(&mod->modframe.xor, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) mod->modtype.xor = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) mod->modfunc[modidx++] = mod_xor_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) mod->modfunc[modidx++] = mod_xor_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) mod->modfunc[modidx++] = mod_xor_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (tb[CGW_MOD_SET]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) canframecpy(&mod->modframe.set, &mb.cf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) mod->modtype.set = mb.modtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (mb.modtype & CGW_MOD_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) mod->modfunc[modidx++] = mod_set_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (mb.modtype & CGW_MOD_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) mod->modfunc[modidx++] = mod_set_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (mb.modtype & CGW_MOD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) mod->modfunc[modidx++] = mod_set_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /* check for checksum operations after CAN frame modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (modidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (tb[CGW_CS_CRC8]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) c->result_idx, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) CGW_CS_CRC8_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) /* select dedicated processing function to reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * runtime operations in receive hot path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) if (c->from_idx < 0 || c->to_idx < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) c->result_idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) mod->csumfunc.crc8 = cgw_csum_crc8_rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) else if (c->from_idx <= c->to_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) mod->csumfunc.crc8 = cgw_csum_crc8_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) mod->csumfunc.crc8 = cgw_csum_crc8_neg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (tb[CGW_CS_XOR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) c->result_idx, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) CGW_CS_XOR_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) /* select dedicated processing function to reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * runtime operations in receive hot path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (c->from_idx < 0 || c->to_idx < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) c->result_idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) mod->csumfunc.xor = cgw_csum_xor_rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) else if (c->from_idx <= c->to_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) mod->csumfunc.xor = cgw_csum_xor_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) mod->csumfunc.xor = cgw_csum_xor_neg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (tb[CGW_MOD_UID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (gwtype == CGW_TYPE_CAN_CAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) /* check CGW_TYPE_CAN_CAN specific attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) memset(ccgw, 0, sizeof(*ccgw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) /* check for can_filter in attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (tb[CGW_FILTER])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) sizeof(struct can_filter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) /* specifying two interfaces is mandatory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) /* both indices set to 0 for flushing all routing entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (!ccgw->src_idx && !ccgw->dst_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) /* only one index set to 0 is an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (!ccgw->src_idx || !ccgw->dst_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /* add the checks for other gwtypes here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) struct rtcanmsg *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) struct cgw_job *gwj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) struct cf_mod mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) struct can_can_gw ccgw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) u8 limhops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (!netlink_capable(skb, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (nlmsg_len(nlh) < sizeof(*r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) r = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (r->can_family != AF_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) /* so far we only support CAN -> CAN routings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (r->gwtype != CGW_TYPE_CAN_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (mod.uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /* check for updating an existing job with identical uid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) hlist_for_each_entry(gwj, &net->can.cgw_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (gwj->mod.uid != mod.uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) /* interfaces & filters must be identical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) /* update modifications with disabled softirq & quit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) memcpy(&gwj->mod, &mod, sizeof(mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) /* ifindex == 0 is not allowed for job creation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (!ccgw.src_idx || !ccgw.dst_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (!gwj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) gwj->handled_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) gwj->dropped_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) gwj->deleted_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) gwj->flags = r->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) gwj->gwtype = r->gwtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) gwj->limit_hops = limhops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) /* insert already parsed information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) memcpy(&gwj->mod, &mod, sizeof(mod));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if (!gwj->src.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) if (gwj->src.dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (!gwj->dst.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (gwj->dst.dev->type != ARPHRD_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) err = cgw_register_filter(net, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) hlist_add_head_rcu(&gwj->list, &net->can.cgw_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) kmem_cache_free(cgw_cache, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static void cgw_remove_all_jobs(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) struct cgw_job *gwj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) struct hlist_node *nx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) hlist_del(&gwj->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) cgw_unregister_filter(net, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) kmem_cache_free(cgw_cache, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct cgw_job *gwj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) struct hlist_node *nx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct rtcanmsg *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct cf_mod mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) struct can_can_gw ccgw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) u8 limhops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (!netlink_capable(skb, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (nlmsg_len(nlh) < sizeof(*r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) r = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (r->can_family != AF_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return -EPFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) /* so far we only support CAN -> CAN routings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (r->gwtype != CGW_TYPE_CAN_CAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /* two interface indices both set to 0 => remove all entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (!ccgw.src_idx && !ccgw.dst_idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) cgw_remove_all_jobs(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) /* remove only the first matching entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (gwj->flags != r->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (gwj->limit_hops != limhops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* we have a match when uid is enabled and identical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (gwj->mod.uid || mod.uid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (gwj->mod.uid != mod.uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) /* no uid => check for identical modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (memcmp(&gwj->mod, &mod, sizeof(mod)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) hlist_del(&gwj->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) cgw_unregister_filter(net, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) kmem_cache_free(cgw_cache, gwj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) static int __net_init cangw_pernet_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) INIT_HLIST_HEAD(&net->can.cgw_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) static void __net_exit cangw_pernet_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) cgw_remove_all_jobs(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static struct pernet_operations cangw_pernet_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) .init = cangw_pernet_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) .exit = cangw_pernet_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) static __init int cgw_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) /* sanitize given module parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) pr_info("can: netlink gateway - max_hops=%d\n", max_hops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) ret = register_pernet_subsys(&cangw_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (!cgw_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) goto out_cache_create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) /* set notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) notifier.notifier_call = cgw_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) ret = register_netdevice_notifier(¬ifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) goto out_register_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) NULL, cgw_dump_jobs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) goto out_rtnl_register1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) cgw_create_job, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) goto out_rtnl_register2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) cgw_remove_job, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) goto out_rtnl_register3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) out_rtnl_register3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) rtnl_unregister(PF_CAN, RTM_NEWROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) out_rtnl_register2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) rtnl_unregister(PF_CAN, RTM_GETROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) out_rtnl_register1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) unregister_netdevice_notifier(¬ifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) out_register_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) kmem_cache_destroy(cgw_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) out_cache_create:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) unregister_pernet_subsys(&cangw_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) static __exit void cgw_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) rtnl_unregister_all(PF_CAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) unregister_netdevice_notifier(¬ifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) unregister_pernet_subsys(&cangw_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) rcu_barrier(); /* Wait for completion of call_rcu()'s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) kmem_cache_destroy(cgw_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) module_init(cgw_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) module_exit(cgw_module_exit);