4 * Definitions for CAN frame Gateway/Router/Bridge
6 * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
7 * Copyright (c) 2011 Volkswagen Group Electronic Research
15 #include <linux/types.h>
16 #include <linux/can.h>
24 /* CAN gateway types */
27 CGW_TYPE_CAN_CAN, /* CAN->CAN routing */
31 #define CGW_TYPE_MAX (__CGW_TYPE_MAX - 1)
33 /* CAN rtnetlink attribute definitions */
36 CGW_MOD_AND, /* CAN frame modification binary AND */
37 CGW_MOD_OR, /* CAN frame modification binary OR */
38 CGW_MOD_XOR, /* CAN frame modification binary XOR */
39 CGW_MOD_SET, /* CAN frame modification set alternate values */
40 CGW_CS_XOR, /* set data[] XOR checksum into data[index] */
41 CGW_CS_CRC8, /* set data[] CRC8 checksum into data[index] */
42 CGW_HANDLED, /* number of handled CAN frames */
43 CGW_DROPPED, /* number of dropped CAN frames */
44 CGW_SRC_IF, /* ifindex of source network interface */
45 CGW_DST_IF, /* ifindex of destination network interface */
46 CGW_FILTER, /* specify struct can_filter on source CAN device */
47 CGW_DELETED, /* number of deleted CAN frames (see max_hops param) */
48 CGW_LIM_HOPS, /* limit the number of hops of this specific rule */
52 #define CGW_MAX (__CGW_MAX - 1)
54 #define CGW_FLAGS_CAN_ECHO 0x01
55 #define CGW_FLAGS_CAN_SRC_TSTAMP 0x02
56 #define CGW_FLAGS_CAN_IIF_TX_OK 0x04
58 #define CGW_MOD_FUNCS 4 /* AND OR XOR SET */
60 /* CAN frame elements that are affected by curr. 3 CAN frame modifications */
61 #define CGW_MOD_ID 0x01
62 #define CGW_MOD_DLC 0x02
63 #define CGW_MOD_DATA 0x04
65 #define CGW_FRAME_MODS 3 /* ID DLC DATA */
67 #define MAX_MODFUNCTIONS (CGW_MOD_FUNCS * CGW_FRAME_MODS)
69 struct cgw_frame_mod {
72 } __attribute__((packed));
74 #define CGW_MODATTR_LEN sizeof(struct cgw_frame_mod)
81 } __attribute__((packed));
83 struct cgw_csum_crc8 {
91 __u8 profile_data[20];
92 } __attribute__((packed));
94 /* length of checksum operation parameters. idx = index in CAN frame data[] */
95 #define CGW_CS_XOR_LEN sizeof(struct cgw_csum_xor)
96 #define CGW_CS_CRC8_LEN sizeof(struct cgw_csum_crc8)
98 /* CRC8 profiles (compute CRC for additional data elements - see below) */
101 CGW_CRC8PRF_1U8, /* compute one additional u8 value */
102 CGW_CRC8PRF_16U8, /* u8 value table indexed by data[1] & 0xF */
103 CGW_CRC8PRF_SFFID_XOR, /* (can_id & 0xFF) ^ (can_id >> 8 & 0xFF) */
107 #define CGW_CRC8PRF_MAX (__CGW_CRC8PRF_MAX - 1)
110 * CAN rtnetlink attribute contents in detail
112 * CGW_XXX_IF (length 4 bytes):
113 * Sets an interface index for source/destination network interfaces.
114 * For the CAN->CAN gwtype the indices of _two_ CAN interfaces are mandatory.
116 * CGW_FILTER (length 8 bytes):
117 * Sets a CAN receive filter for the gateway job specified by the
118 * struct can_filter described in include/linux/can.h
120 * CGW_MOD_(AND|OR|XOR|SET) (length 17 bytes):
121 * Specifies a modification that's done to a received CAN frame before it is
122 * send out to the destination interface.
124 * <struct can_frame> data used as operator
125 * <u8> affected CAN frame elements
127 * CGW_LIM_HOPS (length 1 byte):
128 * Limit the number of hops of this specific rule. Usually the received CAN
129 * frame can be processed as much as 'max_hops' times (which is given at module
130 * load time of the can-gw module). This value is used to reduce the number of
131 * possible hops for this gateway rule to a value smaller then max_hops.
133 * CGW_CS_XOR (length 4 bytes):
134 * Set a simple XOR checksum starting with an initial value into
135 * data[result-idx] using data[start-idx] .. data[end-idx]
137 * The XOR checksum is calculated like this:
141 * for (i = from_idx .. to_idx)
142 * xor ^= can_frame.data[i]
144 * can_frame.data[ result_idx ] = xor
146 * CGW_CS_CRC8 (length 282 bytes):
147 * Set a CRC8 value into data[result-idx] using a given 256 byte CRC8 table,
148 * a given initial value and a defined input data[start-idx] .. data[end-idx].
149 * Finally the result value is XOR'ed with the final_xor_val.
151 * The CRC8 checksum is calculated like this:
155 * for (i = from_idx .. to_idx)
156 * crc = crctab[ crc ^ can_frame.data[i] ]
158 * can_frame.data[ result_idx ] = crc ^ final_xor_val
160 * The calculated CRC may contain additional source data elements that can be
161 * defined in the handling of 'checksum profiles' e.g. shown in AUTOSAR specs
162 * like http://www.autosar.org/download/R4.0/AUTOSAR_SWS_E2ELibrary.pdf
163 * E.g. the profile_data[] may contain additional u8 values (called DATA_IDs)
164 * that are used depending on counter values inside the CAN frame data[].
165 * So far only three profiles have been implemented for illustration.
167 * Remark: In general the attribute data is a linear buffer.
168 * Beware of sending unpacked or aligned structs!