blob: 0505c7f8621338ecd12df9dd044e307077d26b9b [file] [log] [blame]
Oliver Hartkoppc1aabdf2011-09-01 04:23:23 +00001/*
2 * linux/can/gw.h
3 *
4 * Definitions for CAN frame Gateway/Router/Bridge
5 *
6 * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
7 * Copyright (c) 2011 Volkswagen Group Electronic Research
8 * All rights reserved.
9 *
Oliver Hartkoppc1aabdf2011-09-01 04:23:23 +000010 */
11
12#ifndef CAN_GW_H
13#define CAN_GW_H
14
15#include <linux/types.h>
16#include <linux/can.h>
17
18struct rtcanmsg {
19 __u8 can_family;
20 __u8 gwtype;
21 __u16 flags;
22};
23
24/* CAN gateway types */
25enum {
26 CGW_TYPE_UNSPEC,
27 CGW_TYPE_CAN_CAN, /* CAN->CAN routing */
28 __CGW_TYPE_MAX
29};
30
31#define CGW_TYPE_MAX (__CGW_TYPE_MAX - 1)
32
33/* CAN rtnetlink attribute definitions */
34enum {
35 CGW_UNSPEC,
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_MAX
48};
49
50#define CGW_MAX (__CGW_MAX - 1)
51
52#define CGW_FLAGS_CAN_ECHO 0x01
53#define CGW_FLAGS_CAN_SRC_TSTAMP 0x02
Oliver Hartkoppd904d3e2013-01-17 18:43:41 +010054#define CGW_FLAGS_CAN_IIF_TX_OK 0x04
Oliver Hartkoppc1aabdf2011-09-01 04:23:23 +000055
56#define CGW_MOD_FUNCS 4 /* AND OR XOR SET */
57
58/* CAN frame elements that are affected by curr. 3 CAN frame modifications */
59#define CGW_MOD_ID 0x01
60#define CGW_MOD_DLC 0x02
61#define CGW_MOD_DATA 0x04
62
63#define CGW_FRAME_MODS 3 /* ID DLC DATA */
64
65#define MAX_MODFUNCTIONS (CGW_MOD_FUNCS * CGW_FRAME_MODS)
66
67struct cgw_frame_mod {
68 struct can_frame cf;
69 __u8 modtype;
70} __attribute__((packed));
71
72#define CGW_MODATTR_LEN sizeof(struct cgw_frame_mod)
73
74struct cgw_csum_xor {
75 __s8 from_idx;
76 __s8 to_idx;
77 __s8 result_idx;
78 __u8 init_xor_val;
79} __attribute__((packed));
80
81struct cgw_csum_crc8 {
82 __s8 from_idx;
83 __s8 to_idx;
84 __s8 result_idx;
85 __u8 init_crc_val;
86 __u8 final_xor_val;
87 __u8 crctab[256];
88 __u8 profile;
89 __u8 profile_data[20];
90} __attribute__((packed));
91
92/* length of checksum operation parameters. idx = index in CAN frame data[] */
93#define CGW_CS_XOR_LEN sizeof(struct cgw_csum_xor)
94#define CGW_CS_CRC8_LEN sizeof(struct cgw_csum_crc8)
95
96/* CRC8 profiles (compute CRC for additional data elements - see below) */
97enum {
98 CGW_CRC8PRF_UNSPEC,
99 CGW_CRC8PRF_1U8, /* compute one additional u8 value */
100 CGW_CRC8PRF_16U8, /* u8 value table indexed by data[1] & 0xF */
101 CGW_CRC8PRF_SFFID_XOR, /* (can_id & 0xFF) ^ (can_id >> 8 & 0xFF) */
102 __CGW_CRC8PRF_MAX
103};
104
105#define CGW_CRC8PRF_MAX (__CGW_CRC8PRF_MAX - 1)
106
107/*
108 * CAN rtnetlink attribute contents in detail
109 *
110 * CGW_XXX_IF (length 4 bytes):
111 * Sets an interface index for source/destination network interfaces.
112 * For the CAN->CAN gwtype the indices of _two_ CAN interfaces are mandatory.
113 *
114 * CGW_FILTER (length 8 bytes):
115 * Sets a CAN receive filter for the gateway job specified by the
116 * struct can_filter described in include/linux/can.h
117 *
118 * CGW_MOD_XXX (length 17 bytes):
119 * Specifies a modification that's done to a received CAN frame before it is
120 * send out to the destination interface.
121 *
122 * <struct can_frame> data used as operator
123 * <u8> affected CAN frame elements
124 *
125 * CGW_CS_XOR (length 4 bytes):
126 * Set a simple XOR checksum starting with an initial value into
127 * data[result-idx] using data[start-idx] .. data[end-idx]
128 *
129 * The XOR checksum is calculated like this:
130 *
131 * xor = init_xor_val
132 *
133 * for (i = from_idx .. to_idx)
134 * xor ^= can_frame.data[i]
135 *
136 * can_frame.data[ result_idx ] = xor
137 *
138 * CGW_CS_CRC8 (length 282 bytes):
139 * Set a CRC8 value into data[result-idx] using a given 256 byte CRC8 table,
140 * a given initial value and a defined input data[start-idx] .. data[end-idx].
141 * Finally the result value is XOR'ed with the final_xor_val.
142 *
143 * The CRC8 checksum is calculated like this:
144 *
145 * crc = init_crc_val
146 *
147 * for (i = from_idx .. to_idx)
148 * crc = crctab[ crc ^ can_frame.data[i] ]
149 *
150 * can_frame.data[ result_idx ] = crc ^ final_xor_val
151 *
152 * The calculated CRC may contain additional source data elements that can be
153 * defined in the handling of 'checksum profiles' e.g. shown in AUTOSAR specs
154 * like http://www.autosar.org/download/R4.0/AUTOSAR_SWS_E2ELibrary.pdf
155 * E.g. the profile_data[] may contain additional u8 values (called DATA_IDs)
156 * that are used depending on counter values inside the CAN frame data[].
157 * So far only three profiles have been implemented for illustration.
158 *
159 * Remark: In general the attribute data is a linear buffer.
160 * Beware of sending unpacked or aligned structs!
161 */
162
163#endif