gw.h 4.7 KB

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