1 /* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
5 * Definitions for CAN frame Gateway/Router/Bridge
7 * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
8 * Copyright (c) 2011 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 #ifndef _UAPI_CAN_GW_H
46 #define _UAPI_CAN_GW_H
48 #include <linux/types.h>
49 #include <linux/can.h>
57 /* CAN gateway types */
60 CGW_TYPE_CAN_CAN
, /* CAN->CAN routing */
64 #define CGW_TYPE_MAX (__CGW_TYPE_MAX - 1)
66 /* CAN rtnetlink attribute definitions */
69 CGW_MOD_AND
, /* CAN frame modification binary AND */
70 CGW_MOD_OR
, /* CAN frame modification binary OR */
71 CGW_MOD_XOR
, /* CAN frame modification binary XOR */
72 CGW_MOD_SET
, /* CAN frame modification set alternate values */
73 CGW_CS_XOR
, /* set data[] XOR checksum into data[index] */
74 CGW_CS_CRC8
, /* set data[] CRC8 checksum into data[index] */
75 CGW_HANDLED
, /* number of handled CAN frames */
76 CGW_DROPPED
, /* number of dropped CAN frames */
77 CGW_SRC_IF
, /* ifindex of source network interface */
78 CGW_DST_IF
, /* ifindex of destination network interface */
79 CGW_FILTER
, /* specify struct can_filter on source CAN device */
80 CGW_DELETED
, /* number of deleted CAN frames (see max_hops param) */
81 CGW_LIM_HOPS
, /* limit the number of hops of this specific rule */
82 CGW_MOD_UID
, /* user defined identifier for modification updates */
86 #define CGW_MAX (__CGW_MAX - 1)
88 #define CGW_FLAGS_CAN_ECHO 0x01
89 #define CGW_FLAGS_CAN_SRC_TSTAMP 0x02
90 #define CGW_FLAGS_CAN_IIF_TX_OK 0x04
92 #define CGW_MOD_FUNCS 4 /* AND OR XOR SET */
94 /* CAN frame elements that are affected by curr. 3 CAN frame modifications */
95 #define CGW_MOD_ID 0x01
96 #define CGW_MOD_DLC 0x02
97 #define CGW_MOD_DATA 0x04
99 #define CGW_FRAME_MODS 3 /* ID DLC DATA */
101 #define MAX_MODFUNCTIONS (CGW_MOD_FUNCS * CGW_FRAME_MODS)
103 struct cgw_frame_mod
{
106 } __attribute__((packed
));
108 #define CGW_MODATTR_LEN sizeof(struct cgw_frame_mod)
110 struct cgw_csum_xor
{
115 } __attribute__((packed
));
117 struct cgw_csum_crc8
{
125 __u8 profile_data
[20];
126 } __attribute__((packed
));
128 /* length of checksum operation parameters. idx = index in CAN frame data[] */
129 #define CGW_CS_XOR_LEN sizeof(struct cgw_csum_xor)
130 #define CGW_CS_CRC8_LEN sizeof(struct cgw_csum_crc8)
132 /* CRC8 profiles (compute CRC for additional data elements - see below) */
135 CGW_CRC8PRF_1U8
, /* compute one additional u8 value */
136 CGW_CRC8PRF_16U8
, /* u8 value table indexed by data[1] & 0xF */
137 CGW_CRC8PRF_SFFID_XOR
, /* (can_id & 0xFF) ^ (can_id >> 8 & 0xFF) */
141 #define CGW_CRC8PRF_MAX (__CGW_CRC8PRF_MAX - 1)
144 * CAN rtnetlink attribute contents in detail
146 * CGW_XXX_IF (length 4 bytes):
147 * Sets an interface index for source/destination network interfaces.
148 * For the CAN->CAN gwtype the indices of _two_ CAN interfaces are mandatory.
150 * CGW_FILTER (length 8 bytes):
151 * Sets a CAN receive filter for the gateway job specified by the
152 * struct can_filter described in include/linux/can.h
154 * CGW_MOD_(AND|OR|XOR|SET) (length 17 bytes):
155 * Specifies a modification that's done to a received CAN frame before it is
156 * send out to the destination interface.
158 * <struct can_frame> data used as operator
159 * <u8> affected CAN frame elements
161 * CGW_LIM_HOPS (length 1 byte):
162 * Limit the number of hops of this specific rule. Usually the received CAN
163 * frame can be processed as much as 'max_hops' times (which is given at module
164 * load time of the can-gw module). This value is used to reduce the number of
165 * possible hops for this gateway rule to a value smaller then max_hops.
167 * CGW_MOD_UID (length 4 bytes):
168 * Optional non-zero user defined routing job identifier to alter existing
169 * modification settings at runtime.
171 * CGW_CS_XOR (length 4 bytes):
172 * Set a simple XOR checksum starting with an initial value into
173 * data[result-idx] using data[start-idx] .. data[end-idx]
175 * The XOR checksum is calculated like this:
179 * for (i = from_idx .. to_idx)
180 * xor ^= can_frame.data[i]
182 * can_frame.data[ result_idx ] = xor
184 * CGW_CS_CRC8 (length 282 bytes):
185 * Set a CRC8 value into data[result-idx] using a given 256 byte CRC8 table,
186 * a given initial value and a defined input data[start-idx] .. data[end-idx].
187 * Finally the result value is XOR'ed with the final_xor_val.
189 * The CRC8 checksum is calculated like this:
193 * for (i = from_idx .. to_idx)
194 * crc = crctab[ crc ^ can_frame.data[i] ]
196 * can_frame.data[ result_idx ] = crc ^ final_xor_val
198 * The calculated CRC may contain additional source data elements that can be
199 * defined in the handling of 'checksum profiles' e.g. shown in AUTOSAR specs
200 * like http://www.autosar.org/download/R4.0/AUTOSAR_SWS_E2ELibrary.pdf
201 * E.g. the profile_data[] may contain additional u8 values (called DATA_IDs)
202 * that are used depending on counter values inside the CAN frame data[].
203 * So far only three profiles have been implemented for illustration.
205 * Remark: In general the attribute data is a linear buffer.
206 * Beware of sending unpacked or aligned structs!
209 #endif /* !_UAPI_CAN_GW_H */