Skip to content

Commit c1aabdf

Browse files
hartkoppdavem330
authored andcommitted
can-gw: add netlink based CAN routing
This patch adds a CAN Gateway/Router to route (and modify) CAN frames. It is based on the PF_CAN core infrastructure for msg filtering and msg sending and can optionally modify routed CAN frames on the fly. CAN frames can *only* be routed between CAN network interfaces (one hop). They can be modified with AND/OR/XOR/SET operations as configured by the netlink configuration interface known e.g. from iptables. From the netlink view this can-gw implements RTM_{NEW|DEL|GET}ROUTE for PF_CAN. The CAN specific userspace tool to manage CAN routing entries can be found in the CAN utils http://svn.berlios.de/wsvn/socketcan/trunk/can-utils/cangw.c at the SocketCAN SVN on BerliOS. Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1322597 commit c1aabdf

5 files changed

Lines changed: 1138 additions & 0 deletions

File tree

‎include/linux/can/Kbuild‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
header-y += raw.h
22
header-y += bcm.h
3+
header-y += gw.h
34
header-y += error.h
45
header-y += netlink.h

‎include/linux/can/gw.h‎

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

‎net/can/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,16 @@ config CAN_BCM
4040
CAN messages are used on the bus (e.g. in automotive environments).
4141
To use the Broadcast Manager, use AF_CAN with protocol CAN_BCM.
4242

43+
config CAN_GW
44+
tristate "CAN Gateway/Router (with netlink configuration)"
45+
depends on CAN
46+
default N
47+
---help---
48+
The CAN Gateway/Router is used to route (and modify) CAN frames.
49+
It is based on the PF_CAN core infrastructure for msg filtering and
50+
msg sending and can optionally modify routed CAN frames on the fly.
51+
CAN frames can be routed between CAN network interfaces (one hop).
52+
They can be modified with AND/OR/XOR/SET operations as configured
53+
by the netlink configuration interface known e.g. from iptables.
4354

4455
source "drivers/net/can/Kconfig"

‎net/can/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ can-raw-y := raw.o
1010

1111
obj-$(CONFIG_CAN_BCM) += can-bcm.o
1212
can-bcm-y := bcm.o
13+
14+
obj-$(CONFIG_CAN_GW) += can-gw.o
15+
can-gw-y := gw.o

0 commit comments

Comments
 (0)