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